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

The objective of this (JAVA) assignment is to implement a simple menu driver pro

ID: 3708972 • Letter: T

Question

The objective of this (JAVA) assignment is to implement a simple menu driver program based on PhoneBookEntry program developed earlier. Declare a class Main that contains the main method and is the entry point to the program. PhoneBookEntries are stored in a CSV file that needs to be loaded at the beginning of the execution and saved at the end of the execution. The program should allow a case-insensitive search by any substring of first name, last name, or phone number (just like smartphones). The program should also allow for new entry to be added, and an existing entry to be deleted by entry number.

Here is what the menu will look like:

Phone Book

==========

1. Find an entry

2. Add a new entry

3. Remove an entry

4. Display all

5. Exit

Enter choice:

Hints:

1. How to read a CSV file?

There are two methods.

Method 1. Use delimiters in Scanner object to read one token at a time. Here is some sample code:

File phoneBookFile = new File("phonebook.csv");
Scanner inputFile = new Scanner(phoneBookFile);
inputFile.useDelimiter("[, ]+");
int i = 0;
while(inputFile.hasNext()) {

String firstname = inputFile.next();
String lastname = inputFile.next();
String phoneNumber = inputFile.next();
System.out.println("Entry #" + ++i + ": first name: " + firstname + ", last name: " + lastname + ", phone number: " + phoneNumber);

}

inputFile.close();

Method 2. Read a full line and then split it into individual tokens as an array of Strings. Here is some sample code:

File phoneBookFile = new File("phonebook.csv");

Scanner inputFile = new Scanner(phoneBookFile);

int i = 0;

while(inputFile.hasNext()) {

String line = inputFile.nextLine();

String[] entry = line.split(",");

System.out.println("Entry #" + ++i + ": first name: " + entry[0] + ", last name: " + entry[1] + ", phone number: " + entry[2]);

}

inputFile.close();

Phone Book Entry:

Explanation / Answer

import java.io.*;
import java.util.*;


class PhoneBookEntry {

    private String firstName;
    private String lastName;
    private String phoneNumber;

    public PhoneBookEntry(String firstName, String lastName, String phoneNumber) {
        this.firstName = firstName;
        this.lastName = lastName;
        this.phoneNumber = phoneNumber;
    }

    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 getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

}

public class Demo146{
   public static void main(String[] args){
      try {
         ArrayList<PhoneBookEntry> data = new ArrayList<PhoneBookEntry>();
         File phoneBookFile = new File("phonebook.csv");
         Scanner inputFile = new Scanner(phoneBookFile);
         //int i = 0;
         Scanner sc = new Scanner(System.in);
         while(inputFile.hasNext()) {
            String line = inputFile.nextLine();
            String[] entry = line.split(",");
            PhoneBookEntry ent = new PhoneBookEntry(entry[0], entry[1], entry[2]);
            data.add(ent);
         }
         inputFile.close();
         System.out.println("Phone Book");
         System.out.println("===============");
         while(true){
              System.out.println("1.Find an entry");
              System.out.println("2.Add a new entry");
              System.out.println("3.Remove an entry");
              System.out.println("4.Display all");
              System.out.println("5.Exit");
              System.out.println("Enter choice:");
              int n = Integer.parseInt(sc.nextLine());
              if (n == 5)
                 break;
              if (n == 1){
                 System.out.println("1.Search by first name");
                 System.out.println("2.Search by last name");
                 System.out.println("Please choose:");
                 int n1 = Integer.parseInt(sc.nextLine());
                
                 if (n1 ==1 ){
                    System.out.println("Enter substring for first name:");
                    String name = sc.nextLine();
                    int found = 0;
                    for (int i = 0; i<data.size(); i++){
                        if (data.get(i).getFirstName().contains(name)){
                          System.out.println(data.get(i).getFirstName() + "," + data.get(i).getLastName() + "," + data.get(i).getPhoneNumber());
                          found = 1;
                          break;
                        }
                    }
                    if (found == 0)
                       System.out.println("Not Found");

                 }
                 if (n1 ==1 ){
                    System.out.println("Enter substring for last name:");
                    String name = sc.nextLine();
                    int found = 0;
                    for (int i = 0; i<data.size(); i++){
                        if (data.get(i).getLastName().contains(name)){
                          System.out.println(data.get(i).getFirstName() + "," + data.get(i).getLastName() + "," + data.get(i).getPhoneNumber());
                          found = 1;
                          break;
                        }
                    }
                    if (found == 0)
                       System.out.println("Not Found");
                 }

                
              }
              if (n == 2){
                 System.out.println("Enter first name:");
                 String first = sc.nextLine();
                 System.out.println("Enter last name:");
                 String last = sc.nextLine();
                 System.out.println("Enter phone number:");
                 String phone = sc.nextLine();
                 PhoneBookEntry en = new PhoneBookEntry(first,last,phone);
                 data.add(en);

              }          
              if (n == 3){
                  System.out.println("Enter index :");
                  int a = Integer.parseInt(sc.nextLine());
                  if (a >= 0 && a <data.size())
                     data.remove(a);
                  else
                     System.out.println("Invalid index");
              }
              if (n == 4){
                 for (int i = 0; i<data.size(); i++){
                    System.out.println(data.get(i).getFirstName() + "," + data.get(i).getLastName() + "," + data.get(i).getPhoneNumber());
                 }

              }          
          
          
         }
         FileOutputStream fout = new FileOutputStream("phonebook.csv");
         PrintWriter pw = new PrintWriter(fout);
         for (int i = 0; i<data.size(); i++){
             pw.println(data.get(i).getFirstName() + "," + data.get(i).getLastName() + "," + data.get(i).getPhoneNumber());
         }
         pw.close();
         fout.close();
      }
      catch(Exception e){
          e.printStackTrace();
      }
   }
}

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