0

I'm struggling to build a simple login method. (the worst thing is that I think the answer may be pretty obvious, so sorry for anything)

I have a type Client that has the client properties (name, email, username, password, age, etc.), that is storaged in a ClientRepository class. My idea is to compare the user and password til I have the right match and return the object Client logged, so I can access its properties outside.

I have two questions:

1 - The method below must have to return a Client type so I can access its properties later, right? Or theres a better way of doing a login method?

2 - What do I return in the "elses" below (when the login fails)?

Thanks

public Client logIn(String user, String password){
    Cliente logged;
    for(int i = 0; i < clients.size(); i++){
        if(clients.get(i).getUser().equals(user)){
            if(clients.get(i).getPassword().equals(password)){
                logged = clients.get(i);
                System.out.println("LOGIN SUCCESFULL");
                return logged;
            }else{
                System.out.println("WRONG PASSWORD!");
            }
        }else{
            System.out.println("USER DOESNT EXISTS");
        }

    }
}
herb
  • 1
  • 2
  • 1
    1. Seems reasonable. 2. Throw an `Exception` (or return `null`), but I would suggest you throw an `Exception`. 3. [Beware](https://stackoverflow.com/q/513832/2970947), that is not how you compare `String`s in Java. – Elliott Frisch Dec 19 '22 at 01:57
  • Thanks Elliott! This is exactly the answer I was looking for. And thanks for the warning about the String comparison too, gonna fix it! – herb Dec 19 '22 at 02:06

0 Answers0