import java.util.concurrent.*; public class Vehicle implements Runnable{ private Integer currentSpeed; private Integer targetSpeed; private double fuelLevel; private double distanceRemaining; public Route currentRoute = new Route(); public void run() { navigateRoute(); } public Vehicle() { fuelLevel = getFuelLevel(); currentSpeed = 0; currentRoute.setRouteDestination("250 Community College Pkwy SE, Palm Bay, FL 32909"); } public void setTargetSpeed(int newSpeed) {targetSpeed = newSpeed;} public int getCurrentSpeed() {return currentSpeed;} public double getFuelLevel() {return fuelLevel;} public void updateFuelLevel() { //would check fuel sensor and update accordingly fuelLevel = 0.75; } public void navigateRoute() { System.out.println("Start navigation"); boolean turnCompleted = false; for (String[] el : currentRoute.getSelectedRouteSteps()) { RoadSensor trafficSensor = new RoadSensor(); if (targetSpeed > 20 && trafficSensor.detectTrafficLight() == 1 && el[0] != "STRAIGHT") { //simulates stopping at traffic lights on major roadways //for demo purposes, the spherical cow in a vacuum is that this only happens at turns long sleepTime = Double.valueOf(Math.floor(Math.random()*15)).longValue(); while (sleepTime > 0) { System.out.println("Traffic light detected, waiting..."); try {TimeUnit.SECONDS.sleep(1);} //sleep for 1-15 second to simulate traffic light catch (InterruptedException e) {System.err.println("Sleep interrupted");} } System.out.println("Resuming navigation"); } turnCompleted = false; //flag to validate completion of moveset instruction distanceRemaining = Double.parseDouble(el[1]); targetSpeed = Integer.parseInt(el[2]); while (!turnCompleted) { if (currentSpeed < 15 || el[0] == "STRAIGHT") { changeDirection(el[0]); turnCompleted = true; } else { applyBrakes(); } } while (distanceRemaining > 0) { System.out.printf("%.2f miles to next instruction\n", distanceRemaining); System.out.println("Current Speed: " + currentSpeed + " MPH"); controlThrottle(); distanceRemaining = distanceRemaining - (Double.valueOf(currentSpeed)/3600); //travel distance in one second try {TimeUnit.SECONDS.sleep(1);} //sleep action for 1 second to simulate continuous movement catch (InterruptedException e) {System.err.println("Sleep interrupted");} } } System.out.println("Navigation complete, destination reached."); } public void changeDirection(String direction) { switch (direction) { case "LEFT": System.out.println("Turning left..."); break; case "RIGHT": System.out.println("Turning right..."); break; case "STRAIGHT": System.out.println("Continuing straight..."); break; } } public void controlThrottle() { if (currentSpeed < targetSpeed && (targetSpeed - currentSpeed) > 6) { //normal acceleration currentSpeed = currentSpeed + 6; } else if (currentSpeed < targetSpeed && (targetSpeed - currentSpeed) <= 6) { //partial acceleration to reach speed limit currentSpeed = currentSpeed + (targetSpeed - currentSpeed); } else if (currentSpeed > targetSpeed) { //deceleration by coasting currentSpeed--; } else if (currentSpeed == targetSpeed) { //do nothing } } public void applyBrakes() { System.out.println("Applying brakes"); if (currentSpeed >= 10) {currentSpeed -= 10;} else if (currentSpeed < 10) {currentSpeed = 0;} } }