Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This question is answered on the website but, the answer is not was it was asked

ID: 3805400 • Letter: T

Question

This question is answered on the website but, the answer is not was it was asked. It must contain 2 files and phone book enterys must be saved in seperate .txt file.

Write a class named PhoneBookEntry that has fields for a person’s name and phone number. The class should have a constructor and appropriate accessor and mutator methods. Then write a program that creates at least five PhoneBookEntry objects and stores them in an ArrayList. Use a loop to display the contents of each object in the ArrayList.

Explanation / Answer

Here is the working solution-

//Note -please change the output.txt file location in writeContactListOnFile(ArrayList<PhoneBookEntry> contactList) method accordingly before executing the program.

//PhoneBookEntry.java

public class PhoneBookEntry {
   private String name;
   private String phoneNumber;
  
  
   public PhoneBookEntry(String name, String phoneNumber) {
       super();
       this.name = name;
       this.phoneNumber = phoneNumber;
   }
   /**
   * @return the name
   */
   public String getName() {
       return name;
   }
   /**
   * @param name the name to set
   */
   public void setName(String name) {
       this.name = name;
   }
   /**
   * @return the phoneNumber
   */
   public String getPhoneNumber() {
       return phoneNumber;
   }
   /**
   * @param phoneNumber the phoneNumber to set
   */
   public void setPhoneNumber(String phoneNumber) {
       this.phoneNumber = phoneNumber;
   }
   /* (non-Javadoc)
   * @see java.lang.Object#toString()
   */
   @Override
   public String toString() {
       return "PhoneBookEntry [name=" + name + ", phoneNumber=" + phoneNumber + "]";
   }
  

}

//PhoneBookEntryTestClass.java

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.io.IOException;
import java.util.ArrayList;

public class PhoneBookEntryTestClass {
  
   public static void main(String[] args) {
       ArrayList<PhoneBookEntry> contactList= new ArrayList<PhoneBookEntry>(); //creating array of PhoneBookEntry object
       PhoneBookEntry entry1=new PhoneBookEntry("Rahul", "1234567890");
       PhoneBookEntry entry2=new PhoneBookEntry("Keshav", "0987654321");
       PhoneBookEntry entry3=new PhoneBookEntry("Mahesh", "8907654321");
       PhoneBookEntry entry4=new PhoneBookEntry("Ajit", "5432167890");
       PhoneBookEntry entry5=new PhoneBookEntry("Sonu", "8907654321");
       contactList.add(entry1); //adding PhoneBookEntry object into arrayList one by one
       contactList.add(entry2);
       contactList.add(entry3);
       contactList.add(entry4);
       contactList.add(entry5);
//       displayContactList(contactList); //uncomment this method if you want to display the data on console
       writeContactListOnFile(contactList); //comment this method if you do not want to store the data on file

   }

   /**method to write the contact details to output.txt file
   *
   * @param contactList
   */
   private static void writeContactListOnFile(ArrayList<PhoneBookEntry> contactList) {
       try {
           FileWriter writer = new FileWriter("C:\src\main\java\output.txt");// output file location
           BufferedWriter bufferedWriter = new BufferedWriter(writer);

           for(PhoneBookEntry pbe:contactList){
               bufferedWriter.write("person name :"+pbe.getName()+" phone number :"+pbe.getPhoneNumber());
               bufferedWriter.write(" ");
           }
          
           bufferedWriter.close();
       } catch (IOException e) {
           e.printStackTrace();
       }

   }

   /**method to display the contact details over console
   *
   * @param contactList
   */
   private static void displayContactList(ArrayList<PhoneBookEntry> contactList) {
      
       //using for-each loop to display data
       for(PhoneBookEntry pbe:contactList){
           System.out.println("person name :"+pbe.getName()+" phone number :"+pbe.getPhoneNumber());
       }
      
   }

}

//sample output.txt

person name :Rahul phone number :1234567890
person name :Keshav phone number :0987654321
person name :Mahesh phone number :8907654321
person name :Ajit phone number :5432167890
person name :Sonu phone number :8907654321

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote