Answered step by step
Verified Expert Solution
Question
1 Approved Answer
Please help me complete the following java programming tasks. I need a correct answer as a reference for me to correct my own tasks. Thank
Please help me complete the following java programming tasks. I need a correct answer as a reference for me to correct my own tasks. Thank you very much.
In this exercise you need to complete these additional features one by one for the extended phone book program. Here are the tasks in the exercise:
Checking for duplicated entry when a new phone number is addedplz finish it in this question
Finding a phone number entry by nameno need to finish
Delete a phone number entry by nameno need to finish
The exists method, which has been used in the above code, is empty.
The first task is finishing the exists method below so that the code inside the addNumber method works:
if existsname
System.out.printlnPhone number entry already exists!";
return;
Here is a part of the uncompleted java code given in the task. Please finish this part on the premise of not deleting old code AS MUCH AS POSSIBLE:
import java.util.Scanner;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.nio.charset.Charset;
import java.ioBufferedReader;
import java.ioBufferedWriter;
public class PhoneBook
Prepare a scanner for reading inputs
Scanner scanner new ScannerSystemin;
Show the main menu of the program
public void mainmenu
System.out.printlnWelcome to the phone book program.";
System.out.println;
String choice;
do
Print the main menu
System.out.printlnPlease choose one of the following:";
System.out.println;
System.out.println Add New Phone Number";
System.out.println List Phone Numbers";
System.out.println Find Phone Number by Name";
System.out.println Delete Phone Number by Name";
System.out.println Quit";
System.out.println;
System.out.printPlease enter or : ;
Get the selection from the user
choice scanner.next;
Based on the selection run the appropriate method
switch choice
case :
addNumber;
break;
case :
listNumbers;
break;
case :
findNumberByName;
break;
case :
deleteNumberByName;
break;
System.out.println;
while choice.equals;
System.out.printlnBye;
Check whether the name exists in the current phone book
public boolean existsString name
Task Checking for duplicated new entry
Get the path of the text file
Use a BufferedReader to read the data one by one
For each data line create a PhoneBookEntry
If the name in the PhoneBookEntry is equal to the input name
return true
Return false if nothing matches the input name
return false;
Add a new entry to the phone book
public void addNumber
Get the name
System.out.printPlease enter the name: ;
String name scanner.next;
Get the number
System.out.printPlease enter the number: ;
String number scanner.next;
Check whether the name already exists, if so
print a message and exit the method
if existsname
System.out.printlnPhone number entry already exists!";
return;
Get the path of the text file
Path path Paths.getphonebooktxt;
try
Get a BufferedWriter from the file
BufferedWriter writer
Files.newBufferedWriterpath Charset.defaultCharset
StandardOpenOption.APPEND;
Create a new phone book entry from the data
PhoneBookEntry entry new PhoneBookEntryname number;
Add a new phone number in a line
writer.writeentrytoLine;
writer.newLine;
Close the writer
writer.close;
catch Exception e
System.out.printlnI have some problems writing the file!";
In this exercise, each phone number entry is still stored as a separate line. However, the name and the phone number of one entry are separated by a colon, without a space. You can see the format of the phone book from the given phonebook.txt as shown below:
CSO:
FMO:......
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started