import java.io.File; import java.io.FileNotFoundException; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Scanner; public class Lab08 { public static void main(String[] args) { ArrayList roster = new ArrayList(); Scanner in = new Scanner(System.in); int choice = 0; while (choice != 7) { System.out.println("\n1. Add a person to the roster"); System.out.println("2. Remove a person from the roster"); System.out.println("3. Clear the roster"); System.out.println("4. Print the roster"); System.out.println("5. Save the roster"); System.out.println("6. Load a roster"); System.out.println("7. Quit"); do { System.out.print("\nEnter your choice: "); choice = in.nextInt(); if (choice < 1 || choice > 7) { System.out.println("Invalid choice"); } } while (choice < 1 || choice > 7); in.nextLine(); switch (choice) { case 1: System.out.println("Creating a new user...\n"); System.out.print("Enter the first name: "); String firstName = in.nextLine(); System.out.print("Enter the last name: "); String lastName = in.nextLine(); roster.add(new Person(firstName, lastName)); break; case 2: System.out.print("Enter an index to remove: "); int index = in.nextInt(); if (index < 0 || index >= roster.size()) { System.out.println("Invalid index."); } else { roster.remove(index); } break; case 3: roster.clear(); System.out.println("Roster cleared."); break; case 4: for (int i = 0; i < roster.size(); i++) { System.out.println(i + ". " + roster.get(i)); } break; case 5: System.out.print("Enter a file to save to: "); String fileName = in.nextLine(); PrintWriter pw = null; for (Person person : roster) { person.writeTo(pw); } break; case 6: System.out.print("Enter a roster to load: "); String fileName = in.nextLine(); Scanner s = null; Person p = Person.createPerson(s); int i; for (i = 0; p != null; i++) { roster.add(p); p = Person.createPerson(s); } System.out.println(i + " entries loaded."); break; case 7: System.out.println("Goodbye!"); } } } }