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

write a program name phonedir that maintains a list of records containing names

ID: 3629562 • Letter: W

Question

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. That is, after a command is executed, the menu should be displayed again so that the user can choose. 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

 s     Select a record from the record list to become the current record

q     Quit

Explanation / Answer

import java.util.Scanner;
import java.util.ArrayList;

class PhoneDir{

    static class PhoneEntry{
        private String firstName;
        private String lastName;
        private String phoneNumber;
        PhoneEntry(){
            firstName="";
            lastName="";
            phoneNumber="";
        }
        PhoneEntry(String fName, String lName, String pNum){
            firstName=fName;
            lastName=lName;
            phoneNumber=pNum;
        }

        public void setFirstName(String fName){
            firstName=fName;
        }
        public void setLastName(String lName){
            lastName=lName;
        }
        public void setPhoneNumber(String pNum){
            phoneNumber=pNum;
        }

        public String getFirstName(){
            return firstName;
        }
        public String getLastName(){
            return firstName;
        }
        public String getPhoneNumber(){
            return firstName;
        }
        public String toString(){
            return ("First Name = "+firstName+" Last Name = "+ lastName+" Phone Number = "+phoneNumber);
        }
    }

    public static void printMainMenu(){
        System.out.println("Main Menu:");
        System.out.println("a     Show all records s     Select a record from the record list to become the current record n     Add a new record q     Quit");
    }

    public static int printSelectRecord(ArrayList<PhoneEntry> pDir){
        int count=0;
        for(PhoneEntry p : pDir){
            System.out.println((++count)+". "+p.toString());
        }
        return count;
    }

    public static void main(String[] arg){
        ArrayList<PhoneEntry> pDir=new ArrayList<PhoneEntry>();
        Scanner scan = new Scanner(System.in);
        printMainMenu();
        String input=scan.nextLine();
        while(true)    {
            if (input.compareTo("a")==0){
                printSelectRecord(pDir);
            } else if(input.compareTo("s")==0) {
                int aNum=printSelectRecord(pDir);
                System.out.println("Select a number");
                int entryNum=scan.nextInt()-1;
                if(entryNum>aNum){
                    System.out.println("Your entry is out of range!");
                } else {
                    System.out.println("Edit Entry for " + pDir.get(entryNum).toString()+ ":");
                    System.out.println("d     Delete the current record f     Change the first name in the current record "+
                        "l     Change the last name in the current record p     Change the phone number in the current record");                System.out.println("Please enter a letter:");

                    boolean isTrue=true;
                    while (isTrue){
                        input=scan.nextLine();
                        if (input.compareTo("d")==0){
                            pDir.remove(entryNum);
                            isTrue=false;
                        } else if(input.compareTo("f")==0) {
                            System.out.println(" Enter a new First Name: ");
                            pDir.get(entryNum).setFirstName(scan.nextLine());
                            isTrue=false;
                        } else if(input.compareTo("l")==0) {
                            System.out.println("Enter a new Last Name: ");
                            pDir.get(entryNum).setLastName(scan.nextLine());
                            isTrue=false;
                        } else if(input.compareTo("p")==0) {
                            System.out.println(" Enter a new Phone Number: ");
                            pDir.get(entryNum).setPhoneNumber(scan.nextLine());
                            isTrue=false;
                        } else if(input.compareTo("")!=0) {
                            System.out.println("You've entered and invalid letter!"+" Scanner value is:"+input.compareTo("")+".");
                        }
                    }
                }

            } else if(input.compareTo("n")==0) {
                PhoneEntry pEntry=new PhoneEntry();
                System.out.println(" Enter a First Name");
                pEntry.setFirstName(scan.nextLine());
                System.out.println(" Enter a Last Name");
                pEntry.setLastName(scan.nextLine());
                System.out.println(" Enter a Phone Number");
                pEntry.setPhoneNumber(scan.nextLine());
                pDir.add(pEntry);
            } else if(input.compareTo("q")!=0) {
                System.out.println("The letter you entered is invalid!");
            } else {
                System.exit(0);
            }
            System.out.println(" ");
            printMainMenu();
            System.out.println("Enter a letter");
            input=scan.nextLine();
        }
    }
}