-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathElevator.java
91 lines (77 loc) · 2.05 KB
/
Elevator.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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
package design;
public class Elevator implements IElevator{
public static int GROUND_FLOOR = 1;
public int current;
//public PriorityQueue<Integer> levels;
public SchedulingRequestList list;
public Elevator(int floor) {
this.current = floor;
//this.levels = new PriorityQueue<>();
this.list = new SchedulingRequestList();
}
@Override
public void moveUp() {
this.current++;
}
@Override
public void moveDown() {
this.current--;
}
@Override
public ElevatorDirection getDirection() {
if (list.size() > 0) {
if (current < list.peek()) {
return ElevatorDirection.ELEVATOR_DIRECTION_UP;
}
if (current > list.peek()) {
return ElevatorDirection.ELEVATOR_DIRECTION_DOWN;
}
}
return ElevatorDirection.ELEVATOR_DIRECTION_NONE;
}
@Override
public ElevatorState getState() {
if (list.size() > 0) {
return ElevatorState.ELEVATOR_STATE_NON_EMPTY;
}
return ElevatorState.ELEVATOR_STATE_EMPTY;
}
@Override
public void floorsPressed(int floor) {
if (floor == GROUND_FLOOR) {
list.scheduleRequest(floor, ElevatorDirection.ELEVATOR_DIRECTION_UP);
} else {
ElevatorDirection dir = getDirection();
if (dir != ElevatorDirection.ELEVATOR_DIRECTION_NONE) {
if (floor == current) {
/*
* floor button pressed same as current floor*/
System.out.println("Doors open");
} else if ((dir == ElevatorDirection.ELEVATOR_DIRECTION_UP &&
floor < current) || (dir == ElevatorDirection.ELEVATOR_DIRECTION_DOWN &&
floor > current)) {
/*
* the direction is different from the button being pressed*/
System.out.println("Not Allowed");
} else {
list.scheduleRequest(floor, dir);
}
} else {
if (floor > current) {
list.scheduleRequest(floor, ElevatorDirection.ELEVATOR_DIRECTION_UP);
} else if (floor < current) {
list.scheduleRequest(floor, ElevatorDirection.ELEVATOR_DIRECTION_DOWN);
}
}
}
}
public int next() {
return list.peek();
}
public int currentLevel() {
return this.current;
}
public void remove() {
list.remove();
}
}