CIS-180 Laboratory Project

Address Book Implementation

Description

This week you will continue implementing the Address Book application that you have designed. You will be implementing three classes that will allow you to view your address book in a window.

Hopefully, your design includes a class to represent a list of contacts. You might have called this class something like ContactList. This class should have an attribute of type String for the list name, and an attribute for the list of contacts. A good type for the list attribute is ArrayList<Contact> (assuming Contact is the name of your class for address book entries).

Your design should also have included a class for the address book, e.g. AddressBook, and a class for displaying an address book in a window, e.g. AddressBookWindow. Your AddressBook class probably contains an attribute for a list of contact lists. A good type for this attribute would be ArrayList<ContactList>. The AddressBookWindow class should have an attribute of type AddressBook as well as the attributes that are required for the GUI.

If your design is much different from what is described above, you should consult with your lab instructor on how to proceed.

Hints

Using ArrayLists

ArrayLists are objects that maintain lists of other objects. For example, an object of type ArrayList<Contact> is used to maintain a list of Contact objects. The ArrayList class has many methods to manage a list including methods to add and remove objects from the list. For example, we could add and remove some contacts from a contact list as follows:

// Create a contact list
ArrayList<Contact> contactList = new ArrayList<Contact>();

// Create some contact objects
Contact c1 = new Contact(...);  // pass whatever parameters needed to initialize the contact
Contact c2 = new Contact(...);
Contact c3 = new Contact(...);

// Add the contacts to the list
contactList.add(c1);
contactList.add(c2);
contactList.add(c3);

// Remove one of the contacts from the list
contactList.remove(c2);

Now suppose we want to print out the nickname of each contact on the list (assuming you have defined a getNickname method in your Contact class). We could accomplish this using a loop, as follows:

// Access each contact on the list, and print its nickname on the console
for (Contact c: contactList) {
   System.out.println(c.getNickname());
}

Exercises

As you complete each exercise, make sure that your requirement and design documents are kept consistent with what you have done. You may need to customize the exercises somewhat depending on your particular design. If in doubt, consult your lab instructor.

  1. Add a class to your project for the ContactList class. There should be methods to add and remove contacts, to get the size of the list, and to get the Contact at a given position in the list. The implementation of these methods can simply pass the appropriate message to the contact list's array list object.
  2. Add a class to your project for the AddressBook class. There should be a default constructor that creates contact lists for All Contacts, Friends, Family, and Work, and adds them to its list of contact lists.
  3. When your application is complete, the user will create contacts using a ContactEditor add save them on disk so they don't need to be entered each time the application starts. For now, just include code in the AddressBook constructor to create some contacts and add them to the contact lists for testing purposes. You can delete or comment out the testing code when your application is complete.
  4. Add a class to your project for the AddressBookWindow class. This class should extend JFrame. It should have a constructor that takes an AddressBook as a parameter and displays it in a window. If your design calls for a contact editing area within the address book window (rather than in a separate window), you should modify your ContactEditor class from the previous lab to extend JPanel instead of JFrame. Then you can include a contact editor object within the address book window.
  5. The AddressBookWindow class is the most logical place for your application's main method. You can delete the main method you wrote last week and write a new main method to test what you have done this week.