lundi 20 février 2017

Trouble completing a Java ArrayList sorting program that asks user how to sort ArrayList

My code is within a package and divided into two separate .java file files. It prints a list of options for how the ArrayList can be sorted, which the user can select by entering the right number. However, for some reason it does not print any sorted list after I input, well anything. Can anyone help?

Here is my custom Object class, Contact.java:

package Sorter;

import java.util.Comparator;

public class Contact {
    private String firstName;
    private String lastName;
    private String state;
    private Integer age;

public Contact(String firstName, String lastName, String state, Integer age) {

    this.firstName = firstName;
    this.lastName = lastName;
    this.state = state;
}

public String getFirstName() {
    return firstName;
}

public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public String getLastName() {
    return lastName;
}

public void setLastName(String lastName) {
    this.lastName = lastName;
}

public String getState() {
    return state;
}

public void setState(String state) {
    this.state = state;
}

public Integer getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

public static Comparator<Contact> lastNameComparator = new Comparator<Contact>() {

    public int compare(Contact s1, Contact s2) {
        String contactLastName1 = s1.getLastName().toUpperCase();
        String contactLastName2 = s2.getLastName().toUpperCase();

        return contactLastName1.compareTo(contactLastName2);
    }
};

public static Comparator<Contact> stateComparator = new Comparator<Contact>() {

    public int compare(Contact s1, Contact s2) {
        String state1 = s1.getState().toUpperCase();
        String state2 = s2.getState().toUpperCase();

        return state1.compareTo(state2);
    }
};

public static Comparator<Contact> ageComparator = new Comparator<Contact>() {

    public int compare(Contact s1, Contact s2) {
        int age1 = s1.getAge();
        int age2 = s2.getAge();

        return age1 - age2;
    }
};

@Override
public String toString() {
    return ("First Name: " + firstName + ", Last Name: " + lastName + ", State: " + state + ", Age: " + age);
}
}

And here is my Sort.java: package Sorter;

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

public class TestSortOptions {

public static void main(String[] args) {
    ArrayList<Contact> contacts = initializeContactsArray();
    promptForOption(contacts);
}

private static ArrayList<Contact> initializeContactsArray() {
    ArrayList<Contact> contacts = new ArrayList<Contact>();
    contacts.add(new Contact("Joe", "Jones", "IL", 35));
    contacts.add(new Contact("Bill", "Barnes", "OH", 62));
    contacts.add(new Contact("Ida", "Know", "FL", 23));
    contacts.add(new Contact("Adam", "Ant", "MI", 14));
    contacts.add(new Contact("Jane", "Doe", "CA", 41));

    return contacts;
}

private static void promptForOption(ArrayList<Contact> contacts) {
    Scanner input = new Scanner(System.in);

    System.out.println("Options \nSort by Last Name:  [1] " + "\nSort by Home State: [2] "
            + "\nSort by Age:        [3] " + "\nExit Application:   [0] " + "\n\nPlease enter your choice: ");
    String answer = input.next();

    if (answer == "1") {
        Collections.sort(contacts, Contact.lastNameComparator);
        for (Contact contact : contacts) {
            System.out.println(contact);
        }

        if (answer == "2") {
            Collections.sort(contacts, Contact.stateComparator);
            for (Contact contact : contacts) {
                System.out.println(contact);
            }

            if (answer == "3") {
                Collections.sort(contacts, Contact.ageComparator);
                for (Contact contact : contacts) {
                    System.out.println(contact);
                }

                if (answer == "0") {
                    System.exit(0);
                }

                else {
                    System.out.println("Invalid Entry");
                }
            }
        }
    }
}
}

Aucun commentaire:

Enregistrer un commentaire