Issue
I have a login system that loops through all customers to see if a particular username and password match but it does not seem to work. The customers are all in an ObservableArrayList. I referred to href="https://stackoverflow.com/questions/17526608/how-to-find-an-object-in-an-arraylist-by-property">this SO post to make it initially but it still did not work. Any help would be greatly appreciated. Thank you.
Other Info
TextField username,password;
Customer checkCustomer;
ObservableList<Customer> customers;
customers = FXCollections.observableArrayList();
Login System(GUI)
if(t.getSource() == logIn){
//Owner login
if(username.getText().equals("admin") && password.getText().equals("admin")){
System.out.println("success, owner logged in");
window.setScene(primaryOwner);
}
else {
if(findRegisteredCustomer(username.getText(),password.getText()) != null){
System.out.println("Success, customer logged in");
if(checkCustomer.getPoints() < 1000){
status = "Silver";
}
else if(checkCustomer.getPoints() > 1000){
status = "Gold";
}
welcome.setText("Welcome " + checkCustomer.getUsername() + ". " + "You have " + checkCustomer.getPoints() + " points. " + "Your status is " + status + ".");
System.out.println("success, " + checkCustomer.getUsername() + " has logged in");
window.setScene(customerStartScreen);
}
else {
System.out.println("Username: " + username.getText() + ", Password: " + password.getText());
System.out.println("failed, incorrect login details");
}
}
}
find if customer is registered method
public Customer findRegisteredCustomer(String username, String password){
for(Customer customerTest: customers){
if(customerTest.getUsername().equals(username) && customerTest.getPassword().equals(password)){
System.out.println("Registered user");
checkCustomer = customerTest;
return customerTest;
}
return null;
}
return null;
}
Customer class
public class Customer extends User {
private int points;
public Customer(String username, String password){
super(username,password);
points = 0;
}
public Customer(String username, String password, int points){
super(username,password);
this.points = points;
}
public int getPoints() {
return points;
}
public void setPoints(int points) {
this.points = points;
}
}
Solution
You return null after your if block, remove the first return null in the findRegisterdCustomer method and you should be fine. Also you might want to look into hashing, equals methods and default passwords and what the issues can be with those.
Answered By - 97hilfel
Answer Checked By - Gilberto Lyons (JavaFixing Admin)