-
Notifications
You must be signed in to change notification settings - Fork 4
Submission D #9
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 D #9
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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. | ||
|
|
@@ -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; | ||
|
|
||
| /** | ||
| * Creates a new elevator. | ||
|
|
@@ -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
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. the method already has javadoc through the interface its coming from ( |
||
| @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
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)){ | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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) |
||
| 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
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
|
@@ -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); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. prefer |
||
|
|
||
| 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) | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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 | ||
|
|
@@ -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); | ||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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?? | ||
| } | ||
| } | ||
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.
(extra space)