/** Name models a persons full name, with non-null Strings for first, last
 *  and (possibly) middle names.
 */
public class Name {
  // A person always has a first and last name.  If they lack a middle name
  // it will be null.
  private String first, middle, last;

  /** constructor: create a Name with first name f and last name l.
   */
  public Name(String f, String l) {
    first= f;
    last= l;
  }

  /** constructor: create a name with first name f, last name l, and
   *  middle name m.
   */
  public Name(String f, String m, String l) {
    this(f, l); // use the other constructor...
    middle= m;
  }

  /** toString(): return this Name as an informative String.
   */
  public String toString() {
    String result= "First name: " + first;
    result += "\nMiddle name: " + middle;
    result += "\nLast name: " + last;
    return result;
  }

  /** equals: true iff this and other lack middle names and
   *  match otherwise OR this and other both have middle names
   *  and match on all three names.
   */
  public boolean equals(Name other) {
    // first attempt, abandoned after it got too complicated...
    // return other != null &&
    // first.equals(other.first) && last.equals(other.last);\

    // second attempt --- use helper methods to preserve sanity
    return other != null &&
      (equalsNoMiddle(other) || equalsWithMiddle(other));
  }

  /** equalsNoMiddle: true iff this and other both lack middle names
   *  and they match on first and last.
   */
  private boolean equalsNoMiddle(Name other) {
    return middle == null && other.middle == null &&
      first.equals(other.first) && last.equals(other.last);
  }

  /** equalsWithMiddle: true iff this and other both have middle
   *  names and they match on first, last, and middle.
   */
  private boolean equalsWithMiddle(Name other) {
    return middle != null && other.middle != null &&
      first.equals(other.first) && middle.equals(other.middle) &&
      last.equals(other.last);
  }
}


