Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions Contest/Assignment/src/org/togetherjava/event/elevator/Main.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package org.togetherjava.event.elevator;

import org.togetherjava.event.elevator.elevators.Elevator;
import org.togetherjava.event.elevator.elevators.ElevatorSystem;
import org.togetherjava.event.elevator.elevators.TravelDirection;
import org.togetherjava.event.elevator.humans.Human;
import org.togetherjava.event.elevator.simulation.Simulation;

Expand All @@ -14,34 +16,34 @@ public final class Main {
* @param args Not supported
*/
public static void main(final String[] args) {

// Select a desired simulation for trying out your code.
// Start with the simple simulations first, try out the bigger systems once you got it working.
// Eventually try out the randomly generated systems. If you want to debug a problem you encountered
// with one of them, note down the seed that it prints at the beginning and then use the variant that takes this seed.
// That way, it will generate the same system again, and you can repeat the test.
Simulation simulation = Simulation.createSingleElevatorSingleHumanSimulation();
// Simulation simulation = Simulation.createSingleElevatorSingleHumanSimulation();
// Simulation simulation = Simulation.createSimpleSimulation();
// Simulation simulation = Simulation.createRandomSimulation(5, 50, 10);
Simulation simulation = Simulation.createRandomSimulation(5, 50, 10);
// Simulation simulation = Simulation.createRandomSimulation(putDesiredSeedHere, 5, 50, 10);

//
simulation.printSummary();

//
System.out.println("Starting simulation...");
simulation.start();
simulation.prettyPrint();

while (!simulation.isDone()) {
System.out.println("\tSimulation step " + simulation.getStepCount());
simulation.step();
simulation.prettyPrint();
simulation.prettyPrint();

if (simulation.getStepCount() >= 100_000) {
throw new IllegalStateException("Simulation aborted. All humans should have arrived"
+ " by now, but they did not. There is likely a bug in your code.");
}
}
System.out.println("Simulation is done.");

simulation.printResult();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

import java.util.StringJoiner;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.ArrayList;
import java.util.List;

/**
* A single elevator that can serve a given amount of floors.
Expand All @@ -16,6 +18,7 @@ public final class Elevator implements ElevatorPanel {
private final int minFloor;
private final int floorsServed;
private int currentFloor;
private List<Integer> destinations;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(extra space)


/**
* Creates a new elevator.
Expand All @@ -37,35 +40,74 @@ public Elevator(int minFloor, int floorsServed, int currentFloor) {
this.minFloor = minFloor;
this.currentFloor = currentFloor;
this.floorsServed = floorsServed;
this.destinations = new ArrayList<>();
}

/**
* The unique ID of the elevator.
*
* @return the unique ID of the elevator
*/
Comment on lines +46 to +50
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

the method already has javadoc through the interface its coming from (@Override)

@Override
public int getId() {
return id;
}

/**
* The lowest floor the elevator can service.
*
* @return the lowest floor the elevator can service.
*/
public int getMinFloor() {
return minFloor;
}

/**
* The amount of floors the elevator can service.
*
* @return the amount of floors the elevator can service.
*/
public int getFloorsServed() {
return floorsServed;
}

/**
* The floor the elevator is currently at.
*
* @return the floor the elevator is currently at.
*/
@Override
public int getCurrentFloor() {
return currentFloor;
}

/**
* The chosen Elevator will store this destination in it,s arrayList so it knows
* which direction to move in and when it has arrived.
* It will not store the same destination twice.
Comment on lines +85 to +87
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

a bit too much "implementation details" but ok

*
* @param destinationFloor stores the destination of the humans entering it
*/
@Override
public void requestDestinationFloor(int destinationFloor) {
// TODO Implement. This represents a human or the elevator system
// itself requesting this elevator to eventually move to the given floor.
// The elevator is supposed to memorize the destination in a way that
// it can ensure to eventually reach it.

if(!destinations.contains(destinationFloor)){
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

contains on arraylist, a bit problematic perf wise (think about big buildings with lots of humans)
but likely not a big deal in this particular situation as the amount of requests is generally fairly small per elevator

destinations.add(destinationFloor);
}

System.out.println("Request for destination floor received");
}

// create a method that will store the destinations in order:
// closest to the current floor. So every time an elevator moved one floor,
// we have to call this method as wel, because humans entering add to the
// array.
Comment on lines +101 to +104
Copy link
Member Author

@Zabuzard Zabuzard Dec 1, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

dangling comment. potential sign of use of AI? lol


/**
* Moving the elevator one floor in the correct direction and
* when it arrived at it,s destination it will remove that floor from
* it,s arrayList: destinations.
Comment on lines +107 to +109
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

bad javadoc: implementation details

*/
public void moveOneFloor() {
// TODO Implement. Essentially there are three possibilities:
// - move up one floor
Expand All @@ -76,9 +118,31 @@ public void moveOneFloor() {
// meaning that the average time waiting (either in corridor or inside the elevator)
// is minimized across all humans.
// It is essential that this method updates the currentFloor field accordingly.
System.out.println("Request to move a floor received");

// try to let an empty elevator help an elevator with more humans to transport.
//System.out.println("Request to move a floor received on elevator id: " + getId() + "--- " + getCurrentFloor());
System.out.println(destinations);
if(destinations.isEmpty()){
return;
}

int destination = destinations.get(0);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

prefer getFirst()


if (this.currentFloor < destination) {
this.currentFloor++;
} else if (this.currentFloor > destination) {
this.currentFloor--;
} else {
System.out.println("Elevator " + id + " ARRIVED at floor " + currentFloor);
destinations.remove(0);
}
}

/**
* Prints the data of the fields of class Elevator
*
* @return the data of the fields of class Elevator.
**/
@Override
public synchronized String toString() {
return new StringJoiner(", ", Elevator.class.getSimpleName() + "[", "]").add("id=" + id)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
/**
* The system inside an elevator which provides information about the elevator and can be
* used to request a destination floor.
* This interface represents all the actions that an elevator will want to expose to humans.
*/
public interface ElevatorPanel {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,19 +16,33 @@ public final class ElevatorSystem implements FloorPanelSystem {
private final List<Elevator> elevators = new ArrayList<>();
private final List<ElevatorListener> elevatorListeners = new ArrayList<>();

/**
* Registers all Elevator fields in an ArrayList
* @param elevator a single elevator with it,s fields data, created in the Elevator class.
*/
public void registerElevator(Elevator elevator) {
elevators.add(elevator);
}

/**
* Registers all ElevatorListeners objects (humans) in an ArrayList
* @param listener a single human with it,s fields data, created in the Human class.
*/
public void registerElevatorListener(ElevatorListener listener) {
elevatorListeners.add(listener);
}

/**
* Upon calling this, the system is ready to receive elevator requests. Elevators may now start moving.
* Every human in the arrayList listeners calls this method.
* {@link org.togetherjava.event.elevator.humans.Human#onElevatorSystemReady(FloorPanelSystem)} }
*/
public void ready() {
elevatorListeners.forEach(listener -> listener.onElevatorSystemReady(this));
// elevatorListeners.forEach(listener -> listener.onElevatorSystemReady(this));

for (ElevatorListener listener : elevatorListeners) {
listener.onElevatorSystemReady(this);
}
}

@Override
Expand All @@ -39,11 +53,57 @@ public void requestElevator(int atFloor, TravelDirection desiredTravelDirection)
// The human can then enter the elevator and request their actual destination within the elevator.
// Ideally this has to select the best elevator among all which can reduce the time
// for the human spending waiting (either in corridor or in the elevator itself).
System.out.println("Request for elevator received");

if (elevators.isEmpty()){
return;
}
int calculateDistance = 0;

Elevator chosenElevator = this.elevators.get(0);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

(prefer getFirst())

if (chosenElevator.getCurrentFloor() < atFloor){
calculateDistance = atFloor - chosenElevator.getCurrentFloor();
} else if (chosenElevator.getCurrentFloor() > atFloor){
calculateDistance = chosenElevator.getCurrentFloor() - atFloor;
}

for (Elevator elevator : elevators) {

int distance = 0;
if (elevator.getCurrentFloor() < atFloor){
distance = atFloor - elevator.getCurrentFloor();
} else if (elevator.getCurrentFloor() > atFloor){
distance = elevator.getCurrentFloor() - atFloor;
}

if (calculateDistance > distance) {
calculateDistance = distance;
chosenElevator = elevator;
}
Comment on lines +63 to +81
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

logic duplication. could be rewritten to pick it in a single loop without that extra logic beforehand

}
//System.out.println("calculate distance = " + calculateDistance);

// elevator panel is now being used:
chosenElevator.requestDestinationFloor(atFloor);
System.out.println("Request for elevator received at Floor: " + atFloor);
System.out.println("chosen Elevator = " + chosenElevator);
}


/**
* First it runs the {@link org.togetherjava.event.elevator.elevators.Elevator#moveOneFloor()} method on every elevator.
* Then it lets every elevator in the elevator list tell every human in the listener list
* that it has moved one Floor.
*/
public void moveOneFloor() {
// think about this one, could be more efficient?

elevators.forEach(Elevator::moveOneFloor);
// loops through the elevators and moves all the elevators in the arrayList

elevators.forEach(elevator -> elevatorListeners.forEach(listener -> listener.onElevatorArrivedAtFloor(elevator)));
// nested loop, looping through all elevators and tells all humans on which floor it has arrived.
// this part specifically can be done more efficiently as also mentioned in the Javadoc
// inside the ElevatorListener Interface
// currentEnteredElevatorId??
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

/**
* The system in corridors that allows requesting elevators to the current floor.
* This interface represents the action that the elevator system itself exposes to humans
* standing on the corridor.
*/
public interface FloorPanelSystem {
/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,13 @@
/**
* Listeners to elevator events. This is mostly interesting for
* humans who can then request elevators to move to desired floors.
* This interface represents all the actions that a human will be interested in.
*/
public interface ElevatorListener {
/**
* Fired when the elevator system is ready to receive requests. Elevators can now move.
* Fired when the elevator system is ready (elevators are created)
* to receive requests. Elevators can now move.
*
*
* @param floorPanelSystem the system in the corridor that allows
* requesting elevators to the current floor
Expand Down
Loading