aj98

login

Dec 25th, 2018
93
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.30 KB | None | 0 0
  1. package oop_final_project_daycaremanagementsystem;
  2.  
  3. /**
  4.  * @author ajeel
  5.  */
  6. import java.io.*;
  7. import java.util.*;
  8. import javax.swing.*;
  9. import java.awt.*;
  10. import java.awt.event.ActionEvent;
  11. import java.awt.event.ActionListener;
  12.  
  13. public class login extends JFrame {
  14.  
  15.     //plUpper will have labels and textfields and lower will have buttons
  16.     JPanel plUpper, plLower;
  17.     JLabel lbUser, lbPassword;
  18.     JTextField tfUser, tfPassword;
  19.     JButton btLogin, btCreate;
  20.  
  21.     public login() throws HeadlessException {
  22.  
  23.         super("Login");
  24.  
  25.         setLayout(new FlowLayout());
  26.  
  27.         setSize(800, 800);
  28.  
  29.         setDefaultCloseOperation(EXIT_ON_CLOSE);
  30.  
  31.         setVisible(true);
  32.  
  33.         myActionListener forLogin = new myActionListener();
  34.         //initializing all components
  35.         plUpper = new JPanel(new FlowLayout());
  36.  
  37.         plLower = new JPanel(new FlowLayout());
  38.  
  39.         lbUser = new JLabel("Username");
  40.  
  41.         lbPassword = new JLabel("Password");
  42.  
  43.         tfUser = new JTextField("ajeel", 15);
  44.         tfUser.addActionListener(forLogin);
  45.  
  46.         tfPassword = new JTextField("ajeel", 15);
  47.         tfPassword.addActionListener(forLogin);
  48.  
  49.         btCreate = new JButton("Create new account for employee");
  50.         btCreate.addActionListener(forLogin);
  51.         btLogin = new JButton("Login");
  52.         btLogin.addActionListener(forLogin);
  53.  
  54.         plUpper.add(lbUser);
  55.         plUpper.add(tfUser);
  56.         plUpper.add(lbPassword);
  57.         plUpper.add(tfPassword);
  58.  
  59.         plLower.add(btLogin);
  60.         plLower.add(btCreate);
  61.         add(plUpper);
  62.         add(plLower);
  63.     }
  64.  
  65.     public ArrayList<Employee> readAllData() {
  66.         //  ArrayList initialized with size 0
  67.         ArrayList<Employee> employeeList = new ArrayList<Employee>(0);
  68. // Input stream
  69.         ObjectInputStream inputStream = null;
  70.         try {
  71. // open file for reading
  72.             inputStream = new ObjectInputStream(new FileInputStream("Employee.ser"));
  73. // End Of File flag
  74.             boolean EOF = false;
  75. // Keep reading file until file ends
  76.             while (!EOF) {
  77.                 try {
  78. // read object and type cast into CarDetails object
  79.                     Employee myObj = (Employee) inputStream.readObject();
  80. // add object into ArrayList
  81.                     System.out.println(myObj);
  82.                     employeeList.add(myObj);
  83. //System.out.println("Read: " + myObj.getName());
  84.                 } catch (ClassNotFoundException e) {
  85. //System.out.println("Class not found");
  86.                 } catch (EOFException end) {
  87. // EOFException is raised when file ends
  88. // set End Of File flag to true so that loop exits
  89.                     EOF = true;
  90.                 }
  91.             }
  92.         } catch (FileNotFoundException e) {
  93. //System.out.println("Cannot find file");
  94.         } catch (IOException e) {
  95. //System.out.println("IO Exception while opening stream");
  96. //e.printStackTrace();
  97.         } finally { // cleanup code to close stream if it was opened
  98.             try {
  99.                 if (inputStream != null) {
  100.                     inputStream.close();
  101.                 }
  102.             } catch (IOException e) {
  103. // TODO Auto-generated catch block
  104.                 System.out.println("IO Exception while closing file");
  105.             }
  106.         }
  107. // returns ArrayList
  108.         return employeeList;
  109.     }
  110.  
  111.     public class myActionListener implements ActionListener {
  112.  
  113.         public void actionPerformed(ActionEvent e) {
  114.             if (e.getActionCommand().equals("Login")) {
  115.                 //if (!(tfUser.getText().isEmpty() && tfPassword.getText().isEmpty())) {
  116.                 System.out.println("Reading file");
  117.                
  118.                 System.out.println("got till here");
  119.                 ArrayList<Employee> employeeList = readAllData();
  120.                 for (int i = 0; i < employeeList.size(); i++) {
  121.                    
  122.                     System.out.println(employeeList.size());
  123.                     // System.out.println(employeeList.get(i).getUap_employee().getUsername());
  124.                     //if(employeeList.get(i).getUap_employee().getUsername().equals(tfUser.getText())) {
  125.                     if (employeeList.get(i).getUap_employee().getUsername().equals(tfUser.getText()) && employeeList.get(i).getUap_employee().getPassword().equals(tfPassword.getText())) {
  126.                         //createEmployee E = new createEmployee();
  127.                         mainMenu a = new mainMenu();
  128.                        
  129.                     }
  130.                    
  131.                     else if((i ==employeeList.size()-1)  && !(employeeList.get(i).getUap_employee().getUsername().equals(tfUser.getText())) && !(employeeList.get(i).getUap_employee().getPassword().equals(tfPassword.getText())))
  132.                     {JOptionPane.showMessageDialog(new JFrame(), "wrong username or password");
  133.                    
  134.                     }
  135.                    
  136.                     else{
  137.                         System.out.println("working");
  138.                         continue;
  139.                     }
  140.                    
  141.                 }
  142.                
  143.                
  144.  
  145.             } else if (e.getActionCommand().equals("Create new account for employee")) {
  146.                
  147.                 createEmployee a = new createEmployee();
  148.                
  149.             }
  150.  
  151.         }
  152.  
  153.     }
  154. }
Add Comment
Please, Sign In to add comment