Source code for PhonebookEntry.java

package PhoneApp.Data;

/**
 * Manages all storage and validation of data in a
 * phone book entry.
 *
 * @version $Date: 2001/06/25 $
 * @author  Steve Franklin
 * @since   jdk 1.3.1
 */

// Changed: Have to make access to class public to allow
// for inspection and access during queries. 
public class PhonebookEntry extends DataObject {
 /**
  * Accepts a number of fields describing a phone book entry,
  * verifies each field, and updates internal state tracking
  * the validity of all fields (queried through isValid()).
  */
  public PhonebookEntry(String newFirstName, String newLastName,
                        String newPhoneNumber, String newStreet, int newStreetNumber,
                        String newApartmentNumber, String newPostalCode) {
    validState = true;

    validState &= setFirstName(newFirstName);
    validState &= setLastName(newLastName);
    validState &= setPhoneNumber(newPhoneNumber);
    validState &= setStreet(newStreet);
    validState &= setStreetNumber(newStreetNumber);
    validState &= setApartmentNumber(newApartmentNumber);
    validState &= setPostalCode(newPostalCode);
  }


  /** Returns true if all fields of the PhonebookEntry object have been set properly. */
  public boolean isValid() {
    return validState;
  }
  
  /** Converts the contents of the object to a human-readable string. */
  public String toString() {
    String s = "";

    s += phoneNumber + ": " +
         lastName + ", " + firstName + "\t" +
         streetNumber + " " + street + " ";
    
    if ( apartmentNumber != null && apartmentNumber.length() > 0 ) {
      s += "(apt. " + apartmentNumber + ") ";
    }

    if ( region != null ) {
      s += region.toString() + " ";
    }

    s += postalCode;

    return s;
  }
  
  /** Mutator method that sets the first name for a given entry. */
  public boolean setFirstName(String newFirstName) {
    boolean success = true;
    success = testString(newFirstName, VERIFY_TEXT, VERIFY_MANDATORY, 50);
    if ( success ) { firstName = newFirstName; }
    return success;
  }

  /** Mutator method that sets the last name for a given entry. */
  public boolean setLastName(String newLastName) {
    boolean success = true;
    success = testString(newLastName, VERIFY_TEXT, VERIFY_MANDATORY, 100);
    if ( success ) { lastName = newLastName; }
    return success;
  }

  /** Mutator method that sets the phone number & region for a given entry. */
  public boolean setPhoneNumber(String newPhoneNumber) {
    boolean success = true;
    success = testString(newPhoneNumber, VERIFY_TEXT, VERIFY_MANDATORY, 50);
    if ( success ) {
      PhoneRegionList list = new PhoneRegionList();
      PhoneRegion newRegion = list.getRegion(newPhoneNumber);
      if ( newRegion != null ) {
        phoneNumber = newPhoneNumber;
        region = newRegion;
      } else {
        success = false;
      }
    }
    return success;
  }

  /** Mutator method that sets the street for a given entry. */
  public boolean setStreet(String newStreet) {
    boolean success = true;
    success = testString(newStreet, VERIFY_TEXT, VERIFY_MANDATORY, 50);
    if ( success ) { street = newStreet; }
    return success;
  }

  /** Mutator method that sets the street number for a given entry. */
  public boolean setStreetNumber(int newStreetNumber) {
    boolean success = true;
    success = testInt(newStreetNumber, VERIFY_POSITIVE, VERIFY_MANDATORY, 10);
    if ( success ) { streetNumber = newStreetNumber; }
    return success;
  }

  /** Mutator method that sets the apartment number for a given entry. */
  public boolean setApartmentNumber(String newApartmentNumber) {
    boolean success = true;
    success = testString(newApartmentNumber, VERIFY_TEXT, VERIFY_OPTIONAL, 10);
    if ( success ) { apartmentNumber = newApartmentNumber; }
    return success;
  }

  /** Mutator method that sets the postal code for a given entry. */
  public boolean setPostalCode(String newPostalCode) {
    boolean success = true;
    success = testString(newPostalCode, VERIFY_TEXT, VERIFY_MANDATORY, 7);
    if ( success ) { postalCode = newPostalCode; }
    return success;
  }

  // Changed: All variables have been made public to allow queries.
  // In reality, it is much better to wrap these variables with
  // accessor methods. This has been skipped to keep code more compact.

  /** First name or initial for the owner(s) associated with the phonebook entry */
  public String firstName;

  /** Last name for the owner(s) associated with the phonebook entry */
  public String lastName;

  /** Phone number for the new phonebook entry */
  public String phoneNumber;

  /** A region object determined from the phone number */
  public PhoneRegion region = null;

  /** Street name for the owner(s) associated with the phonebook entry */
  public String street;

  /** Street number for the owner(s) */
  public int streetNumber;

  /** Optional apartment number field for the address */
  public String apartmentNumber = "";

  /** Postal code or zip code for the address */
  public String postalCode;
  
  /** Internal variable tracking the validity of the entry fields */
  public boolean validState = false;

}