/** * This class represents a single Type of animal (e.g. "dog" or "cat") and * It also contains the list of all animals (e.g. Spot and Speckles) which are * of this Type. */ import java.util.ArrayList; public class Type { String typeName; ArrayList animals = new ArrayList(); /* Constructor for Type objects: * @paran n the type */ public Type(String n) { typeName = n; } /* Accessor * @return the name of the type. */ public String getName() { return typeName; } /* Accessor * @param i the index of the animal in the list * @return the ith Animal object */ public Animal getAnimal(int i) { return animals.get(i); } /* Accessor * @param i the index of the animal in the list * @return the name of the ith animal */ public String getAnimalName(int i) { return animals.get(i).getName(); } /** Add an Animal to the list of animals assuming there is no animal * in the list with the same name. * @param a the animal to add * @return true if the new animal added, otherwise return false */ public boolean addAnimal(Animal a) { for (Animal an: animals) { if (an.getName().equals(a.getName())) { return false; } } animals.add(a); return true; } /* @return a String representation of the information store in this object. */ public String toString() { String s = "Type Name: " + typeName + " --- Animals with this type: "; for (Animal a: animals) { s += a.getName() + " "; } return s; } }