CEN-3024-Code-Implementation/Route.java

73 lines
3.4 KiB
Java

import java.util.Vector;
public class Route {
private double[] currentLocation; //Lat-Long coordinates for current location
private Vector<Vector<String[]>> routeSequences; //3D array for routes. 1st dim selects route, 2nd dim selects route point, 3rd dim selects lat/long/speed/tolls (indices 0-2)
private int routeSelected;
private boolean tollsFlag; //stores user setting for whether to avoid tolls
private boolean hwyFlag; //stores user preference for whether to avoid major highways
public Vector<Double> timeEstimate; //time estimate in seconds
public Vector<Double> tollsAmount; //stores toll amount if toll roads are used
public Destination currentDestination; //uses user-created class Destination
private void updateCurrentLocation() {
//execute api call out to GPS service to obtain current location
//current location will be hardcoded for testing
double[] tmpLoc = {28.1700863880887, -80.67088403224037};
currentLocation = tmpLoc;
}
private void calculateRoutes() {
//execute api call out to routing service to get route info
//ideally a query would go out using multiple pathfiding algorithms to get route options
//here the same call is made for demo purposes
updateCurrentLocation();
HelperFunctions apiCallToRouting = new HelperFunctions();
//route option 1
routeSequences.add(apiCallToRouting.routingAPI(currentLocation, currentDestination.getDestinationCoordinates(), tollsFlag, hwyFlag));
timeEstimate.add(estimateTime(routeSequences.get(0)));
tollsAmount.add(estimateTolls(routeSequences.get(0)));
//route option 2
routeSequences.add(apiCallToRouting.routingAPI(currentLocation, currentDestination.getDestinationCoordinates(), tollsFlag, hwyFlag));
timeEstimate.add(estimateTime(routeSequences.get(1)));
tollsAmount.add(estimateTolls(routeSequences.get(1)));
//route option 3
routeSequences.add(apiCallToRouting.routingAPI(currentLocation, currentDestination.getDestinationCoordinates(), tollsFlag, hwyFlag));
timeEstimate.add(estimateTime(routeSequences.get(2)));
tollsAmount.add(estimateTolls(routeSequences.get(2)));
}
public int enumRoutes() {
return routeSequences.size();
}
public void selectRoute(int userSelection) {
routeSelected = userSelection;
}
public int getRouteSelected() {return routeSelected;}
public float estimateTolls(Vector<String[]> route) {
float runningTolls = 0;
for (String[] el : route) {
runningTolls += Double.parseDouble(el[3]);
}
return runningTolls;
}
public double estimateTime(Vector<String[]> route) {
//calculates distances between route segments, divided by speed in MPH
//the API would return expected speed due to traffic conditions as part of the speed (index 2) in the route array
double runningTime = 0;
//steps through to get distances, calculates time based on speed estimate, and accumulates on runningTime
for (String[] el : route) {
runningTime += Double.parseDouble(el[1])/Double.parseDouble(el[2]);
}
return runningTime;
}
public void setHwyFlag(boolean flag) {hwyFlag = flag;}
public void setTollsFlag(boolean flag) {tollsFlag = flag;}
public boolean getHwyFlag() {return hwyFlag;}
public boolean getTollsFlag() {return tollsFlag;}
}