X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2FCountry.java;h=5a57362f07d6a2006ba07b75436d13cd426ac133;hb=9e603249fe87bdda9cb9cfa84338e35ce360eb3b;hp=2bb786a2c33d92fb2d64eb9fe6302bc1472062f4;hpb=822afd4fe0aa1669db1e3be57b277f8aff977254;p=Persons_Comparator.git diff --git a/src/Country.java b/src/Country.java index 2bb786a..5a57362 100644 --- a/src/Country.java +++ b/src/Country.java @@ -1,15 +1,28 @@ +import java.io.File; +import java.io.FileNotFoundException; +import java.util.List; +import java.util.Scanner; + public class Country { private String name; private String region; private double lat; private double lng; + private String flag; + private String csvFile = "data/countries.csv"; - - public Country(String name, String region, double lat, double lng) { + public Country(String name, String region, double lat, double lng, String flag) { setName(name); setRegion(region); setLat(lat); setLng(lng); + setFlag(flag); + } + + public Country(String name) { + setName(name); + loadCSVOneCountry(this.name); + } public void setName(String name) { @@ -28,10 +41,71 @@ public class Country { this.lat = lat; } + public double getLat() { + return lat; + } + public void setLng(double lng) { this.lng = lng; } + public double getLng() { + return lng; + } + + public void setFlag(String flag) { + this.flag = flag; + } + + public String getFlag() { + return flag; + } + + public int distanceTo(Country country) { + if ((this.lat == country.lat) && (this.lng == country.lng)) { + return 0; + } else { + double theta = this.lng - country.lng; + Double dist = Math.sin(Math.toRadians(this.lat)) * Math.sin(Math.toRadians(country.lat)) + Math.cos(Math.toRadians(this.lat)) * Math.cos(Math.toRadians(country.lat)) * Math.cos(Math.toRadians(theta)); + dist = Math.acos(dist); + dist = Math.toDegrees(dist); + dist = dist * 60 * 1.1515; + // Kilometers + dist = dist * 1.609344; + return dist.intValue(); + } + } + + public void loadCSVOneCountry(String name) { + Scanner scanner = null; + try { + scanner = new Scanner(new File(csvFile)); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + while (scanner.hasNext()) { + List line = CSVUtils.parseLine(scanner.nextLine()); + String countryList = line.get(0); + String[] countryArray = countryList.split(","); + String latLng = line.get(16); + String[] latLngArray = latLng.split(","); + + + if (countryArray[1].equals(name)) { + setRegion(line.get(12)); + setLat(Double.parseDouble(latLngArray[0])); + setLng(Double.parseDouble(latLngArray[1])); + } + } + scanner.close(); + /*Utils.displayArrayList(countryArrayListOceania); + Utils.displayArrayList(countryArrayListAfrica); + Utils.displayArrayList(countryArrayListAmericas); + Utils.displayArrayList(countryArrayListAsia); + Utils.displayArrayList(countryArrayListEurope);*/ + } + + @Override public String toString() { return "Country{" + @@ -39,6 +113,7 @@ public class Country { ", region='" + region + '\'' + ", lat=" + lat + ", lng=" + lng + + ", flag=" + flag + '}'; } }