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

Need help!! I am going to split this up into multiple questions. I have all code

ID: 645702 • Letter: N

Question

Need help!! I am going to split this up into multiple questions. I have all code done I just can't figure out how to get the right output needed.

AddressBook Class

public class AddressBook {
   // Declare variables
       private static final int ADD_PERSON=1;
       private static final int REMOVE_PERSON=2;
       private static final int SORT_FULL_NAME=3;
       private static final int VIEW_PERSONS=4;
       private static final int EXIT=5;
      
       private List<Person> persons;
       private Scanner input;
      
       //
       public AddressBook(){
           persons = new ArrayList<Person>();
           input = new Scanner(System.in);
       }
      
       // Core interaction logic, shows menu, reads in user option, calls methods
       // to match users input option, and repeats this in a loop until the user
       // chooses to exit the program.
       public void runAddressBook() {

           int userOption;
           while(true) {
               showMenu();
               System.out.println("Enter your option ?");
               userOption = input.nextInt();

               switch(userOption) {

               case ADD_PERSON:
                   addPerson();
                   break;

               case REMOVE_PERSON:
                   removePerson(userOption);
                   break;
               case SORT_FULL_NAME:
                   sortByFullName();
                   break;
               case VIEW_PERSONS:
                   viewPersons();
                   break;
               case EXIT:
                   break;
               default:
                   System.out.println("Invalid Choice");
               }

               if(userOption==EXIT)
                   break;
           }
       }

       // Displays the menu for the user using values
       public void showMenu() {
          
           System.out.println("1. Add person");
           System.out.println("2. Delete person");
           System.out.println("3. Sort by full name");
           System.out.println("4. View persons");
           System.out.println("5. Exit program");
       }
      
       // Attempts to add a person to Address Book after interacting with the user
       // to get the needed information.
       public void addPerson(){
           System.out.println("Please enter full name");
           String fullName = input.next();
             
           System.out.println("Please enter phone number");
           String phoneNumber = input.next();

           System.out.println("Please enter email address");
           String email = input.next();
          
           persons.add(new Person(fullName, phoneNumber, email));
       }
      
       public void removePerson(int index){
           System.out.println("Enter index of person to remove");
           persons.remove(index);
       }
      
      
       public void sortByFullName(){
           Collections.sort(persons, new PersonFullNameComparator());
       }
      
       public void viewPersons(){
           for(int i = 0; i < persons.size(); ++i){
       System.out.println("Person " + (i));
       System.out.println("Name: " + persons.get(i).getFullName());
       System.out.println("Phone Number: " + persons.get(i).getPhoneNumber());
       System.out.println("Email: " + persons.get(i).getEmail());
       }
       }
}

PersonFullNameComparator Class

import java.util.Comparator;

public class PersonFullNameComparator implements Comparator {

   @Override
   public int compare(Person arg0, Person arg1) {
       return arg0.getFullName().compareTo(arg1.getFullName());
   }
}

AddressBookLauncher Class

public class AddressBookLauncher {
   public static void main(String[] args) {
       // AddressBook manager = new AddressBook();
       // manager.runAddressBook();
       (new AddressBook()).runAddressBook();
   }
}

This is the core of the program logic. Static final constants for use in the menu One List(Person> to store references to Person objects One Scanner object for keyboard inputs from the user One constructor, it should instantiate an ArrayList and assign the reference to the List , as well as instantiate the Scanner Show Menu() shows the user menu addPerson() prompts the user to create a Person then adds it into the List removePerson() asks the user to enter an index number tin a person and removes it, if it exists sortByFullName(). should use the Comparator with Collections.sort to sort the references in the List viewPersons() needs to print out the persons one on each line as well as display the idex number each is stored at in the List.

Explanation / Answer

return arg0.getFullName().compareTo(arg1.getFullName())

can be written as:

return arg0.getFullName() - arg1.getFullName();

Addressbooklauncher class can be written as:

public static void main(String[] args) { AddressBook manager = new AddressBook(); manager.setVisible(true); }
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote