// The Science of Computing PhoneEntry class.

// History:
//   August 2003 -- Created by Greg Scragg.
//   October 2003 -- updates to several methods by GWS
//   April 2004 -- Documentation cleaned up and code debugged by Doug Baldwin. Also
//      added a "hashcode" method so that this class would meet the requirement
//      that objects that are equal according to the "equals" message have equal
//      hash codes (per Sun's documentation on Object.equals and Object.hashcode).
//      Made phone entries implement serializable so that data structures that contain
//      them can be written to and read from files.


package geneseo.cs.sc;

import java.io.Serializable;


/**
 * Represents entries in a simple telephone directory, with each entry containing a
 * person's name, address, and telephone number.
 * <p>This class is intended a simple model for &quot;record&quot; objects that can be
 * stored in collections such as lists or trees. This class was created to support
 * the text <cite>Algorithms & Data Structures: The Science of Computing</cite> by
 * Doug Baldwin and Greg Scragg. All references herein to &quot;the text&quot; refer to
 * that book.
 */
 
 public class  PhoneEntry implements Comparable, Serializable {
 
 
     /**
      * The name of the person described by a telephone entry. Comparisons between
      * telephone entries are based on this field.
      */
     
    public String name;
    
    
     /** 
      * The street address for the person described by a telephone entry.
      */
      
    public String address;
    
    
    /**
     * The telephone number for the person described by a telephone entry.
     */
     
    public String phone;
    
    
    /**
     * Initialize a telephone book entry with a person's name, address, and 
     * telephone number. For example
     * <p><code>PhoneEntry p = new PhoneEntry( &quot;Sue&quot;, &quot;1 Main St&quot;, &quot;123-4567&quot; );</code></p>
     * @param who The person's name for the entry.
     * @param newAddress An address for this person.
     * @param phoneNum A telephone number for this person. 
     */

     public PhoneEntry (String who, String newAddress, String phoneNum) {
        name = who;
        address = newAddress;
        phone = phoneNum;
    }
    
    
    /**
     * Compare two telephone entries. The comparison is alphabetical by the names
     * in the entries. For example
     * <p><code>if ( entry1.compareTo(entry2) < 0 ) ...</code></p>
     * @param secondEntry The phone entry to compare this one to.
     * @return A negative number if this entry's name is less than
     *   <code>secondEntry</code>'s; 0 if the names are equal; a positive number
     *   if this entry's name is greater than <code>secondEntry</code>'s.
     */
 
    public int compareTo(Object secondEntry) {
        return this.name.compareTo( ((PhoneEntry)secondEntry).name );
    }
    
    
    /**
     * Determine whether two telephone entries are the same. Entries are considered
     * equal if their name fields are the same. For example
     * <p><code>if ( entry1.equals(entry2) ) ...</code></p>
     * @param secondEntry The telephone entry to compare this one to.
     * @return True if the telephone entries are equal, false if they are not.
     */
 
    public boolean equals(Object secondEntry) {
        return  secondEntry != null  &&  this.compareTo(secondEntry) == 0;
    }
    
    
    /**
     * Generate a hash code for a telephone entry. For example
     * <p><code>int code = entry.hashCode();</code></p>
     * <p>See chapter 14 of <cite>Algorithms and Data Structures: The Science of
     * Computing</cite> for information on what hash codes are and why one uses
     * them.</p>
     * @return The hash code computed for this telephone entry.
     */
    
    public int hashCode() {
        return name.hashCode();
    }
    
    
    /** 
     * Create a string representation of a telephone book entry. For example
     * <p><code>String text = entry.toString();</code></p>
     * @return The string representation of the telephone book entry.
     */
    
    public String toString() {
        return name + ":\n\t address:\t " + address + "\n\t phone: \t" + phone;
    }
    
}