Skip to content

Commit 9ebd93a

Browse files
committed
Initial commit
0 parents  commit 9ebd93a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

67 files changed

+4895
-0
lines changed

.coursecreator/course/course.json

Lines changed: 700 additions & 0 deletions
Large diffs are not rendered by default.

.gitignore

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,162 @@
1+
# Created by https://www.toptal.com/developers/gitignore/api/netbeans,intellij,java,gradle,eclipse
2+
# Edit at https://www.toptal.com/developers/gitignore?templates=netbeans,intellij,java,gradle,eclipse
3+
4+
### Eclipse ###
5+
.metadata
6+
bin/
7+
tmp/
8+
*.tmp
9+
*.bak
10+
*.swp
11+
*~.nib
12+
local.properties
13+
.settings/
14+
.loadpath
15+
.recommenders
16+
17+
# External tool builders
18+
.externalToolBuilders/
19+
20+
# Locally stored "Eclipse launch configurations"
21+
*.launch
22+
23+
# PyDev specific (Python IDE for Eclipse)
24+
*.pydevproject
25+
26+
# Java annotation processor (APT)
27+
.factorypath
28+
29+
# TeXlipse plugin
30+
.texlipse
31+
32+
# STS (Spring Tool Suite)
33+
.springBeans
34+
35+
# Code Recommenders
36+
.recommenders/
37+
38+
# Annotation Processing
39+
.apt_generated/
40+
.apt_generated_test/
41+
42+
# Uncomment this line if you wish to ignore the project description file.
43+
# Typically, this file would be tracked if it contains build/dependency configurations:
44+
#.project
45+
46+
### Eclipse Patch ###
47+
# Spring Boot Tooling
48+
.sts4-cache/
49+
50+
### Intellij ###
51+
# Covers JetBrains IDEs: IntelliJ, RubyMine, PhpStorm, AppCode, PyCharm, CLion, Android Studio, WebStorm and Rider
52+
# Reference: https://intellij-support.jetbrains.com/hc/en-us/articles/206544839
53+
54+
# User-specific stuff (and .idea/, as we do not want to rely on that)
55+
.idea/
56+
*.iml
57+
*.ipr
58+
59+
# Mongo Explorer plugin
60+
.idea/**/mongoSettings.xml
61+
62+
# File-based project format
63+
*.iws
64+
65+
# IntelliJ
66+
out/
67+
68+
# JIRA plugin
69+
atlassian-ide-plugin.xml
70+
71+
# Crashlytics plugin (for Android Studio and IntelliJ)
72+
com_crashlytics_export_strings.xml
73+
crashlytics.properties
74+
crashlytics-build.properties
75+
fabric.properties
76+
77+
### Intellij Patch ###
78+
# Comment Reason: https://github.com/joeblau/gitignore.io/issues/186#issuecomment-215987721
79+
80+
# *.iml
81+
# modules.xml
82+
# .idea/misc.xml
83+
# *.ipr
84+
85+
86+
### Java ###
87+
# Compiled class file
88+
*.class
89+
90+
# Log file
91+
*.log
92+
93+
# BlueJ files
94+
*.ctxt
95+
96+
# Mobile Tools for Java (J2ME)
97+
.mtj.tmp/
98+
99+
# Package Files #
100+
*.jar
101+
*.war
102+
*.nar
103+
*.ear
104+
*.zip
105+
*.tar.gz
106+
*.rar
107+
108+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
109+
hs_err_pid*
110+
111+
### NetBeans ###
112+
**/nbproject/private/
113+
**/nbproject/Makefile-*.mk
114+
**/nbproject/Package-*.bash
115+
build/
116+
nbbuild/
117+
dist/
118+
nbdist/
119+
.nb-gradle/
120+
121+
### Gradle ###
122+
.gradle
123+
124+
# Ignore Gradle GUI config
125+
gradle-app.setting
126+
127+
# Avoid ignoring Gradle wrapper jar file (.jar files are usually ignored)
128+
!gradle-wrapper.jar
129+
130+
# Cache of project
131+
.gradletasknamecache
132+
133+
# # Work around https://youtrack.jetbrains.com/issue/IDEA-116898
134+
# gradle/wrapper/gradle-wrapper.properties
135+
136+
### Gradle Patch ###
137+
**/build/
138+
139+
# Eclipse Gradle plugin generated files
140+
# Eclipse Core
141+
.project
142+
# JDT-specific (Eclipse Java Development Tools)
143+
.classpath
144+
145+
# End of https://www.toptal.com/developers/gitignore/api/netbeans,intellij,java,gradle,eclipse
146+
application/db/
147+
config.json
148+
application/config.json
149+
*.db
150+
*.db-shm
151+
*.db-wal
152+
logviewer/frontend/generated
153+
logviewer/node_modules
154+
logviewer/.npmrc
155+
logviewer/package.json
156+
logviewer/pnpmfile.js
157+
logviewer/tsconfig.json
158+
logviewer/types.d.ts
159+
logviewer/pnpm-lock.yaml
160+
logviewer/webpack.config.js
161+
logviewer/webpack.generated.js
162+
.DS_Store
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package org.togetherjava.event.elevator;
2+
3+
import org.togetherjava.event.elevator.elevators.Elevator;
4+
import org.togetherjava.event.elevator.humans.Human;
5+
import org.togetherjava.event.elevator.simulation.Simulation;
6+
7+
public final class Main {
8+
/**
9+
* Starts the application.
10+
* <p>
11+
* Will create an elevator-system simulation, execute it until it is done
12+
* and pretty print the state to console.
13+
*
14+
* @param args Not supported
15+
*/
16+
public static void main(final String[] args) {
17+
// Select a desired simulation for trying out your code.
18+
// Start with the simple simulations first, try out the bigger systems once you got it working.
19+
// Eventually try out the randomly generated systems. If you want to debug a problem you encountered
20+
// with one of them, note down the seed that it prints at the beginning and then use the variant that takes this seed.
21+
// 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();
28+
29+
System.out.println("Starting simulation...");
30+
simulation.start();
31+
simulation.prettyPrint();
32+
33+
while (!simulation.isDone()) {
34+
System.out.println("\tSimulation step " + simulation.getStepCount());
35+
simulation.step();
36+
simulation.prettyPrint();
37+
38+
if (simulation.getStepCount() >= 100_000) {
39+
throw new IllegalStateException("Simulation aborted. All humans should have arrived"
40+
+ " by now, but they did not. There is likely a bug in your code.");
41+
}
42+
}
43+
System.out.println("Simulation is done.");
44+
45+
simulation.printResult();
46+
}
47+
}
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
package org.togetherjava.event.elevator.elevators;
2+
3+
import java.util.StringJoiner;
4+
import java.util.concurrent.atomic.AtomicInteger;
5+
6+
/**
7+
* A single elevator that can serve a given amount of floors.
8+
* <p>
9+
* An elevator can take floor requests from either humans or the elevator system itself.
10+
* The elevator will eventually move towards the requested floor and transport humans to their destinations.
11+
*/
12+
public final class Elevator implements ElevatorPanel {
13+
private static final AtomicInteger NEXT_ID = new AtomicInteger(0);
14+
15+
private final int id;
16+
private final int minFloor;
17+
private final int floorsServed;
18+
private int currentFloor;
19+
20+
/**
21+
* Creates a new elevator.
22+
*
23+
* @param minFloor the minimum floor that the elevator can serve, must be greater equals 1
24+
* @param floorsServed the amount of floors served in total by this elevator, must be greater equals 2.
25+
* Together with the minFloor this forms a consecutive range of floors with no gaps in between.
26+
* @param currentFloor the floor the elevator starts at, must be within the defined range of floors served by the elevator
27+
*/
28+
public Elevator(int minFloor, int floorsServed, int currentFloor) {
29+
if (minFloor <= 0 || floorsServed < 2) {
30+
throw new IllegalArgumentException("Min floor must at least 1, floors served at least 2.");
31+
}
32+
if (currentFloor < minFloor || currentFloor >= minFloor + floorsServed) {
33+
throw new IllegalArgumentException("The current floor must be between the floors served by the elevator.");
34+
}
35+
36+
this.id = NEXT_ID.getAndIncrement();
37+
this.minFloor = minFloor;
38+
this.currentFloor = currentFloor;
39+
this.floorsServed = floorsServed;
40+
}
41+
42+
@Override
43+
public int getId() {
44+
return id;
45+
}
46+
47+
public int getMinFloor() {
48+
return minFloor;
49+
}
50+
51+
public int getFloorsServed() {
52+
return floorsServed;
53+
}
54+
55+
@Override
56+
public int getCurrentFloor() {
57+
return currentFloor;
58+
}
59+
60+
@Override
61+
public void requestDestinationFloor(int destinationFloor) {
62+
// TODO Implement. This represents a human or the elevator system
63+
// itself requesting this elevator to eventually move to the given floor.
64+
// The elevator is supposed to memorize the destination in a way that
65+
// it can ensure to eventually reach it.
66+
System.out.println("Request for destination floor received");
67+
}
68+
69+
public void moveOneFloor() {
70+
// TODO Implement. Essentially there are three possibilities:
71+
// - move up one floor
72+
// - move down one floor
73+
// - stand still
74+
// The elevator is supposed to move in a way that it will eventually reach
75+
// the floors requested by Humans via requestDestinationFloor(), ideally "fast" but also "fair",
76+
// meaning that the average time waiting (either in corridor or inside the elevator)
77+
// is minimized across all humans.
78+
// It is essential that this method updates the currentFloor field accordingly.
79+
System.out.println("Request to move a floor received");
80+
}
81+
82+
@Override
83+
public synchronized String toString() {
84+
return new StringJoiner(", ", Elevator.class.getSimpleName() + "[", "]").add("id=" + id)
85+
.add("minFloor=" + minFloor)
86+
.add("floorsServed=" + floorsServed)
87+
.add("currentFloor=" + currentFloor)
88+
.toString();
89+
}
90+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package org.togetherjava.event.elevator.elevators;
2+
3+
/**
4+
* The system inside an elevator which provides information about the elevator and can be
5+
* used to request a destination floor.
6+
*/
7+
public interface ElevatorPanel {
8+
/**
9+
* The unique ID of the elevator.
10+
*
11+
* @return the unique ID of the elevator
12+
*/
13+
int getId();
14+
15+
/**
16+
* The floor the elevator is currently at.
17+
*
18+
* @return the current floor
19+
*/
20+
int getCurrentFloor();
21+
22+
/**
23+
* Requesting the elevator to eventually move to the given destination floor, for humans to exit.
24+
*
25+
* @param destinationFloor the desired destination, must be within the range served by this elevator
26+
*/
27+
void requestDestinationFloor(int destinationFloor);
28+
}
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
package org.togetherjava.event.elevator.elevators;
2+
3+
import org.togetherjava.event.elevator.humans.ElevatorListener;
4+
5+
import java.util.ArrayList;
6+
import java.util.List;
7+
8+
/**
9+
* System controlling all elevators of a building.
10+
* <p>
11+
* Once all elevators and humans have been registered via {@link #registerElevator(Elevator)}
12+
* and {@link #registerElevatorListener(ElevatorListener)} respectively,
13+
* the system can be made ready using {@link #ready()}.
14+
*/
15+
public final class ElevatorSystem implements FloorPanelSystem {
16+
private final List<Elevator> elevators = new ArrayList<>();
17+
private final List<ElevatorListener> elevatorListeners = new ArrayList<>();
18+
19+
public void registerElevator(Elevator elevator) {
20+
elevators.add(elevator);
21+
}
22+
23+
public void registerElevatorListener(ElevatorListener listener) {
24+
elevatorListeners.add(listener);
25+
}
26+
27+
/**
28+
* Upon calling this, the system is ready to receive elevator requests. Elevators may now start moving.
29+
*/
30+
public void ready() {
31+
elevatorListeners.forEach(listener -> listener.onElevatorSystemReady(this));
32+
}
33+
34+
@Override
35+
public void requestElevator(int atFloor, TravelDirection desiredTravelDirection) {
36+
// TODO Implement. This represents a human standing in the corridor,
37+
// requesting that an elevator comes to pick them up for travel into the given direction.
38+
// The system is supposed to make sure that an elevator will eventually reach this floor to pick up the human.
39+
// The human can then enter the elevator and request their actual destination within the elevator.
40+
// Ideally this has to select the best elevator among all which can reduce the time
41+
// for the human spending waiting (either in corridor or in the elevator itself).
42+
System.out.println("Request for elevator received");
43+
}
44+
45+
public void moveOneFloor() {
46+
elevators.forEach(Elevator::moveOneFloor);
47+
elevators.forEach(elevator -> elevatorListeners.forEach(listener -> listener.onElevatorArrivedAtFloor(elevator)));
48+
}
49+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package org.togetherjava.event.elevator.elevators;
2+
3+
/**
4+
* The system in corridors that allows requesting elevators to the current floor.
5+
*/
6+
public interface FloorPanelSystem {
7+
/**
8+
* Requests an elevator to move to the given floor to pick up a human.
9+
* @param atFloor the floor to pick up the human at, must be within the range served by the system
10+
* @param desiredTravelDirection the direction the human wants to travel into,
11+
* can be used for determination of the best elevator
12+
*/
13+
void requestElevator(int atFloor, TravelDirection desiredTravelDirection);
14+
}

0 commit comments

Comments
 (0)