/**
 * A person, stored in a database.  Note that a person only has one address
 * associated with it, and you must modify rather than replacing that address.
 * $Id: Person.java,v 1.7 1999/05/09 01:12:35 flynt Exp $
 */
public class Person
  extends DatabaseEntry
{
  // +--------+--------------------------------------------------
  // | Fields |
  // +--------+

  /** The last name of the person. */
  protected String lastName;
  /** The first name of the person. */
  protected String firstName;
  /** The suffix for the name (e.g., Jr.) */
  protected String suffix;
  /** The title (e.g., Dr., Ms.) */
  protected String title;
  /** An address to use to contact the person. */
  protected Address address;
  /** A phone number to use to contact the person. */
  protected String phone;
  /** A fax number to use to contact the person. */
  protected String fax;
  /** An email address to use to contact the person. */
  protected String email;
  /** Local contact information (e.g., Hotel/Room). */
  protected String local;

  // +--------------+--------------------------------------------
  // | Constructors |
  // +--------------+

  /**
   * Create a new person.
   */
  public Person() {
    this.address = new Address();
    lastName = firstName = suffix = title = phone = fax = email = local = "";
  } // Person()

  // +------------------+----------------------------------------
  // | Standard Methods |
  // +------------------+

  /**
   * Basic information about the person.
   */
  public String toString() {
    return this.name;
  } // toString()

  // +-----------+-----------------------------------------------
  // | Accessors |
  // +-----------+

  /**
   * Get a particular field.  Simplifies the Database object.
   */
  public String getField(String fieldName) 
    throws Exception
  {
    fieldName = fieldName.toLowerCase();
    if (fieldName.equals("firstname") || fieldName.equals("first name")
         || fieldName.equals("fname"))
      return getFirstName();
    else if (fieldName.equals("lastname") || fieldName.equals("last name")
         || fieldName.equals("lname"))
      return getLastName();
    else if (fieldName.equals("suffix")) 
      return getSuffix();
    else if (fieldName.equals("title"))
      return getTitle();
    else if (fieldName.equals("phone"))
      return getPhone();
    else if (fieldName.equals("fax"))
      return getFax();
    else if (fieldName.equals("email"))
      return getEmail();
    else if (fieldName.equals("local"))
      return getLocal();
    else if (fieldName.equals("city"))
      return address.getCity();
    else if (fieldName.equals("state"))
      return address.getState();
    else if (fieldName.equals("zip"))
      return address.getZip();
    else if (fieldName.equals("country"))
      return address.getCountry();
    else if (fieldName.equals("address-0") || fieldName.equals("address0") 
               || fieldName.equals("0"))
      return address.getLine(0);
    else if (fieldName.equals("address-1") || fieldName.equals("address1") 
               || fieldName.equals("1"))
      return address.getLine(1);
    else if (fieldName.equals("address-2") || fieldName.equals("address2") 
               || fieldName.equals("2"))
      return address.getLine(2);
    else if (fieldName.equals("address-3") || fieldName.equals("address3") 
               || fieldName.equals("3"))
      return address.getLine(3);
    else if (fieldName.equals("address-4") || fieldName.equals("address4") 
               || fieldName.equals("4"))
      return address.getLine(4);
    else
      return super.getField(fieldName);
  } // getField(String)
    
  /**
   * Get a list of all the fields.
   */
  public String[] listFields() {
    String[] parentFields = super.listFields();
    String[] myFields = {
      "FirstName",
      "LastName",
      "Title",
      "Suffix",
      "Phone",
      "Fax",
      "Email",
      "Local",
      "City",
      "State",
      "Zip",
      "Country",
      "Address-0",
      "Address-1",
      "Address-2",
      "Address-3",
      "Address-4",
    };
    String[] result = new String[parentFields.length + myFields.length];
    for (int i = 0; i < parentFields.length; ++i) {
      result[i] = parentFields[i];
    }
    for (int i = 0; i < myFields.length; ++i) {
      result[parentFields.length+i] = myFields[i];
    }
    return result;
  } // listFields

  /**
   * Get the person's address.  
   *   Note that you cannot set the address.  To update
   *   the address, you must get the address and update its fields.
   * Pre: The person is initialized.
   * Pre: The person has an address.
   * Post: Returns the person's address.
   */
  public Address getAddress() {
    return this.address;
  } // getAddress()

  /**
   * Get the person's email address.
   * Pre: The person is initialized.
   * Pre: The person has an email address.
   * Post: Returns the person's email address.
   */
  public String getEmail() {
    return this.email;
  } // getEmail()

  /**
   * Get the person's fax number.
   * Pre: The person is initialized.
   * Pre: The person has a fax number.
   * Post: Returns the person's fax number.
   */
  public String getFax() {
    return this.fax;
  } // getFax()

  /** 
   * Get the person's first name.
   * Pre: The person is initialized.
   * Pre: The person has a first name.
   * Post: Returns the person's first name.
   */
  public String getFirstName() {
    return this.firstName;
  } // getFirstName()

  /**
   * Get the person's last name.
   * Pre: The person is initialized.
   * Pre: The person has a last name.
   * Post: Returns the person's last name.
   */
  public String getLastName() {
    return this.lastName;
  } // getLastName()

  /**
   * Get local contact information.
   * Pre: The person is initialized.
   * Pre: The person has local contact information.
   * Post: Returns the person's local contact information.
   */
  public String getLocal() {
    return this.local;
  } // getLocal()

  /**
   * Get the person's phone number.
   * Pre: The person is initialized.
   * Pre: The person has a phone number.
   * Post: Returns the person's phone number.
   */
  public String getPhone() {
    return this.phone;
  } // getPhone()

  /**
   * Get the suffix for the person's name.
   * Pre: The person is initialized.
   * Pre: The person has a suffix.
   * Post: Returns the suffix.
   */
  public String getSuffix() {
    return this.suffix;
  } // getSuffix()

  /**
   * Get the person's title.
   * Pre: The person is initialized.
   * Pre: The person has a title.
   * Post: Returns the title.
   */
  public String getTitle() {
    return this.title;
  } // getTitle()


  // +-----------+-----------------------------------------------
  // | Modifiers |
  // +-----------+

  /**
   * Set a particular field.  Simplifies the Database object.
   */
  public void setField(String fieldName, String newValue) 
    throws Exception
  {
    fieldName = fieldName.toLowerCase();
    if (fieldName.equals("firstname") || fieldName.equals("first name")
         || fieldName.equals("fname"))
      setFirstName(newValue);
    else if (fieldName.equals("lastname") || fieldName.equals("last name")
         || fieldName.equals("lname"))
      setLastName(newValue);
    else if (fieldName.equals("suffix")) 
      setSuffix(newValue);
    else if (fieldName.equals("title"))
       setTitle(newValue);
    else if (fieldName.equals("phone"))
       setPhone(newValue);
    else if (fieldName.equals("fax"))
       setFax(newValue);
    else if (fieldName.equals("email"))
       setEmail(newValue);
    else if (fieldName.equals("local"))
       setLocal(newValue);
    else if (fieldName.equals("city"))
       address.setCity(newValue);
    else if (fieldName.equals("state"))
       address.setState(newValue);
    else if (fieldName.equals("zip"))
       address.setZip(newValue);
    else if (fieldName.equals("country"))
       address.setCountry(newValue);
    else if (fieldName.equals("address-0") || fieldName.equals("address0") 
               || fieldName.equals("0"))
       address.setLine(0,newValue);
    else if (fieldName.equals("address-1") || fieldName.equals("address1") 
               || fieldName.equals("1"))
       address.setLine(1,newValue);
    else if (fieldName.equals("address-2") || fieldName.equals("address2") 
               || fieldName.equals("2"))
       address.setLine(2,newValue);
    else if (fieldName.equals("address-3") || fieldName.equals("address3") 
               || fieldName.equals("3"))
       address.setLine(3,newValue);
    else if (fieldName.equals("address-4") || fieldName.equals("address4") 
               || fieldName.equals("4"))
       address.setLine(4,newValue);
    else
       super.setField(fieldName,newValue);
  } // setField(String)
    
  /**
   * Set the person's email address.
   * Pre: The person is initialzed.
   * Post: The value returned by getEmail will be newEmail.
   */
  public void setEmail(String newEmail) {
    this.email = newEmail;
  } // setEmail(String)

  /**
   * Set the person's local contact information.
   * Pre: The person is initialzed.
   * Post: The value returned by getLocal will be newInfo.
   */
  public void setLocal(String newInfo) {
    this.local = newInfo;
  } // setLocal(String)

  /**
   * Set the person's fax number.
   * Pre: The person is initialzed.
   * Post: The value returned by getFax will be newFax.
   */
  public void setFax(String newFax) {
    this.fax = newFax;
  } // setEmail(String)

  /**
   * Set the person's first name. 
   * Pre: The person is initialized.
   * Post: The value returned by getFirstName will be newFirst.
   * Post: The value returned by getName may involve newFirst.
   */
  public void setFirstName(String newFirst) {
    this.firstName = newFirst;
    this.name = buildName();
  } // setFirstName(String)

  /**
   * Set the person's last name. 
   * Pre: The person is initialized.
   * Post: The value returned by getLastName will be newLast.
   * Post: The value returned by getName may involve newLast.
   */
  public void setLastName(String newLast) {
    this.lastName = newLast;
    this.name = buildName();
  } // setLastName(String)

  /**
   * Set the person's phone number.
   * Pre: The person is initialzed.
   * Post: The value returned by getPhone will be newPhone.
   */
  public void setPhone(String newPhone) {
    this.phone = newPhone;
  } // setPhone(String)

  /**
   * Set the person's suffix.
   * Pre: The person is initialzed.
   * Post: The value returned by getSuffix will be newSuffix.
   * Post: The value returned by getName may involve newSuffix.
   */
  public void setSuffix(String newSuffix) {
    this.suffix = newSuffix;
    this.name = buildName();
  } // setSuffix(String)

  /**
   * Set the person's title.
   * Pre: The person is initialzed.
   * Post: The value returned by getTitle will be newTitle.
   * Post: The value returned by getName may involve newTitle.
   */
  public void setTitle(String newTitle) {
    this.title = newTitle;
    this.name = buildName();
  } // setTitle(String)


  // +----------------+------------------------------------------
  // | Helper Methods |
  // +----------------+

  /**
   * Build the name using the components of the name.  Used when some component
   * is updated.
   */
  protected String buildName() {
    String nm = lastName + ", " + firstName;
    if ((suffix != null) && (!suffix.equals("")))
      nm = nm + ", " + suffix;
    if ((title != null) && (!title.equals("")))
      nm = nm + ", " + title;
    return nm;
  } // buildName()

} // class Person

