package labtest1; /** * This class represents a president. * There can be only a single president (singleton). * * @author Franck van Breugel */ public class President { private String firstName; private String lastName; private int numberOfYearsInOffice; private static President instance = null; /** * Initializes this president. * * @param firstName the first name of this president. * @param lastName the last name of this president. * @param numberOfYearsInOffice the number of years in office * of this president. * @pre. numberOfYearsInOffice >= 0 */ private President(String firstName, String lastName, int numberOfYearsInOffice) { this.setFirstName(firstName); this.setLastName(lastName); this.setNumberOfYearsInOffice(numberOfYearsInOffice); } /** * Returns the first name of this president. * * @return the first name of this president. */ public String getFirstName() { return this.firstName; } /** * Sets the first name of this president to the given first name. * * @param firstName the new first name of this president. */ private void setFirstName(String firstName) { this.firstName = firstName; } /** * Returns the last name of this president. * * @return the last name of this president. */ public String getLastName() { return this.lastName; } /** * Sets the last name of this president to the given last name. * * @param lastName the new last name of this president. */ private void setLastName(String lastName) { this.lastName = lastName; } /** * Returns the number of years in office of this president. * * @return the number of years in office of this president. */ public int getNumberOfYearsInOffice() { return this.numberOfYearsInOffice; } /** * Sets the number of years in office of this president to the * given number of years in office. * * @param numberOfYearsInOffice the new number of years in office * of this president. * @pre. numberOfYearsInOffice >= 0 */ public void setNumberOfYearsInOffice(int numberOfYearsInOffice) { this.numberOfYearsInOffice = numberOfYearsInOffice; } /** * Returns the name of this president. * The name consists of the first name followed by a single space * followed by the last name. * * @return the name of this president. */ public String getName() { return this.getFirstName() + " " + this.getLastName(); } /** * Returns a president with the given first name, last name and * number of years in office. * * @param firstName the first name of the president. * @param lastName the last name of the president. * @param numberOfYearsInOffice the number of years in office. * @pre. numberOfYearsInOffice >= 0 * @return a president with the given first name, last name and * number of years in office. */ public static President getInstance(String firstName, String lastName, int numberOfYearsInOffice) { if (President.instance == null) { President.instance = new President(firstName, lastName, numberOfYearsInOffice); } else { President.instance.setFirstName(firstName); President.instance.setLastName(lastName); President.instance.setNumberOfYearsInOffice(numberOfYearsInOffice); } return President.instance; } /** * Returns a president with the given first name, last name and * no years in office. * * @param firstName the first name of the president. * @param lastName the last name of the president. * @return a president with the given first name, last name and * no years in office. */ public static President getInstance(String firstName, String lastName) { return President.getInstance(firstName, lastName, 0); } /** * Returns a string representation of this president. * This representation consists of two lines. * The first line consists of the name of this president. * The second line a * for each year this president is in office. * * @return a string representation of this president. */ public String toString() { StringBuffer representation = new StringBuffer(); representation.append(this.getName()); representation.append("\n"); for (int i = 0; i < this.getNumberOfYearsInOffice(); i++) { representation.append("*"); } return representation.toString(); } }