everyone. I have 4 classes; FrameViewerOne is where I run the application, FrameBuilderOne contains most of the code, including binary search and many other things, Person class implements comparable and I override it with compareTo() method, and my TimerLockout class is where I switch to with a window that is supposed to populate, lock the user out for 15 seconds, and show the countdown as the timer ticks. This is supposed to occur after 3 incorrect attempts from the user. When the timer reaches 0 seconds, I want it to close the TimerLockout class and reopen(setVisible) the original frame. Unfortunately, my code compiles but I can't get the timer to populate. Thank you for your time and any help/suggestions would be greatly appreciated!
Here are the instructions: 1) Use of a "counter" to allow the user 3 failed attempts. 2) If the user fails three times to input the correct information, then the system will lockout for 15 seconds. 2.1) You should show a countdown window by seconds only, not the entire system time.
//FRAMEVIEWERONE CLASS
import javax.swing.JFrame;//The Frame class is part of the javax.swing package.Swing is the nickname for the gui onterface library in java.
import java.io.*;
public class FrameViewerOne
{
public static void main(String[] args)throws IOException{//This is a warning to the compiler that we are throwing an exception! Proper syntax is to have this AFTER the method header.
{
JFrame Frame1 = new FrameBuilderOne();
Frame1.setVisible(true);
Frame1.setSize(200,300);
Frame1.setTitle("Lab One GUI");
Frame1.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//This adds the x button on the top right. If you omit this step, the program will keep running even after you close the frame.
}
}
}
//FRAMEBUILDERONE CLASS
import javax.swing.*;//The asterik(*) tells Java to import everything that is being used in the package.
import java.awt.event.*;//Has the ActionEvent and KeyEvent. awt: Abstract Window Toolkit
import java.awt.event.ActionListener;
import java.util.*;//This does not have an asterik because I am only importing a specific tool (Scanner) from the java package,util. Scanner is the namespace of the package
import java.io.*; //Always use IOException when working with files. FileNotFoundException is a specific IO Exception. Exceptions are objects.
import javax.swing.BoxLayout;
import javax.swing.JPasswordField;
import javax.swing.border.EtchedBorder;
import javax.swing.border.TitledBorder;
public class FrameBuilderOne extends JFrame{
private JButton loginBtn, exitBtn;
private JPanel newPanel, userNamePanel, passWordPanel, studentIdPanel, buttonPanel;
private JTextField usernameTextField;
private JPasswordField passwordTextField;
private JTextField studentIdTextField;
private JLabel usernameLabel, passwordLabel, studentIdLabel;
ArrayList<Person> personAry;
private int attempts;
public FrameBuilderOne()//Constructor
{
createPanel();//Method Call
personAry = checkInfo(new File("USERDATA.txt"));
Collections.sort(personAry);
}
private void createPanel()//Actual Method being called
{
newPanel = new JPanel();//The reserve word, new, denotes that a constructor method is being called
newPanel.setBorder(new EtchedBorder());
newPanel.setBorder(new TitledBorder(new EtchedBorder()));
newPanel.setLayout(new BoxLayout(newPanel, BoxLayout.Y_AXIS));
userNamePanel = new JPanel();
usernameLabel = new JLabel("Username:");
userNamePanel.add(usernameLabel);
usernameTextField = new JTextField(10);
userNamePanel.add(usernameTextField);
newPanel.add(userNamePanel);
passWordPanel = new JPanel();
passwordLabel = new JLabel("Password:");
passWordPanel.add(passwordLabel);
passwordTextField = new JPasswordField(10);
passWordPanel.add(passwordTextField);
newPanel.add(passWordPanel);
passwordTextField.setEchoChar('*');
studentIdPanel = new JPanel();
studentIdLabel = new JLabel("Student ID:");
studentIdPanel.add(studentIdLabel);
studentIdTextField = new JTextField(10);
studentIdPanel.add(studentIdTextField);
newPanel.add(studentIdPanel);
buttonPanel = new JPanel();
loginBtn = new JButton("Login");
buttonPanel.add(loginBtn);
exitBtn = new JButton("Exit");
buttonPanel.add(exitBtn);
newPanel.add(buttonPanel);
ActionListener LoginListener = new ButtonsListen();//Create One Method since since both login and exit buttons share the same/similar task.
loginBtn.addActionListener(LoginListener);
exitBtn.addActionListener(LoginListener);
add(newPanel);
}
public ArrayList<Person> checkInfo(File data)
{
Scanner userInput;
ArrayList <Person> loginAry = new ArrayList<Person>();
try
{
userInput = new Scanner(data);//The reason why this isn't System.in is because you aren't taking input from the keyboard. You are scanning the file.
while (userInput.hasNextLine())
{
String finder = userInput.nextLine();
String [] loginInfo = finder.split(",");//This is a delimeter
String usernameInfo = loginInfo [0];
String passwordInfo = loginInfo [1];
long studentIdInfo = Long.parseLong(loginInfo[2]);
Person moreInfo = new Person(usernameInfo, passwordInfo, studentIdInfo);
loginAry.add(moreInfo);
}
userInput.close();
}
catch (FileNotFoundException e){
JOptionPane.showMessageDialog(null, "Error" + e.getMessage());
}
return loginAry;
}
public class Verify
{
/**
* This method implements the binary search within the personAry which contains the user's attributes.
* @param Person info - this is the information of the object Person which contains the username, password, and studentID.
* @return - this method returns true if info within Person is found. This returns it to the other method which displays the appropriate message to the user.
*/
public boolean validate(Person info)
{
int low = 0;
int high = personAry.size()-1;
int mid;
while(low <= high)
{
mid = (low + high) / 2;
Person middlePerson = personAry.get(mid);
int position = middlePerson.compareTo(info);
if(position < 0)
{
low = mid + 1;
}
else if(position > 0)
{
high = mid - 1;
}
else
{
return true;
}
}
return false;
}
}
public boolean validatePassword(String passwordSaver)//Check the password minimum, case sensitivity, and number in password
{
int caseCount=0;
int numCount=0;
if(passwordSaver.length() < 10)
{
JOptionPane.showMessageDialog(null, "Password must have ten characters");
}
else if(passwordSaver.length() >= 10)
for(int i = 0; i < passwordSaver.length(); i++)
{
char current = passwordSaver.charAt(i);
if(Character.isUpperCase(current)) {caseCount++;}
else if(Character.isDigit(current)){numCount++;}
}
if(caseCount == 0)
{
JOptionPane.showMessageDialog(null, "Password must have at least one uppercase character");
return false;
}
else if (numCount == 0)
{
JOptionPane.showMessageDialog(null, "Password must have at least one uppercase character");
return false;
}
else
{
return true;
}
}
public void createTimerClass()
{
this.setVisible(false);
TimerLockout Frame2 = new TimerLockout(this);
}
public class ButtonsListen implements ActionListener{
public void actionPerformed(ActionEvent e){
if(e.getSource()==loginBtn)
{
String username = usernameTextField.getText();
String id_str = studentIdTextField.getText();
long number = Long.parseLong(id_str);
String password;
char[] passArray = passwordTextField.getPassword();
password = new String(passArray);//Study this, write it out
Person txtFieldInfo = new Person(username, password, number);
Verify verifyLogin = new Verify();
if (!validatePassword(password))//also tells the user what went wrong
{
attempts++;
if(attempts == 3)
{
createTimerClass();
attempts = 0;
}
}
if (verifyLogin.validate(txtFieldInfo))
{
JOptionPane.showMessageDialog(null, "Login Successful");//JOptionPane is in the swing package. You must include null, before the message you want to display.
}
else
{
JOptionPane.showMessageDialog(null,"Invalid Login Information");
}
}
if (e.getSource()==exitBtn)
{
JOptionPane.showMessageDialog(null, "Program Shutting Down");
System.exit(0);
}
}
}
}
//PERSON CLASS
public class Person implements Comparable <Object>//The Comparable interface automatiically populates an empty CompareTo method behind the scenes.
{
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 int compareTo(Object otherPerson)//Within the CompareTo method, you must pass in an object. This is overriding the empty CompareTo Method.
{
Person other = (Person) otherPerson;//You need to cast an object. tempStorage is going to store the username, password, and studentID.
if(other.StudentID > this.StudentID)
{
return 1;
}
else if(other.StudentID < this.StudentID)
{
return -1;
}
else
{
return 0;
}
}
public String getPassword ()
{
return this.password;
}
public String getUsername()
{
return this.username;
}
public Long getStudentID()
{
return this.StudentID;
}
}
//TimerLockout Class
import javax.swing.Timer;
import javax.swing.*;
import java.awt.event.*;
import java.awt.event.ActionListener;
public class TimerLockout extends JFrame
{
private Timer timerLock;
private final int STARTING_TIME = 15;
private int currentTime = 15;
private final int ENDING_TIME = 0;
private FrameBuilderOne Frame1;
private JPanel secondPanel;
private JLabel timerMessage;
public TimerLockout(FrameBuilderOne previousFrame)
{
TimeListener listener = new TimeListener();
timerLock = new Timer(1000, listener);
Frame1 = previousFrame;
createSecondPanel();
}
private void createSecondPanel()
{
JPanel secondPanel = new JPanel();
timerMessage = new JLabel("You Are Temporarily Locked Out");
secondPanel.add(timerMessage);
ActionListener TimeListener = new TimeListener();
}
public class TimeListener implements ActionListener
{
public void actionPerformed(ActionEvent t)
{
while(currentTime >= STARTING_TIME)
{
currentTime--;
}
if(ENDING_TIME == 0)
{
Frame1.setVisible(true);
dispose();
}
}
}
}`