X-Git-Url: https://git.piment-noir.org/?a=blobdiff_plain;f=src%2FCountry.java;h=3b8735dfce66161bbd3e5f82c6eef10be3e09edb;hb=99d1477f4f6b0797445fcacdab1440439a70e82f;hp=5a57362f07d6a2006ba07b75436d13cd426ac133;hpb=9e603249fe87bdda9cb9cfa84338e35ce360eb3b;p=Persons_Comparator.git diff --git a/src/Country.java b/src/Country.java index 5a57362..3b8735d 100644 --- a/src/Country.java +++ b/src/Country.java @@ -11,6 +11,14 @@ public class Country { private String flag; private String csvFile = "data/countries.csv"; + public Country() { + } + + public Country(String name) { + setName(name); + initCountryFromCSV(name); + } + public Country(String name, String region, double lat, double lng, String flag) { setName(name); setRegion(region); @@ -19,54 +27,50 @@ public class Country { setFlag(flag); } - public Country(String name) { - setName(name); - loadCSVOneCountry(this.name); - + public String getName() { + return name; } public void setName(String name) { this.name = name; } - public String getName() { - return name; - } - public void setRegion(String region) { this.region = region; } - public void setLat(double lat) { - this.lat = lat; - } - public double getLat() { return lat; } - public void setLng(double lng) { - this.lng = lng; + public void setLat(double lat) { + this.lat = lat; } public double getLng() { return lng; } - public void setFlag(String flag) { - this.flag = flag; + public void setLng(double lng) { + this.lng = lng; } public String getFlag() { return flag; } + public void setFlag(String flag) { + this.flag = flag; + } + public int distanceTo(Country country) { - if ((this.lat == country.lat) && (this.lng == country.lng)) { + if ((this.getLat() == country.getLat()) && (this.getLng() == country.getLng())) { 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)); + double theta = this.getLng() - country.getLng(); + Double dist = Math.sin(Math.toRadians(this.getLat())) * Math.sin(Math.toRadians(country.getLat())) + + Math.cos(Math.toRadians(this.getLat())) * Math.cos(Math.toRadians(country.getLat())) * + Math.cos(Math.toRadians(theta)); dist = Math.acos(dist); dist = Math.toDegrees(dist); dist = dist * 60 * 1.1515; @@ -76,36 +80,34 @@ public class Country { } } - public void loadCSVOneCountry(String name) { + private void initCountryFromCSV(String name) { Scanner scanner = null; try { scanner = new Scanner(new File(csvFile)); } catch (FileNotFoundException e) { e.printStackTrace(); } + boolean found = false; 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)) { + if (countryArray[0].equals(name)) { + found = true; setRegion(line.get(12)); setLat(Double.parseDouble(latLngArray[0])); setLng(Double.parseDouble(latLngArray[1])); + setFlag(line.get(21)); + break; } } + if (!found) + System.out.println("Country " + name + " not found"); scanner.close(); - /*Utils.displayArrayList(countryArrayListOceania); - Utils.displayArrayList(countryArrayListAfrica); - Utils.displayArrayList(countryArrayListAmericas); - Utils.displayArrayList(countryArrayListAsia); - Utils.displayArrayList(countryArrayListEurope);*/ } - @Override public String toString() { return "Country{" +