Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Description of Program You are to write a program name phonedir that maintains a

ID: 3589468 • Letter: D

Question

Description of Program

You are to write a program name phonedir that maintains a list of records containing names and phone numbers. 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 Linked List.

The list must be kept sorted at all times – based on last name. 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 LIST. Note: Changing the last name will require resorting.

Use as many generic algorithm as you can.

Explanation / Answer

package com.as.test;

import java.util.LinkedList;

import java.util.Scanner;

public class PhoneDirectoryTest {

public static void main(String[] args) {

LinkedList<Person> list = new LinkedList<Person>();

Scanner scanner = new Scanner(System.in);

int i = 1;

while (i == 1) {

System.out.println("menu");

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.println("enter your choice");

String choice = scanner.next();

switch (choice) {

case "n":

System.out.println("enter your first name");

String firstNm = scanner.next();

System.out.println("enter your last name");

String lastNm = scanner.next();

System.out.println("enter your phone no");

String phoneNo = scanner.next();

Person p = new Person(firstNm, lastNm, phoneNo);

list.add(p);

break;

case "a":

int count2=0;

for (Person person : list) {

System.out.println(person);

count2++;

}

if(count2==0)

System.out.println("no records to display");

break;

case "f":

System.out.println("enter your first name");

String first = scanner.next();

int count = 0;

for (Person person : list) {

if (first.equals(person.getFirstNm())) {

System.out.println(person);

count++;

}

}

if (count == 0) {

System.out.println("record not found");

}

break;

case "s":

System.out.println("enter your first name");

String first1 = scanner.next();

System.out.println("enter your last name");

String lastnm = scanner.next();

int count1 = 0;

for (Person person : list) {

if (first1.equals(person.getFirstNm())

&& lastnm.equals(person.getLastNm())) {

System.out.println(person);

count1++;

}

}

if (count1 == 0) {

System.out.println("record not found");

}

break;

case "q":

System.exit(0);

break;

case "d":

System.out.println("the deleted record is");

list.remove(0);

default:

System.out.println("please enter above displayed menu only");

break;

}

}

}

}

============================================================================

package com.as.test;

public class Person {

private String firstNm;

private String lastNm;

private String phoneNo;

public String getFirstNm() {

return firstNm;

}

public void setFirstNm(String firstNm) {

this.firstNm = firstNm;

}

public String getLastNm() {

return lastNm;

}

public void setLastNm(String lastNm) {

this.lastNm = lastNm;

}

public String getPhoneNo() {

return phoneNo;

}

public void setPhoneNo(String phoneNo) {

this.phoneNo = phoneNo;

}

@Override

public String toString() {

return "Person [firstNm=" + firstNm + ", lastNm=" + lastNm

+ ", phoneNo=" + phoneNo + "]";

}

public Person(String firstNm, String lastNm, String phoneNo) {

this.firstNm = firstNm;

this.lastNm = lastNm;

this.phoneNo = phoneNo;

}

}