import java.io.*;
import java.util.*;

public class PhoneBook
{
  private TreeMap<String, String> theBook;

/** Construct a new candidate list with the given information.
**/
  public PhoneBook()
  {
    theBook = new TreeMap<String, String>();
  }

/******************************************************************************/
/* Accessors                                                                  */
/******************************************************************************/

/******************************************************************************/
/* Mutators                                                                   */
/******************************************************************************/

/******************************************************************************/
/* Utilities                                                                  */
/******************************************************************************/
/******************************************************************************/
  public int getLength()
  {
    return this.theBook.size();
  }

/******************************************************************************/
  public void getData(Scanner inDataFile)
  {
    PhoneRecord thePhoneRecord;
    System.out.println("Method 'getData' beginning");
    while(inDataFile.hasNext())
    {
      thePhoneRecord = new PhoneRecord();

      thePhoneRecord.getData(inDataFile);
      this.theBook.put(thePhoneRecord.getField(Fields.FIRSTNAME) +
                       thePhoneRecord.getField(Fields.LASTNAME),
                       thePhoneRecord.getField(Fields.NUMBER));

    } // while(inDataFile.hasNext())

    System.out.println("Method 'getData' ending");
  } // public void getTheList(Scanner inDataFile)

/******************************************************************************/
  public void echoData(PrintWriter outDataFile)
  {
    PhoneRecord theRecord = new PhoneRecord();

    outDataFile.printf("%nThe list of phone records is%n");
    outDataFile.printf("%20s %12s%n","NAME","NUMBER");
    for(String key: this.theBook.keySet())
    {
      outDataFile.printf("%20s %12s%n",key,this.theBook.get(key));
      outDataFile.flush();
    }
  } // public void echoData(PrintWriter outDataFile)

/******************************************************************************/
/** Single layer of printing by match to retrieve data and print
 *  if there is a match.
**/
  public void printWithMatch(PrintWriter outDataFile,String matchKey)
  {
    if(this.theBook.containsKey(matchKey))
    {
      outDataFile.printf("%20s %12s%n","NAME","NUMBER");
      outDataFile.printf("%20s %12s%n",matchKey,this.theBook.get(matchKey));
      outDataFile.flush();
    }
  } // public void printWithMatch(PrintWriter outDataFile,String matchKey)

} // public class PhoneBook

