Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

I need Help with list in my Lab 3 Lab3: 1) Create data storage for each \"user\"

ID: 3669333 • Letter: I

Question

I need Help with list in my Lab 3

Lab3:

1) Create data storage for each "user" with the attached "Person.java" file.

2) Implement the CompareTo() method from the "Comparable" interface inside the Person class.

3) Store all the users in an arrayList of "Person" objects.

4) Use an enhanced for loop to iterate through the arrayList of person objects and then compare the "temp" Person from the user input.

5) In this version, only return zero if the objects match (meaning password=password, username=username, and studentID=studentID)

This is my Person.java File

public class Person
{
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;
}
}

This is my LOGIN.java file

import javax.swing.JFrame;
import java.awt.BorderLayout;
public class Login
{
public static void main(String[] args)
{
LoginGUI cuFrame = new LoginGUI();
cuFrame.setVisible(true);
cuFrame.setTitle("Login");
cuFrame.setSize(300,150);
cuFrame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  
}
}

This is my LOGINGUI.java

import java.awt.event.*;

import java.awt.event.KeyAdapter;

import java.awt.event.ActionListener;
import java.awt.BorderLayout;
import java.awt.Dimension;
import javax.swing.border.*;
import javax.swing.*;

public class LoginGUI extends JFrame
{
private JTextField txtUser;
private JTextField txtPass;
private JTextField txtStuID;
private JLabel lblUser;
private JLabel lblPass;
private JLabel lblStuID;
private String keep = "";
private String finalPassword = "";
private JButton btnLogin;
private JButton btnExit;
private JPanel panel1;
  
public LoginGUI()
{
setLayout(new BorderLayout(10,10));
panel1 = new JPanel();
panel1.setPreferredSize(new Dimension(250, 250));
  
txtUser = new JTextField();
txtUser.setPreferredSize(new Dimension(150,20));
  
txtPass = new JTextField();
txtPass.setPreferredSize(new Dimension(150,20));
  
txtPass.addKeyListener(new KeyAdapter() {
public void keyTyped(KeyEvent arg0) {
JTextField textField = (JTextField) arg0.getSource();
keep += arg0.getKeyChar();
String pressedKey = arg0.getKeyText(arg0.getKeyChar());
String temp = "";
if (arg0.getKeyChar() == KeyEvent.VK_BACK_SPACE) {
finalPassword = finalPassword.substring(0,
finalPassword.length() - 1);
for (int i = 0; i < finalPassword.length(); i++) {
temp += "*";
}
textField.setText(temp);
} else if ("Enter".equals(pressedKey)) {
System.out.println(finalPassword);
} else {
finalPassword += (String.valueOf(arg0.getKeyChar()));
  
arg0.setKeyChar('*');
}} });

txtStuID = new JTextField();
txtStuID.setPreferredSize(new Dimension(150,20));
lblUser = new JLabel("Username: ");
lblPass = new JLabel("Password: ");
lblStuID = new JLabel("StudentID: ");
  
btnLogin = new JButton("Login");
btnExit = new JButton("Exit");
  
panel1.add(lblUser);
panel1.add(txtUser);
panel1.add(lblPass);
panel1.add(txtPass);
panel1.add(lblStuID);
panel1.add(txtStuID);
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)
{
Validate V = new Validate();
//String pw = txtPass.getText() ;
if (!keep.matches(".*[A-Z].*[0-9].*") && keep.length() < 10 )
{
JOptionPane.showMessageDialog(null,
"Your Password needs atleast one Captial Letter or One Number Your Password must contain atleast 10 Characters ");
}
else if (keep.contains(",") || keep.contains(" "))
{
JOptionPane.showMessageDialog(null,
"Your Password must not have Comma or Space");
}
else if (V.Verify(txtUser.getText(), keep, txtStuID.getText()))
{

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("");
txtStuID.setText("");
}
}
  
  
}

This is my Validate.java file

import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;
class Validate
{
public Boolean Verify(String u, String p, String i)
{
try
{
Scanner FileScan = new Scanner(new File("USERDATA2.txt"));
while (FileScan.hasNextLine())
{
String S=FileScan.nextLine();
String[] Sa= S.split(",");
if( (u.equals(Sa[0])) && (p.equals(Sa[1])) && (i.equals(Sa[2])) )
{
FileScan.close();
return true;
}
}
FileScan.close();
}
catch(IOException e)
{
JOptionPane.showMessageDialog(null, e.getMessage());
}
return false;
}
}

This is my USERDATE2.txt if you need it

USER1, fBqj!P4B33, 900000003
USER2, 2Hmkq98NW+, 900000006
USER3, _SNT8wsz85, 900000009
USER4, 5LDz2H%t3t, 900000013
USER5, at!tbX92FC, 900000016
USER6, Kevin123456, 900

Explanation / Answer

You didn't mention where to create the data source.I did that in Validate.java and instead of using data from file for verifying user I am using the arraylist of persons which contains the same data which was there in the file.

If you have any questions please comment on the answer

Person.java

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;
}

//CompareTo method
@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;
   }
   return -1;
}
}

Validate.java

import java.util.*;
import java.io.*;
import javax.swing.JOptionPane;
class Validate
{
   //Arraylist of Person
   ArrayList<Person> persons=new ArrayList<Person>();
  
   public Validate()
   {
       //add method will be called automatically when an instance of Validate is createdS
       add();
   }
  
   //Method for adding all the users in given file USERDATE2 in the arraylist
   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);
       Person user6=new Person("USER6","Kevin123456", (long) 900);
       persons.add(user1);
       persons.add(user2);
       persons.add(user3);
       persons.add(user4);
       persons.add(user5);
       persons.add(user6);
   }
  
  
   public Boolean Verify(String u, String p, String i)
   {
       //Converting user input to a person object
       //had to parse string input for id to long
       Person temp=new Person(u,p,Long.parseLong(i));
       //enhanced for loop
       for(Person person:persons)
       {
           if(person.compareTo(temp)==0)
           {
               return true;
           }
       }
       return false;
   }
}

I have changed only these two files others are unchanged

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote