add method in Country
authorSylvain Papa <sylvain.papa@yahoo.fr>
Fri, 18 Jan 2019 11:30:15 +0000 (12:30 +0100)
committerSylvain Papa <sylvain.papa@yahoo.fr>
Fri, 18 Jan 2019 11:30:15 +0000 (12:30 +0100)
src/Country.java

index 2516c2bfdc4172b41d9f5da9b4fc5fdb207bee7e..5a57362f07d6a2006ba07b75436d13cd426ac133 100644 (file)
@@ -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<String> 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{" +