From 55e8c1ea0f2701f9c0792fd13d71df6d11fdc74d Mon Sep 17 00:00:00 2001 From: DylanVsn <43576618+DylanVsn@users.noreply.github.com> Date: Sat, 19 Oct 2019 21:44:42 +0200 Subject: [PATCH] =?UTF-8?q?l'ascenseur=20s'arr=C3=AAte=20un=20moment=20aux?= =?UTF-8?q?=20=C3=A9tages?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/commandSystem/BasicInstructionQueue.java | 10 +++++++--- src/commandSystem/InstructionQueue.java | 3 ++- src/simulation/Simulation.java | 9 ++++++++- 3 files changed, 17 insertions(+), 5 deletions(-) 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(); }