1
1
package org .togetherjava .event .elevator .elevators ;
2
2
3
- import java .util .StringJoiner ;
3
+ import java .util .* ;
4
4
import java .util .concurrent .atomic .AtomicInteger ;
5
5
6
6
/**
@@ -16,6 +16,9 @@ public final class Elevator implements ElevatorPanel {
16
16
private final int minFloor ;
17
17
private final int floorsServed ;
18
18
private int currentFloor ;
19
+ public List <Integer > humansInside = new ArrayList <>();
20
+ public List <Integer > humansWaiting = new ArrayList <>();
21
+ private TravelDirection travelDirection ;
19
22
20
23
/**
21
24
* Creates a new elevator.
@@ -39,6 +42,18 @@ public Elevator(int minFloor, int floorsServed, int currentFloor) {
39
42
this .floorsServed = floorsServed ;
40
43
}
41
44
45
+ public Optional <TravelDirection > getTravelDirection () {
46
+ return travelDirection != null ? Optional .of (travelDirection ) : Optional .empty ();
47
+ }
48
+
49
+ /**
50
+ * @param direction can be null if it stays still
51
+ */
52
+ public void setTravelDirection (TravelDirection direction ) {
53
+ this .travelDirection = direction ;
54
+ }
55
+
56
+
42
57
@ Override
43
58
public int getId () {
44
59
return id ;
@@ -63,7 +78,17 @@ public void requestDestinationFloor(int destinationFloor) {
63
78
// itself requesting this elevator to eventually move to the given floor.
64
79
// The elevator is supposed to memorize the destination in a way that
65
80
// it can ensure to eventually reach it.
66
- System .out .println ("Request for destination floor received" );
81
+ //System.out.println("Request for destination floor received");
82
+ }
83
+
84
+ @ Override
85
+ public List <Integer > getWaitingHumans () {
86
+ return humansWaiting ;
87
+ }
88
+
89
+ @ Override
90
+ public List <Integer > getHumansInside () {
91
+ return humansInside ;
67
92
}
68
93
69
94
public void moveOneFloor () {
@@ -76,7 +101,46 @@ public void moveOneFloor() {
76
101
// meaning that the average time waiting (either in corridor or inside the elevator)
77
102
// is minimized across all humans.
78
103
// It is essential that this method updates the currentFloor field accordingly.
79
- System .out .println ("Request to move a floor received" );
104
+ //System.out.println("Request to move a floor received");
105
+ if (humansWaiting .isEmpty () && !humansInside .isEmpty ()) {
106
+ int floor = humansInside
107
+ .stream ()
108
+ .min (getComparator ())
109
+ .orElseThrow (() -> new IllegalStateException ("State shouldn't be reachable" ));
110
+
111
+ TravelDirection travelDirection = TravelDirection .getTravelDirection (currentFloor , floor );
112
+
113
+ if (travelDirection == null ) {
114
+ return ;
115
+ }
116
+
117
+ switch (travelDirection ) {
118
+ case UP -> currentFloor ++;
119
+ case DOWN -> currentFloor --;
120
+ }
121
+
122
+ } else if (!humansWaiting .isEmpty ()) {
123
+
124
+ int floor = humansWaiting
125
+ .stream ()
126
+ .min (getComparator ())
127
+ .orElseThrow (() -> new IllegalStateException ("State Shouldn't be possible" ));
128
+
129
+ TravelDirection travelDirection = TravelDirection .getTravelDirection (currentFloor , floor );
130
+
131
+ if (travelDirection == null ) {
132
+ return ;
133
+ }
134
+
135
+ switch (travelDirection ) {
136
+ case UP -> currentFloor ++;
137
+ case DOWN -> currentFloor --;
138
+ }
139
+ }
140
+ }
141
+
142
+ private Comparator <Integer > getComparator () {
143
+ return Comparator .comparingInt ((floorNumber ) -> Math .abs (floorNumber - currentFloor ));
80
144
}
81
145
82
146
@ Override
0 commit comments