Skip to content

Conversation

@Zabuzard
Copy link
Member

@Zabuzard Zabuzard commented Dec 1, 2025

Submission by user C

private final int startingFloor;
private final int destinationFloor;
private final TravelDirection desiredDirection;
private final Object lock = new Object();
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 also lock on this but yeah, this is also an idiom, neat

Comment on lines +53 to +55
synchronized (lock) {
return currentState;
}
Copy link
Member Author

Choose a reason for hiding this comment

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

thats probably obsolete, just a getter

return currentEnteredElevatorId == null
? OptionalInt.empty()
: OptionalInt.of(currentEnteredElevatorId);
synchronized (lock) {
Copy link
Member Author

Choose a reason for hiding this comment

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

probably obsolete as just a getter

.add("destinationFloor=" + destinationFloor)
.add("currentEnteredElevatorId=" + currentEnteredElevatorId)
.toString();
synchronized (lock) {
Copy link
Member Author

Choose a reason for hiding this comment

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

definitely unecessary. looks like synchronized-knowledge isnt super deep yet

currentState = State.ARRIVED;
return;
}
if (currentState.equals(State.WAITING_FOR_ELEVATOR)
Copy link
Member Author

Choose a reason for hiding this comment

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

use == for enums. also inconsistent as == was used right above

Comment on lines +82 to +87
if (currentState.equals(State.WAITING_FOR_ELEVATOR)
&& elevatorPanel.getCurrentFloor() == startingFloor
/* or advertised direction equals TravelDirection.NONE part is needed here as advertised direction
can be none if this was the last stop of the existing queue but more people need to board still */
&& ((elevatorPanel.getAdvertisedDirection() == desiredDirection)
|| elevatorPanel.getAdvertisedDirection() == TravelDirection.NONE)) {
Copy link
Member Author

Choose a reason for hiding this comment

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

that stuff is a bit complex to read/understand. a helper method with proper javadoc would be useful

private final int minFloor;
private final int floorsServed;
private final int maxFloor;
private final int[] requests;
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.

naming issue, what does int represent? (human IDs? floors?)

Comment on lines +72 to +74
synchronized (lock) {
addRequest(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.

a bit of excessive placement of sync everywhere. addRequest syncs itself already, so this one for example isnt needed

.add("currentFloor=" + currentFloor)
.toString();
public String toString() {
synchronized (lock) {
Copy link
Member Author

Choose a reason for hiding this comment

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

(obsolete)

*
* @return a boolean (true means there are more requests in current direction, otherwise false)
*/
private boolean moreRequestsInCurrentDirection() {
Copy link
Member Author

Choose a reason for hiding this comment

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

name should start with is or are or something like that

if (currentDirection == TravelDirection.DOWN) {
return countRequestsBelow() > 0;
}
throw new RuntimeException("currentDirection is in an invalid state!");
Copy link
Member Author

Choose a reason for hiding this comment

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

would be useful to add the current state to the error message

Comment on lines +185 to +198
private void goUp() {
synchronized (lock) {
currentFloor += 1;
}
}

/**
* Moves the elevator down one floor
*/
private void goDown() {
synchronized (lock) {
currentFloor -= 1;
}
}
Copy link
Member Author

Choose a reason for hiding this comment

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

no protection for the floor range

throw new IllegalArgumentException(INVALID_FLOOR_ERROR);
}
synchronized (lock) {
requests[floor - minFloor] += 1;
Copy link
Member Author

Choose a reason for hiding this comment

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

floor - minFloor, would be useful to put that into a well named var

throw new IllegalArgumentException(INVALID_FLOOR_ERROR);
}
synchronized (lock) {
return requests[floor - minFloor];
Copy link
Member Author

Choose a reason for hiding this comment

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

(floor - minFloor, would be useful to put that into a well named var)

throw new IllegalArgumentException(INVALID_FLOOR_ERROR);
}
synchronized (lock) {
requests[floor - minFloor] = 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.

floor - minFloor, would be useful to put that into a well named var.

since its repeated 3 times now, maybe even a helper method

Comment on lines +85 to +95
private static Elevator findNearestElevator(List<Elevator> elevators, int floor) {
Elevator nearest = null;
int nearestDistance = Integer.MAX_VALUE;
for (Elevator elevator : elevators) {
int distance = Math.abs(elevator.getCurrentFloor() - floor);
if (distance < nearestDistance) {
nearest = elevator;
nearestDistance = distance;
}
}
return nearest;
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.

the method mentions returning null is a thing.
in that case Optional would have been the more appriopriate pick

result = findNearestElevator(elevators, floor);
}
if (result == null) {
throw new RuntimeException("Found no elevators < Integer.MAX_VALUE distance away from complete list of elevators!");
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.

that "< Integer.MAX_VALUE distance" part is an assumption of implementation detail that isnt up to this method to make but only the find-method. comments like that easily drift apart from the implementation and rot

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.

also sophisticated implementation that operates relatively close to real life:

  • elevators keep track of button presses, "floor requests"
  • elevators move first in the direction with the most requests and do that until that direction is fully served, then they serve the other direction
  • elevator system tries to find closest elevator currently going in the desired direction, otherwise closest elevator in general
  • extras: multithreading/syncing
  • code quality: okay, some things to improve but okay and has javadoc, cool. use of synchronized is a bit excessive, also on getters 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