Simple Sign in Application

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • Zen25
    New Member
    • Mar 2017
    • 3

    Simple Sign in Application

    This is a basic sign in application I created. Currently I'm studying Java Programming and doing this to practice for a future assignment. Please comment whether if my code is okay or if it needs improvement; and if it needs to be improved how do I do it.

    Code:
    import java.util.*;
    
    class LogIn {
        Scanner key = new Scanner (System.in);
        private String username = "Kate", password = "abc789";
        private String user, pass;
        
        public void SignIn(){
            System.out.println("Enter your username");
            user = key.next();
            System.out.println("Enter your password");
            pass = key.next();
            
        if ((user .equals(username)) && (pass .equals(password)))
            System.out.println("You've successfuly signed in");
        else
            System.out.println("Sign in details incorrect");
        }
    }

    --MAIN CLASS--
    Code:
    public class BankApp {
    
        public static void main(String[] args) {
            LogIn log = new LogIn();
            log.SignIn();
        }
    }
  • chaarmann
    Recognized Expert Contributor
    • Nov 2007
    • 785

    #2
    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 "successful ly" instead of "successful y".
    3.) Use a code formatter. It will also remove unneeded spaces and brackets according to java coding standard. So it would change
    Code:
     if ((user .equals(username)) && (pass .equals(password)))
    to
    Code:
     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:
    Code:
    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:
    Code:
    boolean credentialsAreCorrect = (USERNAME.equals(user) && PASSWORD.equals(pass));
    String message = (credentialsAreCorrect) ? "You've successfully ..." : "Sign in details ...";
    System.out.println(message);

    Comment

    Working...