Compare commits

...

8 Commits
master ... dev

Author SHA1 Message Date
28dbdbf406 getCity String in Person class 2019-04-24 17:40:16 +03:00
02a82cc89d JavaDoc & Clean 2019-04-24 17:39:25 +03:00
f73a8ba394 JetBrians annotations 2019-04-24 17:37:04 +03:00
dc698c3f88 Format 2019-02-27 04:46:21 +03:00
8a37f15a13 New Automatic Location class constructor 2019-02-27 04:39:13 +03:00
223140f752 Debug Class 2019-02-27 04:34:35 +03:00
840dbecdea api keys 2019-02-12 03:46:50 +03:00
7b6b15d1d2 moved main functions 2019-02-02 12:39:15 +03:00
8 changed files with 332 additions and 185 deletions

1
.gitignore vendored
View File

@ -10,3 +10,4 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/target/

View File

@ -0,0 +1,13 @@
<component name="libraryTable">
<library name="Maven: org.jetbrains:annotations:17.0.0">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/17.0.0/annotations-17.0.0.jar!/" />
</CLASSES>
<JAVADOC>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/17.0.0/annotations-17.0.0-javadoc.jar!/" />
</JAVADOC>
<SOURCES>
<root url="jar://$MAVEN_REPOSITORY$/org/jetbrains/annotations/17.0.0/annotations-17.0.0-sources.jar!/" />
</SOURCES>
</library>
</component>

View File

@ -28,6 +28,12 @@
<artifactId>joda-time</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project>

8
src/Debug.java Normal file
View File

@ -0,0 +1,8 @@
public class Debug {
private static final String preFix = "[ Debug ] ";
public static void print(String msg){
System.out.print(preFix + msg);
}
}

View File

@ -13,20 +13,33 @@ import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
public class Location {
private double latitude;
private double longitude;
private String city;
private JSONObject jsonInfo;
public Location(double lat, double lon) {
/**
* Manual location constructor
* @param lat
* @param lon
* @param city
*/
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,6 +67,11 @@ public class Location {
this.longitude = lon;
}
/**
* @deprecated
* @return
*/
public String cityBasedOnIP() {
String city = "";
try {
@ -79,14 +97,13 @@ public class Location {
JSONArray array=(JSONArray)resultObject;
for (Object object : array) {
JSONObject obj =(JSONObject)object;
city = (String)obj.get("city");
city = (String)obj.get(API_Keys.city.name());
}
}
else if (resultObject instanceof JSONObject) {
JSONObject obj =(JSONObject)resultObject;
city = (String)obj.get("city");
city = (String)obj.get(API_Keys.city.name());
}
}
catch (MalformedURLException e) {
@ -103,4 +120,70 @@ 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,
country,
city,
org,
timezone,
isp,
quary,
regionName,
lon,
lat,
as,
countryCode,
region,
status
}
}

View File

@ -10,68 +10,12 @@ 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) {
Person p = new Person(12345, "Hesham", Level.BA);
System.out.println("-------------------------------------------------");
Scanner in = new Scanner(System.in);
System.out.print("Name: ");
p.getAccount().setName(in.nextLine());
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());
List<Course> list = Arrays.asList(Course.EE201, Course.EE250,Course.ISLS201);
p.getEdu().addCourses(list);
Print(p);
}
/**
* Finds the correct enum value of the parameter.
* @param a string naming the account type
* @return AccountType
* @since 0.1
* @exception IncorrectStringException
*/
private static AccountType parseType(String input) {
switch(input) {
case "Investing":
return AccountType.Investing;
case "Saving":
return AccountType.Saving;
default:
return AccountType.Checking;
}
}
/**
* Standered print of Person's information
* @param Person Object.
* @since 0.1
*/
public static void Print(Person p) {
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---------");
int i = 1;
for (Course course : p.getEdu().getCourses()) {
System.out.println("\tCourse "+i+": "+course.name());
}
Questions.run();
}
}

View File

@ -27,6 +27,10 @@ public class Person {
return car;
}
public void setCar(Car car) {
this.car = car;
}
public String getName() {
return name;
}
@ -43,22 +47,16 @@ public class Person {
this.location = location;
}
public void setCar(Car car) {
this.car = car;
}
public boolean hasCar() {
if (car.getMake() != null) {
return true;
}
else return false;
return car.getMake() != null;
}
public boolean isPHD() {
if (this.edu.getLevel() == Level.PHD) {
return true;
return this.edu.getLevel() == Level.PHD;
}
else return false;
public String getCity(){
return location.getCity();
}
}

94
src/Questions.java Normal file
View File

@ -0,0 +1,94 @@
import java.util.Arrays;
import java.util.List;
import java.util.Scanner;
import org.joda.time.LocalDate;
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);
}
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: ");
try {
name = in.nextLine();
}
catch (Exception e) {
e.printStackTrace();
}
return name;
}
/**
* Standered print of Person's information
* @param Person Object.
* @since 0.1
*/
private static void Print(Person p) {
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().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
* @return AccountType
* @since 0.1
* @exception IncorrectStringException
*/
private static AccountType parseType(String input) {
switch(input) {
case "Investing":
return AccountType.Investing;
case "Saving":
return AccountType.Saving;
default:
return AccountType.Checking;
}
}
}