1.) As of java coding standard, use upper case only for class names, but lower case for method names.
So change your method name "SignIn" to "signIn".
2.) Use complete english sentences, ending with dot or whatever. So change "Sign in details incorrect" to "Sign in details
are incorrect
."
(By the way, don't you want the user to retry in this case?)
Also all other sentences are missing the end. "Enter your password" should be "Enter your password:". Look for spelling mistakes. Write "successfully" instead of "successfuly".
3.) Use a code formatter. It will also remove unneeded spaces and brackets according to java coding standard. So it would change
- if ((user .equals(username)) && (pass .equals(password)))
-
to
- if (user.equals(username) && pass.equals(password))
-
4.) use meaningful names and no (homebrew) abbreviations. "pass" means something like "do not pass". So others prefer maybe "pwd" or "passwd" or "pword". So do not use abbreviations! Just name it how it sounds: "password".
If you already have used this variable name, then think how both differ from each other, for example use currentPassword and defaultPassword.
5.) Always use brackets in if-else-statements,; even if there currently is only one statement. (This is part of many coding standards). So other programmers can easily extend the code.
6.) username and password are constants. Coding standard tells you to write them all in uppercase. Correct is:
- private static String USERNAME= "Kate", PASSWORD = "abc789";
-
7.) follow DRY (Don't repeat yourself) principle:
Just think if you don't want to print the message to console but to show a popup or whatever, you have to change it on 2 places instead of one and have to retest 2 cases instead of one.
Also what if input strings are null? Just revert the comparison so it cannot crash!
Therefore I suggest following:
- boolean credentialsAreCorrect = (USERNAME.equals(user) && PASSWORD.equals(pass));
-
String message = (credentialsAreCorrect) ? "You've successfully ..." : "Sign in details ...";
-
System.out.println(message);