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>

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">
<modelVersion>4.0.0</modelVersion>
<groupId>Java</groupId>
<artifactId>Java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>JavaTesting</name>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.9</version>
</dependency>
</dependencies>
<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>
<groupId>Java</groupId>
<artifactId>Java</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>JavaTesting</name>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.8.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>com.googlecode.json-simple</groupId>
<artifactId>json-simple</artifactId>
<version>1.1.1</version>
</dependency>
<dependency>
<groupId>joda-time</groupId>
<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;
public Location(double lat, double lon) {
private JSONObject jsonInfo;
/**
* 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() {
@ -53,7 +66,12 @@ public class Location {
this.latitude = lat;
this.longitude = lon;
}
/**
* @deprecated
* @return
*/
public String cityBasedOnIP() {
String city = "";
try {
@ -79,15 +97,14 @@ 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) {
e.printStackTrace();
@ -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

@ -1,77 +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) {
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());
}
}
}
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();
}
}

View File

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