Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 54 additions & 0 deletions src/CoordinatesFunctions.java
Original file line number Diff line number Diff line change
@@ -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;
}
}
3 changes: 2 additions & 1 deletion src/app/Main.java
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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);
}
Expand Down
55 changes: 55 additions & 0 deletions src/data_access/API/GOStationCoordinatesApiClass.java
Original file line number Diff line number Diff line change
@@ -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<String> 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<String> 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);
}

}
}
5 changes: 4 additions & 1 deletion src/data_access/text_file/FileStationDataAccessObject.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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
Expand Down
12 changes: 12 additions & 0 deletions src/view/ShowIncomingVehiclesView.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand Down