-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathElevatorControllerGUICopy.txt
More file actions
717 lines (506 loc) · 19.6 KB
/
Copy pathElevatorControllerGUICopy.txt
File metadata and controls
717 lines (506 loc) · 19.6 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
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
import java.awt.EventQueue;
import javax.swing.JFrame;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.*;
import javax.swing.JLabel;
import javax.swing.OverlayLayout;
import java.awt.GridBagConstraints;
import java.awt.Insets;
import java.util.HashMap;
import java.util.Map;
import javax.swing.Box;
import java.awt.BorderLayout;
import java.awt.Component;
import java.awt.Dimension;
import javax.swing.BoxLayout;
import javax.swing.ImageIcon;
// this clas will allow sychronized access to the elevator thread
// will control the actions of the elevator
import java.util.concurrent.*;
import java.util.concurrent.atomic.*;
import java.util.*;
public class ElevatorControllerGUI{
// GUI variables
public JLabel[] elevatorFloors;
public Thread pause;
public Map<Dimension, JLabel> people;
public JFrame frame;
public JLabel elevatorFloor1;
public JLabel elevatorFloor2;
public JLabel elevatorFloor3;
public JLabel elevatorFloor4;
public JLabel elevatorFloor5;
public JLabel elevatorFloor6;
public JLabel elevatorFloor7;
public JLabel elevatorFloor8;
public JLabel elevatorFloor9;
public JLabel elevatorFloor10;
// elevator controller variables
public boolean goingUp;
public boolean goingDown;
private boolean doorsOpen;
public int currentFloor;
public int currentTime;
public int numPeople;
public int currentWeight;
public int maxWeight;
private static final AtomicInteger elevatorNumberGenerator = new AtomicInteger(1);
public int elevatorNumber;
public 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 ElevatorControllerGUI(){
// 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 = 0;
// time will go up in every time the elevator goes up a floor
this.currentTime = 0;
this.currentWeight = 0 ;
this.maxWeight = 200;
// 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){
change();
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()) ){
// print what floor the elevator is on
System.out.printf("Elevator %d is on floor %s************\n",this.elevatorNumber,this.currentFloor);
// 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--;
}
}
if (waiting.isEmpty() && inElevator.isEmpty()){
System.out.printf("The elevator is sleeping on floor %d",currentFloor);
}
}
// 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)){
System.out.printf("Stopping on floor %d for people\n", currentFloor);
System.out.printf("****************\nAllowing people in on floor %s...\n",currentFloor);
headingPrinted = true;
}
// check if is 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);
System.out.printf("**********************************************************\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);
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 persons details
System.out.println(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)){
System.out.printf("****************\nLetting people out on floor %s...\n",currentFloor);
headingPrinted = true;
}
// print out who's leaving
System.out.println(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){
// transfer over current values
backUp.request = faulty.request;
backUp.waiting = faulty.waiting;
backUp.currentTime = faulty.currentTime + 1;
// 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);
System.out.printf("Person %s is exiting on floor %d and waiting for backup elevator\n", person.getId(), currentFloor);
}
}
System.out.println(backUp.waiting);
}
public synchronized int getTime(){
return this.currentTime;
}
public static void main(String[] args) {
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
gridLayout window = new gridLayout();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
//@SuppressWarnings("static-access")
public void initialize() {
frame = new JFrame("Elevator");
frame.setBounds(100, 100, 600, 1000);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
GridBagLayout gridBagLayout = new GridBagLayout();
gridBagLayout.columnWidths = new int[]{0, 1, 1,1,1,1,1,1,1,1,1,1,1,1,1};
gridBagLayout.rowHeights = new int[]{0, 0, 0, 0, 0, 0, 0, 0, 0, 0,};
gridBagLayout.columnWeights = new double[] {0.0, 0.2,0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1};
gridBagLayout.rowWeights = new double[]{0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, 0.1, Double.MIN_VALUE};
frame.getContentPane().setLayout(gridBagLayout);
// display floor numbers
int floor = 10;
for(int i=1;i<=10;i++) {
JLabel tmpLabel = new JLabel("" + floor);
GridBagConstraints tmp = new GridBagConstraints();
tmp.insets = new Insets(0, 0, 5, 5);
tmp.gridx = 0;
tmp.gridy = i;
frame.getContentPane().add(tmpLabel, tmp);
floor--;
}
elevatorFloor10 = new JLabel(new ImageIcon("elevator.jpg"));
GridBagConstraints gbc_elevatorFloor10 = new GridBagConstraints();
gbc_elevatorFloor10.insets = new Insets(0, 0, 5, 5);
gbc_elevatorFloor10.gridx = 1;
gbc_elevatorFloor10.gridy = 0;
Image img = new ImageIcon(this.getClass().getResource("elevator.jpg")).getImage();
frame.getContentPane().add(elevatorFloor10, gbc_elevatorFloor10);
elevatorFloor9 = new JLabel("");
GridBagConstraints gbc_elevatorFloor9 = new GridBagConstraints();
gbc_elevatorFloor9.insets = new Insets(0, 0, 5, 5);
gbc_elevatorFloor9.gridx = 1;
gbc_elevatorFloor9.gridy = 1;
elevatorFloor9.setIcon(new ImageIcon(img));
frame.getContentPane().add(elevatorFloor9, gbc_elevatorFloor9);
elevatorFloor8 = new JLabel("");
elevatorFloor8.setLayout(new BorderLayout());
elevatorFloor8.setIcon(new ImageIcon(img));
Image person = new ImageIcon(this.getClass().getResource("greenPersonSmall.png")).getImage();
GridBagConstraints gbc_elevatorFloor8 = new GridBagConstraints();
gbc_elevatorFloor8.insets = new Insets(0, 0, 5, 5);
gbc_elevatorFloor8.gridx = 1;
gbc_elevatorFloor8.gridy = 2;
JLabel over = new JLabel("");
over.setLocation(200,300);
over.setIcon(new ImageIcon(person));
over.setVisible(true);
over.setBounds(50,50,100,100);
elevatorFloor8.add(over,BorderLayout.SOUTH);
//elevatorFloor8.add(over,2,0);
frame.getContentPane().add(elevatorFloor8, gbc_elevatorFloor8);
elevatorFloor7 = new JLabel("");
GridBagConstraints gbc_elevatorFloor7 = new GridBagConstraints();
gbc_elevatorFloor7.insets = new Insets(0, 0, 5, 5);
gbc_elevatorFloor7.gridx = 1;
gbc_elevatorFloor7.gridy = 3;
elevatorFloor7.setIcon(new ImageIcon(img));
frame.getContentPane().add(elevatorFloor7, gbc_elevatorFloor7);
elevatorFloor6 = new JLabel("");
GridBagConstraints gbc_elevatorFloor6 = new GridBagConstraints();
gbc_elevatorFloor6.insets = new Insets(0, 0, 5, 5);
gbc_elevatorFloor6.gridx = 1;
gbc_elevatorFloor6.gridy = 4;
elevatorFloor6.setIcon(new ImageIcon(img));
frame.getContentPane().add(elevatorFloor6, gbc_elevatorFloor6);
elevatorFloor5 = new JLabel("");
GridBagConstraints gbc_elevatorFloor5 = new GridBagConstraints();
gbc_elevatorFloor5.insets = new Insets(0, 0, 5, 5);
gbc_elevatorFloor5.gridx = 1;
gbc_elevatorFloor5.gridy = 5;
elevatorFloor5.setIcon(new ImageIcon(img));
frame.getContentPane().add(elevatorFloor5, gbc_elevatorFloor5);
elevatorFloor4 = new JLabel("");
GridBagConstraints gbc_elevatorFloor4 = new GridBagConstraints();
gbc_elevatorFloor4.insets = new Insets(0, 0, 5, 5);
gbc_elevatorFloor4.gridx = 1;
gbc_elevatorFloor4.gridy = 6;
elevatorFloor4.setIcon(new ImageIcon(img));
//lblNewLabel_6.setVisible(false);
frame.getContentPane().add(elevatorFloor4, gbc_elevatorFloor4);
elevatorFloor3 = new JLabel("");
GridBagConstraints gbc_elevatorFloor3 = new GridBagConstraints();
gbc_elevatorFloor3.insets = new Insets(0, 0, 5, 5);
gbc_elevatorFloor3.gridx = 1;
gbc_elevatorFloor3.gridy = 7;
elevatorFloor3.setIcon(new ImageIcon(img));
//lblNewLabel_7.setVisible(false);
frame.getContentPane().add(elevatorFloor3, gbc_elevatorFloor3);
elevatorFloor2 = new JLabel("");
GridBagConstraints gbc_elevatorFloor2 = new GridBagConstraints();
gbc_elevatorFloor2.insets = new Insets(0, 0, 5, 5);
gbc_elevatorFloor2.gridx = 1;
gbc_elevatorFloor2.gridy = 8;
elevatorFloor2.setIcon(new ImageIcon(img));
frame.getContentPane().add(elevatorFloor2, gbc_elevatorFloor2);
elevatorFloor1 = new JLabel("");
GridBagConstraints gbc_elevatorFloor1 = new GridBagConstraints();
gbc_elevatorFloor1.insets = new Insets(0, 0, 5, 5);
gbc_elevatorFloor1.gridx = 1;
gbc_elevatorFloor1.gridy = 9;
elevatorFloor1.setIcon(new ImageIcon(img));
frame.getContentPane().add(elevatorFloor1, gbc_elevatorFloor1);
elevatorFloors = new JLabel[] {elevatorFloor1,elevatorFloor2,elevatorFloor3,elevatorFloor4,elevatorFloor5,elevatorFloor6,elevatorFloor7,elevatorFloor8,elevatorFloor9,elevatorFloor10};
people = new HashMap<Dimension, JLabel>();
for (int x = 0;x<10;x++) {
for (int y =1;y<10;y++) {
// Image person = new ImageIcon(this.getClass().getResource("/greenPersonSmall.png")).getImage();
JLabel tmpLabel = new JLabel("");
GridBagConstraints tmp = new GridBagConstraints();
tmp.insets = new Insets(0, 0, 5, 5);
tmp.gridx = x;
tmp.gridy = y;
tmpLabel.setIcon(new ImageIcon(person));
people.put(new Dimension(x, y),tmpLabel);
tmpLabel.setVisible(false);
frame.getContentPane().add(tmpLabel, tmp);
}
}
for (JLabel l : elevatorFloors) {
l.setVisible(false);
}
elevatorFloor2.setVisible(true);
//changeFloor(6);
//addPerson(7);
//addPerson(7);
//addPerson(7);
}
public void display(){
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
gridLayout window = new gridLayout();
window.frame.setVisible(true);
} catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void change(){
EventQueue.invokeLater(new Runnable() {
public void run() {
try {
int floorNumber = 1;
int currentFloor = 0;
for(int i = 0;i<=9;i++) {
if(elevatorFloors[i].isVisible()) {
currentFloor = i;
break;
}
}
//System.out.println(currentFloor);
if(currentFloor > floorNumber) {
for(int j=currentFloor;j>=floorNumber-1;j--) {
elevatorFloors[j].setVisible(true);
Thread.sleep(1000);
elevatorFloors[j].setVisible(false);
elevatorFloors[j-1].setVisible(true);
}
}
else {
// 6 > 3 6
for(int x=currentFloor;x<floorNumber;x++) {
elevatorFloors[x].setVisible(true);
Thread.sleep(1000);
elevatorFloors[x].setVisible(false);
elevatorFloors[x+1].setVisible(true);
}
}
}
catch (Exception e) {
e.printStackTrace();
}
}
});
}
public void changeFloorGUI(int floorNumber) {
//9 9 8 7 6
new Thread(new Runnable() {
public void run() {
try {
int currentFloor = 0;
for(int i = 0;i<=9;i++) {
if(elevatorFloors[i].isVisible()) {
currentFloor = i;
break;
}
}
//System.out.println(currentFloor);
if(currentFloor > floorNumber) {
for(int j=currentFloor;j>=floorNumber-1;j--) {
elevatorFloors[j].setVisible(true);
Thread.sleep(1000);
elevatorFloors[j].setVisible(false);
elevatorFloors[j-1].setVisible(true);
}
}
else {
// 6 > 3 6
for(int x=currentFloor;x<floorNumber;x++) {
elevatorFloors[x].setVisible(true);
Thread.sleep(1000);
elevatorFloors[x].setVisible(false);
elevatorFloors[x+1].setVisible(true);
}
}
}
catch(InterruptedException e) {
}
}
}).start();
}
public void addPersonGUI(long id, int floor) {
int x = floor;
int y = 2;
for(int i=y;i<=9;i++) {
JLabel tmpLabel = people.get(new Dimension(i,x));
if(tmpLabel.isVisible() == false) {
y = i;
break;
}
}
JLabel tmpLabel = people.get(new Dimension(y,x));
tmpLabel.setText(String.format("%d",id));
tmpLabel.setVisible(true);
}
}
//// ********** GUI