-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElevatorController.java
More file actions
382 lines (268 loc) · 11.5 KB
/
Copy pathElevatorController.java
File metadata and controls
382 lines (268 loc) · 11.5 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
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.*;
import java.io.*;
public class ElevatorController{
public boolean goingUp;
public boolean goingDown;
private boolean doorsOpen;
private int currentFloor;
public int currentTime;
private int numPeople;
private int currentWeight;
private int maxWeight;
private static final AtomicInteger elevatorNumberGenerator = new AtomicInteger(1);
private int elevatorNumber;
private Elevator elevator;
// a person will be added to the waiting queue when we the current time has reached there arrival time
public ConcurrentHashMap<Integer,ConcurrentLinkedQueue<Person>> waiting;
// all the people who will want to use the elevator
public ConcurrentHashMap<Integer,ConcurrentLinkedQueue<Person>> request;
public ConcurrentHashMap<Integer,ConcurrentLinkedQueue<Person>> inElevator;
// the request hashmap maps the request to the floor they are at
// the person making the request will join the linked queue for that floor
// (hashmap) {floor 0 -> linkedQueue{0 : [person1 - > person 2 -> 3}
public ElevatorController(){
// decide which direction the elevator is going
this.goingUp = true;
this.goingDown = false;
this.doorsOpen = false;
// all the request the elevator must handle
this.request = new ConcurrentHashMap<Integer,ConcurrentLinkedQueue<Person>>();
// all of the people who have arrived and are waiting on a floor
this.waiting = new ConcurrentHashMap<Integer,ConcurrentLinkedQueue<Person>>();
// all the people currently in the elevator
this.inElevator = new ConcurrentHashMap<Integer,ConcurrentLinkedQueue<Person>>();
this.currentFloor = 1;
// time will go up in every time the elevator goes up a floor
this.currentTime = 0;
this.currentWeight = 0 ;
// maximum weight limit
this.maxWeight = 500;
// assign each elevator a number
this.elevatorNumber = elevatorNumberGenerator.getAndIncrement();
}
// add a person into the in elevator, request or waiting queue
public synchronized void addPerson(Person person, int floor, ConcurrentHashMap<Integer,ConcurrentLinkedQueue<Person>> hashmap){
ConcurrentLinkedQueue<Person> tmp = new ConcurrentLinkedQueue<Person>();
if (hashmap.containsKey(floor)){
tmp = hashmap.get(floor);
tmp.add(person);
hashmap.put(floor, tmp);
}
else{
tmp.add(person);
hashmap.put(floor, tmp);
}
}
// add a person into the in elevator, request or waiting queue to the tail
public synchronized void addPersonFirst(Person person, int floor, ConcurrentHashMap<Integer,ConcurrentLinkedQueue<Person>> hashmap){
ConcurrentLinkedQueue<Person> oldQueue = new ConcurrentLinkedQueue<Person>();
ConcurrentLinkedQueue<Person> newQueue = new ConcurrentLinkedQueue<Person>();
if (hashmap.containsKey(floor)){
oldQueue = hashmap.get(floor);
newQueue.add(person);
for (Person p : oldQueue){
newQueue.add(p);
}
hashmap.put(floor, newQueue);
}
else{
oldQueue.add(person);
hashmap.put(floor, oldQueue);
}
}
// remove a person from either the inElevator, request or waiting queue
public synchronized Person removeAPerson(int floor, ConcurrentHashMap<Integer,ConcurrentLinkedQueue<Person>> hashmap) {
ConcurrentLinkedQueue<Person> tmp = new ConcurrentLinkedQueue<Person>();
if (hashmap.containsKey(floor)){
tmp = hashmap.get(floor);
Person personRemoved = tmp.poll();
hashmap.put(floor, tmp);
if (hashmap.get(floor).isEmpty()){
hashmap.remove(floor);
}
return personRemoved;
}
return null;
}
// A person wants to request the elevator, add them into request hashmap
public synchronized void makeRequest(Person person){
addPerson(person,person.getArrivalTime(), request);
}
// if the persons arrival time is <= current time add them to the waiting queue
public synchronized void acceptRequest(){
Person personWaiting = removeAPerson(currentTime, request);
while(personWaiting != null){
addPerson(personWaiting,personWaiting.getArrivalFloor(), waiting);
removeAPerson(personWaiting.getArrivalFloor(), request);
personWaiting = removeAPerson(currentTime, request);
}
}
// this will decide which floor the elevator goes to next
public synchronized void changeFloor(int minFloor,int maxFloor) throws InterruptedException{
// Example for below - this will tell which direction the elevator should go
// Current floor -> 5
// InElevator Keys -> 1,2,3
// these keys are the peoples destination floor
// max = 3
// since max is less then current floor the elevator must go down
if(!(inElevator.isEmpty())){
int max = Collections.max(inElevator.keySet());
if(max < currentFloor){
this.goingUp = false;
this.goingDown = true;
}
else{
this.goingUp = true;
this.goingDown = false;
}
}
else{
// if there is no one in the elevator and its going up
// and there are no people waiting on the upper floors
// go down
if(!(waiting.isEmpty()) && goingUp == true){
int max = Collections.max(waiting.keySet());
if (max < currentFloor){
this.goingUp = false;
this.goingDown = true;
}
}
}
// if there are people waiting or in the elevator
if(!(waiting.isEmpty()) || !(inElevator.isEmpty()) ){
String elevatorFloorString = String.format("Elevator %d is on floor %s\n",this.elevatorNumber,this.currentFloor);
// write to output.dat
writeOutput(elevatorFloorString);
// print what floor the elevator is on
System.out.printf(elevatorFloorString);
// check if the next floor is max or min then decide direction
if (goingUp == true){
if(this.currentFloor == maxFloor){
currentFloor--;
goingUp = false;
goingDown = true;
}
else{
currentFloor++;
}
}
else{
if(currentFloor == minFloor){
goingUp = true;
goingDown = false;
currentFloor++;
}
else{
currentFloor--;
}
}
}
//elevator sleeping if no people waiting or in the elevator
if (waiting.isEmpty() && inElevator.isEmpty()){
String sleepingOutput = String.format("Elevator %d is sleeping on floor %d\n",this.elevatorNumber, this.currentFloor);
writeOutput(sleepingOutput);
System.out.printf(sleepingOutput);
}
// update the time
this.currentTime++;
}
public synchronized void enterElevator() throws InterruptedException{
boolean headingPrinted = false;
// remove a person who's waiting at the current floor
Person personEntering = removeAPerson(currentFloor, waiting);
// while there is people waiting on the current floor
while(personEntering!=null){
// print heading
if (!(headingPrinted)){
String enteringElevator = String.format("Stopping on floor %d for people\n**************************\nAllowing people in on floor %s...\n",this.currentFloor, this.currentFloor);
writeOutput(enteringElevator);
System.out.print(enteringElevator);
headingPrinted = true;
}
// check if elevator will become overloaded
int combinedWeight = this.currentWeight + personEntering.getTotalWeight();
if (combinedWeight >= maxWeight){
// if the elevator is over weight
// the last person in exits the elevator and is put back at the front of the queue on that floor
addPersonFirst(personEntering, currentFloor, waiting);
String weightWarning = String.format("**********************************************************\nElevator %d too heavy with combined weight %d and max weight limit %d\nPerson %s is leaving the elevator\n**********************************************************\n", this.elevatorNumber, combinedWeight, maxWeight, personEntering);
writeOutput(weightWarning);
System.out.print(weightWarning);
break;
}
// add the persons weight to the current weight
this.currentWeight += personEntering.getTotalWeight();
// add the person to the in elevator
addPerson(personEntering, personEntering.getDestinationFloor(),inElevator);
// print person's details
writeOutput(personEntering.toString());
System.out.print(personEntering);
// remove another waiting person on the current floor
personEntering = removeAPerson(currentFloor, waiting);
}
}
public synchronized void exitElevator() throws InterruptedException{
// remove a person from the current floor who's in the elevator
Person personLeaving = removeAPerson(currentFloor, this.inElevator);
boolean headingPrinted = false;
// while there are people on this floor remove them from the elevator
while(personLeaving!=null){
// print heading to show people are leaving
if(!(headingPrinted)){
String exitingElevator = String.format("*******************************\nLetting people out on floor %s...\n",currentFloor);
writeOutput(exitingElevator);
System.out.print(exitingElevator);
headingPrinted = true;
}
// print/output who's leaving elevator
writeOutput(personLeaving.toString());
System.out.print(personLeaving);
// adjust weight according to the people who are leaving
this.currentWeight-= personLeaving.getTotalWeight() ;
// remove another person from this floor
personLeaving = removeAPerson(currentFloor,inElevator);
}
}
public synchronized void transferPeople(ElevatorController faulty, ElevatorController backUp) {
// outptut a fault has occured
// print out that another elevator is being dispatched
String faultWarning = "**********FAULT PEOPLE BEING TRANSFERRED TO ANOTHER ELEVATOR***********\n**********Starting backup elevator from floor 1************************\n";
System.out.println(faultWarning);
writeOutput(faultWarning);
// transfer over current values
backUp.request = faulty.request;
backUp.waiting = faulty.waiting;
backUp.currentTime = faulty.currentTime ;
// transfer people in elevator to current floor of backup elevator waiting queue
for ( Map.Entry<Integer,ConcurrentLinkedQueue<Person>> entry : inElevator.entrySet()) {
for (Person person : entry.getValue()){
addPersonFirst(person, currentFloor,backUp.waiting);
String backupWarning = String.format("Person %d is exiting on floor %d and waiting for backup elevator\n", person.getId(), this.currentFloor);
writeOutput(backupWarning);
System.out.print(backupWarning);
}
}
}
public synchronized int getTime(){
return this.currentTime;
}
public synchronized void writeOutput(String outputString) {
try{
// write to output.dat
File file =new File("output.dat");
if(!file.exists()){
file.createNewFile();
}
FileWriter fileWritter = new FileWriter(file.getName(),true);
BufferedWriter bw = new BufferedWriter(fileWritter);
bw.write(outputString);
bw.close();
}
catch(IOException e){
System.out.println("Exception occurred:");
e.printStackTrace();
}
}
}