package mug13; import java.util.Hashtable; public class WorldBuilderObject implements WorldBuilder { //Fields Hashtable people; Hashtable places; Hashtable verbs; Hashtable things; //methods /** * Add Person to world */ public void addPerson(Person bob) throws NameTakenException { if(! people.get(bob.getName()).equals(null)) { throw new NameTakenException(); } else if(! things.get(bob.getName()).equals(null)) { throw new NameTakenException(); } else people.put(bob.getName(), bob); } /** * Remove Person from world */ public void removePerson(String bob) { people.remove(bob); } /** * Add Person to world */ public void addThing(Thing jim) throws NameTakenException { if(! people.get(jim.getName()).equals(null)) { throw new NameTakenException(); } else if(! things.get(jim.getName()).equals(null)) { throw new NameTakenException(); } else things.put(jim.getName(), jim); } /** * Remove Person from world */ public void removeThing(String jim) { things.remove(jim); } /** * Add a new location. */ public void addLocation(Place loc) { places.put(loc.getName(),loc); } /** * Remove an existing location. */ public void removeLocation(String loc) { places.remove(loc); } /** * Maps names to objects */ public Noun mapNoun(String name) throws NotHereException { try { Noun returned = (Noun) this.mapPlace(name); return returned; } catch(NotHereException e) { try { Noun returned = (Noun) this.mapThingPerson(name); return returned; } catch(NotHereException f) { throw f; } } } public Place mapPlace(String name) throws NotHereException { Place returned = (Place) this.places.get(name); if(returned == null) throw new NotHereException(); return returned; } public ThingPerson mapThingPerson(String name) throws NotHereException { try { ThingPerson returned = (ThingPerson) this.mapPerson(name); return returned; } catch(NotHereException e) { try { ThingPerson returned = (ThingPerson) this.mapThing(name); return returned; } catch(NotHereException f) { throw f; } } } public Person mapPerson(String name) throws NotHereException { Person returned = (Person) this.people.get(name); if(returned == null) throw new NotHereException(); return returned; } public Thing mapThing(String name) throws NotHereException { Thing returned = (Thing) this.things.get(name); if(returned == null) throw new NotHereException(); return returned; } /** * Maps verb names to the appropriat verb */ public Verb mapVerb(String verb) throws InvalidVerbException { Verb actionTaken = (Verb) this.verbs.get(verb); if(actionTaken == null) { throw new InvalidVerbException(); } else return actionTaken; } }