Java Reference
In-Depth Information
Output
Enter first name and last name or quit to end:
Maria Lopez
The phone number for Maria Lopez is 401-555-1234
Enter first name and last name or quit to end:
Hunter
Error in entering name. Try again.
Enter first name and last name or quit to end:
Hunter Smith
Hunter Smith is not in the directory.
Enter first name and last name or quit to end:
quit
Bye!
19.9
Beginning the implementation.
The class
TelephoneDirectory
begins as shown in Listing 19-3.
We assume that the class
SortedDictionary
implements a sorted version of the ADT dictionary
having distinct search keys. A sorted dictionary requires its search keys to belong to a class that
implements the interface
Comparable
. We assume that
Name
does so.
LISTING 19-3
An outline of the class
TelephoneDirectory
import
java.util.Scanner;
public class
TelephoneDirectory
{
private
DictionaryInterface<Name, String> phoneBook;
public
TelephoneDirectory()
{
phoneBook =
new
SortedDictionary<Name, String>();
}
// end default constructor
/** Reads a text file of names and telephone numbers.
@param data a text scanner for the text file of data */
public void
readFile(Scanner data)
{
. . .
<
See Segment 19.10.
>
}
// end readFile
/** Gets the phone number of a given person. */
public
String getPhoneNumber(Name personName)
{
. . .
<
See Segment 19.11.
>
}
// end getPhoneNumber
}
// end TelephoneDirectory
19.10
To implement the method
readFile
, you need to know what the data file looks like. Suppose that
each line in the file contains three strings—a first name, a last name, and a telephone number—
separated by blanks. Thus, a typical line might appear as
Suzanne Nouveaux
401-555-1234