boarding floor correction, invalidFloor added

parent cfc8cbd7
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="VcsDirectoryMappings">
<mapping directory="$PROJECT_DIR$" vcs="Git" />
</component>
</project>
\ No newline at end of file
...@@ -15,18 +15,6 @@ ...@@ -15,18 +15,6 @@
</properties> </properties>
<dependencies> <dependencies>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency> <dependency>
<groupId>junit</groupId> <groupId>junit</groupId>
<artifactId>junit</artifactId> <artifactId>junit</artifactId>
......
...@@ -4,7 +4,7 @@ public class Elevator { ...@@ -4,7 +4,7 @@ public class Elevator {
private static final int minFloor = 0; private static final int minFloor = 0;
private static final int maxFloor = 10; private static final int maxFloor = 10;
private static int processingTime = 500; private static int processingTime = 50;
private int currentFloor; private int currentFloor;
private Direction currentDirection; private Direction currentDirection;
...@@ -54,7 +54,7 @@ public class Elevator { ...@@ -54,7 +54,7 @@ public class Elevator {
System.out.println("Chilling for now"); System.out.println("Chilling for now");
} }
public void lunchtimeElevatorRush() { public void lunchtimeElevatorRush() throws InterruptedException {
Random random = new Random(); Random random = new Random();
for (int i = 0; i < 30; i++) { for (int i = 0; i < 30; i++) {
callElevator(random.nextInt(11), callElevator(random.nextInt(11),
...@@ -63,14 +63,26 @@ public class Elevator { ...@@ -63,14 +63,26 @@ public class Elevator {
} }
// implement // implement
public void callElevator(int start, int destination) { public void callElevator(int start, int destination) throws InterruptedException {
if(requestedPathsMap.containsKey(start)) { if (isInvalidFloor(start) && isInvalidFloor(destination)) {
requestedPathsMap.get(start).add(destination); System.out.println("Both floors entered are invalid: " + start + ", " + destination);
} }
else { else if(isInvalidFloor(start) || isInvalidFloor(destination)) {
List<Integer> floorRequest = new ArrayList<>(); if (isInvalidFloor(start)) {
floorRequest.add(destination); System.out.println("Starting floor entered is invalid: " + start);
requestedPathsMap.put(start, floorRequest); } else if(isInvalidFloor(destination)) {
System.out.println("Destination floor entered is invalid: " + destination);
}
}
else{
if(requestedPathsMap.containsKey(start)) {
requestedPathsMap.get(start).add(destination);
}
else {
List<Integer> floorRequest = new ArrayList<>();
floorRequest.add(destination);
requestedPathsMap.put(start, floorRequest);
}
} }
} }
...@@ -81,8 +93,10 @@ public class Elevator { ...@@ -81,8 +93,10 @@ public class Elevator {
// add all path requests of current floor. // add all path requests of current floor.
if(requestedPathsMap.containsKey(floor)) { if(requestedPathsMap.containsKey(floor)) {
for(int x : requestedPathsMap.get(floor)) { for(int x : requestedPathsMap.get(floor)) {
System.out.println("Boarding at floor: " + floor); if(!currentFloorDestinations[x]){
currentFloorDestinations[x] = true; System.out.println("Boarding at floor: " + floor);
currentFloorDestinations[x] = true;
}
} }
} }
...@@ -100,7 +114,8 @@ public class Elevator { ...@@ -100,7 +114,8 @@ public class Elevator {
} }
} }
if(toRemove != -1) { if(toRemove != -1) {
requestedFloors.remove(toRemove); requestedPathsMap.get(key).remove(toRemove);
} }
} }
else if(key > floor && currentDirection == Direction.DOWN) { else if(key > floor && currentDirection == Direction.DOWN) {
...@@ -115,10 +130,9 @@ public class Elevator { ...@@ -115,10 +130,9 @@ public class Elevator {
} }
} }
if(toRemove != -1) { if(toRemove != -1) {
requestedFloors.remove(toRemove); requestedPathsMap.get(key).remove(toRemove);
} }
} }
} }
// the lift will continue to move in the direction it is currently moving in // the lift will continue to move in the direction it is currently moving in
......
import java.util.Arrays;
import java.util.List;
public class Testing {
public static void main(String[] args) {
List<Integer> numbers = Arrays.asList(1,2,3,4,5);
Integer num = 4;
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment