Java Reference
In-Depth Information
Code 12.1
continued
The AddressBook
class
/**
* Return all the contact details, sorted according
* to the sort order of the ContactDetails class.
* @return A sorted list of the details.
*/
public String listDetails()
{
// Because each entry is stored under two keys, it is
// necessary to build a set of the ContactDetails.
// This eliminates duplicates.
StringBuilder allEntries = new StringBuilder();
Set<ContactDetails> sortedDetails =
new TreeSet<ContactDetails>(book.values());
for (ContactDetails details : sortedDetails) {
allEntries.append(details);
allEntries.append( '\n' );
allEntries.append( '\n' );
}
return allEntries.toString();
}
}
New details can be stored in the address book via its addDetails method. This assumes that
the details represent a new contact and not a change of details for an existing one. To cover the
latter case, the changeDetails method removes an old entry and replaces it with the revised
details. The address book provides two ways to retrieve entries: the getDetails method
takes a name or phone number as the key and returns the matching details; the search method
returns an array of all those details that start with a given search string (for instance, the search
string "08459" would return all entries with phone numbers having that area prefix).
There are two introductory versions of the address-book project for you to explore. Both
provide access to the same version of AddressBook , as shown in Code 12.1. The address-
book-v1t project provides a text-based user interface, similar in style to the interface of the zuul
game discussed in Chapter 6. Commands are currently available to list the address book's con-
tents, search it, and add a new entry. Probably more interesting as an interface, however, is the
address-book-v1g version, which incorporates a simple GUI. Experiment with both versions to
gain some experience with what the application can do.
Exercise 12.1 Open the address-book-v1g project and create an AddressBookDemo ob-
ject. Call its showInterface method to display the GUI and interact with the sample address
book.
Exercise 12.2 Repeat your experimentation with the text interface of the address-book-v1t
project.
 
Search WWH ::




Custom Search