elevator problem complete

parents
# Default ignored files
/shelf/
/workspace.xml
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="CompilerConfiguration">
<annotationProcessing>
<profile name="Maven default annotation processors profile" enabled="true">
<sourceOutputDir name="target/generated-sources/annotations" />
<sourceTestOutputDir name="target/generated-test-sources/test-annotations" />
<outputRelativeToContentRoot value="true" />
<module name="ElevatorChallenge" />
</profile>
</annotationProcessing>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="RemoteRepositoriesConfiguration">
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="https://repo.maven.apache.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="macysSnapshots" />
<option name="name" value="Macys Snapshots" />
<option name="url" value="http://artifacts.devops.fds.com/public-snapshots" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Maven Central repository" />
<option name="url" value="https://repo1.maven.org/maven2" />
</remote-repository>
<remote-repository>
<option name="id" value="jboss.community" />
<option name="name" value="JBoss Community repository" />
<option name="url" value="https://repository.jboss.org/nexus/content/repositories/public/" />
</remote-repository>
<remote-repository>
<option name="id" value="central" />
<option name="name" value="Central Repository" />
<option name="url" value="http://artifacts.devops.fds.com/public" />
</remote-repository>
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="ExternalStorageConfigurationManager" enabled="true" />
<component name="MavenProjectsManager">
<option name="originalFiles">
<list>
<option value="$PROJECT_DIR$/pom.xml" />
</list>
</option>
</component>
<component name="ProjectRootManager" version="2" languageLevel="JDK_18" project-jdk-name="corretto-1.8" project-jdk-type="JavaSDK">
<output url="file://$PROJECT_DIR$/out" />
</component>
</project>
\ No newline at end of file
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.linkedin-learning</groupId>
<artifactId>ElevatorChallenge</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<java.version>1.8</java.version>
<maven.compiler.source>1.8</maven.compiler.source>
<maven.compiler.target>1.8</maven.compiler.target>
</properties>
<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>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.13.2</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<pluginManagement><!-- lock down plugins versions to avoid using Maven defaults (may be moved to parent pom) -->
<plugins>
<!-- clean lifecycle, see https://maven.apache.org/ref/current/maven-core/lifecycles.html#clean_Lifecycle -->
<plugin>
<artifactId>maven-clean-plugin</artifactId>
<version>3.1.0</version>
</plugin>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
</plugin>
<plugin>
<artifactId>maven-surefire-plugin</artifactId>
<version>2.22.1</version>
</plugin>
</plugins>
</pluginManagement>
</build>
</project>
public enum Direction {
UP,
DOWN,
IDLE
}
\ No newline at end of file
import java.util.*;
public class Elevator {
private static final int minFloor = 0;
private static final int maxFloor = 10;
private static int processingTime = 500;
private int currentFloor;
private Direction currentDirection;
private Map<Integer, List<Integer>> requestedPathsMap;
// The purpose of currentFloorDestinations is to check which direction the lift must move based
// on the requests that the current occupants of destinations,
// the shortest path for current occupants will get the highest priority
private Boolean[] currentFloorDestinations;
public Elevator() {
this.currentFloor = 0;
this.currentDirection = Direction.UP;
this.requestedPathsMap = new HashMap<>();
this.currentFloorDestinations = new Boolean[maxFloor + 1];
Arrays.fill(this.currentFloorDestinations, Boolean.FALSE);
}
public void setProcessingTime(int processingTime) {
Elevator.processingTime = processingTime;
}
public int getCurrentFloor() {
return this.currentFloor;
}
public Map<Integer, List<Integer>> getRequestedPathsMap() {
return this.requestedPathsMap;
}
public Boolean[] getCurrentFloorDestinations() {
return this.currentFloorDestinations;
}
public void start() throws InterruptedException {
currentDirection = Direction.UP;
do {
System.out.println("--------");
processFloor(currentFloor);
System.out.println("--------");
} while(currentDirection != Direction.IDLE);
System.out.println("No one is waiting and " +
"no one is looking to go anywhere");
System.out.println("Chilling for now");
}
public void lunchtimeElevatorRush() {
Random random = new Random();
for (int i = 0; i < 30; i++) {
callElevator(random.nextInt(11),
random.nextInt(10) + 1);
}
}
// implement
public void callElevator(int start, int destination) {
if(requestedPathsMap.containsKey(start)) {
requestedPathsMap.get(start).add(destination);
}
else {
List<Integer> floorRequest = new ArrayList<>();
floorRequest.add(destination);
requestedPathsMap.put(start, floorRequest);
}
}
// implement
private void processFloor(int floor) throws InterruptedException {
// remove current floor from currentFloorDestinations.
currentFloorDestinations[floor] = false;
// add all path requests of current floor.
if(requestedPathsMap.containsKey(floor)) {
for(int x : requestedPathsMap.get(floor)) {
System.out.println("Boarding at floor: " + floor);
currentFloorDestinations[x] = true;
}
}
// remove current floor from all values of requestedPathsMap.
for (Integer key: requestedPathsMap.keySet()) {
if(key < floor && currentDirection == Direction.UP) {
List<Integer> requestedFloors = requestedPathsMap.get(key);
int toRemove = -1;
if(requestedFloors.contains(floor)) {
for (int i = 0; i < requestedFloors.size(); i++) {
if(requestedFloors.get(i) == floor) {
toRemove = i;
System.out.println("UNBOARDING:" + requestedFloors.get(i));
}
}
}
if(toRemove != -1) {
requestedFloors.remove(toRemove);
}
}
else if(key > floor && currentDirection == Direction.DOWN) {
List<Integer> requestedFloors = requestedPathsMap.get(key);
int toRemove = -1;
if(requestedFloors.contains(floor)) {
for (int i = 0; i < requestedFloors.size(); i++) {
if(requestedFloors.get(i) == floor) {
toRemove = i;
System.out.println("UNBOARDING:" + requestedFloors.get(i));
}
}
}
if(toRemove != -1) {
requestedFloors.remove(toRemove);
}
}
}
// the lift will continue to move in the direction it is currently moving in
// unless there are no further requests in that particular direction.
if(currentDirection == Direction.UP){
for(int i = floor; i > -1; i--) {
if (currentFloorDestinations[i]) {
currentDirection = Direction.DOWN;
break;
}
}
for(int i = floor; i < 11; i++) {
if (currentFloorDestinations[i]) {
currentDirection = Direction.UP;
break;
}
}
}
else if(currentDirection == Direction.DOWN){
for(int i = floor; i < 11; i++) {
if (currentFloorDestinations[i]) {
currentDirection = Direction.UP;
break;
}
}
for(int i = floor; i > -1; i--) {
if (currentFloorDestinations[i]) {
currentDirection = Direction.DOWN;
break;
}
}
}
moveElevator();
}
// implement
private void moveElevator() throws InterruptedException {
boolean allEmpty = true;
// check if all requestedPathsMap values are empty.
// if true set currentDirection to IDLE.
for (List<Integer> requestedFloors: requestedPathsMap.values()) {
if (!requestedFloors.isEmpty()) {
allEmpty = false;
break;
}
}
if(allEmpty) {
currentDirection = Direction.IDLE;
}
if(currentDirection == Direction.UP){
moveUp();
}
else if(currentDirection == Direction.DOWN) {
moveDown();
}
}
private void moveUp() throws InterruptedException {
currentFloor++;
System.out.println("GOING UP TO " + currentFloor);
Thread.sleep(processingTime);
}
private void moveDown() throws InterruptedException {
currentFloor--;
System.out.println("GOING DOWN TO " + currentFloor);
Thread.sleep(processingTime);
}
private boolean isInvalidFloor(int floor) {
return floor < minFloor || floor > maxFloor;
}
}
import java.util.Scanner;
public class ElevatorChallenge {
static void automaticElevator() throws InterruptedException {
Elevator elevator = new Elevator();
elevator.lunchtimeElevatorRush();
elevator.start();
}
static void manualElevator() throws InterruptedException {
int start = Integer.MIN_VALUE;
int end = Integer.MIN_VALUE;
int stop = 0;
Elevator elevator = new Elevator();
Scanner sc = new Scanner(System.in);
do{
System.out.println("Enter a starting floor 0 - 10");
start = sc.nextInt();
System.out.println("Enter a destination floor 0 - 10");
end = sc.nextInt();
elevator.callElevator(start, end);
System.out.println("Press -1 to exit or any key to continue:");
stop = sc.nextInt();
}
while(stop != -1);
elevator.start();
}
public static void main(String[] args) throws InterruptedException {
manualElevator();
}
}
import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import java.io.ByteArrayOutputStream;
import java.io.PrintStream;
import static org.junit.Assert.assertEquals;
public class ElevatorTest {
private final ByteArrayOutputStream printOut =
new ByteArrayOutputStream();
@Before
public void setUpStreams() {
System.setOut(new PrintStream(printOut));
}
@After
public void restoreStreams() {
System.setOut(System.out);
}
@Test
public void callElevator() throws InterruptedException {
Elevator elevator = new Elevator();
elevator.setProcessingTime(10);
elevator.callElevator(0, 4);
elevator.start();
assertEquals(4, elevator.getCurrentFloor());
elevator.callElevator(0, 5);
elevator.start();
assertEquals(5, elevator.getCurrentFloor());
elevator.callElevator(1, 5);
elevator.start();
assertEquals(5, elevator.getCurrentFloor());
elevator.callElevator(4, 10);
elevator.start();
assertEquals(10, elevator.getCurrentFloor());
elevator.callElevator(10, 7);
elevator.start();
assertEquals(7, elevator.getCurrentFloor());
}
// @Test
// public void callElevator_invalidStart() {
// Elevator elevator = new Elevator();
//
// elevator.callElevator(-3, 8);
//
// assertEquals(0, elevator.getCurrentFloor());
// assertEquals("INVALID FLOORS. Try again\n",
// printOut.toString());
// }
// @Test
// public void callElevator_invalidDestination() {
// Elevator elevator = new Elevator();
//
// elevator.callElevator(0, 14);
// assertEquals(0, elevator.getCurrentFloor());
// assertEquals("INVALID FLOORS. Try again\n",
// printOut.toString());
// }
//
// @Test
// public void callElevator_sameStartAndDestination() {
// Elevator elevator = new Elevator();
//
// elevator.callElevator(5, 5);
// assertEquals(0, elevator.getCurrentFloor());
// assertEquals("INVALID FLOORS. Try again\n",
// printOut.toString());
// }
//
//// @Test
// public void startElevator() throws InterruptedException {
// Elevator elevator = new Elevator();
// elevator.setProcessingTime(10);
//
// elevator.callElevator(0, 4);
// elevator.callElevator(0, 5);
// elevator.callElevator(1, 5);
// elevator.callElevator(4, 6);
// elevator.callElevator(-3, 8);
// elevator.callElevator(0, 18);
// elevator.callElevator(8, 4);
// elevator.callElevator(10, 2);
//
// elevator.start();
//
// assertEquals("INVALID FLOORS. Try again\n" +
// "INVALID FLOORS. Try again\n" +
// "--------\n" +
// "BOARDING at Floor 0\n" +
// "GOING UP TO 1\n" +
// "--------\n" +
// "--------\n" +
// "BOARDING at Floor 1\n" +
// "GOING UP TO 2\n" +
// "--------\n" +
// "--------\n" +
// "GOING UP TO 3\n" +
// "--------\n" +
// "--------\n" +
// "GOING UP TO 4\n" +
// "--------\n" +
// "--------\n" +
// "UNBOARDING at Floor 4\n" +
// "BOARDING at Floor 4\n" +
// "GOING UP TO 5\n" +
// "--------\n" +
// "--------\n" +
// "UNBOARDING at Floor 5\n" +
// "GOING UP TO 6\n" +
// "--------\n" +
// "--------\n" +
// "UNBOARDING at Floor 6\n" +
// "GOING UP TO 7\n" +
// "--------\n" +
// "--------\n" +
// "GOING UP TO 8\n" +
// "--------\n" +
// "--------\n" +
// "BOARDING at Floor 8\n" +
// "GOING UP TO 9\n" +
// "--------\n" +
// "--------\n" +
// "GOING UP TO 10\n" +
// "--------\n" +
// "--------\n" +
// "BOARDING at Floor 10\n" +
// "GOING DOWN TO 9\n" +
// "--------\n" +
// "--------\n" +
// "GOING DOWN TO 8\n" +
// "--------\n" +
// "--------\n" +
// "GOING DOWN TO 7\n" +
// "--------\n" +
// "--------\n" +
// "GOING DOWN TO 6\n" +
// "--------\n" +
// "--------\n" +
// "GOING DOWN TO 5\n" +
// "--------\n" +
// "--------\n" +
// "GOING DOWN TO 4\n" +
// "--------\n" +
// "--------\n" +
// "UNBOARDING at Floor 4\n" +
// "GOING DOWN TO 3\n" +
// "--------\n" +
// "--------\n" +
// "GOING DOWN TO 2\n" +
// "--------\n" +
// "--------\n" +
// "UNBOARDING at Floor 2\n" +
// "--------\n" +
// "No one is waiting and no one is looking to " +
// "go anywhere\n" +
// "Chilling for now\n", printOut.toString());
//
//// assertEquals(Map.of(), elevator.getRequestedPathsMap());
// assertFalse(Arrays
// .asList(elevator.getCurrentFloorDestinations())
// .contains(Boolean.TRUE));
// }
}
\ No newline at end of file
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