l'ascenseur s'arrête un moment aux étages

This commit is contained in:
DylanVsn 2019-10-19 21:44:42 +02:00
parent 9d1796ef4c
commit 55e8c1ea0f
3 changed files with 17 additions and 5 deletions

View File

@ -23,14 +23,15 @@ public class BasicInstructionQueue implements InstructionQueue {
currentDirection = Direction.UP; 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 state prevents any event from affecting the queue except the cancel
* emergency stop event. * emergency stop event.
*/ */
if (emergencyState && event.getType() != EventType.CANCELEMERGENCYSTOP) if (emergencyState && event.getType() != EventType.CANCELEMERGENCYSTOP)
return; return haveToWait;
switch (event.getType()) { switch (event.getType()) {
@ -96,13 +97,16 @@ public class BasicInstructionQueue implements InstructionQueue {
case REACHEDFLOORSIGNAL: case REACHEDFLOORSIGNAL:
currentFloor = event.getCurrentFloor(); currentFloor = event.getCurrentFloor();
currentDirection = event.getCurrentDirection(); currentDirection = event.getCurrentDirection();
if (currentFloor == getNextInstruction()) if (currentFloor == getNextInstruction()) {
removeInstruction(currentFloor); removeInstruction(currentFloor);
haveToWait = true;
}
break; break;
default: default:
break; break;
} }
return haveToWait;
} }
/** /**

View File

@ -8,8 +8,9 @@ public interface InstructionQueue {
* Compute a request into the instruction queue. * Compute a request into the instruction queue.
* *
* @param event - the request to compute * @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. * Returns the next floor the elevator has to go to.

View File

@ -12,6 +12,7 @@ import Events.*;
public class Simulation implements ElevatorListener { public class Simulation implements ElevatorListener {
private boolean running = false; private boolean running = false;
private int currentFloor = 0; private int currentFloor = 0;
private int secondToWait = 1;
private BasicInstructionQueue queue = new BasicInstructionQueue(); private BasicInstructionQueue queue = new BasicInstructionQueue();
private Elevator elevator; private Elevator elevator;
@ -33,6 +34,7 @@ public class Simulation implements ElevatorListener {
public void floorChange(Direction direction) { public void floorChange(Direction direction) {
System.out.println("Floor reached: " + direction); System.out.println("Floor reached: " + direction);
boolean haveToWait;
switch (direction) { switch (direction) {
case UP: case UP:
currentFloor++; currentFloor++;
@ -41,7 +43,12 @@ public class Simulation implements ElevatorListener {
currentFloor--; currentFloor--;
break; 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(); update();
} }