Java Reference
In-Depth Information
on how to report errors when they occur. We also provide a brief introduction to how to perform
textual input/output, as file processing is one of the situations where errors can easily arise.
12.1
The address-book project
We shall use the address-book family of projects to illustrate some of the principles of error
reporting and error handling that arise in many applications. The projects represent an appli-
cation that stores personal-contact details—name, address, and phone number—for an arbi-
trary number of people. Such a contacts list might be used on a mobile phone or in an e-mail
program, for instance. The contact details are indexed in the address book by both name and
phone number. The main classes we shall be discussing are AddressBook (Code 12.1) and
ContactDetails . In addition, the AddressBookDemo class is provided as a convenient
means of setting up an initial address book with some sample data.
Code 12.1
The AddressBook
class
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;
import java.util.Set;
import java.util.SortedMap;
import java.util.TreeMap;
import java.util.TreeSet;
/**
* A class to maintain an arbitrary number of contact details.
* Details are indexed by both name and phone number.
* @author David J. Barnes and Michael Kölling.
* @version 2011.07.31
*/
public class AddressBook
{
// Storage for an arbitrary number of details.
private TreeMap<String, ContactDetails> book;
private int numberOfEntries;
/**
* Perform any initialization for the address book.
*/
public AddressBook()
{
book = new TreeMap<String, ContactDetails>();
numberOfEntries = 0;
}
/**
* Look up a name or phone number and return the
* corresponding contact details.
 
 
Search WWH ::




Custom Search