Skip to content
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

Submission B #4

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package org.togetherjava.event.elevator.elevators;

import java.util.Hashtable;
import java.util.StringJoiner;
import java.util.concurrent.atomic.AtomicInteger;

Expand All @@ -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<Integer, TravelDirection> requests = new Hashtable<>();

/**
* Creates a new elevator.
*
Expand All @@ -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;
}
Expand All @@ -47,7 +52,9 @@ public int getId() {
public int getMinFloor() {
return minFloor;
}

public int getMaxFloor() {
return maxFloor;
}
public int getFloorsServed() {
return floorsServed;
}
Expand All @@ -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(floorDifference<smallestFloorDifference)
{
smallestFloorDifference=floorDifference;
smallestFloor=floor;
}
}
return smallestFloor;
}
public void moveOneFloor() {
// TODO Implement. Essentially there are three possibilities:
// - move up one floor
Expand All @@ -76,7 +108,36 @@ 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");
int closestFloor=getClosestFloor();
if(closestFloor==-1)
{
return;
}
if(closestFloor<currentFloor)
{
if(currentFloor==minFloor)
{
requests.remove(closestFloor);
moveOneFloor();
return;
}
--currentFloor;
}
else if(closestFloor>currentFloor)
{
if(currentFloor==maxFloor)
{
requests.remove(closestFloor);
moveOneFloor();
return;
}
++currentFloor;
}
else
{
requests.remove(closestFloor);
moveOneFloor();
}
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@
* the system can be made ready using {@link #ready()}.
*/
public final class ElevatorSystem implements FloorPanelSystem {
private final List<Elevator> elevators = new ArrayList<>();
private final List<ElevatorListener> elevatorListeners = new ArrayList<>();
private final List<Elevator> elevators= new ArrayList<>();
private final List<ElevatorListener> elevatorListeners= new ArrayList<>();

public void registerElevator(Elevator elevator) {
elevators.add(elevator);
Expand All @@ -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<minimumFloorDifference&&isInRange(currentFloor,elevator))
{
minimumFloorDifference=floorDifference;
closestElevator=elevator;
}
}
return closestElevator;
}
@Override
public void requestElevator(int atFloor, TravelDirection desiredTravelDirection) {
// TODO Implement. This represents a human standing in the corridor,
Expand All @@ -39,7 +61,9 @@ 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");
Elevator chosenElevator= getClosestElevator(atFloor);
if(chosenElevator!=null)
chosenElevator.requestDestinationFloor(atFloor);
}

public void moveOneFloor() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import org.togetherjava.event.elevator.elevators.ElevatorPanel;
import org.togetherjava.event.elevator.elevators.FloorPanelSystem;
import org.togetherjava.event.elevator.elevators.TravelDirection;

import java.util.OptionalInt;
import java.util.StringJoiner;
Expand Down Expand Up @@ -55,12 +56,26 @@ public int getDestinationFloor() {
return destinationFloor;
}

private TravelDirection getTravelDirection()
{
if(destinationFloor>=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
Expand All @@ -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() {
Expand Down