package test3; import java.util.Random; /** * This class represents a badge. Each badge has a name. * * @author Franck van Breugel */ public class Badge { private String name; /** * Initializes this badge with the name "No Name". */ public Badge() { this("No Name"); } /** * Initializes this badge with the given name. * * @param name the name of this badge. * @pre. name != null */ public Badge(String name) { this.setName(name); } /** * Returns the name of this badge. * * @return the name of this badge. */ public String getName() { return this.name; } /** * Sets the name of this badge to the given name. * * @param name the new name of this badge. * @pre. name != null */ public void setName(String name) { this.name = name; } /** * Tests whether this badge is the same as the given object. * Two badges are the same if their names are the same, ignoring * capitalizations. For example, badges with the names "John doe" * and "john DOE" are considered equal. * * @param object an object. * @return true if this badge is the same as the given object, * false otherwise. */ public boolean equals(Object object) { boolean equal; if (object != null && this.getClass() == object.getClass()) { Badge other = (Badge) object; equal = this.getName().toLowerCase().equals(other.getName().toLowerCase()); } else { equal = false; } return equal; } // Although equals is overridden, you do not have to override hashCode in this test /** * Random object that can be used in the getRandom method. */ private static Random random = new Random(System.currentTimeMillis()); /** * Returns a badge with either the name "John Doe" or the * name "Jane Doe". The name is chosen randomly. * * @return a badge with either the name "John Doe" or the * name "Jane Doe". */ public static Badge getRandom() { Badge badge; if (Badge.random.nextBoolean()) { badge = new Badge("John Doe"); } else { badge = new Badge("Jane Doe"); } return badge; } }