Last active
December 2, 2015 18:02
-
-
Save LLin233/e74e65215a6268547adc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* Build a flight schedule where we can identify a closest airport for a plane to land. | |
* A plane is in the air. The pilot uses this program to search for the closest airport in the area where it can land. The airport should * be able to accomodate the size of the plane. For example, if the plane size is 737, the airport must be a large one. | |
*/ | |
Class Airport { | |
double latitude; | |
double longitude; | |
boolean isAvailable; | |
//getters() | |
//setters() | |
} | |
public class Utils { | |
/** | |
* @method return distance of 2 Geo Locations | |
* */ | |
private double EARTH_RADIUS = 6378.137; | |
private static double rad(double d) | |
{ | |
return d * Math.PI / 180.0; | |
} | |
public static double getDistance(double lat1, double lng1, double lat2, double lng2) | |
{ | |
double radLat1 = rad(lat1); | |
double radLat2 = rad(lat2); | |
double a = radLat1 - radLat2; | |
double b = rad(lng1) - rad(lng2); | |
double s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) + | |
Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2))); | |
s = s * EARTH_RADIUS; | |
s = Math.round(s * 100) / 100.0; | |
return s; | |
} | |
} | |
public class GeoComparator implements Comparator<Airport> { | |
private double currentLatitude; | |
private double currentLongitude; | |
public GeoComparator(double curLat, double curLng) { | |
this.currentLatitude = curLat; | |
this.currentLongitude = curLng; | |
} | |
@Override | |
public int compare(Airport airport1, Airport airport2) { | |
double distance1 = Utils.getDistance(currentLatitude, currentLongitude, airport1.getLatitude(), airport2.getLongitude()); | |
double distance2 = Utils.getDistance(currentLatitude, currentLongitude, airport2.getLatitude(), airport2.getLongitude()); | |
if(distance1 > distance2){ | |
return 1; | |
}else if(distance1 < distance2){ | |
return -1; | |
}else{ | |
return 0; | |
} | |
} | |
} | |
public Airport getCloestAvailableAirport(Plane plane) { | |
List<Airport> list = getListFromGoogle(plane.getCurrentLatitude(), plane.getcurrentLongitude()); //dummy method, get airport list with gro location from google places api, JSON, convert them to model Airport | |
Collections.sort(list,new GeoComparator(plane.getCurrentLatitude(), plane.getcurrentLongitude())); | |
for (Airport airport : list) { | |
if (airport.isAvailable) { | |
return airport; | |
} | |
} | |
throw new IllegalArgumentException("No Available Airport at this time"); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment