Skip to content

Conversation

@Zabuzard
Copy link
Member

@Zabuzard Zabuzard commented Dec 1, 2025

Submission by user B


simulation.printResult();
} catch (SimulationException ex) {
log.error("Elevator Simulation error: {}", ex.getMessage());
Copy link
Member Author

Choose a reason for hiding this comment

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

should append the exception itself as last param log.error(..., e)

Comment on lines +18 to +19
// public static final String LOG_ELEVATOR_DOESNT_MOVE_NO_PENDING_FLOORS =
// "elevator {} doesn't move, no pending floors";
Copy link
Member Author

Choose a reason for hiding this comment

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

leftover code

Comment on lines +21 to +22
public static final String LOG_ELEVATOR_MOVING_FROM_TO =
"elevator {} moving {} from {} to {} (target: {})";
Copy link
Member Author

Choose a reason for hiding this comment

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

pattern-strings should have sth like PATTERN in the name so its clear that they still need a formatter

Comment on lines +28 to +36
public static void printSummary(Simulation simulation) {
View view = simulation.getView();
view.printSummary();
}

public static void prettyPrint(Simulation simulation) {
View view = simulation.getView();
view.prettyPrint();
}
Copy link
Member Author

Choose a reason for hiding this comment

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

this feels super obsolete and doesnt really save anyone anything.
simulation.prettyPrint() versus SimulationUtils.prettyPrint(simulation)

Comment on lines +47 to +51
// log.info(
// "creating random simulation: elevatorsCount {}, humansCount {}, floorsServed {}",
// amountOfElevators,
// amountOfHumans,
// floorsServed);
Copy link
Member Author

Choose a reason for hiding this comment

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

dangling log

Comment on lines +63 to +101
public void onElevatorSystemReady(FloorPanelSystem floorPanelSystem) {

final int start = this.startingFloor;

log.info("checking human at floor {}, state", start);

final int destination = this.destinationFloor;

if (start == destination) {

final HumanState state = HumanState.ARRIVED;

log.info("starting floor is the same as destination floor => marking state as {}", state);

this.currentState = state;

return;
}

TravelDirection direction = start < destination ? TravelDirection.UP : TravelDirection.DOWN;

log.info("computed direction for human at floor {} is {}", start, direction);

floorPanelSystem.requestElevator(start, direction);

HumanState newState = HumanState.WAITING_FOR_ELEVATOR;

log.info("updating human at floor {} state from {} to {}", start, this.currentState, newState);

this.currentState = newState;

log.info(" human at floor {} state updated", start);

log.info(
"human startin from floor {}, requesting elevator towards floor {}, direction {}",
start,
destination,
direction);
}
Copy link
Member Author

Choose a reason for hiding this comment

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

kinda weird style with all these newlines between every single line, makes the code hard to read if there is no proper grouping applied

Comment on lines +88 to +92
HumanState newState = HumanState.WAITING_FOR_ELEVATOR;

log.info("updating human at floor {} state from {} to {}", start, this.currentState, newState);

this.currentState = newState;
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.

should be tight coupled and not interrupted with a log to prevent bugs
all those extra variables are also sorta obsolete


this.currentState = newState;

log.info(" human at floor {} state updated", start);
Copy link
Member Author

Choose a reason for hiding this comment

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

these logs shouldnt be INFO but DEBUG level

Comment on lines +120 to +125
// log.debug(
// "human at floor {}, (state: {}) received elevator {} arrival at floor {}",
// this.startingFloor,
// this.currentState,
// elevatorId,
// elevatorCurrentFloor);
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 logs (also elsewhere in the codebase) instead of making proper use of logger levels

@Getter private final int minFloor;
@Getter private final int floorsServed;
private int currentFloor;
private final Set<Integer> pendingFloors = new LinkedHashSet<>();
Copy link
Member Author

Choose a reason for hiding this comment

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

good pick for a data structure

// destinationFloor);

if (destinationFloor < this.minFloor || destinationFloor >= this.minFloor + this.floorsServed) {
throw new SimulationException("destination outof range for elevator " + this.id);
Copy link
Member Author

Choose a reason for hiding this comment

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

sorta wrong pick for an exception. elevators dont necessarily deal with simulations

Comment on lines +89 to +91
if (!this.pendingFloors.contains(destinationFloor)) {

this.pendingFloors.add(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.

could be simplified to if (!pendingFloors.add(destinationFloor)) as attempting to add on a Set already tells whether it could be added or not

Comment on lines +121 to +133
if (!this.pendingFloors.isEmpty()) {

this.activeTarget = this.pendingFloors.iterator().next();

// log.debug(
// "Elevator {}, has no active target, selecting next pending floor: {}",
// this.id,
// this.activeTarget);

} else {
// log.debug("elevator {} is idle (no pending floors)", this.id);
return;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

difficult to read. early-return would be better:

if (pendingFloors.isEmpty()) return;
...

Comment on lines +145 to +147
if (!this.pendingFloors.isEmpty()) {
this.activeTarget = this.pendingFloors.iterator().next();
log.debug("elvator {} selecting next target floor: {}", this.id, this.activeTarget);
Copy link
Member Author

Choose a reason for hiding this comment

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

this "lets pick the next target" logic is duplicated. maybe better for an extra well named helper method such as

private OptionalInt pickNextTarget() {...}

or

private void selectNextActiveTarget() {...}

this.currentFloor++;

log.debug(
SimulationUtils.LOG_ELEVATOR_MOVING_FROM_TO,
Copy link
Member Author

Choose a reason for hiding this comment

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

simulation is from another package and doesnt necessarily deal with elevators, its the wrong util class for using here, design-wise


Elevator target = staging.getFirst();

int distanceFlag = Math.abs(atFloor - target.getCurrentFloor());
Copy link
Member Author

Choose a reason for hiding this comment

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

its not a flag, confusing name

Comment on lines +87 to +89
for (int i = 1; i < staging.size(); i++) {

Elevator elevator = staging.get(i);
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.

why not enhanced for loop as also used elsewhere in the code?

Copy link
Member Author

@Zabuzard Zabuzard left a comment

Choose a reason for hiding this comment

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

working solution, the logic is overall a bit weak but totally works:

  • elevator system assigns closest elevator (based on distance, not travel dir)
  • humans enter/exit whenever elevator is at desired floor
  • elevators keep a set of floor requests that they process one by one. the choice of LinkedHashSet for this is good and the implementation makes sure to clear any subsequent requests of floors visited meanwhile, so no "unecessary movement". but there is no sense of priority or similar. when a request queue would look like (4, 1, 5) then the elevator would move to floor 4, then to 1 and then to 5 instead of maybe first going from 4 to 5 and then to 1.
  • code quality: tried to add some more biz/corp style by changing packages, auto-formatting everything with a different style, adding lombok for a few @Getter, adding logger, a SimulationUtils class and the like. in general, the direction is okay. but its mostly executed a bit poor. for example the logger isnt used properly, no log levels, no proper log configuration. or the SimulationUtils is used in places where the util is inappropriate design-wise, some of the util methods in there are questionable etc

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants