Folow up of diary-applications-with-accountsDiary ClassAccountLogin objAccountLogin = new AccountLogin();AccountRemover objAccountRemover = new AccountRemover();AccountCreator objAccountCreator = new AccountCreator();Accounts objAccounts = new Accounts();Exit objExit = new Exit();public static void main(String[] args) {        Diary d = new Diary();        d.startDiary();    }public void startDiary(){    objAccounts.loadAccounts();    while(true){    showMainMenu();    usersChoice();    }}public void showMainMenu(){    System.out.println();    System.out.println(Welcome to Diary!);    System.out.println(1- New Account);    System.out.println(2- Login To Your Account);    System.out.println(3- Remove Account);    System.out.println(4- Exit);    System.out.println();}public void usersChoice(){    int choice = 0;    Scanner scan = new Scanner(System.in);    while(true){        try{            scan = new Scanner(System.in);            System.out.print(Choose an option: );            choice = scan.nextInt();            break;        }        catch(Exception e){            System.out.println(Invalid Input!\n);            }        }    switch (choice){        case 1:            choiceNewAccount(scan);            break;        case 2:            choiceLogin(scan);            break;        case 3:            choiceRemoveAccount(scan);            break;        case 4:            exit(scan);            break;        default:            System.out.println(Invalid Input!\n);            break;        }}public void choiceNewAccount(Scanner scan){    objAccountCreator.createAccount(scan);}public void choiceLogin(Scanner scan){    objAccountLogin.login(scan);}public void choiceRemoveAccount(Scanner scan){    objAccountRemover.removeAccount(scan);       }public void exit(Scanner scan){    objExit.tryExit(scan);    }                    }AccountCreatorpublic void createAccount(Scanner scan){    try{        System.out.print(Enter Your Username: );        String username = scan.next();               if(!isValidUsername(username)) System.out.println(Username Already Exists!);        else{            while(true){                System.out.print(Enter Your Password: );                String pass1 = scan.next();                System.out.print(Re-Enter Your Passowrd: );                String pass2 = scan.next();                if(!arePasswordsMatch(pass1,pass2)) System.out.println(Passowrds Don't Match!);                else {                    addToAccountsList(username,pass1);                    createTheUsersFile(username);                    System.out.println(The Account Has Been Successfully Created!);                    break;                }            }        }        } catch(Exception e){            System.out.println(Could Not Create Account!);        }    }private boolean isValidUsername(String username){              return getUsernamesList().stream().noneMatch((valid) -> (valid.equals(username)));}private boolean arePasswordsMatch(String pass1, String pass2){           return pass1.equals(pass2); }private boolean createTheUsersFile(String username){    File newUserFile = new File(D:+username+.txt);    try {        newUserFile.createNewFile();    } catch (IOException ex) {        Logger.getLogger(AccountCreator.class.getName()).log(Level.SEVERE, null, ex);    }    return newUserFile.exists();}AccountLoginprotected String loginName(Scanner scan){    System.out.print(Enter Username: );    String username = scan.next();    System.out.print(Enter Password: );    String password = scan.next();    if(getUsernamesList().contains(username)){        if(password.equals(getPasswordsList().get(getUsernamesList().indexOf(username)))) return username;        return -1;    }    return -1;}public void login(Scanner scan){    String username = loginName(scan);    if(username.equals(-1)) System.out.println(Can Not Log In!);    else{        System.out.printf(%s Has Logged In! \n,username);        System.out.println();        outerloop:        while(true){        showLoginMenu();        int usersLoginChoice = usersChoice(scan,username);        if(usersLoginChoice == 3) break;        switchTo(usersLoginChoice,scan,username);        }    }      }private void showLoginMenu(){    System.out.println();    System.out.println(1- Write Diary);    System.out.println(2- Read Diary);    System.out.println(3- Logout);    System.out.println();}private int usersChoice(Scanner scan , String username){    System.out.print(Choose An Option: );     while(true){        try{            return scan.nextInt();        } catch(Exception e){            System.out.println(Invalid Input!);            return -1;        }    }}private void switchTo(int usersLoginChoice, Scanner scan, String username){    switch(usersLoginChoice){        case 1:            writeDiary(scan,username);            break;        case 2:            readDiary(username);            break;        default:            System.out.println(Invalid Input!);            break;    }}private void writeDiary(Scanner scan, String username){    File diaryFile = new File(D:+username+.txt);    String input = 1;    System.out.println(Start Writing Your Diary (-1 to stop): );    while(!input.equals(-1)){                   try (PrintWriter writeDiary = new PrintWriter(new BufferedWriter(new FileWriter(diaryFile, true)))){            input = scan.nextLine();            if(!input.equals(-1)) writeDiary.println(input);        } catch(Exception e){            System.out.println(Could Not Write Diary!);            break;        }    }    System.out.println();    System.out.println(Diary Saved!);}private void readDiary(String username){           File diaryFile = new File(D:+username+.txt);    try(Scanner readDiary = new Scanner(diaryFile)){        while(readDiary.hasNext()) System.out.println(readDiary.nextLine());    } catch(Exception e){        System.out.println(Could Not Open Diary File!);    }               }AccountRemoverpublic void removeAccount(Scanner scan){        remove(scan);        refresh(); }public void remove(Scanner scan){    String accountName = loginName(scan);    if(!accountName.equals(-1)){    removeFile(accountName);    removeFromList(accountName);    System.out.println(Account Has Been Successfully Removed!);    }    else System.out.println(Could Not Remove Account!);}public void removeFile(String accountName){    File deleteUsername = new File(D:+accountName+.txt);    deleteUsername.delete();         }public void removeFromList(String accountName){                   if(!accountName.equals(-1)) {         for(int i = 0; i < getUsernamesList().size();i++)                          if(getUsernamesList().get(i).equals(accountName)){                getUsernamesList().remove(i);                getPasswordsList().remove(i);                break;            }    }    else System.out.println(Can Not Erase Account!);}public void refresh(){    refreshUsernames();    refreshPasswords();}public void refreshUsernames(){    try (PrintWriter usernamesOverWriter = new PrintWriter(new BufferedWriter(new FileWriter(this.usernamesFile)))){        getUsernamesList().stream().forEach((b) -> {        usernamesOverWriter.println(b);                });    usernamesOverWriter.flush();    } catch (IOException ex) {        Logger.getLogger(AccountRemover.class.getName()).log(Level.SEVERE, null, ex);    }}public void refreshPasswords(){    try (PrintWriter passwordsOverWriter = new PrintWriter(new BufferedWriter(new FileWriter(this.passwordsFile)))){        getPasswordsList().stream().forEach((b) -> {        passwordsOverWriter.println(b);                });    passwordsOverWriter.flush();    } catch (IOException ex) {        Logger.getLogger(AccountRemover.class.getName()).log(Level.SEVERE, null, ex);    }}Exitpublic char readExit(Scanner scan){    System.out.print(Are you sure you want to exit? (y/n): );    return scan.next().charAt(0);}public void tryExit(Scanner scan){    boolean invalidExitInput = true;    while(invalidExitInput){        invalidExitInput = false;        char userConfirmedExit = readExit(scan);        if(userConfirmedExit == 'y') System.exit(0);        else if(userConfirmedExit != 'n') {            System.out.println(Invalid Input);            invalidExitInput = true;        }    }}Accountsprotected File usernamesFile = new File(D:Usernames.txt);protected File passwordsFile = new File(D:Passwords.txt);private static ArrayList<String> accounts_Usernames = new ArrayList<>();private static ArrayList<String> accounts_Passwords = new ArrayList<>();  protected void addToAccountsList(String username, String password){           addToAccounts(username,password);    loadAccounts();}private void addToAccounts(String username,String password){    addToUsernames(username);    addToPasswords(password);}private void addToUsernames(String username){    if(usernamesFile.exists()){        try (PrintWriter usernamesWriter = new PrintWriter(new BufferedWriter(new FileWriter(this.usernamesFile, true)))) {            usernamesWriter.println(username);            usernamesWriter.flush();        } catch (IOException ex) {            Logger.getLogger(Accounts.class.getName()).log(Level.SEVERE, null, ex);        }    } else System.err.println(File: Usernames Does Not Exist!);       }private void addToPasswords(String password){    if(passwordsFile.exists()){            try (PrintWriter passwordsWriter = new PrintWriter(new BufferedWriter(new FileWriter(this.passwordsFile, true)))) {                passwordsWriter.println(password);                passwordsWriter.flush();            } catch (IOException ex) {            Logger.getLogger(Accounts.class.getName()).log(Level.SEVERE, null, ex);        }    } else System.err.println(File: Passwords Does Not Exist!);  }protected void loadAccounts(){    loadUsernames();    loadPasswords();}private void loadUsernames(){           try (Scanner usernamesScanner = new Scanner(usernamesFile)) {        while(usernamesScanner.hasNext()){            String username = usernamesScanner.next();            if(!accounts_Usernames.contains(username))accounts_Usernames.add(username);        }    } catch (FileNotFoundException ex) {        Logger.getLogger(Accounts.class.getName()).log(Level.SEVERE, null, ex);    }}private void loadPasswords(){    try (Scanner passwordsScanner = new Scanner(passwordsFile)) {        while(passwordsScanner.hasNext()){            String password = passwordsScanner.next();            if(!accounts_Passwords.contains(password))accounts_Passwords.add(password);        }    } catch (FileNotFoundException ex) {        Logger.getLogger(Accounts.class.getName()).log(Level.SEVERE, null, ex);    }}protected ArrayList<String> getUsernamesList(){    return accounts_Usernames;}protected ArrayList<String> getPasswordsList(){    return accounts_Passwords;} 
Diary Application with accounts (v.2)
java;console;authentication
Diary class should have its collaborators passed in constructor - API doesn't lie about its dependencies then, and code can be easily tested thanks to not being forced to use concrete object instances. In most cases, things like loading collaborators should be done in constructor.class Dairy {   AccountLogin objAccountLogin;   AccountRemover objAccountRemover;   AccountCreator objAccountCreator;   Accounts objAccounts;   Exit objExit;   public static void main(String[] args) {       Diary d = new Diary(new AccountLogin(), new AccountRemover(), ...);       d.startDiary();   }   public Dairy(AccountLogin accountLogin, AccountRemover accountRemover, AccountCreator accountCreator   Accounts accounts, Exit exit) {       this.objAccountLogin = accountLogin;       this.objAccountRemover = accountRemover;       //etc.   }}In your switch cases your methods just delegate to another single ones - it leads to spaghetti code - you can do it in your case: switch (choice){    case 1:        objAccountCreator.createAccount(scan);        break; }If there'd appear more logic than one line of code in your choiceNewAccount() method, it can be left, but there's no need for it to be accesible for other objects - make it private.Why loginName() in AccountLogin is protected? You don't seem to have any class inheritance, different packages, so you can make it public.In AccountCreator you loose all information about error and its place - log this information contained in e:catch (Exception e) {   System.out.println(Could Not Create Account!);   e.printStackTrace(); }loadUsernames() and loadPasswords() duplciate code. Do job in loadAccounts() and pass files as a parameter, to be explicit in what those methods need to work. In other methods consider passing parameters for preventing prematurely coupled design.