diff --git a/Contest/Assignment/src/org/togetherjava/event/elevator/elevators/Elevator.java b/Contest/Assignment/src/org/togetherjava/event/elevator/elevators/Elevator.java index 51333b2..25de683 100644 --- a/Contest/Assignment/src/org/togetherjava/event/elevator/elevators/Elevator.java +++ b/Contest/Assignment/src/org/togetherjava/event/elevator/elevators/Elevator.java @@ -1,5 +1,6 @@ package org.togetherjava.event.elevator.elevators; +import java.util.Hashtable; import java.util.StringJoiner; import java.util.concurrent.atomic.AtomicInteger; @@ -14,9 +15,12 @@ public final class Elevator implements ElevatorPanel { private final int id; private final int minFloor; + private final int maxFloor; private final int floorsServed; private int currentFloor; + Hashtable requests = new Hashtable<>(); + /** * Creates a new elevator. * @@ -35,6 +39,7 @@ public Elevator(int minFloor, int floorsServed, int currentFloor) { this.id = NEXT_ID.getAndIncrement(); this.minFloor = minFloor; + this.maxFloor = minFloor + floorsServed-1; this.currentFloor = currentFloor; this.floorsServed = floorsServed; } @@ -47,7 +52,9 @@ public int getId() { public int getMinFloor() { return minFloor; } - + public int getMaxFloor() { + return maxFloor; + } public int getFloorsServed() { return floorsServed; } @@ -63,9 +70,34 @@ public void requestDestinationFloor(int destinationFloor) { // 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. - System.out.println("Request for destination floor received"); - } + if(destinationFloor==currentFloor) + return; + TravelDirection travelDirection=getTravelDirection(destinationFloor); + requests.put(destinationFloor,travelDirection); + } + private TravelDirection getTravelDirection(int floor) + { + if(floor>currentFloor) + return TravelDirection.UP; + else + return TravelDirection.DOWN; + } + private int getClosestFloor() + { + int smallestFloorDifference=floorsServed,smallestFloor=-1; + for (Integer floor : requests.keySet()) { + if(floor==null) + continue; + int floorDifference=Math.abs(floor-currentFloor); + if(floorDifferencecurrentFloor) + { + if(currentFloor==maxFloor) + { + requests.remove(closestFloor); + moveOneFloor(); + return; + } + ++currentFloor; + } + else + { + requests.remove(closestFloor); + moveOneFloor(); + } } @Override diff --git a/Contest/Assignment/src/org/togetherjava/event/elevator/elevators/ElevatorSystem.java b/Contest/Assignment/src/org/togetherjava/event/elevator/elevators/ElevatorSystem.java index fadfe56..20c12e8 100644 --- a/Contest/Assignment/src/org/togetherjava/event/elevator/elevators/ElevatorSystem.java +++ b/Contest/Assignment/src/org/togetherjava/event/elevator/elevators/ElevatorSystem.java @@ -13,8 +13,8 @@ * the system can be made ready using {@link #ready()}. */ public final class ElevatorSystem implements FloorPanelSystem { - private final List elevators = new ArrayList<>(); - private final List elevatorListeners = new ArrayList<>(); + private final List elevators= new ArrayList<>(); + private final List elevatorListeners= new ArrayList<>(); public void registerElevator(Elevator elevator) { elevators.add(elevator); @@ -31,6 +31,28 @@ public void ready() { elevatorListeners.forEach(listener -> listener.onElevatorSystemReady(this)); } + private boolean isInRange(int floor,Elevator elevator) + { + return floor>=elevator.getMinFloor() && floor<=elevator.getMaxFloor(); + } + private Elevator getClosestElevator(int atFloor) + { + int minimumFloorDifference=Math.abs(elevators.get(0).getCurrentFloor()-atFloor); + if(minimumFloorDifference==0) + return elevators.get(0); + Elevator closestElevator=elevators.get(0); + for(Elevator elevator:elevators) + { + int currentFloor=elevator.getCurrentFloor(); + int floorDifference=Math.abs(currentFloor-atFloor); + if(floorDifference=startingFloor) + return TravelDirection.UP; + else + return TravelDirection.DOWN; + } @Override public void onElevatorSystemReady(FloorPanelSystem floorPanelSystem) { // TODO Implement. The system is now ready and the human should leave // their initial IDLE state, requesting an elevator by clicking on the buttons of // the floor panel system. The human will now enter the WAITING_FOR_ELEVATOR state. - System.out.println("Ready-event received"); + if(startingFloor==destinationFloor) + { + currentEnteredElevatorId=null; + currentState=State.ARRIVED; + return; + } + floorPanelSystem.requestElevator(startingFloor,getTravelDirection()); + currentState=State.WAITING_FOR_ELEVATOR; } @Override @@ -70,7 +85,18 @@ public void onElevatorArrivedAtFloor(ElevatorPanel elevatorPanel) { // elevator and request their actual destination floor. The state has to change to TRAVELING_WITH_ELEVATOR. // If the human is currently traveling with this elevator and the event represents // arrival at the human's destination floor, the human can now exit the elevator. - System.out.println("Arrived-event received"); + if(currentState==State.WAITING_FOR_ELEVATOR && elevatorPanel.getCurrentFloor()==startingFloor) + { + elevatorPanel.requestDestinationFloor(destinationFloor); + currentState=State.TRAVELING_WITH_ELEVATOR; + currentEnteredElevatorId=elevatorPanel.getId(); + } + else if(currentState==State.TRAVELING_WITH_ELEVATOR && elevatorPanel.getId()==currentEnteredElevatorId && elevatorPanel.getCurrentFloor()==destinationFloor) + { + currentEnteredElevatorId=null; + currentState=State.ARRIVED; + } + } public OptionalInt getCurrentEnteredElevatorId() {