package test3; import java.util.Set; import java.util.TreeSet; /** * A class that represents a smart phone. Every smart is a telephone * that has a phone number and a set of contacts. The smart phone and * its phone number forms an aggregation. The smart phone and its * set of contacts forms a composition. * */ public class SmartPhone { /** * The phone number for this smart phone. */ private PhoneNumber number; /** * The set of contacts for this smart phone. */ private Set contacts; /** * Initializes this smart phone to have a valid phone number. * Do not duplicate the code in this constructor; it is here for * testing purposes in case your constructors are not implemented * correctly. */ public SmartPhone() { try { Test3Utils.initialize(this); } catch (Exception x) { System.out.println("something went wrong"); throw new NullPointerException(); } } /** * Initializes this smart phone given its telephone number. * The set of contacts is initialized as an empty set. * * @param number * the telephone number of this phone * @pre. number != null */ public SmartPhone(PhoneNumber number) { this.number = number; this.contacts = new TreeSet(); } /** * Initializes this smart phone given its telephone number and a set of * contacts. * * @param number * the telephone number of this phone * @param contacts * a set of contacts for this phone * @pre. number != null and contacts != null */ public SmartPhone(PhoneNumber number, Set contacts) { this.number = number; this.contacts = new TreeSet(); for (Contact c : contacts) { this.contacts.add(new Contact(c)); } } /** * Return the phone number for this smart phone. * * @return the phone number for this smart phone */ public PhoneNumber getNumber() { return this.number; } /** * Set then phone number for this smart phone. * * @param number the phone number for this smart phone * @pre. number != null */ public void setNumber(PhoneNumber number) { this.number = number; } /** * Returns true if this smart phone has a contact with the * given phone number. * * @param number a phone number * @return true if this smart phone has a contact with the * given phone number */ public boolean hasContact(PhoneNumber number) { for (Contact c : this.contacts) { if (c.getNumber().equals(number)) { return true; } } return false; } /** * Returns a deep copy of the set of contacts for this phone. * * @return the set of contacts for this phone */ public Set getContacts() { Set contacts = new TreeSet(); for (Contact c : this.contacts) { contacts.add(new Contact(c)); } return contacts; } /** * Add a new contact to the set of the contacts for this phone. * If the argument contact is already in this phone's set of * contacts then the set of contacts is not modified. * * @param contact * a contact to add to this phone * @throws Exception * if the phone already has the specified contact in its set of * contacts */ public void addContact(Contact contact) throws Exception { if (this.contacts.contains(contact)) { throw new Exception("already have this contact"); } this.contacts.add(contact); } /** * Returns a string representation of this smart phone. The returned string * is the phone number (as returned by the Phone version of toString). * * @return a string representation of this smart phone */ @Override public String toString() { return this.number.toString(); } }