New Automatic Location class constructor
This commit is contained in:
parent
223140f752
commit
8a37f15a13
@ -16,18 +16,23 @@ public class Location {
|
||||
private double latitude;
|
||||
private double longitude;
|
||||
private String city;
|
||||
private String lastInfoString;
|
||||
private JSONObject jsonInfo;
|
||||
|
||||
public Location(double lat, double lon) {
|
||||
public Location(double lat, double lon, String city) {
|
||||
latitude = lat;
|
||||
longitude = lon;
|
||||
city = "Unknown";
|
||||
//city = cityBasedOnIP(); //API intansive
|
||||
this.city = city;
|
||||
}
|
||||
|
||||
/**
|
||||
* Initialize with automatic online info from (ip-api.com).
|
||||
*/
|
||||
public Location() {
|
||||
//city = cityBasedOnIP();
|
||||
city = "Unknown";
|
||||
jsonInfo = apiInformationObject();
|
||||
this.city = jsonInfo.get(API_Keys.city.name()).toString();
|
||||
this.latitude = (double)jsonInfo.get(API_Keys.lat.name());
|
||||
this.longitude = (double)jsonInfo.get(API_Keys.lon.name());
|
||||
|
||||
}
|
||||
|
||||
public double getLatitude() {
|
||||
@ -55,6 +60,11 @@ public class Location {
|
||||
this.longitude = lon;
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @deprecated
|
||||
* @return
|
||||
*/
|
||||
public String cityBasedOnIP() {
|
||||
String city = "";
|
||||
try {
|
||||
@ -81,15 +91,12 @@ public class Location {
|
||||
for (Object object : array) {
|
||||
JSONObject obj =(JSONObject)object;
|
||||
city = (String)obj.get(API_Keys.city.name());
|
||||
lastInfoString = (String)obj.toString();
|
||||
}
|
||||
|
||||
}
|
||||
else if (resultObject instanceof JSONObject) {
|
||||
JSONObject obj =(JSONObject)resultObject;
|
||||
city = (String)obj.get(API_Keys.city.name());
|
||||
lastInfoString = (String)obj.toString();
|
||||
|
||||
}
|
||||
}
|
||||
catch (MalformedURLException e) {
|
||||
@ -107,6 +114,54 @@ public class Location {
|
||||
return city;
|
||||
}
|
||||
|
||||
private JSONObject apiInformationObject(){
|
||||
JSONObject jsonObj = null;
|
||||
try {
|
||||
|
||||
URL url = new URL("http://ip-api.com/json");
|
||||
HttpURLConnection con = (HttpURLConnection) url.openConnection();
|
||||
con.setRequestMethod("GET");
|
||||
con.setRequestProperty("Content-Type", "application/json");
|
||||
int status = con.getResponseCode();
|
||||
BufferedReader in = new BufferedReader(
|
||||
new InputStreamReader(con.getInputStream()));
|
||||
String inputLine;
|
||||
StringBuffer content = new StringBuffer();
|
||||
while ((inputLine = in.readLine()) != null) {
|
||||
content.append(inputLine);
|
||||
}
|
||||
in.close();
|
||||
|
||||
JSONParser parser = new JSONParser();
|
||||
Object resultObject = parser.parse(content.toString());
|
||||
|
||||
if (resultObject instanceof JSONArray) {
|
||||
JSONArray array=(JSONArray)resultObject;
|
||||
for (Object object : array) {
|
||||
jsonObj =(JSONObject)object;
|
||||
}
|
||||
|
||||
}
|
||||
else if (resultObject instanceof JSONObject) {
|
||||
jsonObj =(JSONObject)resultObject;
|
||||
}
|
||||
}
|
||||
catch (MalformedURLException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch (ProtocolException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
catch (ParseException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
||||
return jsonObj;
|
||||
}
|
||||
|
||||
|
||||
private enum API_Keys {
|
||||
zip,
|
||||
|
@ -10,7 +10,10 @@ import org.joda.time.LocalDate;
|
||||
//TODO: save Persons as list (databse)
|
||||
public class Main {
|
||||
|
||||
|
||||
/**
|
||||
* Main Entry point of Application.
|
||||
* @param args
|
||||
*/
|
||||
public static void main(String[] args) {
|
||||
Questions.run();
|
||||
|
||||
|
@ -5,28 +5,28 @@ import java.util.Scanner;
|
||||
|
||||
import org.joda.time.LocalDate;
|
||||
|
||||
public class Questions {
|
||||
class Questions {
|
||||
|
||||
|
||||
public static void run(){
|
||||
|
||||
try {
|
||||
Person p = new Person(12345, "Hesham", Level.BA);
|
||||
System.out.println("-------------------------------------------------");
|
||||
Scanner in = new Scanner(System.in);
|
||||
p.getAccount().setName(askForName(in));
|
||||
System.out.print("ID: ");
|
||||
p.getAccount().setID(in.nextInt());
|
||||
System.out.print("Account Type (Cheking, Investing, Saving): ");
|
||||
p.getAccount().setAccountType(parseType(in.next()));
|
||||
System.out.print("School: ");
|
||||
p.getEdu().setSchool(in.next());
|
||||
System.out.print("Major: ");
|
||||
p.getEdu().setMajor(in.next());
|
||||
in.close();
|
||||
List<Course> list = Arrays.asList(Course.EE201, Course.EE250,Course.ISLS201);
|
||||
p.getEdu().addCourses(list);
|
||||
Print(p);
|
||||
Person p = new Person(12345, "Hesham", Level.BA);
|
||||
System.out.println("-------------------------------------------------");
|
||||
Scanner in = new Scanner(System.in);
|
||||
p.getAccount().setName(askForName(in));
|
||||
System.out.print("ID: ");
|
||||
p.getAccount().setID(in.nextInt());
|
||||
System.out.print("Account Type (Cheking, Investing, Saving): ");
|
||||
p.getAccount().setAccountType(parseType(in.next()));
|
||||
System.out.print("School: ");
|
||||
p.getEdu().setSchool(in.next());
|
||||
System.out.print("Major: ");
|
||||
p.getEdu().setMajor(in.next());
|
||||
in.close();
|
||||
List<Course> list = Arrays.asList(Course.EE201, Course.EE250,Course.ISLS201);
|
||||
p.getEdu().addCourses(list);
|
||||
Print(p);
|
||||
}
|
||||
catch (Exception ex) {
|
||||
System.out.print(" Error in input. \n");
|
||||
@ -55,15 +55,18 @@ public class Questions {
|
||||
LocalDate date = new LocalDate();
|
||||
|
||||
System.out.println(
|
||||
"\n\n\n\t---------INFO---------\n"
|
||||
+ "\t --- "+ date.toString()+" ---\n"
|
||||
+ "\tName: "+p.getAccount().getName()+"\n"
|
||||
+ "\tID: "+p.getAccount().getID()+"\n"
|
||||
+ "\tMajor: "+p.getEdu().getMajor()+"\n"
|
||||
+ "\tSchool: "+p.getEdu().getSchool()+"\n"
|
||||
+ "\tCity: "+p.getLocation().cityBasedOnIP()+"\n"
|
||||
+ "\tAccountType: "+p.getAccount().getAccountType()+"\n"
|
||||
+ "\t---------COURSES---------");
|
||||
"\n\n\n\t---------INFO---------\n"
|
||||
+ "\t --- "+ date.toString()+" ---\n"
|
||||
+ "\tName: "+p.getAccount().getName()+"\n"
|
||||
+ "\tID: "+p.getAccount().getID()+"\n"
|
||||
+ "\tMajor: "+p.getEdu().getMajor()+"\n"
|
||||
+ "\tSchool: "+p.getEdu().getSchool()+"\n"
|
||||
+ "\tCity: "+p.getLocation().getCity()+"\n"
|
||||
+ "\tLat: " + p.getLocation().getLatitude()+"\n"
|
||||
+ "\tLon: " + p.getLocation().getLongitude()+"\n"
|
||||
|
||||
+ "\tAccountType: "+p.getAccount().getAccountType()+"\n"
|
||||
+ "\t---------COURSES---------");
|
||||
int i = 1;
|
||||
for (Course course : p.getEdu().getCourses()) {
|
||||
System.out.println("\tCourse "+i+": "+course.name());
|
||||
|
Loading…
Reference in New Issue
Block a user