11package org .togetherjava .event .elevator .elevators ;
22
3- import java .util .StringJoiner ;
3+ import java .util .* ;
44import java .util .concurrent .atomic .AtomicInteger ;
55
66/**
@@ -16,6 +16,9 @@ public final class Elevator implements ElevatorPanel {
1616 private final int minFloor ;
1717 private final int floorsServed ;
1818 private int currentFloor ;
19+ public List <Integer > humansInside = new ArrayList <>();
20+ public List <Integer > humansWaiting = new ArrayList <>();
21+ private TravelDirection travelDirection ;
1922
2023 /**
2124 * Creates a new elevator.
@@ -39,6 +42,18 @@ public Elevator(int minFloor, int floorsServed, int currentFloor) {
3942 this .floorsServed = floorsServed ;
4043 }
4144
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+
4257 @ Override
4358 public int getId () {
4459 return id ;
@@ -63,7 +78,17 @@ public void requestDestinationFloor(int destinationFloor) {
6378 // itself requesting this elevator to eventually move to the given floor.
6479 // The elevator is supposed to memorize the destination in a way that
6580 // 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 ;
6792 }
6893
6994 public void moveOneFloor () {
@@ -76,7 +101,46 @@ public void moveOneFloor() {
76101 // meaning that the average time waiting (either in corridor or inside the elevator)
77102 // is minimized across all humans.
78103 // 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 ));
80144 }
81145
82146 @ Override
0 commit comments