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: 3597305 • 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:

How to read a CSV file?

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();

Explanation / Answer

Implemeted the code as per the requirement. But deleting entry from the file is not working. Please comment on the answer, so that I'll get notification then I'll edit the answer.

Code:

-----------

package com.chegg.expert;

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.File;

import java.io.FileNotFoundException;

import java.io.FileReader;

import java.io.FileWriter;

import java.io.IOException;

import java.io.PrintWriter;

import java.io.Writer;

import java.util.Scanner;

import java.util.StringTokenizer;

public class PhoneBook {

static Scanner inputFile, user_in,find_scanner ;

static String line = "",firstname,lastname,phoneNumber,find;

static int i = 0, choic = 0;

static File f = new File("");

static String s = f.getAbsolutePath();

static File phoneBookFile = new File( s+"/src/com/chegg/expert/phonebook.csv");

public static void main(String[] args) throws IOException {

user_in = new Scanner(System.in);

find_scanner = new Scanner(System.in);

boolean quit = true;

while(quit) {

System.out.println("Phone Book " +

"=========="

+ " 1. Find an entry "

+ "2. Add a new entry "

+ "3. Remove an entry "

+ "4. Display all "

+ "5. Exit");

choic = user_in.nextInt();

switch(choic) {

case 1:

System.out.println("Enter text find the record: ");

find = find_scanner.nextLine();

findEntry(find);

break;

case 2:

System.out.println("Enter first name: ");

firstname = find_scanner.next();

System.out.println("Enter second name: ");

lastname = find_scanner.next();

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

phoneNumber = find_scanner.next();

addEntry(firstname,lastname,phoneNumber);

break;

case 3:

System.out.println("Enter first name: ");

firstname = find_scanner.next();

System.out.println("Enter second name: ");

lastname = find_scanner.next();

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

phoneNumber = find_scanner.next();

removeEntry(firstname,lastname,phoneNumber);

break;

case 4:

display();

break;

case 5:

quit=false;

break;

}

}

}

public static void display() throws FileNotFoundException {

inputFile = new Scanner(phoneBookFile);

while(inputFile.hasNext()) {

line = inputFile.nextLine();

StringTokenizer tokenizer = new StringTokenizer(line, "[, ]+");

firstname = tokenizer.nextToken();

lastname = tokenizer.nextToken();

phoneNumber = tokenizer.nextToken();

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

}

System.out.println();

}

public static void findEntry(String find) throws FileNotFoundException {

find = find.toLowerCase();

System.out.println("Finder "+find);

inputFile = new Scanner(phoneBookFile);

while(inputFile.hasNext()) {

line = inputFile.nextLine();

StringTokenizer tokenizer = new StringTokenizer(line, "[, ]+");

firstname = tokenizer.nextToken();

lastname = tokenizer.nextToken();

phoneNumber = tokenizer.nextToken();

if(firstname.toLowerCase().contains(find) || lastname.toLowerCase().contains(find) || phoneNumber.contains(find)) {

System.out.println(firstname +" "+lastname + " "+phoneNumber);

}

}

System.out.println();

}

public static void addEntry(String fname, String lname, String num) throws IOException {

PrintWriter output;

output = new PrintWriter (new FileWriter(phoneBookFile, true)); //clears file every time

output.printf("%s ",firstname+" "+lastname+" "+phoneNumber);

output.close();

}

public static void removeEntry(String fname, String lname, String num) throws IOException {

inputFile = new Scanner(phoneBookFile);

PrintWriter writer;

writer = new PrintWriter (new FileWriter(phoneBookFile, true)); //clears file every time

String currentLine;

while(inputFile.hasNext()) {

line = inputFile.nextLine();

StringTokenizer tokenizer = new StringTokenizer(line, "[, ]+");

firstname = tokenizer.nextToken();

lastname = tokenizer.nextToken();

phoneNumber = tokenizer.nextToken();

if(firstname.equalsIgnoreCase(fname) && lastname.equalsIgnoreCase(lname) && phoneNumber.equalsIgnoreCase(num)){

if(inputFile.hasNext()) {

writer.print(" ");

}

else {

writer.append("");

}

}

}

writer.close();

}

}

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