fin refactoring et javadoc instruction queue
This commit is contained in:
parent
055979374c
commit
0541b38e50
@ -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<Integer>();
|
||||
emergencyState = false;
|
||||
currentFloor = 0;
|
||||
direction = Direction.NONE;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds a new floor to the argument's <b>queue</b> 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<Integer> 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 <b>queue</b> 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<Integer> 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);
|
||||
}
|
||||
|
@ -17,4 +17,4 @@ public interface InstructionQueue {
|
||||
* @return the next floor if it exists, else -1
|
||||
*/
|
||||
public int getNextInstruction();
|
||||
}
|
||||
}
|
@ -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;
|
||||
|
Reference in New Issue
Block a user