diff --git a/.classpath b/.classpath new file mode 100644 index 0000000..6aa9e0d --- /dev/null +++ b/.classpath @@ -0,0 +1,20 @@ + + + + + + + + + + + + + + + + + + + + diff --git a/.project b/.project new file mode 100644 index 0000000..357aebb --- /dev/null +++ b/.project @@ -0,0 +1,23 @@ + + + Java + + + + + + org.eclipse.jdt.core.javabuilder + + + + + org.eclipse.m2e.core.maven2Builder + + + + + + org.eclipse.m2e.core.maven2Nature + org.eclipse.jdt.core.javanature + + diff --git a/.settings/org.eclipse.jdt.core.prefs b/.settings/org.eclipse.jdt.core.prefs new file mode 100644 index 0000000..e24b722 --- /dev/null +++ b/.settings/org.eclipse.jdt.core.prefs @@ -0,0 +1,13 @@ +eclipse.preferences.version=1 +org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled +org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.8 +org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve +org.eclipse.jdt.core.compiler.compliance=1.8 +org.eclipse.jdt.core.compiler.debug.lineNumber=generate +org.eclipse.jdt.core.compiler.debug.localVariable=generate +org.eclipse.jdt.core.compiler.debug.sourceFile=generate +org.eclipse.jdt.core.compiler.problem.assertIdentifier=error +org.eclipse.jdt.core.compiler.problem.enumIdentifier=error +org.eclipse.jdt.core.compiler.problem.forbiddenReference=warning +org.eclipse.jdt.core.compiler.release=disabled +org.eclipse.jdt.core.compiler.source=1.8 diff --git a/.settings/org.eclipse.m2e.core.prefs b/.settings/org.eclipse.m2e.core.prefs new file mode 100644 index 0000000..14b697b --- /dev/null +++ b/.settings/org.eclipse.m2e.core.prefs @@ -0,0 +1,4 @@ +activeProfiles= +eclipse.preferences.version=1 +resolveWorkspaceProjects=true +version=1 diff --git a/pom.xml b/pom.xml new file mode 100644 index 0000000..505e2a7 --- /dev/null +++ b/pom.xml @@ -0,0 +1,27 @@ + + 4.0.0 + Java + Java + 0.0.1-SNAPSHOT + JavaTesting + + src + + + maven-compiler-plugin + 3.8.0 + + 1.8 + 1.8 + + + + + + + com.googlecode.json-simple + json-simple + 1.1.1 + + + \ No newline at end of file diff --git a/src/Account.java b/src/Account.java new file mode 100644 index 0000000..b04b545 --- /dev/null +++ b/src/Account.java @@ -0,0 +1,56 @@ + +public class Account { + + private String Name; + private int ID; + private AccountType type; + private double balance; + + public Account(String name, int ID, AccountType type) { + this.ID = ID; + this.Name = name; + this.type = type; + } + + public Account(String name, int ID) { + this.ID = ID; + this.Name = name; + } + + public void credit(double c) { + balance += c; + } + + public void debit(double d) { + balance -= d; + } + + public double getBalance() { + return balance; + } + + public String getName() { + return Name; + } + + public void setName(String name) { + Name = name; + } + + public int getID() { + return ID; + } + + public void setID(int iD) { + ID = iD; + } + + public void setAccountType(AccountType type) { + this.type = type; + } + + public AccountType getAccountType() { + return this.type; + } + +} diff --git a/src/AccountType.java b/src/AccountType.java new file mode 100644 index 0000000..06e3c48 --- /dev/null +++ b/src/AccountType.java @@ -0,0 +1,6 @@ + +public enum AccountType { + Checking, + Saving, + Investing +} diff --git a/src/Car.java b/src/Car.java new file mode 100644 index 0000000..1883999 --- /dev/null +++ b/src/Car.java @@ -0,0 +1,57 @@ + +public class Car { + + private String make; + private int yearMade; + private float engineSize; + private int numberOfCylinders; + private int capacity; + private float currentSpeed; + private Location location; + + public String getMake() { + return make; + } + public void setMake(String make) { + this.make = make; + } + public int getYearMade() { + return yearMade; + } + public void setYearMade(int yearMade) { + this.yearMade = yearMade; + } + public float getEngineSize() { + return engineSize; + } + public void setEngineSize(float engineSize) { + this.engineSize = engineSize; + } + public int getNumberOfCylinders() { + return numberOfCylinders; + } + public void setNumberOfCylinders(int numberOfCylinders) { + this.numberOfCylinders = numberOfCylinders; + } + public int getCapacity() { + return capacity; + } + public void setCapacity(int capacity) { + this.capacity = capacity; + } + public float getCurrentSpeed() { + return currentSpeed; + } + public void setCurrentSpeed(float currentSpeed) { + this.currentSpeed = currentSpeed; + } + public Location getLocation() { + return location; + } + public void setLocation(Location location) { + this.location = location; + } + + + +} diff --git a/src/Education.java b/src/Education.java new file mode 100644 index 0000000..20c291a --- /dev/null +++ b/src/Education.java @@ -0,0 +1,36 @@ + +public class Education { + private String School; + private String Major; + private Level level; + + public Education(String school, String major, Level lvl) { + School = school; + Major = major; + level = lvl; + } + + public String getSchool() { + return School; + } + + public void setSchool(String school) { + School = school; + } + + public String getMajor() { + return Major; + } + + public void setMajor(String major) { + Major = major; + } + + public Level getLevel() { + return level; + } + public void setLevel(Level level) { + this.level = level; + } +} + diff --git a/src/Level.java b/src/Level.java new file mode 100644 index 0000000..930e635 --- /dev/null +++ b/src/Level.java @@ -0,0 +1,8 @@ + +public enum Level { + High , + BA, + Master, + PHD + +} diff --git a/src/Location.java b/src/Location.java new file mode 100644 index 0000000..4c04051 --- /dev/null +++ b/src/Location.java @@ -0,0 +1,103 @@ + +import java.net.MalformedURLException; +import java.net.ProtocolException; +import java.net.URL; +import java.io.BufferedReader; +import java.io.IOException; +import java.io.InputStreamReader; +import java.net.HttpURLConnection; +import org.json.simple.parser.JSONParser; +import org.json.simple.parser.ParseException; +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) { + latitude = lat; + longitude = lon; + city = cityBasedOnIP(); + } + + public Location() { + city = cityBasedOnIP(); + } + + public double getLatitude() { + return latitude; + } + + public String getCity() { + return city; + } + + public void setLatitude(double latitude) { + this.latitude = latitude; + } + + public double getLongitude() { + return longitude; + } + + public void setLongitude(double longitude) { + this.longitude = longitude; + } + + public void setLocation(double lat, double lon) { + this.latitude = lat; + this.longitude = lon; + } + + private String cityBasedOnIP() { + String city = ""; + 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) { + JSONObject obj =(JSONObject)object; + city = (String)obj.get("city"); + } + + } + else if (resultObject instanceof JSONObject) { + JSONObject obj =(JSONObject)resultObject; + city = (String)obj.get("city"); + + } + } + catch (MalformedURLException e) { + e.printStackTrace(); + } + catch (ProtocolException e) { + e.printStackTrace(); + } + catch (IOException e) { + e.printStackTrace(); + } + catch (ParseException e) { + e.printStackTrace(); + } + return city; + } +} diff --git a/src/Main.java b/src/Main.java new file mode 100644 index 0000000..584a7d0 --- /dev/null +++ b/src/Main.java @@ -0,0 +1,59 @@ +import java.util.Scanner; + + +public class Main { + + public static void main(String[] args) { + Person p = new Person(12345, "Hesham", Level.BA); + System.out.println(p.getLocation().getCity()); + 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()); + 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) { + System.out.println( + "\t---------INFO---------\n" + + "\tName: "+p.getAccount().getName()+"\n" + + "\tID: "+p.getAccount().getID()+"\n" + + "\tMajor: "+p.getEdu().getMajor()+"\n" + + "\tSchool: "+p.getEdu().getSchool()+"\n" + + "\tAccountType: "+p.getAccount().getAccountType()); + + + } +} diff --git a/src/Person.java b/src/Person.java new file mode 100644 index 0000000..2ee9304 --- /dev/null +++ b/src/Person.java @@ -0,0 +1,64 @@ +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; + } + +}