PLEASE SHOW OUTPUT AND DO ALL THAT\'S REQUIRED. Description of Program You are t
ID: 3853669 • Letter: P
Question
PLEASE SHOW OUTPUT AND DO ALL THAT'S REQUIRED.
Description of Program
You are to write a program name phonedir.java that maintains a list of records containing names (last and first) and phone numbers of a ohone company customers. The program will prompt the user for a command, execute the command, then prompt the user for another command. The commands must be chosen from the following possibilities:
a Show all records
d Delete the current record
f Change the first name in the current record
l Change the last name in the current record
n Add a new record
p Change the phone number in the current record
q Quit
s Select a record from the record list to become the current record
The following example illustrates the behavior of each command (user input is in bold)
c:phonedir [enter]
A Program to keep a Phone Directory:
a Show all records
d Delete the current record
f Change the first name in the current record
l Change the last name in the current record
n Add a new record
p Change the phone number in the current record
q Quit
s Select a record from the record list to become the current record
Enter a command from the list above (q to quit): f
No current record
a Show all records
d Delete the current record
f Change the first name in the current record
l Change the last name in the current record
n Add a new record
p Change the phone number in the current record
q Quit
s Select a record from the record list to become the current record
Enter a command from the list above (q to quit): n
Enter first name: Barry
Enter last name: Drake
Enter phone number: 770-591-8071
Current record is: Barry Drake 770-591-8071
a Show all records
d Delete the current record
f Change the first name in the current record
l Change the last name in the current record
n Add a new record
p Change the phone number in the current record
q Quit
s Select a record from the record list to become the current record
Enter a command from the list above (q to quit): n
Enter first name: Ada
Enter last name: Caswell
Enter phone number: 770-251-3456
Current record is: Ada Caswell 770-251-3456
MENU DISPLAYED AGAIN
Enter a command from the list above (q to quit): a
First Name Last Name Phone Number
------------- ------------- ------------------
Ada Caswell 770-251-3456
Barry Drake 770-591-8071
MENU DISPLAYED AGAIN
Enter a command from the list above (q to quit): n
Enter first name: Elwood
Enter last name: Havens
Enter phone number: 404-345-8897
Current record is: Elwood Havens 404-345-8897
MENU DISPLAYED AGAIN
Enter a command from the list above (q to quit): a
First Name Last Name Phone Number
------------- ------------- ------------------
Ada Caswell 770-251-3456
Barry Drake 770-591-8071
Elwood Havens 404-345-8897
MENU DISPLAYED AGAIN
Enter a command from the list above (q to quit): f
Enter new first name: Jake
Current record is: Jake Havens 404-345-8897
MENU DISPLAYED AGAIN
Enter a command from the list above (q to quit): s
Enter first name: Carl
Enter last name: Patton
No matching record found.
MENU DISPLAYED AGAIN
Enter a command from the list above (q to quit): s
Enter first name: Barry
Enter last name: Drake
Current record is: Barry Drake 770-591-8071
MENU DISPLAYED AGAIN
Enter a command from the list above (q to quit): d
Deleted: Barry Drake 770-591-8071
MENU DISPLAYED AGAIN
Enter a command from the list above (q to quit): a
First Name Last Name Phone Number
------------- ------------- ------------------
Ada Caswell 770-251-3456
Elwood Havens 404-345-8897
MENU DISPLAYED AGAIN
Enter a command from the list above (q to quit): z
Illegal command
Enter a command from the list above (q to quit): q
The output of your program must match the format illustrated in this example.
Here are some other additional requirements for this program:
You may assume that phone number contains no spaces.
After a deletion, there is no record currently selected
Each record (first name, last name and phone number) must be stored as an object. These objects must be stored in a LinkedList.
The LinkedList must be kept sorted at all times – based on last name, then first name and finally phone number. There cannot be two identical entries - the entries must differ by at least one of the variables i.e. last name or first name or phone number. Sorting is to be achieved when an insertion or modification is done. NO SORTING ROUTINE IS ALLOWED. ALWAYS INSERT A NEW RECORD OR EDITED RECORD INTO ITS' CORRECT PLACE IN THE LINKEDLIST. Note: Changing the last name will require resorting.
Use as many generic algorithm as you can.
Explanation / Answer
//Header files
import java.util.*;
import java.io.*;
public class Phonedir
{
public static void main(String [] args)
{
String First_name, Last_name, Phone_Num;
String First, Last, Num;
int choice=0;
Scanner in = new Scanner(System.in);
LinkedList<T> listOfTs = new LinkedList<T>();
System.out.println("A Program to keep a Phone Directory:");
//looping menu
while(true)
{
Menu();
//enter the choice
if(choice==0)
System.out.println("No choice selected");
else
System.out.println(listOfTs.get(choice-1));
System.out.print("Enter the choice ");
String command = in.next().trim();
//show all entries
if(command.equalsIgnoreCase("a"))
{
System.out.println();
System.out.println("First NameLast NamePhone Number");
//for loop
for(int i=0; i<listOfTs.size(); i++)
System.out.println(listOfRecords.get(i));
}
//Delete choice
else if(command.equalsIgnoreCase("d"))
{
if(choice==0)
System.out.println("No Record selected");
else
{
System.out.println("Deleted: "+listOfRecords.remove(choice-1));
choice=0; //update choice counter of reference of linked list
}
}
//change first name of choice Record
else if(command.equalsIgnoreCase("f"))
{
if(choice==0)
System.out.println("No Record selected");
else
{
System.out.print("New first name: ");
First = in.next().trim();
listOfRecords.get(choice-1).changeFirst(First);
}
}
//Change last name of selected record
else if(command.equalsIgnoreCase("l"))
{
if(choice==0)
System.out.println("No Record selected");
else
{
System.out.print("New last name: ");
Last = in.next().trim();
listOfRecords.get(choice-1).changeLast(Last);
}
}
//add new contact
else if(command.equalsIgnoreCase("n"))
{
System.out.print("Enter first name: ");
First_name = in.next().trim();
System.out.print("Enter last name: ");
Last_name = in.next().trim();
//check in valid phone number input
//if phone number does not equal 10 then reprompt
while(true)
{
System.out.print("Enter phone number");
Phone_Num = in.next().trim();
if(Phone_Num.length()!=10)
System.out.println("Please enter correct phone number!");
else
break;
}
listOfRecords.add(new Record(First_name, Last_name, Phone_Num));
choice = listOfRecords.size();
}
//Change phone number
else if(command.equalsIgnoreCase("p"))
{
if(choice==0)
System.out.println("No Record selected");
else
{
System.out.print("Enter new phone number: ");
Num = in.next().trim();
listOfRecords.get(choice-1).changePhone(Num);
}
}
//Exit
else if(command.equalsIgnoreCase("q"))
{
System.out.println("Quit");
System.exit(1);
}
else
if(command.equalsIgnoreCase("s"))
{
String testName;
int test=0;
System.out.print("Enter first name: ");
First_name = in.next().trim();
for(int i=0; i<listOfRecords.size(); i++)
{
testName = listOfRecords.get(i).getFirst();
if(testName.equals(First_name))
{
choice=i+1;
test++;
}
}
//print if no match found
if(test==1);
System.out.println("No Record found");
}
else
System.out.println("Choice not correct");
}
}
//print menu
static void Menu()
{
System.out.println();
System.out.println(" (a) Show all records");
System.out.println(" (d) Delete the current record");
System.out.println(" (f) Change the first name in the current record");
System.out.println(" (l) Change the last name in the current record");
System.out.println(" (n) Add a new record");
System.out.println(" (p) Change the phone number in the current record");
System.out.println(" (q) Quit");
System.out.println(" (s) Select a record from the record list to become current record");
}
}
Now create class for record
class Data
{
private String firstName;
private String lastName;
private String phoneNum;
//constructor
public Data(String f, String l, String n)
{
firstName=f;
lastName=l;
phoneNum=formatPhone(n);
}
public String getFirst()
{
return firstName;
}
public String getLast()
{
return lastName;
}
public String getNumber()
{
return phoneNum;
}
public void changePhone(String p)
{
phoneNum=formatPhone(p);
}
public void changeFirst(String f)
{
firstName=f;
}
public void changeLast(String l)
{
lastName=l;
}
public String toString()
{
return firstName +" "+ lastName +" "+phoneNum;
}
public String formatPhone(String num)
{
return num.substring(0,3)+"-"+num.substring(3,6)+"-"+num.substring(6,10);
}
} //end the program