-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElevatorSystemControl.java
73 lines (64 loc) · 1.67 KB
/
ElevatorSystemControl.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
package design;
import java.util.ArrayList;
import java.util.LinkedList;
import java.util.Queue;
public class ElevatorSystemControl implements IElevatorControl {
public static int MAX_ELEVATORS = 16;
public static int MAX_FLOORS = 100;
public int nElevators;
public int nFloors;
public Queue<Integer> pickUpFloors;
public ArrayList<Elevator> elevators;
public ElevatorSystemControl(int elevators, int floors) {
if (elevators > MAX_ELEVATORS) {
this.nElevators = MAX_ELEVATORS;
} else {
this.nElevators = elevators;
}
if (floors > MAX_FLOORS) {
this.nFloors = MAX_FLOORS;
} else {
this.nFloors = floors;
}
this.pickUpFloors = new LinkedList<>();
init();
}
private void init() {
elevators = new ArrayList<>();
for (int i = 0; i < nElevators; i++) {
elevators.add(new Elevator(1));
}
}
@Override
public void pickUp(int floor) {
pickUpFloors.offer(floor);
}
@Override
public void destination(int id, int destFloor) {
elevators.get(id).floorsPressed(destFloor);
}
@Override
public void timeStepping() {
for (Elevator ele : elevators) {
ElevatorState state = ele.getState();
if (state == ElevatorState.ELEVATOR_STATE_EMPTY) {
if (!pickUpFloors.isEmpty()) {
ele.floorsPressed(pickUpFloors.poll());
}
} else if (state == ElevatorState.ELEVATOR_STATE_NON_EMPTY) {
ElevatorDirection dir = ele.getDirection();
if (dir == ElevatorDirection.ELEVATOR_DIRECTION_UP) {
ele.moveUp();
} else if (dir == ElevatorDirection.ELEVATOR_DIRECTION_DOWN) {
ele.moveDown();
} else {
/*
* Elevator is not moving case
* Destination Floor is reached
* */
ele.remove();
}
}
}
}
}