Facebook
From Khoa, 4 Months ago, written in Java.
Embed
Download Paste or View Raw
Hits: 213
  1. import java.util.Scanner;
  2. import java.io.FileWriter;
  3. import java.io.IOException;
  4.  
  5. public class Persons {
  6.     private String firstName;
  7.     private String lastName;
  8.     private String age;
  9.     private String phoneNumber;
  10.     private String apt;
  11.  
  12.     public Persons(String firstName, String lastName, String age, String phoneNumber, String apt) {
  13.         this.firstName = firstName;
  14.         this.lastName = lastName;
  15.         this.age = age;
  16.         this.phoneNumber = phoneNumber;
  17.         this.apt = apt;
  18.     }
  19.  
  20.     public String getFirstName() {
  21.         return firstName;
  22.     }
  23.  
  24.     public void setFirstName(String firstName) {
  25.         this.firstName = firstName;
  26.     }
  27.  
  28.     public String getLastName() {
  29.         return lastName;
  30.     }
  31.  
  32.     public void setLastName(String lastName) {
  33.         this.lastName = lastName;
  34.     }
  35.  
  36.     public String getAge() {
  37.         return age;
  38.     }
  39.  
  40.     public void setAge(String age) {
  41.         this.age = age;
  42.     }
  43.  
  44.     public String getPhoneNumber() {
  45.         return phoneNumber;
  46.     }
  47.  
  48.     public void setPhoneNumber(String phoneNumber) {
  49.         this.phoneNumber = phoneNumber;
  50.     }
  51.  
  52.     public String getApt() {
  53.         return apt;
  54.     }
  55.  
  56.     public void setApt(String apt) {
  57.         this.apt = apt;
  58.     }
  59.  
  60.     public static void main(String[] args) {
  61.         Scanner scanner = new Scanner(System.in);
  62.  
  63.         // Creating an array of Persons objects and providing information for 10 persons
  64.         Persons[] personsArray = new Persons[10];
  65.         personsArray[0] = new Persons("John", "Doe", "30", "1234567890", "101");
  66.         personsArray[1] = new Persons("Khoa", "Tran", "56", "234234234", "234");
  67.         // Add information for the remaining persons...
  68.  
  69.         // Options menu
  70.         System.out.println("Options:");
  71.         System.out.println("1. Print all persons' information to output file");
  72.         System.out.println("2. Find a person by last name");
  73.  
  74.         // Get user choice
  75.         System.out.print("Enter your choice (1 or 2): ");
  76.         int choice = scanner.nextInt();
  77.         scanner.nextLine(); // Consume the newline character
  78.  
  79.         // Perform action based on user choice
  80.         switch (choice) {
  81.             case 1:
  82.                 printAllPersonsInfo(personsArray);
  83.                 break;
  84.             case 2:
  85.                 findPersonByLastName(personsArray);
  86.                 break;
  87.             default:
  88.                 System.out.println("Invalid choice. Exiting program.");
  89.                 break;
  90.         }
  91.  
  92.         // Closing the scanner
  93.         scanner.close();
  94.     }
  95.  
  96.     private static void printAllPersonsInfo(Persons[] personsArray) {
  97.         FileWriter outputFile = null;
  98.         try {
  99.             outputFile = new FileWriter("report.txt");
  100.  
  101.             for (Persons person : personsArray) {
  102.                 if (person != null) {
  103.                     printPersonInfo(person, outputFile);
  104.                 }
  105.             }
  106.  
  107.             System.out.println("All the info has been print out to the report file!");
  108.         } catch (IOException e) {
  109.             System.out.println("Error creating the output file: " + e.getMessage());
  110.         } finally {
  111.             // Closing the output file
  112.             if (outputFile != null) {
  113.                 try {
  114.                     outputFile.close();
  115.                 } catch (IOException e) {
  116.                     System.out.println("Error closing the output file: " + e.getMessage());
  117.                 }
  118.             }
  119.         }
  120.     }
  121.  
  122.     private static void findPersonByLastName(Persons[] personsArray) {
  123.         Scanner scanner = new Scanner(System.in);
  124.         boolean personFound = false;
  125.  
  126.         System.out.print("Enter a last name to search: ");
  127.         String searchLastName = scanner.nextLine();
  128.  
  129.         FileWriter outputFile = null;
  130.  
  131.         for (Persons person : personsArray) {
  132.             if (person != null && searchLastName.equalsIgnoreCase(person.getLastName())) {
  133.                 personFound = true;
  134.                 printPersonInfo(person, null); // Do not write to output file for individual search
  135.  
  136.                 try {
  137.                     // Writing the information to the output file
  138.                     outputFile = new FileWriter("fileReport.txt", true);
  139.                     outputFile.write("Person found!\n");
  140.                     outputFile.write("First Name: " + person.getFirstName() + "\n");
  141.                     outputFile.write("Last Name: " + person.getLastName() + "\n");
  142.                     outputFile.write("Age: " + person.getAge() + "\n");
  143.                     outputFile.write("Phone Number: " + person.getPhoneNumber() + "\n");
  144.                     outputFile.write("Apartment: " + person.getApt() + "\n");
  145.                     System.out.println("Your person info has been print out to the output file!");
  146.                 } catch (IOException e) {
  147.                     System.out.println("Error writing to the output file: " + e.getMessage());
  148.                 } finally {
  149.                     if (outputFile != null) {
  150.                         try {
  151.                             outputFile.close();
  152.                         } catch (IOException e) {
  153.                             System.out.println("Error closing the output file: " + e.getMessage());
  154.                         }
  155.                     }
  156.                 }
  157.  
  158.                 break; // Exit the loop once a person is found
  159.             }
  160.         }
  161.  
  162.         if (!personFound) {
  163.             System.out.println("Person not found with the entered last name.");
  164.         }
  165.  
  166.         // Closing the scanner
  167.         scanner.close();
  168.     }
  169.  
  170.     private static void printPersonInfo(Persons person, FileWriter outputFile) {
  171.         System.out.println("Person found!");
  172.         System.out.println("First Name: " + person.getFirstName());
  173.         System.out.println("Last Name: " + person.getLastName());
  174.         System.out.println("Age: " + person.getAge());
  175.         System.out.println("Phone Number: " + person.getPhoneNumber());
  176.         System.out.println("Apartment: " + person.getApt());
  177.  
  178.         if (outputFile != null) {
  179.             try {
  180.                 // Writing the information to the output file
  181.                 outputFile.write("Person found!\n");
  182.                 outputFile.write("First Name: " + person.getFirstName() + "\n");
  183.                 outputFile.write("Last Name: " + person.getLastName() + "\n");
  184.                 outputFile.write("Age: " + person.getAge() + "\n");
  185.                 outputFile.write("Phone Number: " + person.getPhoneNumber() + "\n");
  186.                 outputFile.write("Apartment: " + person.getApt() + "\n");
  187.             } catch (IOException e) {
  188.                 System.out.println("Error writing to the output file: " + e.getMessage());
  189.             }
  190.         }
  191.     }
  192. }
  193.