From: Sylvain Papa Date: Fri, 18 Jan 2019 11:30:15 +0000 (+0100) Subject: add method in Country X-Git-Url: https://git.piment-noir.org/?a=commitdiff_plain;h=9e603249fe87bdda9cb9cfa84338e35ce360eb3b;p=Persons_Comparator.git add method in Country --- diff --git a/src/Country.java b/src/Country.java index 2516c2b..5a57362 100644 --- a/src/Country.java +++ b/src/Country.java @@ -1,9 +1,15 @@ +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, String flag) { setName(name); @@ -13,6 +19,12 @@ public class Country { setFlag(flag); } + public Country(String name) { + setName(name); + loadCSVOneCountry(this.name); + + } + public void setName(String name) { this.name = name; } @@ -64,6 +76,36 @@ public class Country { } } + 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{" +