import java.io.*; import java.util.*; /** This class represents a name with components title, first name, last name--e.g., Mr. Dave Collins, Dr. Sarah Jones. */ public class Name implements Comparable { private String title; /** Holds the title component of the name. */ private String firstName; /** Holds the first name component of the name. */ private String lastName; /** Holds the last name component of the name. */ /** Create a completely empty Name instance. *. public Name() { } /** Create a Name instance from a String of the form "title firstName lastName". E.g., "Mr. Dave Collins". No checking is done to insure the validity of the String argument. */ public Name(String name) { setNameString(name); } /** Create a Name instance from three Strings with the title, first name, and last name. */ public Name(String t, String f, String l) { setTitle(t); setFirstName(f); setLastName(l); } /** Compare this name to aName. Return -1, 0, or 1 if this name should sort before, in the same position as, or after aName. */ public int compareTo(Name aName) { int result; if ((result = this.lastName.compareToIgnoreCase(aName.lastName)) < 0) return -1; else return result > 0 ? 1 : (this.firstName.compareToIgnoreCase(aName.firstName)); } /** Compare this name to anObject; cast anObject to Name and invoke compareTo(Name aName). */ public int compareTo(Object anObject) { return compareTo((Name)anObject); } /** Return true if this name is the same as aName. Note that equals() must be consistent with compareTo(). */ public boolean equals(Name aName) { if (this.lastName.equals(aName.lastName)) return true; else return (this.firstName.equals(aName.firstName)); } /** Return true if this name is the same as anObject; cast anObject to Name and invoke equals(Name aName). */ public boolean equals(Object anObject) { return equals((Name)anObject); } /** Return the title String. E.g., Mr., Ms., Dr. */ public String getTitle() { return title; } /** Return the first name String. */ public String getFirstName() { return firstName; } /** Return the last name (surname) String. */ public String getLastName() { return lastName; } /** Return a single String concatenating the components of the name. E.g., "Mr. Dave Collins". */ public String getNameString() { return getTitle() + " " + getFirstName() + " " + getLastName(); } /** Set the title String. E.g., Mr., Ms., Dr. */ public void setTitle(String s) { title = s; } /** Set the first name String. */ public void setFirstName(String s) { firstName = s; } /** Set the last name (surname) String. */ public void setLastName(String s) { lastName = s; } /** Set the name components from a String of the form "title firstName lastName". E.g., "Mr. Dave Collins". No checking is done to insure the validity of the String argument. */ public void setNameString(String name) { StringTokenizer st = new StringTokenizer(name); if (st.hasMoreElements()) title = st.nextToken(); if (st.hasMoreElements()) firstName = st.nextToken(); if (st.hasMoreElements()) lastName = st.nextToken(); } }