package mug13; import java.util.Hashtable; import java.util.Vector; public class WorldBuilderObject implements WorldBuilder { //Fields Hashtable people; Hashtable places; Hashtable verbs; Hashtable things; //constructor //methods /** * Add Person to world */ public void addPerson(Person bob) throws NameTakenException { if(! this.people.get(bob.getName()).equals(null)) { throw new NameTakenException(); } else if(! this.things.get(bob.getName()).equals(null)) { throw new NameTakenException(); } else if(! this.places.get(bob.getName()).equals(null)) { throw new NameTakenException(); } else { //place person into world people.put(bob.getName(), bob); //person is in the abyss, and has no location until log in } } /** * Remove Person from world */ public void removePerson(String bill) { try { //map bill to the appropriate person Person bob = this.mapPerson(bill); //remove the things they have from the world Vector removeThese = bob.things(); int recurse = removeThese.size(); for(int i = 0; i < recurse; ++i) { this.removeThing((String) removeThese.get(i)); } //remove person from the room try { Place loc = this.mapPlace(bob.getWordAttribute("location")); loc.removePerson(bob.getName()); } catch(Exception e) { } //remove person from world people.remove(bob); } catch(Exception e) { } } /** * Add Person to world */ public void addThing(Thing jim) throws NameTakenException { //check for name availibility if(! this.people.get(jim.getName()).equals(null)) { throw new NameTakenException(); } else if(! this.things.get(jim.getName()).equals(null)) { throw new NameTakenException(); } else if(! this.places.get(jim.getName()).equals(null)) { throw new NameTakenException(); } else { //add thing to world this.things.put(jim.getName(), jim); try { Place loc = this.mapPlace(jim.getWordAttribute("location")); loc.addThing(jim.getName()); } catch(Exception e) { } } } /** * Remove Person from world */ public void removeThing(String jim) { try { //map jim to the appropriate Thing Thing jimBob = this.mapThing(jim); //remove Thing from backpack or place try { Place loc = this.mapPlace(jimBob.getWordAttribute("location")); loc.removeThing(jimBob.getName()); } catch(NotHereException e) { try { Person loc = this.mapPerson(jimBob.getWordAttribute("location")); loc.removeFromPack(jimBob.getName()); } catch(Exception g) { } } //remove form world this.things.remove(jim); } catch(NotHereException f) { } } /** * Add a new location. */ public void addLocation(Place loc) throws NameTakenException { if(! this.people.get(loc.getName()).equals(null)) { throw new NameTakenException(); } else if(! this.things.get(loc.getName()).equals(null)) { throw new NameTakenException(); } else if(! this.places.get(loc.getName()).equals(null)) { throw new NameTakenException(); } else { this.places.put(loc.getName(), loc); } } /** * Remove an existing location. */ public void removeLocation(String loc) { try { //map the Place Place bob = this.mapPlace(loc); //remove the things it has from the world Vector removeThese = bob.things(); int recurse = removeThese.size(); for(int i = 0; i < recurse; ++i) { this.removeThing((String) removeThese.get(i)); } //move the persons here into the void Vector moveThese = bob.persons(); int pRecurse = moveThese.size(); for(int i = 0; i < pRecurse; ++i) { try { //Person to move Person moved = this.mapPerson((String) moveThese.get(i)); //move the person moved.changeWordAttribute("location", null); } catch(Exception e) { } } //remove the location from the world this.places.remove(loc); } catch(Exception e) { } } /** * 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; } }