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

The objective of this assignment is to implement a simple menu driver program ba

ID: 3592256 • Letter: T

Question

The objective of this 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();

Explanation / Answer

public class Sample

{

public static void main( String args[] )

{

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

Scanner s = new Scanner(System.in);

Scanner inputFile = new Scanner(phoneBookFile);

inputFile.useDelimiter( "[, ]+" );

int ch;

System.out.println(" 1.Find an entry:");

System.out.println("2.Add an entry:");;

System.out.println("3.Remove an entry");

System.out.println("4.Display all");

System.out.println("5.Exit");

ch = Integer.parseInt(s.readLine());

while(ch != 5)

{

switch(ch)

{

case 1: System.out.println("Enter a subsstring of FirstName or LastName or Phone number");

String substring = s.readLine();

findEntry("phonebook.csv",substring);

break;

case 2 : System.out.println("Enter FirstName:");

String f_name = s.readLine();

System.out.println("Enter LastName:");

String l_name = s.readLine();

System.out.println("Enter phone number:");

String phn_num = s.readLine();

String final_string = f_name + "," + l_name + "," + ph_num;

addEntry("phonebook.csv",final_string);

break;

case 3: System.out.println("enter the firstname to remove");

substring = s.readLine();

removeEntry("phonebook.csv", substring);

break;

case 4: displayAll("phonebook.csv");

break;

case 5 : System.exit(0);

}

}

}

public void findEntry(String fileName, String substring)

{

Scanner input = new Scanner(fileName);

input.setDelimiter("[, ]+");

while(input.hasNext())

{

String f_name = input.getNext();

String l_name = input.getNext();

String phn_num = input.getNext();

if(f_name.toLowerCase().contains(substring.toLowerCase()))

System.out.println(f_name + l_name + phn_num);

else if (l_name.toLowerCase().contains(substring.toLowerCase()))

System.out.println(f_name + l_name + phn_num);

else if ( phn_num.toLowerCase().contains(substring.toLowerCase()))

System.out.println(f_name + l_name + phn_num);

else

System.out.println("Not found!");

}

input.close();

}

public void addEntry(String fileName, String substring)

{

Files.write( Paths.get(fileName), substring.getBytes(), StandanrdOpenOption.APPEND);

}

public void displayAll(String fileName)

{

Scanner input = new Scanner(fileName);

input.setDelimiter("[, ]+");

while( input.hasNext() )

{

String f_name = input.getNext();

String l_name = input.getNext();

String phn_num = input.getNext();

Sytem.out.println( f_name + ", " + l_name + ", " + ph_num);

}

input.close();

}

public void removeEntry(String fileName, String first_name)

{

File tempFile = new File ("temp.csv");

Scanner reader = new Scanner( fileName);

reader.setDelimiter( "[, ]+");

BufferedWriter writer = new BufferedWriter( new FileWriter(tempFile) );

while ( reader.hasNext() )

{

String f_name = reader.getNext();

String l_name = reader.getNext();

String phn_num = reader.getNext();

if(first_name.equalsIgnoreCase( f_name ))

continue;

else

writer.write(f_name + ", " + l_name + ", " + phn_num);

}

writer.close();

reader.close();

tempFile.renameTo(fileName);

}

public void displayAll(String fileName)

{

Scanner input = new Scanner(fileName);
while(input.hasNext())
{
System.out.println(input.getNext());
}
input.close();
}

}

/* hope this might help */

/* if any queries please comment */

/* thank you */

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