Source of TelephoneDirectory.java


  1: import java.util.Iterator;
  2: import java.util.Scanner;
  3: /**
  4:    A class of telephone directories.
  5:    @author Frank M. Carrano
  6:    @author Timothy M. Henry
  7:    @version 5.0
  8: */
  9: public class TelephoneDirectory
 10: {
 11:    private DictionaryInterface<Name, String> phoneBook; // Sorted dictionary with distinct search keys

 13:    public TelephoneDirectory() 
 14:    { 
 15:       phoneBook = new SortedDictionary<>();
 16:    } // end default constructor
 17:    
 18:    // 20.10
 19:    /** Reads a text file of names and telephone numbers.
 20:        @param data  A text scanner for the text file of data. */
 21:    public void readFile(Scanner data)
 22:    {
 23:       while (data.hasNext())
 24:       {
 25:          String firstName   = data.next();
 26:          String lastName    = data.next();
 27:          String phoneNumber = data.next();

 29:          Name fullName = new Name(firstName, lastName);
 30:          phoneBook.add(fullName, phoneNumber);
 31:       } // end while

 33:       data.close();  
 34:    } // end readFile
 35:    
 36:    // 20.11
 37:    /** Gets the phone number of a given person. */
 38:    public String getPhoneNumber(Name personName)
 39:    {
 40:       return phoneBook.getValue(personName);
 41:    } // end getPhoneNumber

 43:    public String getPhoneNumber(String firstName, String lastName)
 44:    {
 45:       Name fullName = new Name(firstName, lastName);
 46:       return phoneBook.getValue(fullName);
 47:    } // end getPhoneNumber

 49: } // end TelephoneDirectory