Answered step by step
Verified Expert Solution
Link Copied!

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 added(plz finish it in this question)
Finding a phone number entry by name(no need to finish)
Delete a phone number entry by name(no 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 (exists(name)){
System.out.println("Phone 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.io.BufferedReader;
import java.io.BufferedWriter;
public class PhoneBook
{
// Prepare a scanner for reading inputs
Scanner scanner = new Scanner(System.in);
// Show the main menu of the program
public void mainmenu()
{
System.out.println("Welcome to the phone book program.");
System.out.println();
String choice;
do {
// Print the main menu
System.out.println("Please choose one of the following:");
System.out.println();
System.out.println("1) Add New Phone Number");
System.out.println("2) List Phone Numbers");
System.out.println("3) Find Phone Number by Name");
System.out.println("4) Delete Phone Number by Name");
System.out.println("5) Quit");
System.out.println();
System.out.print("Please enter 1,2,3,4 or 5: ");
// Get the selection from the user
choice = scanner.next();
// Based on the selection run the appropriate method
switch (choice){
case "1":
addNumber();
break;
case "2":
listNumbers();
break;
case "3":
findNumberByName();
break;
case "4":
deleteNumberByName();
break;
}
System.out.println();
} while (!choice.equals("5"));
System.out.println("Bye!");
}
// Check whether the name exists in the current phone book
public boolean exists(String name){
//
// Task 1- 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.print("Please enter the name: ");
String name = scanner.next();
// Get the number
System.out.print("Please enter the number: ");
String number = scanner.next();
// Check whether the name already exists, if so,
// print a message and exit the method
if (exists(name)){
System.out.println("Phone number entry already exists!");
return;
}
// Get the path of the text file
Path path = Paths.get("phonebook.txt");
try {
// Get a BufferedWriter from the file
BufferedWriter writer =
Files.newBufferedWriter(path, Charset.defaultCharset(),
StandardOpenOption.APPEND);
// Create a new phone book entry from the data
PhoneBookEntry entry = new PhoneBookEntry(name, number);
// Add a new phone number in a line
writer.write(entry.toLine());
writer.newLine();
// Close the writer
writer.close();
}
catch (Exception e){
System.out.println("I 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:2358-6333
FMO:......

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Practical Azure SQL Database For Modern Developers Building Applications In The Microsoft Cloud

Authors: Davide Mauri, Silvano Coriani, Anna Hoffma, Sanjay Mishra, Jovan Popovic

1st Edition

1484263693, 978-1484263693

Students also viewed these Databases questions

Question

Provide examples and answer questions regarding desired behaviors.

Answered: 1 week ago