-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathElevatorImplementation.java
More file actions
231 lines (189 loc) · 5.57 KB
/
ElevatorImplementation.java
File metadata and controls
231 lines (189 loc) · 5.57 KB
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
package Solutions;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.TreeSet;
public class ElevatorImplementation {
public static void main(String[] args) {
System.out.println("Welcome to MyLift");
// RequestListenerThread to read requested floor and add to Set
Thread requestListenerThread = new Thread(new RequestListener(), "RequestListenerThread");
// RequestProcessorThread to read Set and process requested floor
Thread requestProcessorThread = new Thread(new RequestProcessor(), "RequestProcessorThread");
Elevator.getInstance().setRequestProcessorThread(requestProcessorThread);
requestListenerThread.start();
requestProcessorThread.start();
}
}
class Elevator {
private static Elevator elevator = null;
private TreeSet requestSet = new TreeSet();
private int currentFloor = 0;
private Direction direction = Direction.UP;
private Elevator() {
};
private Thread requestProcessorThread;
/**
* @return singleton instance
*/
static Elevator getInstance() {
if (elevator == null) {
elevator = new Elevator();
}
return elevator;
}
/**
* Add request to Set
*
* @param floor
*/
public synchronized void addFloor(int f) {
requestSet.add(f);
if (requestProcessorThread.getState() == Thread.State.WAITING) {
// Notify processor thread that a new request has come if it is
// waiting
notify();
} else {
// Interrupt Processor thread to check if new request should be
// processed before current request or not.
requestProcessorThread.interrupt();
}
}
/**
* @return next request to process based on elevator current floor and
* direction
*/
public synchronized int nextFloor() {
Integer floor = null;
if (direction == Direction.UP) {
if (requestSet.ceiling(currentFloor) != null) {
floor = (Integer) requestSet.ceiling(currentFloor);
} else {
floor = (Integer) requestSet.floor(currentFloor);
}
} else {
if (requestSet.floor(currentFloor) != null) {
floor = (Integer) requestSet.floor(currentFloor);
} else {
floor = (Integer) requestSet.ceiling(currentFloor);
}
}
if (floor == null) {
try {
System.out.println("Waiting at Floor :" + getCurrentFloor());
wait();
} catch (InterruptedException e) {
e.printStackTrace();
}
} else {
// Remove the request from Set as it is the request in Progress.
requestSet.remove(floor);
}
return (floor == null) ? -1 : floor;
}
public int getCurrentFloor() {
return currentFloor;
}
/**
* Set current floor and direction based on requested floor
*
* @param currentFloor
* @throws InterruptedException
*/
public void setCurrentFloor(int currentFloor) throws InterruptedException {
if (this.currentFloor > currentFloor) {
setDirection(Direction.DOWN);
} else {
setDirection(Direction.UP);
}
this.currentFloor = currentFloor;
System.out.println("Floor : " + currentFloor);
Thread.sleep(3000);
}
public Direction getDirection() {
return direction;
}
public void setDirection(Direction direction) {
this.direction = direction;
}
public Thread getRequestProcessorThread() {
return requestProcessorThread;
}
public void setRequestProcessorThread(Thread requestProcessorThread) {
this.requestProcessorThread = requestProcessorThread;
}
public TreeSet getRequestSet() {
return requestSet;
}
public void setRequestSet(TreeSet requestSet) {
this.requestSet = requestSet;
}
}
class RequestProcessor implements Runnable {
@Override
public void run() {
while (true) {
Elevator elevator = Elevator.getInstance();
int floor = elevator.nextFloor();
int currentFloor = elevator.getCurrentFloor();
try {
if (floor >= 0) {
if (currentFloor > floor) {
while (currentFloor > floor) {
elevator.setCurrentFloor(--currentFloor);
}
} else {
while (currentFloor < floor) {
elevator.setCurrentFloor(++currentFloor);
}
}
System.out.println("Welcome to Floor : " + elevator.getCurrentFloor());
}
} catch (InterruptedException e) {
// If a new request has interrupted a current request processing
// then check -
// -if the current request is already processed
// -otherwise add it back in request Set
if (elevator.getCurrentFloor() != floor) {
elevator.getRequestSet().add(floor);
}
}
}
}
}
class RequestListener implements Runnable {
@Override
public void run() {
while (true) {
String floorNumberStr = null;
try {
// Read input from console
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(System.in));
floorNumberStr = bufferedReader.readLine();
} catch (IOException e) {
e.printStackTrace();
}
if (isValidFloorNumber(floorNumberStr)) {
System.out.println("User Pressed : " + floorNumberStr);
Elevator elevator = Elevator.getInstance();
elevator.addFloor(Integer.parseInt(floorNumberStr));
} else {
System.out.println("Floor Request Invalid : " + floorNumberStr);
}
}
}
/**
* This method is used to define maximum floors this elevator can process.
*
* @param s
* - requested floor
* @return true if requested floor is integer and upto two digits. (max
* floor = 99)
*/
private boolean isValidFloorNumber(String s) {
return (s != null) && s.matches("\\d{1,2}");
}
}
enum Direction {
UP, DOWN
}