import java.util.Scanner; import java.io.FileWriter; import java.io.IOException; public class Persons { private String firstName; private String lastName; private String age; private String phoneNumber; private String apt; public Persons(String firstName, String lastName, String age, String phoneNumber, String apt) { this.firstName = firstName; this.lastName = lastName; this.age = age; this.phoneNumber = phoneNumber; this.apt = apt; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } public String getAge() { return age; } public void setAge(String age) { this.age = age; } public String getPhoneNumber() { return phoneNumber; } public void setPhoneNumber(String phoneNumber) { this.phoneNumber = phoneNumber; } public String getApt() { return apt; } public void setApt(String apt) { this.apt = apt; } public static void main(String[] args) { Scanner scanner = new Scanner(System.in); // Creating an array of Persons objects and providing information for 10 persons Persons[] personsArray = new Persons[10]; personsArray[0] = new Persons("John", "Doe", "30", "1234567890", "101"); personsArray[1] = new Persons("Khoa", "Tran", "56", "234234234", "234"); // Add information for the remaining persons... // Options menu System.out.println("Options:"); System.out.println("1. Print all persons' information to output file"); System.out.println("2. Find a person by last name"); // Get user choice System.out.print("Enter your choice (1 or 2): "); int choice = scanner.nextInt(); scanner.nextLine(); // Consume the newline character // Perform action based on user choice switch (choice) { case 1: printAllPersonsInfo(personsArray); break; case 2: findPersonByLastName(personsArray); break; default: System.out.println("Invalid choice. Exiting program."); break; } // Closing the scanner scanner.close(); } private static void printAllPersonsInfo(Persons[] personsArray) { FileWriter outputFile = null; try { outputFile = new FileWriter("report.txt"); for (Persons person : personsArray) { if (person != null) { printPersonInfo(person, outputFile); } } System.out.println("All the info has been print out to the report file!"); } catch (IOException e) { System.out.println("Error creating the output file: " + e.getMessage()); } finally { // Closing the output file if (outputFile != null) { try { outputFile.close(); } catch (IOException e) { System.out.println("Error closing the output file: " + e.getMessage()); } } } } private static void findPersonByLastName(Persons[] personsArray) { Scanner scanner = new Scanner(System.in); boolean personFound = false; System.out.print("Enter a last name to search: "); String searchLastName = scanner.nextLine(); FileWriter outputFile = null; for (Persons person : personsArray) { if (person != null && searchLastName.equalsIgnoreCase(person.getLastName())) { personFound = true; printPersonInfo(person, null); // Do not write to output file for individual search try { // Writing the information to the output file outputFile = new FileWriter("fileReport.txt", true); outputFile.write("Person found!\n"); outputFile.write("First Name: " + person.getFirstName() + "\n"); outputFile.write("Last Name: " + person.getLastName() + "\n"); outputFile.write("Age: " + person.getAge() + "\n"); outputFile.write("Phone Number: " + person.getPhoneNumber() + "\n"); outputFile.write("Apartment: " + person.getApt() + "\n"); System.out.println("Your person info has been print out to the output file!"); } catch (IOException e) { System.out.println("Error writing to the output file: " + e.getMessage()); } finally { if (outputFile != null) { try { outputFile.close(); } catch (IOException e) { System.out.println("Error closing the output file: " + e.getMessage()); } } } break; // Exit the loop once a person is found } } if (!personFound) { System.out.println("Person not found with the entered last name."); } // Closing the scanner scanner.close(); } private static void printPersonInfo(Persons person, FileWriter outputFile) { System.out.println("Person found!"); System.out.println("First Name: " + person.getFirstName()); System.out.println("Last Name: " + person.getLastName()); System.out.println("Age: " + person.getAge()); System.out.println("Phone Number: " + person.getPhoneNumber()); System.out.println("Apartment: " + person.getApt()); if (outputFile != null) { try { // Writing the information to the output file outputFile.write("Person found!\n"); outputFile.write("First Name: " + person.getFirstName() + "\n"); outputFile.write("Last Name: " + person.getLastName() + "\n"); outputFile.write("Age: " + person.getAge() + "\n"); outputFile.write("Phone Number: " + person.getPhoneNumber() + "\n"); outputFile.write("Apartment: " + person.getApt() + "\n"); } catch (IOException e) { System.out.println("Error writing to the output file: " + e.getMessage()); } } } }