New Automatic Location class constructor

This commit is contained in:
hesham 2019-02-27 04:39:13 +03:00
parent 223140f752
commit 8a37f15a13
3 changed files with 124 additions and 63 deletions

View File

@ -16,18 +16,23 @@ public class Location {
private double latitude; private double latitude;
private double longitude; private double longitude;
private String city; 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; latitude = lat;
longitude = lon; longitude = lon;
city = "Unknown"; this.city = city;
//city = cityBasedOnIP(); //API intansive
} }
/**
* Initialize with automatic online info from (ip-api.com).
*/
public Location() { public Location() {
//city = cityBasedOnIP(); jsonInfo = apiInformationObject();
city = "Unknown"; 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() { public double getLatitude() {
@ -54,7 +59,12 @@ public class Location {
this.latitude = lat; this.latitude = lat;
this.longitude = lon; this.longitude = lon;
} }
/**
* @deprecated
* @return
*/
public String cityBasedOnIP() { public String cityBasedOnIP() {
String city = ""; String city = "";
try { try {
@ -81,15 +91,12 @@ public class Location {
for (Object object : array) { for (Object object : array) {
JSONObject obj =(JSONObject)object; JSONObject obj =(JSONObject)object;
city = (String)obj.get(API_Keys.city.name()); city = (String)obj.get(API_Keys.city.name());
lastInfoString = (String)obj.toString();
} }
} }
else if (resultObject instanceof JSONObject) { else if (resultObject instanceof JSONObject) {
JSONObject obj =(JSONObject)resultObject; JSONObject obj =(JSONObject)resultObject;
city = (String)obj.get(API_Keys.city.name()); city = (String)obj.get(API_Keys.city.name());
lastInfoString = (String)obj.toString();
} }
} }
catch (MalformedURLException e) { catch (MalformedURLException e) {
@ -107,6 +114,54 @@ public class Location {
return city; 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 { private enum API_Keys {
zip, zip,

View File

@ -1,18 +1,21 @@
import java.sql.Date; import java.sql.Date;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.List; import java.util.List;
import java.util.Scanner; import java.util.Scanner;
import org.joda.*; import org.joda.*;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
//TODO: save Person as JSON //TODO: save Person as JSON
//TODO: save Persons as list (databse) //TODO: save Persons as list (databse)
public class Main { public class Main {
/**
public static void main(String[] args) { * Main Entry point of Application.
Questions.run(); * @param args
*/
} public static void main(String[] args) {
} Questions.run();
}
}

View File

@ -5,35 +5,35 @@ import java.util.Scanner;
import org.joda.time.LocalDate; import org.joda.time.LocalDate;
public class Questions { class Questions {
public static void run(){ public static void run(){
try { try {
Person p = new Person(12345, "Hesham", Level.BA); Person p = new Person(12345, "Hesham", Level.BA);
System.out.println("-------------------------------------------------"); System.out.println("-------------------------------------------------");
Scanner in = new Scanner(System.in); Scanner in = new Scanner(System.in);
p.getAccount().setName(askForName(in)); p.getAccount().setName(askForName(in));
System.out.print("ID: "); System.out.print("ID: ");
p.getAccount().setID(in.nextInt()); p.getAccount().setID(in.nextInt());
System.out.print("Account Type (Cheking, Investing, Saving): "); System.out.print("Account Type (Cheking, Investing, Saving): ");
p.getAccount().setAccountType(parseType(in.next())); p.getAccount().setAccountType(parseType(in.next()));
System.out.print("School: "); System.out.print("School: ");
p.getEdu().setSchool(in.next()); p.getEdu().setSchool(in.next());
System.out.print("Major: "); System.out.print("Major: ");
p.getEdu().setMajor(in.next()); p.getEdu().setMajor(in.next());
in.close(); in.close();
List<Course> list = Arrays.asList(Course.EE201, Course.EE250,Course.ISLS201); List<Course> list = Arrays.asList(Course.EE201, Course.EE250,Course.ISLS201);
p.getEdu().addCourses(list); p.getEdu().addCourses(list);
Print(p); Print(p);
} }
catch (Exception ex) { catch (Exception ex) {
System.out.print(" Error in input. \n"); System.out.print(" Error in input. \n");
ex.printStackTrace(); ex.printStackTrace();
} }
} }
private static String askForName(Scanner in) { private static String askForName(Scanner in) {
String name = null; String name = null;
System.out.print("Name: "); System.out.print("Name: ");
@ -43,9 +43,9 @@ public class Questions {
catch (Exception e) { catch (Exception e) {
e.printStackTrace(); e.printStackTrace();
} }
return name; return name;
} }
/** /**
* Standered print of Person's information * Standered print of Person's information
* @param Person Object. * @param Person Object.
@ -55,21 +55,24 @@ public class Questions {
LocalDate date = new LocalDate(); LocalDate date = new LocalDate();
System.out.println( System.out.println(
"\n\n\n\t---------INFO---------\n" "\n\n\n\t---------INFO---------\n"
+ "\t --- "+ date.toString()+" ---\n" + "\t --- "+ date.toString()+" ---\n"
+ "\tName: "+p.getAccount().getName()+"\n" + "\tName: "+p.getAccount().getName()+"\n"
+ "\tID: "+p.getAccount().getID()+"\n" + "\tID: "+p.getAccount().getID()+"\n"
+ "\tMajor: "+p.getEdu().getMajor()+"\n" + "\tMajor: "+p.getEdu().getMajor()+"\n"
+ "\tSchool: "+p.getEdu().getSchool()+"\n" + "\tSchool: "+p.getEdu().getSchool()+"\n"
+ "\tCity: "+p.getLocation().cityBasedOnIP()+"\n" + "\tCity: "+p.getLocation().getCity()+"\n"
+ "\tAccountType: "+p.getAccount().getAccountType()+"\n" + "\tLat: " + p.getLocation().getLatitude()+"\n"
+ "\t---------COURSES---------"); + "\tLon: " + p.getLocation().getLongitude()+"\n"
+ "\tAccountType: "+p.getAccount().getAccountType()+"\n"
+ "\t---------COURSES---------");
int i = 1; int i = 1;
for (Course course : p.getEdu().getCourses()) { for (Course course : p.getEdu().getCourses()) {
System.out.println("\tCourse "+i+": "+course.name()); System.out.println("\tCourse "+i+": "+course.name());
} }
} }
/** /**
* Finds the correct enum value of the parameter. * Finds the correct enum value of the parameter.
* @param string naming the account type * @param string naming the account type
@ -86,7 +89,7 @@ public class Questions {
default: default:
return AccountType.Checking; return AccountType.Checking;
} }
} }
} }