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

JAVA - Address Book Managing data is an important aspect of programming. For thi

ID: 3667211 • Letter: J

Question

JAVA - Address Book

Managing data is an important aspect of programming. For this assignment we will use a collections data structure to emulate persistent storage and data management. Consider the example of a simple address book with a name and phone number.

CONSOLE

Operation

? If the user selects the first menu option, the application displays the names and phone numbers that have been saved. Then, it displays the menu again.

? If the user selects the second menu option, the application prompts the user to enter a name and phone number. Then, it displays the menu again.

? If the user selects the third menu option, the application exits.

Specifications

? Create static methods that can be used to validate the data in this application. The user should only be able to select one of the menu choices, and the user must enter some text for name and phone number.

? Create static methods that display the menu and the output to the user.

? Create other methods as needed to solve the problem.

? You can use any collection data structure. Make sure when you display the phone book you sort the output by name!

1 - List entries 2 - Add entry 3-Exit Enter menu number 1 Name Phone Bill Gates Larry Ellison Steve Jobs (111) 222-3333 (444) 555-6666 777-888-9999 1 - List entries 2 - Add entry 3 - Exit Enter menu number 2 Enter name: Mike Murach Enter phone numbe 800-221-5528 This entry has been saved 1 - List entries 2 - Add entry 3 - Exit Enter menu number 1 Name Phone Bill Gates Larry Ellison Steve Jobs Mike Murach (111) 222-3333 (444) 555-6666 777-888-9999 800-221-5528 1 - List entries 2 - Add entry 3-Exit Enter menu number: 3 Goodbye

Explanation / Answer

//Phone.java

//The class Phone that implements the Comparable interface to sort by name
public class Phone implements Comparable<Phone>
{
   //variables
   private String name;
   private String number;
  
   //Constructor to set name and number
   public Phone(String name,String number)
   {
       this.name=name;
       this.number=number;
   }
  
   //setter methods
   public void setName(String name)
   {
       this.name=name;
   }
   public void setNumber(String number)
   {
       this.number=number;
   }
  
   //getter metods
   public String getName()
   {
       return name;
   }
  
   public String getNumber()
   {
       return number;      
   }
  
  
   //Returns the formatted string output of phone name and number
   @Override
   public String toString()
   {      
       return String.format("%-20s%-20s", name,number);
   }

  
   /**Compares the name to sort*/
   @Override
   public int compareTo(Phone otherPhone)
   {
       if(name.compareTo(otherPhone.getName())<0)
           return -1;
       else if(name.compareTo(otherPhone.getName())>0)
           return 1;
       else
           return 0;
   }
}


----------------------------------------------------------------------------------------------------------------


/**The java progdram PhoneDriver that display a menu of choices.
* Then prompt user to add a phone entry to the ArrayList of type Phone
* and repeats the loop until user selects to exit */
//PhoneDriver.java
import java.util.ArrayList;
import java.util.Collections;
import java.util.Scanner;
public class PhoneDriver
{  
   private static Scanner scanner=new Scanner(System.in);
   public static void main(String[] args)
   {  
       int choice;
       boolean repeat=true;
      
       //Create an ArrayList of type Phone data type
       ArrayList<Phone>phoneList=new ArrayList<Phone>();
       phoneList.add(new Phone("Bill Gates", "(111) 222-333"));
       phoneList.add(new Phone("Larry Ellison", "(444) 555-666"));
       phoneList.add(new Phone("Steve Jobs", "(777) 888-999"));
      
      
       //repeat the loop until user enters 3 to stop
       while(repeat)
       {
           choice=menu();
          
           switch(choice)
           {
           case 1:
               printList(phoneList);
               break;
           case 2:
               addEntry(phoneList);
               break;
           case 3:
               repeat=false;
               System.out.println("Goodbye");
               break;
           }
       }
      
      
   }

   /**The method addEntry takes phoneList and prompts
   * name and number and add to the phoneList*/
   private static void addEntry(ArrayList<Phone> phoneList)
   {      
       System.out.println("Enter name:");
       //prompt for name
       String name=scanner.nextLine();
      
       System.out.println("Enter phone number:");
       //prompt for number
       String number=scanner.nextLine();
      
       //Add phone object to the phoneList
       phoneList.add(new Phone(name, number));
      
   }

   /**The method printList takes phoneList and sorts the phoneList based
   * on name and prints the phoneList */
   private static void printList(ArrayList<Phone> phoneList)
   {      
      
       //Call sort method on Collections to sort before print
       Collections.sort(phoneList);  
       System.out.printf("%-20s%-20s ","Name","Phone");
       System.out.printf("-------------- ------------------- ");
       for (int i = 0; i < phoneList.size(); i++)       
           System.out.println(phoneList.get(i));      
   }

   /**The method menu that prompts user a menu choice and return the menu choice */
   private static int menu()
   {
       int choice;
       System.out.println("1- List entries");
       System.out.println("2- Add entry");
       System.out.println("3- Exit");
      
       System.out.println("Enter menu number :");
       //read user input
       choice=Integer.parseInt(scanner.nextLine());
      
       return choice;
   }
}


------------------------------------------------------------------------------------------------------------------------

Sample output:

1- List entries
2- Add entry
3- Exit
Enter menu number :
1
Name                Phone             
-------------- -------------------
Bill Gates          (111) 222-333     
Larry Ellison       (444) 555-666     
Steve Jobs          (777) 888-999     
1- List entries
2- Add entry
3- Exit
Enter menu number :
2
Enter name:
Mike Murach
Enter phone number:
800-221-5528
1- List entries
2- Add entry
3- Exit
Enter menu number :
1
Name                Phone             
-------------- -------------------
Bill Gates          (111) 222-333     
Larry Ellison       (444) 555-666     
Mike Murach         800-221-5528      
Steve Jobs          (777) 888-999     
1- List entries
2- Add entry
3- Exit
Enter menu number :
3


Goodbye