NoobCoderChick
NoobCoderChick

Reputation: 627

ArrayList of objects sort using comparable

so I'm working on an address book assignment and I'm stuck on getting Comparable to sort the contacts by last name. I'm trying stuff that we haven't really learned like ArrayLists of objects, comparable and Serializable and comparable is confusing me the most.

Any tips on why the contacts aren't sorting? Second question, I wanted to try and make the first character of the first and last name an uppercase but I just couldn't figure it out so I made the whole thing uppercase in the toString method, any ideas how to get only the first char upper?

public class AddressBook implements Serializable{

private ArrayList<String> newBook = new ArrayList<String>();
private String dataFile;
private ArrayList<Contact> card =new ArrayList<Contact>(50);
private Contact[] contacts;
private int size = 0;
private int capacity = 0;
private String firstName;
private String lastName;


public static void main(String[] args) {
    AddressBook AB = new AddressBook();
    AB.addressBookMenu();
}


public void addressBookMenu() {
    Scanner scan = new Scanner(System.in);
    String option = "";

    System.out.println("PLEASE SELECT ONE OF THE FOLLOWING OPTIONS: ");
    System.out.println("\t add   --> Add a new contact ");
    System.out.println("\t find  --> Find a contact ");
    System.out.println("\t edit  --> Edit an existing contact ");
    System.out.println("\t view  --> View the current address book");
    System.out.println("\t save  --> Save the current address book");
    System.out.println("\t quit  --> quit");
    System.out.println();
    option = scan.nextLine();

    while(!(option.equalsIgnoreCase("quit"))) {
        Contact con = new Contact(firstName, lastName);
        if(option.equalsIgnoreCase("add")) {
            System.out.println("Enter First Name: ");
            String tempFirst = scan.nextLine();
            System.out.println("Enter Last Name: ");
            String tempLast = scan.nextLine();

            con.setFirstName(tempFirst);
            con.setLastName(tempLast); 
            card.add(con);  
            writeContact();
        }   

        //View address book
        if(option.equalsIgnoreCase("view")) {
            System.out.println("\tADDRESS BOOK" + "\n" +
                    "=============================");

            Collections.sort(card);
            con.getFullName();
            readContact();
        }

        System.out.println();
        System.out.println("PLEASE SELECT ONE OF THE FOLLOWING OPTIONS: ");
        System.out.println("\t add   --> Add a new contact ");
        System.out.println("\t find  --> Find a contact ");
        System.out.println("\t edit  --> Edit an existing contact ");
        System.out.println("\t view  --> View the current address book");
        System.out.println("\t save  --> Save the current address book");
        System.out.println("\t quit  --> quit");
        System.out.println();
        option = scan.nextLine();
    }
}

public void writeContact() {
    try (FileOutputStream out = new FileOutputStream("addressbook.txt")) {  
        ObjectOutputStream os = new ObjectOutputStream(out);

        os.writeObject(card);
        os.close();

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

public void readContact() {
    try (FileInputStream in = new FileInputStream("addressbook.txt")) {
        ObjectInputStream is = new ObjectInputStream(in);
        ArrayList<Contact> card = (ArrayList<Contact>)is.readObject();

        for(Contact temp : card) {
            System.out.println(temp);
        }

        is.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}
}

Contact Class

public class Contact implements Comparable<Contact>, Serializable{

private String firstName;
private String lastName;
private String email;
private String phone;

public Contact() {
    firstName = "";
    lastName = "";
}
public Contact(String ln, String fn) {
    lastName = ln;
    firstName = fn;
}

public void setFirstName(String fn) {
    firstName = fn;
}
public void setLastName(String ln) {
    lastName = ln;
}
public void setFullName(String fn, String ln) {
    firstName = fn;
    lastName = ln;

}

public String getFirstName() {
    return firstName;
}
public String getLastName() {
    return lastName;
}
public String getFullName() {
    return lastName + firstName;
}

public String toString() {
    return 
            "FIRST NAME: " + getFirstName().substring(0).toUpperCase() + "\t" +
            "LAST NAME: " + getLastName().substring(0).toUpperCase() + "\n";            
}

@Override
public int compareTo(Contact nextContact) {
    return lastName.compareTo(nextContact.lastName);
}

}

Upvotes: 1

Views: 157

Answers (2)

janos
janos

Reputation: 124646

Any tips on why the contacts aren't sorting?

They are sorting. But then you don't print the sorted card. You re-read the contacts in readContact and then print them, unsorted.

Probably you meant to write it this way instead:

if(option.equalsIgnoreCase("view")) {
    System.out.println("\tADDRESS BOOK" + "\n" +
            "=============================");

    readContact();
    Collections.sort(card);
    printContacts();
}

And in readContact change this line:

    ArrayList<Contact> card = (ArrayList<Contact>)is.readObject();

To this:

    card = (ArrayList<Contact>)is.readObject();

And move the printing out from readContact to its own method:

void printContacts() {
    for(Contact temp : card) {
        System.out.println(temp);
    }
}

Second question, [...] any ideas how to get only the first char upper?

Sure, with a helper method like this:

private String toTitleCase(String name) {
    return Character.toTitleCase(name.charAt(0)) + name.substring(1).toLowerCase();
}

Upvotes: 1

noodlez040
noodlez040

Reputation: 236

Your problem is as follows: This code snippet

Collections.sort(card);
con.getFullName();
readContact();

is actually sorting the card collection you have, and then you call readContact() method which creates a local card collection inside it, which shadows the card collection you have in your main program, and prints its contacts, as they were written to the file before. they don't get sorted.

the solution would be like this:

if(option.equalsIgnoreCase("view")) {
    System.out.println("\tADDRESS BOOK" + "\n" +
                "=============================");

    con.getFullName(); // <------ ALSO, NOT QUITE SURE WHAT THIS IS FOR
    readContact();
}

public void readContact() {
    try (FileInputStream in = new FileInputStream("addressbook.txt")) {
        ObjectInputStream is = new ObjectInputStream(in);
        ArrayList<Contact> card = (ArrayList<Contact>)is.readObject();

        Collections.sort(card); // <----------- THIS ADDED

        for(Contact temp : card) {
            System.out.println(temp);
        }

        is.close();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
}

Upvotes: 2

Related Questions