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 # virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid* 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>

70
pom.xml
View File

@ -1,33 +1,39 @@
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <modelVersion>4.0.0</modelVersion>
<groupId>Java</groupId> <groupId>Java</groupId>
<artifactId>Java</artifactId> <artifactId>Java</artifactId>
<version>0.0.1-SNAPSHOT</version> <version>0.0.1-SNAPSHOT</version>
<name>JavaTesting</name> <name>JavaTesting</name>
<build> <build>
<sourceDirectory>src</sourceDirectory> <sourceDirectory>src</sourceDirectory>
<plugins> <plugins>
<plugin> <plugin>
<artifactId>maven-compiler-plugin</artifactId> <artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version> <version>3.8.0</version>
<configuration> <configuration>
<source>1.8</source> <source>1.8</source>
<target>1.8</target> <target>1.8</target>
</configuration> </configuration>
</plugin> </plugin>
</plugins> </plugins>
</build> </build>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>com.googlecode.json-simple</groupId> <groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId> <artifactId>json-simple</artifactId>
<version>1.1.1</version> <version>1.1.1</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>joda-time</groupId> <groupId>joda-time</groupId>
<artifactId>joda-time</artifactId> <artifactId>joda-time</artifactId>
<version>2.9.9</version> <version>2.9.9</version>
</dependency> </dependency>
<dependency>
</dependencies> <groupId>org.jetbrains</groupId>
<artifactId>annotations</artifactId>
<version>RELEASE</version>
<scope>compile</scope>
</dependency>
</dependencies>
</project> </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; import org.json.simple.JSONObject;
public class Location { public class Location {
private double latitude; private double latitude;
private double longitude; private double longitude;
private String city; 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; 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() {
@ -53,7 +66,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 {
@ -79,15 +97,14 @@ public class Location {
JSONArray array=(JSONArray)resultObject; JSONArray array=(JSONArray)resultObject;
for (Object object : array) { for (Object object : array) {
JSONObject obj =(JSONObject)object; JSONObject obj =(JSONObject)object;
city = (String)obj.get("city"); city = (String)obj.get(API_Keys.city.name());
} }
} }
else if (resultObject instanceof JSONObject) { else if (resultObject instanceof JSONObject) {
JSONObject obj =(JSONObject)resultObject; JSONObject obj =(JSONObject)resultObject;
city = (String)obj.get("city"); city = (String)obj.get(API_Keys.city.name());
}
}
} }
catch (MalformedURLException e) { catch (MalformedURLException e) {
e.printStackTrace(); e.printStackTrace();
@ -103,4 +120,70 @@ 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 {
zip,
country,
city,
org,
timezone,
isp,
quary,
regionName,
lon,
lat,
as,
countryCode,
region,
status
}
} }

View File

@ -1,77 +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.
Person p = new Person(12345, "Hesham", Level.BA); * @param args
System.out.println("-------------------------------------------------"); */
Scanner in = new Scanner(System.in); public static void main(String[] args) {
System.out.print("Name: "); Questions.run();
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());
}
}
}

View File

@ -1,64 +1,62 @@
public class Person { public class Person {
private Account account; private Account account;
private Education edu; private Education edu;
private String name; private String name;
private Car car; private Car car;
private Location location; private Location location;
public Person(int ID, String name, Level lvl) { public Person(int ID, String name, Level lvl) {
account = new Account(name,ID); account = new Account(name, ID);
edu = new Education(null, null, lvl); edu = new Education(null, null, lvl);
car = new Car(); car = new Car();
location = new Location(); location = new Location();
this.name = name; this.name = name;
} }
public Account getAccount() { public Account getAccount() {
return account; return account;
} }
public Education getEdu() { public Education getEdu() {
return edu; return edu;
} }
public Car getCar() { public Car getCar() {
return car; return car;
} }
public String getName() { public void setCar(Car car) {
return name; this.car = car;
} }
public void setName(String name) { public String getName() {
this.name = name; return name;
} }
public Location getLocation() { public void setName(String name) {
return location; this.name = name;
} }
public void setLocation(Location location) { public Location getLocation() {
this.location = location; return location;
} }
public void setCar(Car car) { public void setLocation(Location location) {
this.car = car; this.location = location;
} }
public boolean hasCar() { public boolean hasCar() {
if (car.getMake() != null) { return car.getMake() != null;
return true; }
}
else return false; public boolean isPHD() {
} return this.edu.getLevel() == Level.PHD;
}
public boolean isPHD() {
if (this.edu.getLevel() == Level.PHD) { public String getCity(){
return true; return location.getCity();
} }
else return false;
} }
}

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;
}
}
}