PLEASE SHOW Screen Shot of Output AND DO ALL THAT\'S REQUIRED. Description of Pr
ID: 3853891 • Letter: P
Question
PLEASE SHOW Screen Shot of 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
Here is the code for the given question. Output is also shown. Post a comment in case of any issues, I shall respond. If happy with the answer, please do rate it. Thank you.
Record.java
package phonedir;
//Record implements Comparable<Record> so that 2 records can be compared while adding them to list in sorted order.
public class Record implements Comparable<Record>
{
private String firstname;
private String lastname;
private String phone;
public Record()
{
this.firstname ="";
this.lastname = "";
this.phone ="";
}
public Record(String fname, String lname, String phone)
{
this.firstname = fname;
this.lastname = lname;
this.phone = phone;
}
public String getFirstname() {
return firstname;
}
public void setFirstname(String firstname) {
this.firstname = firstname;
}
public String getLastname() {
return lastname;
}
public void setLastname(String lastname) {
this.lastname = lastname;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
@Override
public int compareTo(Record o) {
if(lastname.compareToIgnoreCase(o.lastname) < 0) //compare last names first
return -1;
else if (lastname.compareToIgnoreCase(o.lastname) == 0) //if last names match, compare first names
{
if(firstname.compareToIgnoreCase(o.firstname) < 0) //compare first names
return -1;
else if (firstname.compareToIgnoreCase(o.firstname) == 0) //if first names match, compare phone
{
if(phone.compareToIgnoreCase(o.phone) < 0) //compare phone
return -1;
else if (phone.compareToIgnoreCase(o.phone) == 0)
{
return 0;
}
else
return 1;
}
else
return 1;
}
else
return 1;
}
public String toString()
{
return firstname + " " + lastname +" " + phone;
}
}
phonedir.java
package phonedir;
import java.util.Scanner;
public class phonedir {
private LinkedList phonenumbers;
private Scanner keybd = new Scanner(System.in);
public phonedir()
{
phonenumbers = new LinkedList();
}
private void showAllRecords()
{
phonenumbers.showAll();
}
private void deleteRecord()
{
if(phonenumbers.getCurrent() == null)
System.out.println("No current record");
else
phonenumbers.deleteCurrent();
}
private void changeFirstname()
{
Record rec = phonenumbers.getCurrent();
if(rec == null)
System.out.println("No current record");
else
{
System.out.print("Enter new first name: ");
String fname = keybd.next().trim();
rec.setFirstname(fname);
phonenumbers.resortCurrent();
System.out.println("Current record is: " + phonenumbers.getCurrent());
}
}
private void changeLastname()
{
Record rec = phonenumbers.getCurrent();
if(rec == null)
System.out.println("No current record");
else
{
System.out.print("Enter new last name: ");
String lname = keybd.next().trim();
rec.setLastname(lname);
phonenumbers.resortCurrent();
System.out.println("Current record is: " + phonenumbers.getCurrent());
}
}
public void addRecord()
{
String fname, lname, phone;
System.out.print("Enter first name: ");
fname = keybd.next();
System.out.print("Enter last name: ");
lname = keybd.next();
System.out.print("Enter phone number: ");
phone = keybd.next();
phonenumbers.add(new Record(fname, lname, phone));
System.out.println("Current record is: " + phonenumbers.getCurrent());
}
public void changePhone()
{
Record rec = phonenumbers.getCurrent();
if(rec == null)
System.out.println("No current record");
else
{
System.out.print("Enter new phone number: ");
String phone = keybd.next().trim();
rec.setPhone(phone);
phonenumbers.resortCurrent();
System.out.println("Current record is: " + phonenumbers.getCurrent());
}
}
public void selectRecord()
{
String fname, lname;
System.out.print("Enter first name: ");
fname = keybd.next();
System.out.print("Enter last name: ");
lname = keybd.next();
phonenumbers.selectRecord(fname, lname);
if(phonenumbers.getCurrent() == null)
System.out.println("No matching record found.");
else
System.out.println("Current record is: " + phonenumbers.getCurrent());
}
public void mainMenu()
{
String choice;
while(true)
{
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 the current record");
System.out.print(" Enter a command from the list above (q to quit): ");
choice = keybd.next().trim().toLowerCase();
if(choice.equals("a"))
showAllRecords();
else if(choice.equals("d"))
deleteRecord();
else if(choice.equals("f"))
changeFirstname();
else if (choice.equals("l"))
changeLastname();
else if(choice.equals("n"))
addRecord();
else if(choice.equals("p"))
changePhone();
else if(choice.equals("s"))
selectRecord();
else if(choice.equals("q"))
return;
else
System.out.println("Illegal command");
}
}
public static void main(String[] args) {
phonedir dir = new phonedir();
System.out.println("A program to keep phone directory: ");
dir.mainMenu();
}
}
LinkedList.java
package phonedir;
public class LinkedList {
private Node head;
private Node current , previous;
class Node
{
Record record;
Node next;
Node(Record rec)
{
this(rec, null);
}
Node(Record rec, Node next)
{
this.record = rec;
this.next =next;
}
}
public Record getCurrent()
{
if(current != null)
return current.record;
else
return null;
}
public void add(Record rec)
{
Node n = new Node(rec);
Node curr = head, prev = null;
if(head == null)
{
head = n;
previous = null;
}
else
{
while(curr != null)
{
if(n.record.compareTo(curr.record) < 0)
break;
prev = curr;
curr = curr.next;
}
if(prev == null) //adding before head
{
n.next = head;
head = n;
}
else
{
n.next = curr;
prev.next = n;
}
}
previous = prev;
current = n;
}
public void showAll()
{
System.out.printf(" %-30s %-30s %-20s", "First Name", "Last Name", "Phone Number");
System.out.printf(" %-30s %-30s %-20s", "==========", "=========", "============");
Node p = head;
Record r;
while(p != null)
{
r = p.record;
System.out.printf(" %-30s %-30s %-20s", r.getFirstname(), r.getLastname(), r.getPhone());
p = p.next;
}
}
public Record selectRecord(String fname, String lname)
{
previous = null;
current = head;
while(current != null)
{
if(current.record.getFirstname().equalsIgnoreCase(fname) && current.record.getLastname().equalsIgnoreCase(lname))
break;
previous = current;
current = current.next;
}
return getCurrent();
}
public void resortCurrent()
{
Node n = current;
//delete the current and then add it again
if(current != null)
{
if(previous != null)
previous.next = current.next;
else
{
head = current.next;
}
add(n.record);
}
}
public void deleteCurrent()
{
if(current != null)
{
if(previous != null)
previous.next = current.next;
else
{
head = current.next;
}
previous = current = null;
}
}
}
output
A program to keep 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
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): a
First Name Last Name Phone Number
========== ========= ============
Ada Caswell 770-251-3456
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: Elwood
Enter last name: Havens
Enter phone number: 404-345-8897
Current record is: Elwood Havens 404-345-8897
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): a
First Name Last Name Phone Number
========== ========= ============
Ada Caswell 770-251-3456
Barry Drake 770-591-8071
Elwood Havens 404-345-8897
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
Enter new first name: Jake
Current record is: Jake Havens 404-345-8897
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): s
Enter first name: Carl
Enter last name: Patton
Current record is: null
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): s
Enter first name: Barry
Enter last name: Drake
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): d
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): a
First Name Last Name Phone Number
========== ========= ============
Ada Caswell 770-251-3456
Jake Havens 404-345-8897
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): z
Illegal command
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): q