From 0541b38e507f44bd5158a6456a8769de7f111025 Mon Sep 17 00:00:00 2001 From: MathieuPietri Date: Tue, 15 Oct 2019 15:06:13 +0200 Subject: [PATCH] fin refactoring et javadoc instruction queue --- src/commandSystem/BasicInstructionQueue.java | 106 +++++++++++-------- src/commandSystem/InstructionQueue.java | 2 +- src/commandSystem/Test.java | 2 +- 3 files changed, 61 insertions(+), 49 deletions(-) diff --git a/src/commandSystem/BasicInstructionQueue.java b/src/commandSystem/BasicInstructionQueue.java index 610fcb4..a8f76a7 100644 --- a/src/commandSystem/BasicInstructionQueue.java +++ b/src/commandSystem/BasicInstructionQueue.java @@ -12,7 +12,7 @@ public class BasicInstructionQueue implements InstructionQueue { private boolean emergencyState; private int currentFloor; - private Direction direction; + private Direction currentDirection; public BasicInstructionQueue() { @@ -20,22 +20,7 @@ public class BasicInstructionQueue implements InstructionQueue { downQueue = new ArrayList(); emergencyState = false; currentFloor = 0; - direction = Direction.NONE; - } - - /** - * Adds a new floor to the argument's queue then sorts the elements. - * - * @param queue the list to be sorted. - * @param floor the index of the floor that will be added. - * @param reversed if set to true, the queue will be sorted by descending order. - */ - private void appSort(ArrayList queue, int floor, boolean reversed) { - queue.add(floor); - if (reversed) - Collections.sort(queue, Collections.reverseOrder()); - else - Collections.sort(queue); + currentDirection = Direction.NONE; } public void computeEvent(Event event) { @@ -94,7 +79,7 @@ public class BasicInstructionQueue implements InstructionQueue { */ case EMERGENCYSTOP: emergencyState = true; - clearQueue(); + clearQueues(); break; /* @@ -109,7 +94,7 @@ public class BasicInstructionQueue implements InstructionQueue { */ case REACHEDFLOORSIGNAL: currentFloor = event.getCurrentFloor(); - direction = event.getCurrentDirection(); + currentDirection = event.getCurrentDirection(); removeInstruction(currentFloor); break; @@ -118,32 +103,6 @@ public class BasicInstructionQueue implements InstructionQueue { } } - /** - * Removes one instruction from the queue - has to be called when the elevator - * reaches targeted floor. - * - * @param floor the reached floor - * @param direction the direction of the elevator - */ - private void removeInstruction(int floor) { - switch (direction) { - case UP: - if (upQueue.contains(floor)) - upQueue.remove(upQueue.indexOf(floor)); - else // we go up to the top of descending queue - downQueue.remove(downQueue.indexOf(floor)); - break; - case DOWN: - if (downQueue.contains(floor)) - downQueue.remove(downQueue.indexOf(floor)); - else // we go down to the bottom of ascending queue - upQueue.remove(upQueue.indexOf(floor)); - break; - default: - break; - } - } - /** * Returns the next floor the elevator has to go to * @@ -151,7 +110,7 @@ public class BasicInstructionQueue implements InstructionQueue { */ public int getNextInstruction() { int nextFloor = -1; - switch (direction) { + switch (currentDirection) { // get the first element of upQueue that is ABOVE the elevator's current floor case UP: for (int i = 0; i < upQueue.size() && nextFloor < 0; i++) { @@ -185,7 +144,60 @@ public class BasicInstructionQueue implements InstructionQueue { return nextFloor; } - private void clearQueue() { + /** + * Adds a new floor to the argument's queue then sorts the elements. + * + * @param queue the list to be sorted. + * @param floor the index of the floor that will be added. + * @param reversed if set to true, the queue will be sorted by descending order. + */ + private void appSort(ArrayList queue, int floor, boolean reversed) { + queue.add(floor); + if (reversed) + Collections.sort(queue, Collections.reverseOrder()); + else + Collections.sort(queue); + } + + /** + * Removes one instruction from the queue - has to be called when the elevator + * reaches targeted floor. + * + * @param floor the reached floor. + * @param currentDirection the direction of the elevator. + */ + private void removeInstruction(int floor) { + switch (currentDirection) { + + /* + * Elevator is headed upwards. + */ + case UP: + if (upQueue.contains(floor)) + upQueue.remove(upQueue.indexOf(floor)); + else + downQueue.remove(downQueue.indexOf(floor)); + break; + + /* + * Elevator is headed downwards. + */ + case DOWN: + if (downQueue.contains(floor)) + downQueue.remove(downQueue.indexOf(floor)); + else + upQueue.remove(upQueue.indexOf(floor)); + break; + + default: + break; + } + } + + /** + * Clears both queues. + */ + private void clearQueues() { downQueue.removeAll(downQueue); upQueue.removeAll(upQueue); } diff --git a/src/commandSystem/InstructionQueue.java b/src/commandSystem/InstructionQueue.java index df9bd80..ae1ed84 100644 --- a/src/commandSystem/InstructionQueue.java +++ b/src/commandSystem/InstructionQueue.java @@ -17,4 +17,4 @@ public interface InstructionQueue { * @return the next floor if it exists, else -1 */ public int getNextInstruction(); -} +} \ No newline at end of file diff --git a/src/commandSystem/Test.java b/src/commandSystem/Test.java index c0dd45e..df36959 100644 --- a/src/commandSystem/Test.java +++ b/src/commandSystem/Test.java @@ -7,7 +7,7 @@ import Events.ReachedFloorEvent; public class Test { private static boolean running = false; - private static BasicInstructionQueue queue; + private static InstructionQueue queue; private static Direction currentDirection; private static int currentFloor;