Design and implement a user interface class, Book.java , that provides the opera
ID: 3717699 • Letter: D
Question
Design and implement a user interface class,Book.java, that provides the operations described below.
The goal of this assignment is to create a user interface that provides the following operations:
-Add: Adds a person’s name and phone number to the phone book.
-Delete: Deletes a given person’s name and phone number from the phone book, given only the
name.
-Find: Locates a person’s phone number, given only the person’s name.
-Change: Changes a person’s phone number, given the person’s name and new phone number.
-Quit: Quits the application, after saving the phone book in a text file (newPhoneBook.txt)
You can procced as follows:
-Design and implement the class Person.java, which represents the name and phone number of a
person. You will store instances of this class in the phone book.
-Design and implement the class Book.java, which represents the phone book. The class should
contain a binary search tree as a data member. This tree contains the people in the phone book.
-Add member functions that use a text file to save and restore the tree.
Program Input
When it begins, the program gets its input from a text file, phonebook.txt. Here is a sample input file:
Ryan Gosling 569-534-5454
Meg Ryan 443-438-4934
Jed Amarn 343-554-3232
OutPut
The program saves data into a text file,newPhoneBook.txt, when the user quits the program. Here is the output for the sample input file given above:
Jed Amarn 343-554-3232
Ryan Gosling 569-534-5454
Meg Ryan 443-438-4934
Explanation / Answer
ans:-
given that,
program:-
// File Name: PhoneRecord.java
// Class PhoneRecord definition
public class PhoneRecord
{
// Instance variables to store data
String firstName;
String lastName;
String phoneNumber;
// Parameterized constructor
PhoneRecord(String fn, String ln, String ph)
{
firstName = fn;
lastName = ln;
phoneNumber = ph;
}// End of constructor
}// End of class
--------------------------------------------------------------------------------------------
// File Name: PhoneDirectory.java
import java.util.*;
public class PhoneDirectory
{
String fName, lName, pn, zp;
// Creates an array of object of class PhoneRecord
PhoneRecord pr[] = new PhoneRecord[50];
// Record counter
int record = 0;
// Scanner class object created to accept data
Scanner sc = new Scanner(System.in);;
// Method to display menu and accept user choice
String menu()
{
String choice;
// Displays menu information
System.out.println(" Phone directory commands: ");
System.out.println(" a - Add a new phone number ");
System.out.println(" d - Display All ");
System.out.println(" da -Display all with same Last Name ");
System.out.println(" f - Find a phone number ");
System.out.println(" r - Remove a phone number ");
System.out.println(" q – Quit ");
System.out.println(" Enter command (a, d, da, f, r,or q): ");
// Accepts user choice
choice = sc.next();
sc.nextLine();
// Returns user choice
return choice;
}// End of method
// Method to add a phone record
void addNumber()
{
// Accepts data from the user
System.out.println(" Enter first name: ");
fName = sc.next();
System.out.println(" Enter last name: ");
lName = sc.next();
System.out.println(" Enter phone number: ");
pn = sc.next();
// Adds the record
pr[record] = new PhoneRecord(fName, lName, pn);
// Increase the record counter
record++;
System.out.println(" 1 record added. Total Records: " + record);
}// End of method
// Method to display all the records
void displayRecords()
{
// Loops till end of records
for(int x = 0; x < record; x++)
{
// Displays record number
System.out.print(x + ". ");
// Displays information
System.out.println(pr[x].firstName + " " + pr[x].lastName + " " + pr[x].phoneNumber);
}// End of for loop
}// End of method
// Method to display all records with same last name
void displayRecordsWithSameLastName()
{
// Accepts last name from the user
System.out.println(" Enter the last name: ");
lName = sc.next();
// Loops till end of records
for(int x = 0; x < record; x++)
{
// Checks if the last name entered by the user and last name in the phone record are same
if(pr[x].lastName.equalsIgnoreCase(lName))
{
// If same display the information
System.out.println(pr[x].firstName + " " + pr[x].lastName + " " + pr[x].phoneNumber);
}// End of if condition
}// End of for loop
}// End of method
// Method to display phone number matching with specified first name
void findNumber()
{
// Accept first name from the user
System.out.println(" Enter name to look up:: ");
fName = sc.next();
// Loops till end of records
for(int x = 0; x < record; x++)
{
// Compares first name entered by the user and the first name in the phone record
if(pr[x].firstName.equalsIgnoreCase(fName))
{
// If same display the information
System.out.println(pr[x].firstName + " " + pr[x].lastName + " " + pr[x].phoneNumber);
}// End of if condition
}// End of while loop
}// End of method
// Method to delete a record based on the phone number entered by the user
void removeNumber()
{
// Initializes flag to zero for record not found
int f = 0, x;
// Accepts phone number from the user
System.out.println(" Enter phone no: ");
pn = sc.next();
// Loops till end of records
for(x = 0; x < record; x++)
{
// Checks if the phone number entered by the user is same as the phone number in phone record
if(pr[x].phoneNumber.equalsIgnoreCase(pn))
{
// Set the flag to one for record found
f = 1;
// Come out of the loop
break;
}// End of if
}// End of for
// Checks if flag value is zero display record not found
if(f == 0)
System.out.println("The phone no do not exist.");
// Otherwise record found
else
{
// Loops till end of records
for(int y = x; y < record; y++)
// Swaps record one position back
pr[y] = pr[y+1];
// Displays message
System.out.print(" Record with phone no: " + pn + " deleted.");
// Decrease the record counter by one
record--;
}// End of else
}// End of method
// Main method definition
public static void main(String[] args)
{
// Creates an object of class PhoneDirectory
PhoneDirectory pw = new PhoneDirectory();
// Loops till user enters 'q'
do
{
// Displays menu
String choice = pw.menu();
// Checks the menu option and calls the appropriate method
if(choice.equalsIgnoreCase("a"))
pw.addNumber();
else if(choice.equalsIgnoreCase("d"))
pw.displayRecords();
else if(choice.equalsIgnoreCase("da"))
pw.displayRecordsWithSameLastName();
else if(choice.equalsIgnoreCase("f"))
pw.findNumber();
else if(choice.equalsIgnoreCase("r"))
pw.removeNumber();
else if(choice.equalsIgnoreCase("q"))
System.exit(0);
else
System.out.println(" Command was not recognized; please enter only a, d, da, f, r, rc or q. ");
}while(true); // End of do - while
}// End of main method
}// End of method
1) Add Person
2) Delete Person
3) Search Person
4) Change Phone Number
5) Quit
3
Enter Name for search:jed amarn
Phone Number of Lalchand is: 343-554-3232
1) Add Person
2) Delete Person
3) Search Person
4) Change Phone Number
5) Quit
4
Enter the name of person: Ryan Gosling
Enter new Number: 569-534-5454
1) Add Person
2) Delete Person
3) Search Person
4) Change Phone Number
5) Quit
3
Enter Name for search: Meg Ryan
Phone Number of Lalchand is: 443-438-4934
1) Add Person
2) Delete Person
3) Search Person
4) Change Phone Number
5) Quit
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.