// CSE 143, Winter 2009, Marty Stepp import java.util.*; // for list, map /** * A Person object represents one person in the simulation along with an * ordered list of the person's preferred choices of people to marry. */ public class Person implements Comparable { private String name; private Person fiancee; // null if single private Queue preferences; // from most to least preferred private Map rankings; // name -> preference (1=best) /** * Constructs a new person with the given name. * Precondition: name != null */ public Person(String theName) { name = theName; fiancee = null; preferences = new LinkedList(); rankings = new TreeMap(); } /** * Returns an integer indicating the ordering of this person relative to * the given other person. A negative return value means that this * person's name comes earlier in alphabetical order than the given * person's. A positive return value means that this person's name comes * later in ABC order. A return value of 0 means they have the same name. */ public int compareTo(Person other) { return name.compareTo(other.name); } /** * Sets this person to be engaged to the given other person. Subsequent * calls to getFiancee on this person will return other, and subsequent * calls to getFiancee on other will return this person. If either this * person or other were previously engaged, their previous engagement is * called off and the previous partner is set to be single. * * Precondition: other != null */ public void engageTo(Person other) { setFiancee(other); other.setFiancee(this); } /** Returns this person's name, such as "George". */ public String getName() { return name; } /** * Returns the person to whom this person is currently engaged. * If this person is single, returns null. */ public Person getFiancee() { return fiancee; } /** * Returns a queue of the names of people this person would like to marry, * in order from most preferred (front of queue) to least preferred (end * of queue). You can and should modify the contents of this queue as * your program is running. For example, you can remove a person from * the queue to indicate that you have already considered proposing to * that person. */ public Queue getPreferences() { return preferences; } /** * Returns a map of this person's rankings of all other potential * partners. The keys of the map are people's names, and the values are * ints representing this person's ranking for that potential partner. * * You can pass a person's name to the map's get method to find out the * ranking for that name. For example, if you have a Person object stored * in a variable named boy and you want to know the boy's ranking for the * girl named "Carrie", you could write: * * int rank = boy.getRankings().get("Carrie"); */ public Map getRankings() { return rankings; } /** * Returns true if this person has no current engagement partner * (if this person's fiancee is null); otherwise returns false. */ public boolean isSingle() { return fiancee == null; } /** * Returns a string representation of this Person that includes the * person's name and fiancee status if any, such as "Newman: single" * or "George: engaged to Charlotte (rank 1)". */ public String toString() { String result = name; if (isSingle()) { result += ": single"; } else { result += ": engaged to " + fiancee.getName() + " (rank " + rankings.get(fiancee.getName()) + ")"; } return result; } /** * Returns a longer string representation of this Person that includes the * person's name and fiancee status if any, as well as their complete list * of preferences for the members of the opposite sex. */ public String toStringLong() { String result = toString(); result += "\n rankings : " + rankings + "\n preferences: " + preferences + "\n"; return result; } // sets this person and other to be engaged to each other private void setFiancee(Person other) { if (!isSingle()) { // break up if (fiancee != null) { fiancee.fiancee = null; } fiancee = null; } fiancee = other; } }