Skip to content

Commit bf989af

Browse files
committed
Submission
1 parent 3021c60 commit bf989af

File tree

9 files changed

+143
-26
lines changed

9 files changed

+143
-26
lines changed

Diff for: Contest/Assignment/src/org/togetherjava/event/elevator/Main.java

+7-9
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,19 @@ public static void main(final String[] args) {
1919
// Eventually try out the randomly generated systems. If you want to debug a problem you encountered
2020
// with one of them, note down the seed that it prints at the beginning and then use the variant that takes this seed.
2121
// That way, it will generate the same system again, and you can repeat the test.
22-
Simulation simulation = Simulation.createSingleElevatorSingleHumanSimulation();
23-
// Simulation simulation = Simulation.createSimpleSimulation();
24-
// Simulation simulation = Simulation.createRandomSimulation(5, 50, 10);
25-
// Simulation simulation = Simulation.createRandomSimulation(putDesiredSeedHere, 5, 50, 10);
26-
27-
simulation.printSummary();
22+
//Simulation simulation = Simulation.createSingleElevatorSingleHumanSimulation();
23+
//Simulation simulation = Simulation.createSimpleSimulation();
24+
//Simulation simulation = Simulation.createRandomSimulation(6, 1, 3, 10);
25+
Simulation simulation = Simulation.createRandomSimulation(4, 12, 1000, 23);
2826

2927
System.out.println("Starting simulation...");
3028
simulation.start();
31-
simulation.prettyPrint();
29+
//simulation.prettyPrint();
3230

3331
while (!simulation.isDone()) {
34-
System.out.println("\tSimulation step " + simulation.getStepCount());
32+
//System.out.println("\tSimulation step " + simulation.getStepCount());
3533
simulation.step();
36-
simulation.prettyPrint();
34+
//simulation.prettyPrint();
3735

3836
if (simulation.getStepCount() >= 100_000) {
3937
throw new IllegalStateException("Simulation aborted. All humans should have arrived"

Diff for: Contest/Assignment/src/org/togetherjava/event/elevator/elevators/Elevator.java

+67-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
package org.togetherjava.event.elevator.elevators;
22

3-
import java.util.StringJoiner;
3+
import java.util.*;
44
import 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

Diff for: Contest/Assignment/src/org/togetherjava/event/elevator/elevators/ElevatorPanel.java

+6
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
package org.togetherjava.event.elevator.elevators;
22

3+
import java.util.List;
4+
35
/**
46
* The system inside an elevator which provides information about the elevator and can be
57
* used to request a destination floor.
@@ -25,4 +27,8 @@ public interface ElevatorPanel {
2527
* @param destinationFloor the desired destination, must be within the range served by this elevator
2628
*/
2729
void requestDestinationFloor(int destinationFloor);
30+
31+
List<Integer> getWaitingHumans();
32+
33+
List<Integer> getHumansInside();
2834
}

Diff for: Contest/Assignment/src/org/togetherjava/event/elevator/elevators/ElevatorSystem.java

+25-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,9 @@
33
import org.togetherjava.event.elevator.humans.ElevatorListener;
44

55
import java.util.ArrayList;
6+
import java.util.Comparator;
67
import java.util.List;
8+
import java.util.function.Predicate;
79

810
/**
911
* System controlling all elevators of a building.
@@ -39,11 +41,32 @@ public void requestElevator(int atFloor, TravelDirection desiredTravelDirection)
3941
// The human can then enter the elevator and request their actual destination within the elevator.
4042
// Ideally this has to select the best elevator among all which can reduce the time
4143
// for the human spending waiting (either in corridor or in the elevator itself).
42-
System.out.println("Request for elevator received");
44+
//System.out.println("Request for elevator received");
45+
Elevator elevatorToUse = elevators
46+
.stream()
47+
.sorted(getComparator(atFloor))
48+
.filter(getElevatorFilter(desiredTravelDirection))
49+
.findFirst()
50+
.orElse(elevators
51+
.stream()
52+
.min(getComparator(atFloor))
53+
.orElseThrow(() -> new IllegalStateException("There are no Elevators")));
54+
55+
elevatorToUse.setTravelDirection(desiredTravelDirection);
56+
57+
elevatorToUse.humansWaiting.add(atFloor);
58+
}
59+
60+
private Predicate<Elevator> getElevatorFilter(TravelDirection desiredTravelDirection) {
61+
return (elevator1) -> elevator1.getTravelDirection().isEmpty() || elevator1.getTravelDirection().get() == desiredTravelDirection;
62+
}
63+
64+
private Comparator<Elevator> getComparator(int atFloor) {
65+
return Comparator.comparingInt((ele) -> Math.abs(atFloor - ele.getCurrentFloor()));
4366
}
4467

4568
public void moveOneFloor() {
4669
elevators.forEach(Elevator::moveOneFloor);
47-
elevators.forEach(elevator -> elevatorListeners.forEach(listener -> listener.onElevatorArrivedAtFloor(elevator)));
70+
elevatorListeners.forEach((human) -> elevators.forEach(human::onElevatorArrivedAtFloor));
4871
}
4972
}

Diff for: Contest/Assignment/src/org/togetherjava/event/elevator/elevators/FloorPanelSystem.java

-2
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ public interface FloorPanelSystem {
1010
* @param atFloor the floor to pick up the human at, must be within the range served by the system
1111
* @param desiredTravelDirection the direction the human wants to travel into,
1212
* can be used for determination of the best elevator
13-
* @apiNote This represents a human standing in the corridor, pressing a button on the wall,
14-
* requesting that an elevator comes to pick them up for travel into the given direction.
1513
*/
1614
void requestElevator(int atFloor, TravelDirection desiredTravelDirection);
1715
}

Diff for: Contest/Assignment/src/org/togetherjava/event/elevator/elevators/TravelDirection.java

+11-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,15 @@
22

33
public enum TravelDirection {
44
UP,
5-
DOWN,
5+
DOWN;
6+
7+
public static TravelDirection getTravelDirection(int startingFloor, int destinationFloor) {
8+
if (startingFloor > destinationFloor) {
9+
return DOWN;
10+
} else if (startingFloor < destinationFloor) {
11+
return UP;
12+
} else {
13+
return null;
14+
}
15+
}
616
}

Diff for: Contest/Assignment/src/org/togetherjava/event/elevator/humans/ElevatorListener.java

-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,6 @@ public interface ElevatorListener {
2121
*
2222
* @param elevatorPanel the system inside the elevator which provides information
2323
* about the elevator and can be used to request a destination floor.
24-
* @implNote The default implementation fires this event from all elevators to all humans, not only to humans that are
25-
* relevant (i.e. humans that can enter the elevator).
2624
*/
2725
void onElevatorArrivedAtFloor(ElevatorPanel elevatorPanel);
2826
}

Diff for: Contest/Assignment/src/org/togetherjava/event/elevator/humans/Human.java

+23-3
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import org.togetherjava.event.elevator.elevators.ElevatorPanel;
44
import org.togetherjava.event.elevator.elevators.FloorPanelSystem;
5+
import org.togetherjava.event.elevator.elevators.TravelDirection;
56

67
import java.util.OptionalInt;
78
import java.util.StringJoiner;
@@ -14,7 +15,7 @@
1415
* for example requesting an elevator, eventually entering and exiting them.
1516
*/
1617
public final class Human implements ElevatorListener {
17-
private State currentState;
18+
public State currentState;
1819
private final int startingFloor;
1920
private final int destinationFloor;
2021
/**
@@ -60,7 +61,9 @@ public void onElevatorSystemReady(FloorPanelSystem floorPanelSystem) {
6061
// TODO Implement. The system is now ready and the human should leave
6162
// their initial IDLE state, requesting an elevator by clicking on the buttons of
6263
// the floor panel system. The human will now enter the WAITING_FOR_ELEVATOR state.
63-
System.out.println("Ready-event received");
64+
this.currentState = State.WAITING_FOR_ELEVATOR;
65+
floorPanelSystem.requestElevator(this.startingFloor, TravelDirection.getTravelDirection(startingFloor, destinationFloor));
66+
//System.out.println("Ready-event received");
6467
}
6568

6669
@Override
@@ -70,7 +73,24 @@ public void onElevatorArrivedAtFloor(ElevatorPanel elevatorPanel) {
7073
// elevator and request their actual destination floor. The state has to change to TRAVELING_WITH_ELEVATOR.
7174
// If the human is currently traveling with this elevator and the event represents
7275
// arrival at the human's destination floor, the human can now exit the elevator.
73-
System.out.println("Arrived-event received");
76+
if (this.currentState == State.WAITING_FOR_ELEVATOR && elevatorPanel.getCurrentFloor() == this.startingFloor && elevatorPanel.getWaitingHumans().contains(this.startingFloor)) {
77+
this.currentState = State.TRAVELING_WITH_ELEVATOR;
78+
79+
elevatorPanel.getWaitingHumans().remove(Integer.valueOf(this.startingFloor));
80+
elevatorPanel.getHumansInside().add(this.destinationFloor);
81+
82+
this.currentEnteredElevatorId = elevatorPanel.getId();
83+
84+
//this line is completely useless but.... it wants me to use it so i guess im using it.
85+
elevatorPanel.requestDestinationFloor(this.destinationFloor);
86+
87+
} else if (this.currentState == State.TRAVELING_WITH_ELEVATOR && this.currentEnteredElevatorId != null && this.currentEnteredElevatorId == elevatorPanel.getId() && elevatorPanel.getCurrentFloor() == this.destinationFloor) {
88+
this.currentState = State.ARRIVED;
89+
this.currentEnteredElevatorId = null;
90+
91+
elevatorPanel.getHumansInside().remove(Integer.valueOf(this.destinationFloor));
92+
}
93+
//System.out.println("Arrived-event received");
7494
}
7595

7696
public OptionalInt getCurrentEnteredElevatorId() {

Diff for: Contest/Assignment/test/SanityTest.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -110,15 +110,15 @@ private static void verifySnapshotSanity(SimulationSnapshot previousSnapshot,
110110

111111
// Only if the human actually travelled around
112112
if (human.getStartingFloor() != human.getDestinationFloor()) {
113+
assertEquals(Human.State.TRAVELING_WITH_ELEVATOR, previousState,
114+
"When a human exits an elevator, their previous state must be traveling. But '%s' was in state %s.".formatted(
115+
human, previousState));
116+
113117
OptionalInt maybeElevatorId = previousHumanSnapshot.currentElevatorId();
114118
assertTrue(maybeElevatorId.isPresent(), ERROR_MESSAGE_PRE
115119
+ "When a human exits an elevator, they must have had a current elevator id previously. But '%s' does not.".formatted(
116120
human));
117121

118-
assertEquals(Human.State.TRAVELING_WITH_ELEVATOR, previousState,
119-
"When a human exits an elevator, their previous state must be traveling. But '%s' was in state %s.".formatted(
120-
human, previousState));
121-
122122
ElevatorSnapshot currentElevatorSnapshot =
123123
currentSnapshot.getElevatorSnapshot(maybeElevatorId.orElseThrow());
124124
assertEquals(human.getDestinationFloor(),

0 commit comments

Comments
 (0)