Hello, I'm working on a 2-Step authentication program, that currently stores a user's account info such as username, password, and twoStepChoice within an 'Account' object, and each of those objects is stored within an ArrayList of type 'Account' and my question is, before ending the program, how can I save those 'Account' objects onto a .txt file, of course, with their respective account details still linked, onto a .txt file, and when restarting the program, how can I read in the objects with their respective details?
My Code Below (Excuse my comments, they're just for recalling what each method does):
Note: If you run the code, I do output the account details, which security-wise you don't want to do of course, but this is just to see that everything is working and being saved properly. Additionally, any advice, words of improvement, or positive criticism is very much welcomed.
The class that deals with the functions of the program:
package LoginProject; import java.util.ArrayList; import java.util.Scanner; public class LoginFunctions { // ArrayList to hold 'Account' objects, Scanner for input, 'option' for methods, 'twoStep' for checking whether to execute 2-Step Authentication or not private static ArrayList<Account> myAccount = new ArrayList<Account>(); private static Scanner sc = new Scanner(System.in); private static int option; private static boolean twoStep; // Used to display Account info public static void displayAccount() { for(int i=0; i<myAccount.size(); i++) { System.out.println("\nUsername: " + myAccount.get(i).getUsername()); System.out.println("Password: " + myAccount.get(i).getPassword()); System.out.println("2-Step Authentication Choice: " + myAccount.get(i).isTwoStepChoice()); } } // Used to check if there exists an account with the String 'username' passed private static int findAccount(String username) { for(int i=0; i<myAccount.size(); i++) { if(myAccount.get(i).getUsername().equals(username)) return i; } return -1; } // Used to add/create a new Account private static void addAccount(String username, String password, boolean twoStepChoice) { if(findAccount(username) == -1) { myAccount.add(new Account(username, password, twoStepChoice)); } else { System.out.println("Username already taken."); } } // Used to print the options at the start of the program public static void printOptions() { boolean valid = false; while(!valid) { System.out.print("If you're a new user, enter 1, else, enter 2: "); valid = validate(); sc.nextLine(); } newOrOldUser(); } // Used to either create the new Account, or execute the 2-Step Authentication/proceed with the program private static void newOrOldUser() { String username, password; boolean valid = false; switch(option) { case 1: System.out.print("\nEnter your Username: "); username = sc.nextLine(); System.out.print("\nEnter your desired password: "); password = sc.nextLine(); // Prompts user if they would like to activate 2-Step or not, with validation while(!valid) { System.out.print("\nIf you would like to use 2-Step Authentication, enter 1, otherwise, enter 2: "); valid = validate(); sc.nextLine(); } if(option == 1) twoStep = true; else twoStep = false; addAccount(username, password, twoStep); break; case 2: System.out.println("\nWelcome back."); break; } } // Used to validate the given input at the start of the program (new or existing user) private static boolean validate() { boolean valid = false;; if(sc.hasNextInt()) { option = sc.nextInt(); if(option == 1 || option == 2) { valid = true; } else { System.out.println("Invalid number. Please try again.\n"); } } else { System.out.println("Invalid option. Please try again.\n"); sc.next(); } return valid; } private static void saveAccount() { } }//end LoginFunctions
Account Class:
package LoginProject; public class Account { private String username; private String password; private boolean twoStepChoice; public Account(String username, String password, boolean twoStepChoice) { this.username = username; this.password = password; this.twoStepChoice = twoStepChoice; } public String getUsername() { return username; } public String getPassword() { return password; } public boolean isTwoStepChoice() { return twoStepChoice; } //To create new 'Account' objects public static Account createAccount(String username, String password, boolean twoStepChoice) { return new Account(username, password, twoStepChoice); } }//end Accounts
The Main Class:
package LoginProject; import java.util.Scanner; public class TwoStepVerification { private static Scanner sc = new Scanner(System.in); private static LoginFunctions lf = new LoginFunctions(); public static void main(String[] args) { System.out.print("Welcome to Blank Program!\n\n"); lf.printOptions(); lf.displayAccount(); System.out.println("\nProgram ran successfully!"); }//end MAIN }//end CLASS