diff --git a/src/CoordinatesFunctions.java b/src/CoordinatesFunctions.java new file mode 100644 index 0000000..6e67267 --- /dev/null +++ b/src/CoordinatesFunctions.java @@ -0,0 +1,54 @@ +// Java program for the haversine formula +public class CoordinatesFunctions { + + // private final double GOTRAINSPEED = 38.88; // 140(km/hr) = 38.88(m/s) + + /** + * Takes the Longitude and Latitude of two points and solves for distance. + * + * @param lat1 + * @param lon1 + * @param lat2 + * @param lon2 + * @return distance + */ + static double haversineDistance(double lat1, double lon1, + double lat2, double lon2) + { + /** + * The distance between latitudes and longitudes + */ + double distanceLatitude = Math.toRadians(lat2 - lat1); + double distanceLongitude = Math.toRadians(lon2 - lon1); + + /** + * Convert to Radians + */ + lat1 = Math.toRadians(lat1); + lat2 = Math.toRadians(lat2); + + /** + * Using Haversine to compute distance between the two + */ + double a = Math.pow(Math.sin(distanceLatitude / 2), 2) + + Math.pow(Math.sin(distanceLongitude / 2), 2) * + Math.cos(lat1) * + Math.cos(lat2); + double rad = 6371; + double c = 2 * Math.asin(Math.sqrt(a)); + + return rad * c; // Distance between the two + } + + /** + * Calculates the time using speed and distance. + * + * @param distance + * @return time + */ + static double time(double distance) { // double speed may be changed + final double GOTRAINSPEED = 38.88; + + return distance / GOTRAINSPEED; + } +} diff --git a/src/app/Main.java b/src/app/Main.java index 22368d5..dfeb236 100644 --- a/src/app/Main.java +++ b/src/app/Main.java @@ -1,5 +1,6 @@ package app; +import data_access.API.GOStationCoordinatesApiClass; import data_access.API.GOVehicleApiClass; import data_access.text_file.FileStationDataAccessObject; import data_access.API.GOStationApiClass; @@ -56,7 +57,7 @@ public static void main(String[] args) { FileStationDataAccessObject stationDataAccessObject; try { stationDataAccessObject = new FileStationDataAccessObject("./revisedStopData.txt", new StationFactory(), - new TrainFactory(), new GOStationApiClass(), new GOVehicleApiClass()); + new TrainFactory(), new GOStationApiClass(), new GOVehicleApiClass(), new GOStationCoordinatesApiClass()); } catch (IOException e) { throw new RuntimeException(e); } diff --git a/src/data_access/API/GOStationCoordinatesApiClass.java b/src/data_access/API/GOStationCoordinatesApiClass.java new file mode 100644 index 0000000..875088a --- /dev/null +++ b/src/data_access/API/GOStationCoordinatesApiClass.java @@ -0,0 +1,55 @@ +package data_access.API; + +import okhttp3.HttpUrl; +import okhttp3.OkHttpClient; +import okhttp3.Request; +import okhttp3.Response; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +public class GOStationCoordinatesApiClass implements TrainApiInterface { + private final String PARTIAL_API_URL = "OpenDataAPI/api/V1"; + public GOStationCoordinatesApiClass () { + } + public List retrieveStationCoordinates(String stationId){ + OkHttpClient client = new OkHttpClient().newBuilder() + .build(); + HttpUrl httpUrl = new HttpUrl.Builder() + .scheme("http") + .host("api.openmetrolinx.com") + .addPathSegment(PARTIAL_API_URL) + .addPathSegment("Stop") + .addPathSegment("Details") + .addPathSegment(stationId) //getting station information for the station with the id denoted by stationId + .addQueryParameter("key", API_KEY) + .build(); + + Request request = new Request.Builder() + .url(httpUrl) + .addHeader("content-type", "application/json") + .build(); + try { + Response response = client.newCall(request).execute(); + String trainInfoJsonData = response.body().string(); + JSONObject metaDataJsonObj = new JSONObject(trainInfoJsonData); // This is where the MetaData is located at + + JSONObject stopJsonObj = metaDataJsonObj.getJSONObject("Stop"); + List coordinatesList = new ArrayList<>(); + JSONObject longitude = stopJsonObj.getJSONObject("Longitude"); + JSONObject latitude = stopJsonObj.getJSONObject("Latitude"); + coordinatesList.add(longitude.getString("Longitude")); + coordinatesList.add(latitude.getString("Latitude")); + + return coordinatesList; + + } catch (IOException | JSONException e) { + throw new RuntimeException(e); + } + + } +} diff --git a/src/data_access/text_file/FileStationDataAccessObject.java b/src/data_access/text_file/FileStationDataAccessObject.java index f5027cf..49f865b 100644 --- a/src/data_access/text_file/FileStationDataAccessObject.java +++ b/src/data_access/text_file/FileStationDataAccessObject.java @@ -1,6 +1,7 @@ package data_access.text_file; import data_access.API.GOStationApiClass; +import data_access.API.GOStationCoordinatesApiClass; import data_access.API.GOVehicleApiClass; import entity.*; import use_case.StationInfo.StationInfoDataAccessInterface; @@ -21,14 +22,16 @@ public class FileStationDataAccessObject implements SearchDataAccessInterface, S private final GOStationApiClass goStationApiClass; private final GOVehicleApiClass goVehicleApiClass; + private final GOStationCoordinatesApiClass goStationCoordinatesApiClass; public FileStationDataAccessObject(String txtFilePath, StationFactory stationFactory, TrainFactory trainFactory, - GOStationApiClass goStationApiClass, GOVehicleApiClass goVehicleApiClass) throws IOException { + GOStationApiClass goStationApiClass, GOVehicleApiClass goVehicleApiClass, GOStationCoordinatesApiClass goStationCoordinatesApiClass) throws IOException { this.stationFactory = stationFactory; this.trainFactory = trainFactory; this.goStationApiClass = goStationApiClass; this.goVehicleApiClass = goVehicleApiClass; + this.goStationCoordinatesApiClass = goStationCoordinatesApiClass; stationTxtFile = new File(txtFilePath); // Reading the provided txt file that has a path specified by attribute txtFilePath diff --git a/src/view/ShowIncomingVehiclesView.java b/src/view/ShowIncomingVehiclesView.java index e08b1a9..34b6248 100644 --- a/src/view/ShowIncomingVehiclesView.java +++ b/src/view/ShowIncomingVehiclesView.java @@ -18,6 +18,8 @@ public class ShowIncomingVehiclesView extends JPanel implements ActionListener, private final ShowIncomingVehiclesViewModel showIncomingVehiclesViewModel; JLabel stationName; JLabel stationIncomingVehicles; + JLabel stationEstimatedTimeOfArrival; + JLabel stationTimeOfArrival; public ShowIncomingVehiclesView(ShowIncomingVehiclesViewModel showIncomingVehiclesViewModel) { this.showIncomingVehiclesViewModel = showIncomingVehiclesViewModel; @@ -32,12 +34,22 @@ public ShowIncomingVehiclesView(ShowIncomingVehiclesViewModel showIncomingVehicl JLabel stationIncomingVehiclesLabel = new JLabel("Incoming Vehicles: "); stationIncomingVehicles = new JLabel(); + JLabel stationEstimatedTimeOfArrivalLabel = new JLabel("Estimated Time of Arrival: "); + stationEstimatedTimeOfArrival = new JLabel(); + + JLabel stationTimeOfArrivalLabel = new JLabel("Time of Arrival: "); + stationTimeOfArrival = new JLabel(); + this.setLayout(new BoxLayout(this, BoxLayout.Y_AXIS)); this.add(title); this.add(stationNameLabel); this.add(stationName); this.add(stationIncomingVehiclesLabel); this.add(stationIncomingVehicles); + this.add(stationEstimatedTimeOfArrivalLabel); + this.add(stationEstimatedTimeOfArrival); + this.add(stationTimeOfArrivalLabel); + this.add(stationTimeOfArrival); } @Override