/** * "Normal" address, which may contain zero to three lines of information * plus city, state, zip, and country. * * @author Samuel A. Rebelsky * @version 1.0 of February 2000 */ public class Address { // +--------+--------------------------------------------------- // | Fields | // +--------+ /** * The three lines of the address. Set to null when they * are not used. (Ideally, these would be represented as * an array. However, we do not yet know about arrays.) */ protected String line1; protected String line2; protected String line3; /** The city. This should never be null. */ protected String city; /** The state or region. This should never be null. */ protected String state; /** * The zip code. This may be null if unknown. We use a string * to support 9-digits zips and alphanumeric zips sometimes used * in other countries. */ protected String zip; /** The country. Optional. */ protected String country; // +--------------+--------------------------------------------- // | Constructors | // +--------------+ /** * Create a simple address. */ public Address(String line, String city, String state, String zip) { // This is a call to another constructor in the same class. this(line, null, null, city, state, zip, null); } // Address(String, String) /** * Create a full address. */ public Address(String line1, String line2, String line3, String city, String state, String zip, String country) { this.line1 = line1; this.line2 = line2; this.line3 = line3; this.city = city; this.state = state; this.zip = zip; this.country = country; } // Address(String*7) // +-----------+------------------------------------------------ // | Accessors | // +-----------+ /** * Get the city. */ public String getCity() { return this.city; } // getCity() /** * Get the country. May return null. */ public String getCountry() { return this.country; } // getCountry() /** * Get one line. The parameter should be an integer between * 1 and 3. */ public String getLine(int num) { if (num == 1) return this.line1; else if (num == 2) return this.line2; else if (num == 3) return this.line3; else return null; } // getLine() /** * Get the state or region. */ public String getState() { return this.state; } // getState() /** * Get the zip code or postal code. May return null. */ public String getZip() { return this.zip; } // getZip() /** * Convert to a string, typically in preparation for printing. * The multi-line string for the address ends with a carriage * return. */ public String toString() { return this.toString(""); } // toString() /** * Convert to a string, prefixing every line with a string. * Typically used when the address should be indented (in which * case the prefix is just a few spaces). Note that the multi- * line string for an address ends with a carriage return. */ public String toString(String prefix) { String address = ""; for (int i = 1; i <= 3; i++) { String line = getLine(i); if (line != null) address = address + prefix + line + "\n"; } // for address = address + prefix + city; if (state != null) address = address + ", " + state; if (zip != null) address = address + " " + zip; address = address + "\n"; if (country != null) address = address + prefix + country + "\n"; return address; } // toString() // +-----------+------------------------------------------------ // | Modifiers | // +-----------+ /** * Set the city. The city should never be null. */ public void setCity(String newCity) { this.city = newCity; } // getCity() /** * Set the country. The country may be null. */ public void setCountry(String newCountry) { this.country = newCountry; } // getCountry() /** * Set one line. The first parameter should be an integer between * 1 and 3. Any or all lines may be null. */ public void setLine(int num, String newLine) { if (num == 1) this.line1 = newLine; else if (num == 2) this.line2 = newLine; else if (num == 3) this.line3 = newLine; else ; // Do nothing. } // getLine() /** * Set the state or region. May be null. */ public void setState(String newState) { this.state = newState; } // getState() /** * Set the zip code or postal code. May be null. */ public void setZip(String newZip) { this.zip = newZip; } // getZip() } // class Address