//This is my login class file rt java.awt.BorderLayout; public class Login { pub
ID: 3678904 • Letter: #
Question
//This is my login class file
rt java.awt.BorderLayout;
public class Login
{
public static void main(String[] args)
{
LoginGUI frame = new LoginGUI();
frame.setTitle("Login");
frame.setVisible(true);
frame.setSize(300,350);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
}
// This is my LoginGUI
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.border.*;
import java.io.*;
import java.util.*;
import javax.swing.JOptionPane;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
public class LoginGUI extends JFrame
{
private JTextField txtStudentID;
private JTextField txtUser;
private JPasswordField txtPass;
private JLabel lblStudentID;
private JLabel lblUser;
private JLabel lblPass;
private JButton btnLogin;
private JButton btnExit;
private JPanel panel1;
public LoginGUI()
{
setLayout(new BorderLayout(10,10));
panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(350, 350));
txtStudentID = new JTextField();
txtStudentID.setPreferredSize(new Dimension(150,25));
txtUser = new JTextField();
txtUser.setPreferredSize(new Dimension(150,25));
txtPass = new JPasswordField(15);
txtPass.setEchoChar('*');
txtPass.setPreferredSize(new Dimension(150,25));
lblStudentID = new JLabel("Student ID: ");
lblStudentID.setFont(new Font("NEW TIMES ROMAN",Font.ITALIC,15));
lblStudentID.setForeground(Color.BLUE);
lblUser = new JLabel("Username: ");
lblUser.setFont(new Font("NEW TIMES ROMAN",Font.ITALIC,15));
lblUser.setForeground(Color.BLUE);
lblPass = new JLabel("Password: ");
lblPass.setFont(new Font("NEW TIMES ROMAN",Font.ITALIC,15));
lblPass.setForeground(Color.BLUE);
btnLogin = new JButton("Login");
btnLogin.setFont(new Font("NEW TIMES ROMAN",Font.ITALIC,15));
btnLogin.setForeground(Color.RED);
btnExit = new JButton("Exit");
btnExit.setFont(new Font("NEW TIMES ROMAN",Font.ITALIC,15));
btnExit.setForeground(Color.RED);
panel1.add(lblStudentID);
panel1.add(txtStudentID);
panel1.add(lblUser);
panel1.add(txtUser);
panel1.add(lblPass);
panel1.add(txtPass);
panel1.add(btnLogin);
panel1.add(btnExit);
ActionListener LoginAction = new LoginListener();
btnLogin.addActionListener(LoginAction);
ActionListener ExitAction = new ExitListener();
btnExit.addActionListener(ExitAction);
add(panel1, BorderLayout.CENTER);
}
class ExitListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
}
class LoginListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
int valid = validate(event, txtStudentID, txtUser, txtPass);
if (valid == 0)
{
JOptionPane.showMessageDialog(null, "Your user account has been validated!");
}
else
{
JOptionPane.showMessageDialog(null, "Your Username or Password is incorrect. Please try again");
}
txtUser.setText("");
txtPass.setText("");
}
}
private static int validate(ActionEvent event, JTextField txtStudentID, JTextField txtUser, JTextField txtPass)
{
int errors = 0;
String studentID = txtStudentID.getText();
if(errors == 0)
{
try
{
Long.parseLong(studentID);
}
catch(Exception ex)
{
errors++;
JOptionPane.showMessageDialog(null, "Invalid input. Please enter a valid student ID");
}
}
String username = txtUser.getText();
if(errors == 0)
{
if(username.length() == 0)
{
errors++;
JOptionPane.showMessageDialog(null, "Invalid input. Please enter a valid username");
}
}
String password = txtPass.getText().trim();
if(errors == 0)
{
if(password.length() == 0 || password.length() < 10)
{
errors++;
JOptionPane.showMessageDialog(null, "Invalid input. Password is less than 10 characters");
}
if(password.contains(",") && errors == 0)
{
errors++;
JOptionPane.showMessageDialog(null, "Invalid input. Password cannot contain a comma");
}
if(checkForOneUppercase(password) == false && errors == 0)
{
errors++;
JOptionPane.showMessageDialog(null, "Invalid input. Password must contain at least one uppercase letter");
}
if(checkForOneDigit(password) == false && errors == 0)
{
errors++;
JOptionPane.showMessageDialog(null, "Invalid input. Password must contain at least one digit");
}
if(checkForBlanks(password) == true && errors == 0)
{
errors++;
JOptionPane.showMessageDialog(null, "Invalid input. Password must not contain blank spaces");
}
}
return errors;
}
private static boolean checkForOneUppercase(final String upperCase)
{
if(upperCase != null)
{
for (int i = 0; i < upperCase.length(); i++)
{
char c = upperCase.charAt(i);
if (Character.isLetter(c))
{
if (Character.isUpperCase(c))
{
return true;
}
}
}
}
return false;
}
private static boolean checkForOneDigit(final String digit)
{
if(digit != null)
{
for (int i = 0; i < digit.length(); i++)
{
char c = digit.charAt(i);
try
{
int> return true;
}
catch(Exception e)
{
}
}
}
return false;
}
public static boolean checkForBlanks(final String blanks)
{
if(blanks != null)
{
for(int i = 0; i < blanks.length(); i++)
{
if(Character.isWhitespace(blanks.charAt(i)))
{
return true;
}
}
}
return false;
}
class Validate
{
ArrayList<Person> persons = new ArrayList<Person>();
public Validate()
{
add();
}
public void add()
{
Person user1 = new Person ("USER1", "fBqj!P4B33", (long) 900000003);
Person user2 = new Person ("USER2", "2Hmkq98NW+", (long) 900000006);
Person user3 = new Person ("USER3", "_SNT8wsz85", (long) 900000009);
Person user4 = new Person ("USER4", "5LDz2H%t3t", (long) 900000013);
Person user5 = new Person ("USER5", "at!tbX92FC", (long) 900000016);
persons.add(user1);
persons.add(user2);
persons.add(user3);
persons.add(user4);
persons.add(user5);
}
public Boolean Verify(String u, String p, String id)
{
Person temp = new Person(u, p, Long.parseLong(id));
for (Person person:persons)
{
if (person.compareTo(temp) == 0)
{
return true;
}
}
return false;
}
}
}
// This is my person class file
public class Person implements Comparable
{
private String username;
private String password;
private Long StudentID;
public Person(String User, String Pass, Long ID)
{
this.username = User;
this.password = Pass;
this.StudentID = ID;
}
public String getPassword ()
{
return this.password;
}
public String getUsername()
{
return this.username;
}
public Long getStudentID()
{
return this.StudentID;
}
@Override
public int compareTo(Object o)
{
Person temp = (Person)o;
if (this.username == temp.username && this.password == temp.password && this.StudentID == temp.StudentID)
{
return 0;
}
else
{
return -1;
}
}
}
-the use of a "counter" to allow the user 3 failed attempts. - If the user fails three times to input the correct information, then the system will lockout for 15 seconds. - You should show a countdown window by seconds only, not the entire system time.
Explanation / Answer
Solution:
package com.nancy.experiment;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.util.ArrayList;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.Timer;
public class LoginGUI extends JFrame implements PropertyChangeListener, ActionListener
{
private JTextField txtStudentID;
private JTextField txtUser;
private JPasswordField txtPass;
private JLabel lblStudentID;
private JLabel lblUser;
private JLabel lblPass;
private JButton btnLogin;
private JButton btnExit;
private JPanel panel1;
private final JOptionPane optionPane = new JOptionPane();
private static final int TIME_OUT = 15;
private static int counter = TIME_OUT;
private final Timer timer = new Timer(1000, this);
private JDialog dialogBox = new JDialog();
private static int counting = 0;
public LoginGUI()
{
setLayout(new BorderLayout(10,10));
panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(350, 350));
txtStudentID = new JTextField();
txtStudentID.setPreferredSize(new Dimension(150,25));
txtUser = new JTextField();
txtUser.setPreferredSize(new Dimension(150,25));
txtPass = new JPasswordField(15);
txtPass.setEchoChar('*');
txtPass.setPreferredSize(new Dimension(150,25));
lblStudentID = new JLabel("Student ID: ");
lblStudentID.setFont(new Font("NEW TIMES ROMAN",Font.ITALIC,15));
lblStudentID.setForeground(Color.BLUE);
lblUser = new JLabel("Username: ");
lblUser.setFont(new Font("NEW TIMES ROMAN",Font.ITALIC,15));
lblUser.setForeground(Color.BLUE);
lblPass = new JLabel("Password: ");
lblPass.setFont(new Font("NEW TIMES ROMAN",Font.ITALIC,15));
lblPass.setForeground(Color.BLUE);
btnLogin = new JButton("Login");
btnLogin.setFont(new Font("NEW TIMES ROMAN",Font.ITALIC,15));
btnLogin.setForeground(Color.RED);
btnExit = new JButton("Exit");
btnExit.setFont(new Font("NEW TIMES ROMAN",Font.ITALIC,15));
btnExit.setForeground(Color.RED);
panel1.add(lblStudentID);
panel1.add(txtStudentID);
panel1.add(lblUser);
panel1.add(txtUser);
panel1.add(lblPass);
panel1.add(txtPass);
panel1.add(btnLogin);
panel1.add(btnExit);
ActionListener LoginAction = new LoginListener();
btnLogin.addActionListener(LoginAction);
ActionListener ExitAction = new ExitListener();
btnExit.addActionListener(ExitAction);
add(panel1, BorderLayout.CENTER);
}
class ExitListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
System.exit(0);
}
}
public void createWindow() {
timer.setCoalesce(false);
optionPane.setMessage(message());
optionPane.setMessageType(JOptionPane.INFORMATION_MESSAGE);
optionPane.setOptionType(JOptionPane.DEFAULT_OPTION);
optionPane.addPropertyChangeListener(this);
dialogBox.add(optionPane);
dialogBox.pack();
add(new JLabel(this.getTitle(), JLabel.CENTER));
dialogBox.setLocationRelativeTo(this);
dialogBox.setVisible(true);
timer.start();
}
public void propertyChange(PropertyChangeEvent e) {
String prop = e.getPropertyName();
if (JOptionPane.VALUE_PROPERTY.equals(prop)) {
function();
}
}
public void actionPerformed(ActionEvent e) {
counter--;
optionPane.setMessage(message());
if (counter == 0) {
function();
setEnabled(true);
panel1.setEnabled(true);
}
timer.restart();
}
private String message() {
return "Window closing in " + counter + " seconds.Please wait.";
}
private void function() {
dialogBox.setVisible(false);
dialogBox.dispatchEvent(new WindowEvent(
dialogBox, WindowEvent.WINDOW_CLOSING));
}
class LoginListener implements ActionListener
{
public void actionPerformed(ActionEvent event)
{
int valid = validate(event, txtStudentID, txtUser, txtPass);
if (valid == 0)
{
JOptionPane.showMessageDialog(null, "Your user account has been validated!");
}
else
{
JOptionPane.showMessageDialog(null, "Your Username or Password is incorrect. Please try again");
if(counting == 3) {
JOptionPane.showMessageDialog(null, "failing " + counting + " attempts.Please wait for 15 min.");
counting = 0;
counter = TIME_OUT;
setEnabled(false);
panel1.setEnabled(false);
createWindow();
try {
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
txtUser.setText("");
txtPass.setText("");
}
}
private static int validate(ActionEvent event, JTextField txtStudentID, JTextField txtUser, JTextField txtPass)
{
int errors = 0;
String studentID = txtStudentID.getText();
if(errors == 0)
{
try
{
Long.parseLong(studentID);
}
catch(Exception ex)
{
errors++;
counting++;
JOptionPane.showMessageDialog(null, "Invalid input. Please enter a valid student ID");
}
}
String username = txtUser.getText();
if(errors == 0)
{
if(username.length() == 0)
{
errors++;
counting++;
JOptionPane.showMessageDialog(null, "Invalid input. Please enter a valid username");
}
}
String password = txtPass.getText().trim();
if(errors == 0)
{
if(password.length() == 0 || password.length() < 10)
{
errors++;
JOptionPane.showMessageDialog(null, "Invalid input. Password is less than 10 characters");
}
if(password.contains(",") && errors == 0)
{
errors++;
JOptionPane.showMessageDialog(null, "Invalid input. Password cannot contain a comma");
}
if(checkForOneUppercase(password) == false && errors == 0)
{
errors++;
JOptionPane.showMessageDialog(null, "Invalid input. Password must contain at least one uppercase letter");
}
if(checkForOneDigit(password) == false && errors == 0)
{
errors++;
JOptionPane.showMessageDialog(null, "Invalid input. Password must contain at least one digit");
}
if(checkForBlanks(password) == true && errors == 0)
{
errors++;
JOptionPane.showMessageDialog(null, "Invalid input. Password must not contain blank spaces");
}
if(errors>0)
counting++;
}
return errors;
}
private static boolean checkForOneUppercase(final String upperCase)
{
if(upperCase != null)
{
for (int i = 0; i < upperCase.length(); i++)
{
char c = upperCase.charAt(i);
if (Character.isLetter(c))
{
if (Character.isUpperCase(c))
{
return true;
}
}
}
}
return false;
}
private static boolean checkForOneDigit(final String digit)
{
if(digit != null)
{
for (int i = 0; i < digit.length(); i++)
{
char c = digit.charAt(i);
try
{
int> return true;
}
catch(Exception e)
{
}
}
}
return false;
}
public static boolean checkForBlanks(final String blanks)
{
if(blanks != null)
{
for(int i = 0; i < blanks.length(); i++)
{
if(Character.isWhitespace(blanks.charAt(i)))
{
return true;
}
}
}
return false;
}
class Validate
{
ArrayList<Person> persons = new ArrayList<Person>();
public Validate()
{
add();
}
public void add()
{
Person user1 = new Person ("USER1", "fBqj!P4B33", (long) 900000003);
Person user2 = new Person ("USER2", "2Hmkq98NW+", (long) 900000006);
Person user3 = new Person ("USER3", "_SNT8wsz85", (long) 900000009);
Person user4 = new Person ("USER4", "5LDz2H%t3t", (long) 900000013);
Person user5 = new Person ("USER5", "at!tbX92FC", (long) 900000016);
persons.add(user1);
persons.add(user2);
persons.add(user3);
persons.add(user4);
persons.add(user5);
}
public Boolean Verify(String u, String p, String id)
{
Person temp = new Person(u, p, Long.parseLong(id));
for (Person person:persons)
{
if (person.compareTo(temp) == 0)
{
return true;
}
}
return false;
}
}
}
____________________________________
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.