diff --git a/src/commandSystem/BasicInstructionQueue.java b/src/commandSystem/BasicInstructionQueue.java index 7c7edb8..e41a4a5 100644 --- a/src/commandSystem/BasicInstructionQueue.java +++ b/src/commandSystem/BasicInstructionQueue.java @@ -23,14 +23,15 @@ public class BasicInstructionQueue implements InstructionQueue { currentDirection = Direction.UP; } - public void computeEvent(Event event) { + public boolean computeEvent(Event event) { + boolean haveToWait = false; /* * Emergency state prevents any event from affecting the queue except the cancel * emergency stop event. */ if (emergencyState && event.getType() != EventType.CANCELEMERGENCYSTOP) - return; + return haveToWait; switch (event.getType()) { @@ -96,13 +97,16 @@ public class BasicInstructionQueue implements InstructionQueue { case REACHEDFLOORSIGNAL: currentFloor = event.getCurrentFloor(); currentDirection = event.getCurrentDirection(); - if (currentFloor == getNextInstruction()) + if (currentFloor == getNextInstruction()) { removeInstruction(currentFloor); + haveToWait = true; + } break; default: break; } + return haveToWait; } /** diff --git a/src/commandSystem/InstructionQueue.java b/src/commandSystem/InstructionQueue.java index ae1ed84..4940f7f 100644 --- a/src/commandSystem/InstructionQueue.java +++ b/src/commandSystem/InstructionQueue.java @@ -8,8 +8,9 @@ public interface InstructionQueue { * Compute a request into the instruction queue. * * @param event - the request to compute + * @return true if the elevator have to wait at floor */ - public void computeEvent(Event event); + public boolean computeEvent(Event event); /** * Returns the next floor the elevator has to go to. diff --git a/src/simulation/Simulation.java b/src/simulation/Simulation.java index e4e28ad..580205d 100644 --- a/src/simulation/Simulation.java +++ b/src/simulation/Simulation.java @@ -12,6 +12,7 @@ import Events.*; public class Simulation implements ElevatorListener { private boolean running = false; private int currentFloor = 0; + private int secondToWait = 1; private BasicInstructionQueue queue = new BasicInstructionQueue(); private Elevator elevator; @@ -33,6 +34,7 @@ public class Simulation implements ElevatorListener { public void floorChange(Direction direction) { System.out.println("Floor reached: " + direction); + boolean haveToWait; switch (direction) { case UP: currentFloor++; @@ -41,7 +43,12 @@ public class Simulation implements ElevatorListener { currentFloor--; break; } - queue.computeEvent(new ReachedFloorEvent(currentFloor, direction)); + haveToWait = queue.computeEvent(new ReachedFloorEvent(currentFloor, direction)); + if (haveToWait) { + long t1 = System.nanoTime(); + elevator.stop(); + while ((System.nanoTime() - t1) / 1_000_000_000 < secondToWait); + } update(); }