CEN-3024-Code-Implementation/Route.java

101 lines
4.2 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 = new Destination();
public Route() {
this.routeSequences = new Vector<Vector<String[]>>();
this.timeEstimate = new Vector<Double>();
this.tollsAmount = new Vector<Double>();
this.routeSelected = -1;
}
public Route(String destination) {
setRouteDestination(destination);
this.timeEstimate = new Vector<Double>();
this.tollsAmount = new Vector<Double>();
}
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
this.routeSequences = new Vector<Vector<String[]>>();
updateCurrentLocation();
HelperFunctions apiCallToRouting = new HelperFunctions();
//route option 1
tollsFlag = false;
hwyFlag = false;
routeSequences.add(apiCallToRouting.routingAPI(currentLocation, currentDestination.getDestinationAddress(), tollsFlag, hwyFlag));
timeEstimate.add(estimateTime(routeSequences.get(0)));
tollsAmount.add(estimateTolls(routeSequences.get(0)));
//route option 2
tollsFlag = false;
hwyFlag = true;
routeSequences.add(apiCallToRouting.routingAPI(currentLocation, currentDestination.getDestinationAddress(), tollsFlag, hwyFlag));
timeEstimate.add(estimateTime(routeSequences.get(1)));
tollsAmount.add(estimateTolls(routeSequences.get(1)));
}
public String[] enumRoutes() {
//for testing, values are hardcoded. Would generate headings based on route characteristics ideally
String[] routeNames = {"1. via I-95 SOUTH\n\tNo Tolls\n\tUses Highways", "2. via US-1 and Babcock St.\n\tNo Tolls\n\tAvoids Highways"};
return routeNames;
}
public int countRoutesAvailable() {
return routeSequences.size();
}
public void selectRoute(int userSelection) {
routeSelected = userSelection;
}
public Vector<String[]> getSelectedRouteSteps() {
return routeSequences.get(routeSelected);
}
public double estimateTolls(Vector<String[]> route) {
double 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 setRouteDestination(String destination) {
currentDestination.setDestinationAddress(destination);
calculateRoutes();
}
public void setHwyFlag(boolean flag) {hwyFlag = flag;}
public void setTollsFlag(boolean flag) {tollsFlag = flag;}
public boolean getHwyFlag() {return hwyFlag;}
public boolean getTollsFlag() {return tollsFlag;}
public int getRouteSelected() {return routeSelected;}
}