I created a simple log-in system in Java for a project. Security isn't a concern here, hence the plaintext password, but I'm interested in how I can improve the quality of my code. Everything seems to work but it just feels like messy code.
public boolean logIn(String name, String password)
{
if (getLoggedInUser() != null)
{
throw new IllegalArgumentException("You are already logged in");
}
boolean login = false;
for (User user: getUsers())
{
if (user.toString().equals(name) && user.getPassword().equals(password))
{
loggedInUser = user;
login = true;
}
}
if (!login) //what if the username + password combo is not correct
{
for (User user: getUsers())
{
if (user.toString().equals(name))
{
throw new IllegalArgumentException("Your password is incorrect");
}
}
throw new IllegalArgumentException("Your username is incorrect");
}
return login;
}
Code Review aims to help improve working code. If you are trying to figure out why your program crashes or produces a wrong result, ask on Stack Overflow instead. Code Review is also not the place to ask for implementing new features.
\$\endgroup\$getLoggedInUser
andgetUsers
methods,User
object, ect. \$\endgroup\$