Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.awt.*;
- import java.awt.event.*;
- import java.sql.*;
- import javax.swing.*;
- import java.util.Date;
- import java.text.SimpleDateFormat;
- public class Login implements ActionListener
- {
- Date todaysDate = new java.util.Date();
- SimpleDateFormat formatter = new SimpleDateFormat("EEE, dd-MM-yyyy HH:mm:ss");
- String formattedDate = formatter.format(todaysDate);
- private JFrame frame;
- private JTabbedPane jtp;
- private String name = "";
- private String pw = "";
- private boolean loggedIn = false;
- private JButton showDialogButton;
- int counter = 0;
- private JLabel user = new JLabel("Username (Staff ID)");
- private JLabel pass = new JLabel("Password");
- private JTextField username = new JTextField(20);
- private JPasswordField password = new JPasswordField(20);
- private Object[] stuff = {user, username, pass, password};
- public Login()
- {
- frame = new JFrame("Eternal Nights");
- //set up login
- //if proceed to login button is pressed... enter JDialog for login
- showDialogButton = new JButton("Proceed to Login");
- showDialogButton.addActionListener(this);
- frame.add(showDialogButton);
- /*code for the actual databse frame to be used by staff*/
- jtp = new JTabbedPane( );
- jtp.addTab("Sales", new JLabel("Sales Tab"));
- jtp.addTab("Drinks", new JLabel("Drinks Tab"));
- jtp.addTab("Members", new JLabel("Members Tab"));
- jtp.addTab("Orders", new JLabel("Orders Tab"));
- jtp.addTab("Staff", new JLabel("Staff Tab"));
- jtp.setVisible(false);
- //puts the proceed to login button on the pane
- frame.getContentPane().setLayout(new FlowLayout());
- frame.add(showDialogButton);
- // sets the jframe up
- frame.setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
- frame.setPreferredSize(new Dimension(500, 300));
- frame.pack();
- frame.setLocationRelativeTo(null);
- frame.setVisible(true); //shows the jframe
- frame.add(jtp);
- }
- public void actionPerformed(ActionEvent e)
- {
- if(e.getSource()==showDialogButton)
- {
- String formattedDate = formatter.format(todaysDate);
- System.out.println(formattedDate + "Login called");
- while(loggedIn==false)
- {
- int result = JOptionPane.showConfirmDialog(null, stuff, "Login", JOptionPane.OK_CANCEL_OPTION, JOptionPane.PLAIN_MESSAGE);
- if(result == JOptionPane.OK_OPTION)
- {
- String tempUser = new String(username.getText());
- String tempPass = new String(password.getPassword());
- if((tempUser.equals(name))&&(tempPass.equals(pw)))
- {
- loggedIn = true;
- System.out.println(formattedDate + " - " + tempUser + " logged in");
- frame.remove(showDialogButton);
- jtp.setVisible(true);
- frame.setSize(500,299);
- JOptionPane.showMessageDialog(frame, "Login Successful! Welcome " + tempUser + "!");
- counter = 0;
- }
- else {
- frame.remove(showDialogButton);
- System.out.println(formattedDate + " - Login failed due to incorrect input");
- frame.setSize(500,299);
- JOptionPane.showMessageDialog(frame, "Login failed, either the Staff ID or password is incorrect");
- counter ++;
- System.out.println(counter + "login fails in a row");
- if(counter == 3)
- {
- frame.remove(showDialogButton);
- frame.setSize(500,299);
- JOptionPane.showMessageDialog(frame, "Login failed, too many incorrect inputs - please restart the system");
- System.out.println(formattedDate + " - Login failed due to incorrect input");
- break; //change to setting a new random password to login with then prompt to change password after
- }
- }
- }
- if(result == JOptionPane.CANCEL_OPTION)
- {
- JOptionPane.showMessageDialog(frame, "Login Cancelled");
- frame.add(showDialogButton);
- System.out.println(formattedDate + " - Login Cancelled");
- frame.setSize(500,301);
- break;
- }
- }
- }
- }
- public static void main(String[] args)
- {
- // schedule this for the event dispatch thread (edt)
- SwingUtilities.invokeLater(new Runnable()
- {
- public void run()
- {
- new Login();
- }
- });
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement