Back to project page ZorbsCity.
The source code is released under:
GNU General Public License
If you think the Android project ZorbsCity listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.
package jonathan.geoffroy.zorbscity.model.cityObjects.buildings; /* ww w . j a v a2s.c o m*/ import jonathan.geoffroy.zorbscity.model.Simulator; import jonathan.geoffroy.zorbscity.model.SimulatorEvent; import jonathan.geoffroy.zorbscity.model.TimeLine; import jonathan.geoffroy.zorbscity.model.cityObjects.people.DeliveryMan; import jonathan.geoffroy.zorbscity.model.cityObjects.resources.Resource; import jonathan.geoffroy.zorbscity.model.helpers.NeededList; import jonathan.geoffroy.zorbscity.model.simulator.events.ObjectCreatedEvent; /** * An Enterprise is a Building which permit to create a resource, or deliver a service, depending on the type of the Enterprise. * Enterprises use People to transport created resources to a Storage. * Enterprises need that inhabitants works for it to create a Resource. * @author Jonathan GEOFFROY * @version 0.1 */ public abstract class Enterprise extends Building { /** * auto-generated serial (using eclipse) */ private static final long serialVersionUID = 865893382776868256L; public static final int COMMON_NEEDED_EMPLOYEES = 8; private static final int COMMON_TIME_TO_CREATE_RESOURCE = 100; public Enterprise() { needed = initNeeded(); } /** * * @return the time to create a resource, i.e. the number of call to simulationAct to create a new ressource. */ public int timeToCreateResource() { return COMMON_TIME_TO_CREATE_RESOURCE; } /** * * @return a new instance of the created resource */ protected abstract Resource createRessouce(); /** * * @return the list of the needed resource */ protected abstract NeededList initNeeded(); @Override public void onAddedIntoCity(Simulator sim) { /* Just for tests * Remove them for few days, when you have implemented the delivery men */ for(Resource r : resources) { r.addAmount(200); } if(enoughEmployees() && enoughResources()) { beginToCreateResource(sim); } } @Override public void addPeople(Simulator sim, int numberPeople) { super.addPeople(sim, numberPeople); if(enoughEmployees() && enoughResources()) { beginToCreateResource(sim); } } @Override public void addResource(Simulator sim, Resource r) { super.addResource(sim, r); if(enoughEmployees() && enoughResources()) { beginToCreateResource(sim); } } protected void beginToCreateResource(Simulator sim) { assert(enoughEmployees() && enoughResources()); TimeLine timeLine = sim.getTimeLine(); timeLine.add(new ObjectCreatedEvent(this, timeLine.getTime() + timeToCreateResource())); } private boolean enoughEmployees() { return numberPeople == neededEmployees(); } @Override public void onTriggeredEvent(SimulatorEvent event, Simulator sim) { super.onTriggeredEvent(event, sim); /* * ****** ****** ***** ***** ****** ***** ***** ****** ****** ****** * Argh! some bad code here, be it seems that * onTriggeredEvent(ObjectCreatedEvent event, Simulator sim) * is not called when event instanceof ObjectCreateEvent ?! * ****** ****** ***** ***** ****** ***** ***** ****** ****** ****** */ if(event instanceof ObjectCreatedEvent) { onTriggeredEvent((ObjectCreatedEvent) event, sim); } else { System.out.println("uncatched event : " + event.getClass()); } } /** * Decrement each needed resource, and begin to create another resource if have enough resources * Should be called when a Enterprise receive an ObjectCreatedEvent */ public void onTriggeredEvent(ObjectCreatedEvent event, Simulator sim) { Resource createdResource = createRessouce(); DeliveryMan deliveryMan = new DeliveryMan(this, createdResource); sim.add(deliveryMan); resources.useNeeded(needed); if(enoughEmployees() && enoughResources()) { beginToCreateResource(sim); } } public int neededEmployees() { return COMMON_NEEDED_EMPLOYEES; } }