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();
}
}
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); }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.