/* Write-only names. Once you create a name, you can only look at it, * but never change it. Note that this does not correctly handle * names in which the suffix is preceded by a comma. * * @author Samuel A. Rebelsky * @version 1.0 of February 2000 */ public class Name { // +--------+--------------------------------------------------- // | Fields | // +--------+ /** The first name. Should never be null. */ protected String first; /** * The last name. Should only be null for people that have * one name, like Arvind, Prince, or Madonna. */ protected String last; /** The middle name. Can be null. */ protected String middle; /** The "von"; a prefix for the last name. Is often null. */ protected String von; /** The suffix, such as "Jr.", "Sr.", or "III". */ protected String suffix; // +--------------+--------------------------------------------- // | Constructors | // +--------------+ /** * Create a simple name. */ public Name(String firstName, String lastName) { this.first = firstName; this.last = lastName; this.middle = null; this.von = null; this.suffix = null; } // Name(String, String) /** * Create a name, specifying all the pieces. */ public Name(String firstName, String middleName, String von, String lastName, String suffix) { this.first = firstName; this.middle = middleName; this.von = von; this.last = lastName; this.suffix = suffix; } // Name(String,String,String,String,String) // +-----------+------------------------------------------------ // | Accessors | // +-----------+ /** * Get the first name. */ public String getFirstName() { return this.first; } // getFirstName() /** * Get the last name. */ public String getLastName() { return this.last; } // getLastName() /** * Get the middle name. */ public String getMiddleName() { return this.middle; } // getMiddleName() /** * Get the suffix. */ public String getSuffix() { return this.suffix; } // getSuffix() /** * Get the "von", the prefix for the last name. */ public String getVon() { return this.von; } // getVon() /** * Convert to a string, typically in preparation for printing. */ public String toString() { // Special case: Only a first name. if (last == null) return first; // Normal case else { String name = first; if (middle != null) name = name + " " + middle; if (von != null) name = name + " " + von; name = name + " " + last; if (suffix != null) name = name + " " + suffix; return name; } // Normal case } // toString() } // class Name