Advertisement
Guest User

Untitled

a guest
May 21st, 2017
70
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.24 KB | None | 0 0
  1. import java.awt.*;
  2. import java.awt.event.*;
  3. import java.sql.*;
  4. import javax.swing.*;
  5.  
  6. import java.util.Date;
  7. import java.text.SimpleDateFormat;
  8.  
  9. public class Login implements ActionListener
  10. {
  11.  
  12.     Date todaysDate = new java.util.Date();
  13.     SimpleDateFormat formatter = new SimpleDateFormat("EEE, dd-MM-yyyy HH:mm:ss");
  14.     String formattedDate = formatter.format(todaysDate);
  15.     private JFrame frame;
  16.     private JTabbedPane jtp;
  17.     private String name = "";
  18.     private String pw = "";
  19.     private boolean loggedIn = false;
  20.     private JButton showDialogButton;
  21.     int counter = 0;
  22.    
  23.  
  24.     private JLabel user = new JLabel("Username (Staff ID)");
  25.     private JLabel pass = new JLabel("Password");
  26.     private JTextField username = new JTextField(20);
  27.     private JPasswordField password = new JPasswordField(20);
  28.  
  29.     private Object[] stuff = {user, username, pass, password};
  30.    
  31.    
  32.  
  33.     public Login()
  34.     {
  35.         frame = new JFrame("Eternal Nights");
  36.         //set up login
  37.  
  38.         //if proceed to login button is pressed... enter JDialog for login
  39.         showDialogButton = new JButton("Proceed to Login");
  40.         showDialogButton.addActionListener(this);
  41.  
  42.         frame.add(showDialogButton);
  43.     /*code for the actual databse frame to be used by staff*/
  44.         jtp = new JTabbedPane( );
  45.  
  46.         jtp.addTab("Sales", new JLabel("Sales Tab"));
  47.         jtp.addTab("Drinks", new JLabel("Drinks Tab"));
  48.         jtp.addTab("Members", new JLabel("Members Tab"));
  49.         jtp.addTab("Orders", new JLabel("Orders Tab"));
  50.         jtp.addTab("Staff", new JLabel("Staff Tab"));
  51.  
  52.         jtp.setVisible(false);
  53.        
  54.        
  55.        
  56.         //puts the proceed to login button on the pane
  57.         frame.getContentPane().setLayout(new FlowLayout());
  58.         frame.add(showDialogButton);
  59.  
  60.         // sets the jframe up
  61.         frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
  62.         frame.setPreferredSize(new Dimension(500, 300));
  63.         frame.pack();
  64.         frame.setLocationRelativeTo(null);
  65.         frame.setVisible(true); //shows the jframe
  66.  
  67.  
  68.  
  69.         frame.add(jtp);
  70.        
  71.     }
  72.  
  73.     public void actionPerformed(ActionEvent e)
  74.     {
  75.         if(e.getSource()==showDialogButton)
  76.         {
  77.             String formattedDate = formatter.format(todaysDate);
  78.             System.out.println(formattedDate + "Login called");
  79.             while(loggedIn==false)
  80.             {
  81.                 int result = JOptionPane.showConfirmDialog(null, stuff, "Login", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
  82.  
  83.                 if(result == JOptionPane.OK_OPTION)
  84.                 {
  85.                     String tempUser = new String(username.getText());
  86.                     String tempPass = new String(password.getPassword());
  87.  
  88.                     if((tempUser.equals(name))&&(tempPass.equals(pw)))
  89.                     {
  90.                        
  91.                         loggedIn = true;
  92.                         System.out.println(formattedDate + " - "  + tempUser + " logged in");
  93.                         frame.remove(showDialogButton);                    
  94.                         jtp.setVisible(true);
  95.                         frame.setSize(500,299);
  96.                         JOptionPane.showMessageDialog(frame, "Login Successful! Welcome " + tempUser + "!");
  97.                         counter = 0;
  98.                        
  99.                    
  100.  
  101.                     }
  102.                    
  103.                    
  104.    
  105.    
  106.    
  107.                    
  108.                    
  109.                     else {
  110.                    
  111.                    
  112.                         frame.remove(showDialogButton);
  113.                         System.out.println(formattedDate + " - Login failed due to incorrect input");
  114.                         frame.setSize(500,299);                    
  115.                         JOptionPane.showMessageDialog(frame, "Login failed, either the Staff ID or password is incorrect");
  116.                         counter ++;
  117.                         System.out.println(counter + "login fails in a row");
  118.                        
  119.                         if(counter == 3)
  120.                         {
  121.                         frame.remove(showDialogButton);
  122.                         frame.setSize(500,299);
  123.                         JOptionPane.showMessageDialog(frame, "Login failed, too many incorrect inputs - please restart the system");
  124.                        
  125.                         System.out.println(formattedDate + " - Login failed due to incorrect input");
  126.                        
  127.                         break;  //change to setting a new random password to login with then prompt to change password after
  128.                        
  129.                        
  130.                        
  131.                        
  132.                         }
  133.                             }
  134.                 }
  135.  
  136.                 if(result == JOptionPane.CANCEL_OPTION)
  137.                 {
  138.                 JOptionPane.showMessageDialog(frame, "Login Cancelled");
  139.                     frame.add(showDialogButton);
  140.                     System.out.println(formattedDate + " - Login Cancelled");
  141.                     frame.setSize(500,301);
  142.                     break;
  143.  
  144.                    
  145.                
  146.                 }
  147.  
  148.             }
  149.         }
  150.  
  151.     }
  152.  
  153.     public static void main(String[] args)
  154.     {
  155.    
  156.  
  157.     // schedule this for the event dispatch thread (edt)
  158.         SwingUtilities.invokeLater(new Runnable()
  159.         {
  160.             public void run()
  161.             {
  162.                 new Login();
  163.                
  164.             }
  165.         });
  166.     }
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement