package labtest1; /** * This class represents a general. * There can be at most MAX_NUMBER generals (that is, at most * MAX_NUMBER General objects can be created). * * @author Franck van Breugel */ public class General { private String name; private boolean retired; private static int number = 0; // number of created General objects /** * The maximum number of generals that can be created. */ public static final int MAX_NUMBER = 5; /** * Initializes a general with the given name who is not retired. * * @param name the name of this general. */ private General(String name) { this.setName(name); this.setRetired(false); General.number++; } /** * Returns the name of this general. * * @return the name of this general. */ public String getName() { return this.name; } /** * Sets the name of this general to the given name. * * @param name the new name of this general. */ private void setName(String name) { this.name = name; } /** * Checks if this general is retired. * * @return true if this general is retired, false otherwise. */ public boolean isRetired() { return this.retired; } /** * Sets the retired status of this general to the given status. * * @param retired the new retired status of this general. */ private void setRetired(boolean retired) { this.retired = retired; } /** * Sets this general to be retired. */ public void retire() { this.setRetired(true); } /** * Returns a general with the given name who is not retired if * the number of created generals has not reached MAX_NUMBER. * Returns null otherwise. * * @param name the name of the general. * @return a general with the given name who is not retired if * the number of created generals has not reached MAX_NUMBER, * null otherwise. */ public static General getInstance(String name) { General instance; if (General.number < General.MAX_NUMBER) { instance = new General(name); } else { instance = null; } return instance; } /** * Tests if this general is the same as the given object. * Two generals are the same if they have the same name. * Note that a general may not have a name (its name is null). * * @param object an object. * @return true if this general is the same as the given object, * false otherwise. */ public boolean equals(Object object) { boolean equal; if (object != null && this.getClass() == object.getClass()) { General other = (General) object; equal = (this.getName() == null && other.getName() == null) || (this.getName() != null && this.getName().equals(other.getName())); } else { equal = false; } return equal; } /** * Returns the hashcode of this general. * Its hashcode is the hashcode of its name. * If the its name is null, its hashcode is zero. * * @return the hashcode of this general. */ public int hashCode() { int hashCode; if (this.getName() != null) { hashCode = this.getName().hashCode(); } else { hashCode = 0; } return hashCode; } /** * Returns a string representation of this general. * This representation consists of two lines. * The first line consists of the name of this general. * The second line either consists of * "retired" or "not retired". * * @return a string representation of this general. */ public String toString() { return this.getName() + "\n" + (this.isRetired() ? "retired" : "not retired"); } }