// This class supports items in the Alberta class. // Part of the reason for having this class is to show that different // classes implementing the same interface can be completely unrelated. import java.util.Hashtable; public class Catalogue { // A Catalogue is a listing of Item data, // that is, a mapping from id to name, base price. // Each Item entity can then use the Catalogue to lookup. // This variable maps ids to index entries. Hashtable index = new Hashtable (); // This is number of elements in the Catalogue. int numElements = 0; // These variables represent the Catalogue. // They are indexed from 1; the 0'th element is always null to //handle non-entered lookups. private String [] names; private int [] prices; // in cents // Make a new Catalogue withe some entries. public Catalogue () { names = new String [100]; prices = new int [100]; // These are redundant but make the point. names [0] = null; prices [0] = 0; } // This returns the index number of an id, or 0 if none such. private int indexNumber (String id) { Integer entry = (Integer) index. get (id); if (entry == null) return 0; else return entry. intValue (); } // Returns the name of an item, or null if no such id. public String getName (String id) { return names [indexNumber (id)]; } // Returns the price of an item, or 0 if no such id. public int getPrice (String id) { return prices [indexNumber (id)]; } // Store data for an id. public void put (String id, String name, int price) { int i = indexNumber (id); if (i == 0) { // Wasn't there: put a new one. numElements = numElements + 1; i = numElements; index. put (id, new Integer (i)); } names [i] = name; prices [i] = price; } }