-
Notifications
You must be signed in to change notification settings - Fork 4
Submission C #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Submission C #8
Conversation
| private final int startingFloor; | ||
| private final int destinationFloor; | ||
| private final TravelDirection desiredDirection; | ||
| private final Object lock = new Object(); |
There was a problem hiding this comment.
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
| synchronized (lock) { | ||
| return currentState; | ||
| } |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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) |
There was a problem hiding this comment.
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
| 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)) { |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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?)
| synchronized (lock) { | ||
| addRequest(destinationFloor); | ||
| } |
There was a problem hiding this comment.
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) { |
There was a problem hiding this comment.
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() { |
There was a problem hiding this comment.
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!"); |
There was a problem hiding this comment.
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
| private void goUp() { | ||
| synchronized (lock) { | ||
| currentFloor += 1; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Moves the elevator down one floor | ||
| */ | ||
| private void goDown() { | ||
| synchronized (lock) { | ||
| currentFloor -= 1; | ||
| } | ||
| } |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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]; |
There was a problem hiding this comment.
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; |
There was a problem hiding this comment.
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
| 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; |
There was a problem hiding this comment.
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!"); |
There was a problem hiding this comment.
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
Zabuzard
left a comment
There was a problem hiding this 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
synchronizedis a bit excessive, also on getters etc
Submission by user C