diff --git a/src/Location.java b/src/Location.java index a65dfda..94062ee 100644 --- a/src/Location.java +++ b/src/Location.java @@ -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() { @@ -54,7 +59,12 @@ public class Location { this.latitude = lat; 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, diff --git a/src/Main.java b/src/Main.java index f3a4a3b..1d2b204 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,18 +1,21 @@ -import java.sql.Date; -import java.util.ArrayList; -import java.util.Arrays; -import java.util.List; -import java.util.Scanner; -import org.joda.*; -import org.joda.time.LocalDate; - -//TODO: save Person as JSON -//TODO: save Persons as list (databse) -public class Main { - - - public static void main(String[] args) { - Questions.run(); - - } -} +import java.sql.Date; +import java.util.ArrayList; +import java.util.Arrays; +import java.util.List; +import java.util.Scanner; +import org.joda.*; +import org.joda.time.LocalDate; + +//TODO: save Person as JSON +//TODO: save Persons as list (databse) +public class Main { + + /** + * Main Entry point of Application. + * @param args + */ + public static void main(String[] args) { + Questions.run(); + + } +} diff --git a/src/Questions.java b/src/Questions.java index 502db3d..2765db6 100644 --- a/src/Questions.java +++ b/src/Questions.java @@ -5,35 +5,35 @@ 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 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 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"); ex.printStackTrace(); } } - + private static String askForName(Scanner in) { String name = null; System.out.print("Name: "); @@ -43,9 +43,9 @@ public class Questions { catch (Exception e) { e.printStackTrace(); } - return name; + return name; } - + /** * Standered print of Person's information * @param Person Object. @@ -55,21 +55,24 @@ 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()); } } - + /** * Finds the correct enum value of the parameter. * @param string naming the account type @@ -86,7 +89,7 @@ public class Questions { default: return AccountType.Checking; } - + } - + }