//This is my login class file rt java.awt.BorderLayout; public class Login { pub
ID: 3675004 • 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;
}
}
}
You need to implement/add the Binary Search for you data. You need to decide how to sort the data first with either username or Student ID. You can use the "collections.sort()" method. You cannot use the "collections.binarySearch()" method. This is where you need to do your own.
Do not remove the "compareTo()" method in the "Person" class.
Explanation / Answer
class Validate {
ArrayList<Person> persons = new ArrayList<Person>();
Comparator<Person> compareByName = new Comparator<Person>() {
public int compare(Person arg0, Person arg1) {
return arg0.getUsername().compareTo(arg1.getUsername());
}
};
Comparator<Person> compareById = new Comparator<Person>() {
public int compare(Person o1, Person o2) {
return (int) (o1.getStudentID() - o2.getStudentID());
}
};
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 binarySearch(String userName) {
Collections.sort(persons, compareByName);
int start = 0, end = persons.size() - 1;
while (start <= end) {
int m = start + (end - start) / 2;
if (persons.get(m).getUsername().equalsIgnoreCase(userName))
return true;
if (persons.get(m).getUsername().compareToIgnoreCase(userName) < 0)
start = m + 1;
else
end = m - 1;
}
return false;
}
public Boolean binarySearch(long id) {
Collections.sort(persons, compareById);
int start = 0, end = persons.size() - 1;
while (start <= end) {
int m = start + (end - start) / 2;
if (persons.get(m).getStudentID() == id)
return true;
if (persons.get(m).getStudentID() < id)
start = m + 1;
else
end = m - 1;
}
return false;
}
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.