1.3-Object-Georienteerd-Programmeren

A sortable phonebook

We are going to build a phonebook in which we (obviously) want to store entries in sorted manner. To keep things simple, we’ll just have a simple Person class that holds a last name, first name and a phone number. So, for example:

The above list is sorted on last name first, then first name and finally phone number (remember your Databases course?). You will need to implement the Comparable<T> interface to ensure the same order in our phonebook..

Build the phonebook with a print method that prints the persons that are stored in the phonebook sorted. Make sure that each person implements the Comparable<T> interface, so that you can compare persons with each other.

Hints

Example

The following code …

public static void main(String[] args) {
    Phonebook phoneBook = new Phonebook();

    // Create people (in random order)
    phoneBook.addPerson(new Person("Tristan", "Pothoven", "0699999999"));
    phoneBook.addPerson(new Person("Piet", "Jansen", "0601234567"));
    phoneBook.addPerson(new Person("Tristan", "Pothoven", "0698765432"));
    phoneBook.addPerson(new Person("Jan", "Jansen", "0612345678"));

    // Print phonebook
    System.out.println(phoneBook);
}

… produces the following output:

Contents of the phonebook: 
Jansen, Jan (0612345678)
Jansen, Piet (0601234567)
Pothoven, Tristan (0698765432)
Pothoven, Tristan (0699999999)