/** * A contact. * * @author Franck van Breugel * */ public class Contact { private String name; private String address; //@ invariant this.name != null && this.address != null private int number; private static final int FIRST_NUMBER = 10001; private static int nextNumber = Contact.FIRST_NUMBER; /** * Initializes this contact with the given name and address. * * @param name the name of this contact. * @pre. name != null * @param address the address of this contact. * @pre. address != null */ public Contact(String name, String address) { this.setName(name); this.setAddress(address); this.setNumber(Contact.nextNumber); Contact.nextNumber++; } /** * Returns the name of this contact. * * @return the name of this contact. */ public String getName() { return this.name; } /** * Returns the address of this contact. * * @return the address of this contact. */ public String getAddress() { return this.address; } /** * Sets the name of this contact to the given name. * * @param name the new name of this contact. */ private void setName(String name) { this.name = name; } /** * Sets the address of this contact to the given address. * * @param address the new address of this contact. */ private void setAddress(String address) { this.address = address; } /** * Returns the number of this contact. * @return the number of this contact. */ public int getNumber() { return this.number; } /** * Set the number of this contact to the given number. * * @param number the new number of this contact. */ private void setNumber(int number) { this.number = number; } /** * Returns the hash code of this contact. * * @return the hash code of this contact. */ public int hashCode() { return this.getName().hashCode() + this.getAddress().hashCode(); } /** * Tests whether this contact is the same as the given object. * Two contacts are the same if they have the same name and * address. * * @param object an object. * @return true if this contact is the same as the given object, * false else. */ public boolean equals(Object object) { boolean equal; if (object != null && this.getClass() == object.getClass()) { Contact other = (Contact) object; equal = this.getName().equals(other.getName()) && this.getAddress().equals(other.getAddress()); } else { equal = false; } return equal; } /** * Returns a string representation of this contact. * The string consists of the name and the address of * this contact separated by " - ". * * @return a string representation of this contact. */ public String toString() { return this.getName() + " - " + this.getAddress(); } }