correction bugs instructionqueue
This commit is contained in:
parent
0541b38e50
commit
2ed258e60e
@ -20,7 +20,7 @@ public class BasicInstructionQueue implements InstructionQueue {
|
|||||||
downQueue = new ArrayList<Integer>();
|
downQueue = new ArrayList<Integer>();
|
||||||
emergencyState = false;
|
emergencyState = false;
|
||||||
currentFloor = 0;
|
currentFloor = 0;
|
||||||
currentDirection = Direction.NONE;
|
currentDirection = Direction.UP;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void computeEvent(Event event) {
|
public void computeEvent(Event event) {
|
||||||
@ -64,7 +64,7 @@ public class BasicInstructionQueue implements InstructionQueue {
|
|||||||
/*
|
/*
|
||||||
* If the requested floor is under the elevator, adds the instruction to the
|
* If the requested floor is under the elevator, adds the instruction to the
|
||||||
* downQueue then sorts it. if it's above, adds the instruction to the upQueue
|
* downQueue then sorts it. if it's above, adds the instruction to the upQueue
|
||||||
* then sorts it. If it's the same floor, doesn't do anything
|
* then sorts it. If it's the same floor, doesn't do anything.
|
||||||
*/
|
*/
|
||||||
case CALLFROMELEVATOR:
|
case CALLFROMELEVATOR:
|
||||||
if (event.getRequestedFloor() > currentFloor && !upQueue.contains(event.getRequestedFloor()))
|
if (event.getRequestedFloor() > currentFloor && !upQueue.contains(event.getRequestedFloor()))
|
||||||
@ -110,6 +110,9 @@ public class BasicInstructionQueue implements InstructionQueue {
|
|||||||
*/
|
*/
|
||||||
public int getNextInstruction() {
|
public int getNextInstruction() {
|
||||||
int nextFloor = -1;
|
int nextFloor = -1;
|
||||||
|
|
||||||
|
//System.out.println("" + upQueue + downQueue);
|
||||||
|
|
||||||
switch (currentDirection) {
|
switch (currentDirection) {
|
||||||
// get the first element of upQueue that is ABOVE the elevator's current floor
|
// get the first element of upQueue that is ABOVE the elevator's current floor
|
||||||
case UP:
|
case UP:
|
||||||
|
@ -9,19 +9,22 @@ public class Test {
|
|||||||
private static boolean running = false;
|
private static boolean running = false;
|
||||||
private static InstructionQueue queue;
|
private static InstructionQueue queue;
|
||||||
|
|
||||||
private static Direction currentDirection;
|
private static Direction currentDirection = Direction.UP;
|
||||||
private static int currentFloor;
|
private static int currentFloor = 0;
|
||||||
|
|
||||||
public static void iter() {
|
public static void iter() {
|
||||||
int nextDestination = queue.getNextInstruction();
|
int nextDestination = queue.getNextInstruction();
|
||||||
|
|
||||||
if (!running) {
|
if (!running) {
|
||||||
// would be -1 if the queue doesn't have any next instruction
|
// would be -1 if the queue doesn't have any next instruction
|
||||||
|
|
||||||
|
//System.out.println("next dest : " + nextDestination);
|
||||||
if (nextDestination != -1) {
|
if (nextDestination != -1) {
|
||||||
running = true;
|
running = true;
|
||||||
currentDirection = currentFloor < nextDestination ? Direction.UP : Direction.DOWN;
|
currentDirection = currentFloor < nextDestination ? Direction.UP : Direction.DOWN;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
//System.out.println("not running");
|
||||||
// if we just reached a new floor
|
// if we just reached a new floor
|
||||||
if (currentFloor == nextDestination) {
|
if (currentFloor == nextDestination) {
|
||||||
queue.computeEvent(new ReachedFloorEvent(currentFloor, currentDirection));
|
queue.computeEvent(new ReachedFloorEvent(currentFloor, currentDirection));
|
||||||
@ -32,16 +35,14 @@ public class Test {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
System.out.println("elevator floor " + currentFloor + " direction " +
|
System.out.println("elevator floor " + currentFloor + " direction " +
|
||||||
(currentDirection == Direction.UP? "up" : "down") + " running: " + running);
|
(currentDirection) + " running: " + running);
|
||||||
}
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
//TODO envoyer les notifs de changements d'étages
|
|
||||||
queue = new BasicInstructionQueue();
|
queue = new BasicInstructionQueue();
|
||||||
iter();
|
iter();
|
||||||
iter();
|
iter();
|
||||||
queue.computeEvent(new CallFromFloorEvent(2, Direction.DOWN));
|
queue.computeEvent(new CallFromFloorEvent(2, Direction.DOWN));
|
||||||
iter();
|
|
||||||
System.out.println("someone calls from floor 2 to go down");
|
System.out.println("someone calls from floor 2 to go down");
|
||||||
iter();
|
iter();
|
||||||
queue.computeEvent(new CallFromFloorEvent(1, Direction.UP));
|
queue.computeEvent(new CallFromFloorEvent(1, Direction.UP));
|
||||||
|
Reference in New Issue
Block a user