public class Vehicle implements Runnable{ private Integer currentSpeed; private Integer targetSpeed; private double fuelLevel; //percentage of full tank private double fuelEconomy = 0.03125; //gallons consumed per mile of travel (32 MPG) private double tripOdometer; //miles traveled in current trip private double distanceRemaining; private String currentLocation; public Route currentRoute = new Route(); public void run() { navigateRoute(); } public Vehicle() { fuelLevel = 1; //12 gallon tank as full 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() { updateFuelLevel(); return fuelLevel; } public void updateFuelLevel() { //would check fuel sensor and update accordingly fuelLevel = (12 - (tripOdometer * fuelEconomy))/12; } public void navigateRoute() { int isRoadWet = RoadSensor.roadQuality("WET"); //check whether road is wet boolean turnCompleted = false; //flag to validate completion of moveset instruction //at start of trip, check for whether road is wet if (isRoadWet == 1) {System.out.println("Road is wet, trip will have reduced speed for safety.");} for (String[] el : currentRoute.getSelectedRouteSteps()) { turnCompleted = false; distanceRemaining = Double.parseDouble(el[1]); targetSpeed = Integer.parseInt(el[2]); currentLocation = el[4]; int isRoadUneven = RoadSensor.roadQuality("SURFACE"); //checks for presence of traffic light before completing next moveset to simulate cross-traffic if (targetSpeed > 20 && RoadSensor.detectTrafficLight() && el[0] != "STRAIGHT") { vehicleHalt("Traffic light"); } //checks whether road surface is degraded on current road to drive more cautiously if (isRoadUneven == 1) {System.out.println("Road surface is uneven, speed will be reduced.");} while (!turnCompleted) { //move action routines to transition between movesets if (currentSpeed <= 15 || el[0] == "STRAIGHT") { changeDirection(el[0]); turnCompleted = true; } else { applyBrakes(); } } while (distanceRemaining > 0) { //forward motion routines to drive current road segment if (RoadSensor.identifyHazard()) { vehicleHalt("Hazard"); } else { displayInformation(); //displays current route and state information to console controlThrottle(isRoadWet + isRoadUneven); tripOdometer += Double.valueOf(currentSpeed)/3600; distanceRemaining = distanceRemaining - (Double.valueOf(currentSpeed)/3600); //travel distance in one second HelperFunctions.sleep(1); //sleep 1 second to simulate continuous movement } } } } public void changeDirection(String direction) { switch (direction) { case "LEFT": System.out.println("Turning left...\n"); break; case "RIGHT": System.out.println("Turning right...\n"); break; case "STRAIGHT": System.out.println("Continuing straight...\n"); break; } } public void controlThrottle(int unsafeCondition) { int safeTargetSpeed = targetSpeed; //adjust driving speed for road conditions. 5 MPH reduction per unsafe condition detected if (targetSpeed > 20) {safeTargetSpeed = targetSpeed - (5 * unsafeCondition);} if (currentSpeed < safeTargetSpeed && (safeTargetSpeed - currentSpeed) > 6) { //normal acceleration currentSpeed = currentSpeed + 6; } else if (currentSpeed < safeTargetSpeed && (safeTargetSpeed - currentSpeed) <= 6) { //partial acceleration to reach speed limit currentSpeed = currentSpeed + (safeTargetSpeed - currentSpeed); } else if (currentSpeed > safeTargetSpeed) { //deceleration by coasting currentSpeed--; } else if (currentSpeed == safeTargetSpeed) { //do nothing } } public void applyBrakes() { System.out.println("Applying brakes"); if (currentSpeed >= 10) {currentSpeed -= 10;} else if (currentSpeed < 10) {currentSpeed = 0;} } public void vehicleHalt(String type) { //helper function to simulate a environmentally-driven vehicle stop long sleepTime = Double.valueOf(Math.floor((Math.random()*10)+5)).longValue(); //wait time, between 5 and 15 seconds while (sleepTime > 0) { System.out.println(type + " detected, stopping (" + sleepTime + ")..."); while (currentSpeed > 0) {applyBrakes();} HelperFunctions.sleep(1); //sleep 1 second to simulate time for obstruction to clear sleepTime--; } System.out.println(type + " cleared, resuming navigation\n"); } public void displayInformation() { //System.out.print("\033[H\033[2J"); System.out.printf("%.2f miles to next instruction\n", distanceRemaining); System.out.println("Current Location: " + currentLocation); System.out.println("Current Speed: " + currentSpeed + " MPH\n"); } }