From b340a28fcacc7c2d230897ec810b88718b954f42 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 18 Jan 2019 16:25:23 +0100 Subject: [PATCH] Fix the country name fetching from CSV. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Add a println if the country name do not exist. Signed-off-by: Jérôme Benoit --- src/Country.java | 11 +++++++++-- src/Main.java | 12 ++++++------ src/Person.java | 6 +++--- src/Region.java | 10 +++++----- 4 files changed, 23 insertions(+), 16 deletions(-) diff --git a/src/Country.java b/src/Country.java index 8a41df6..7f2486b 100644 --- a/src/Country.java +++ b/src/Country.java @@ -68,7 +68,9 @@ public class Country { return 0; } else { 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)); + 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; @@ -85,18 +87,23 @@ public class Country { } 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)); } } + if (!found) + System.out.println("Country " + name + " not found"); scanner.close(); } diff --git a/src/Main.java b/src/Main.java index 17cf896..fa1f199 100644 --- a/src/Main.java +++ b/src/Main.java @@ -6,19 +6,19 @@ public class Main { String programName = "Person Comparator"; ArrayList personArrayList = new ArrayList<>(); - Person person1 = new Person("Alan", "United States of America", 180, 95, "black"); + Person person1 = new Person("Alan", "United States", 180, 95, "black"); personArrayList.add(person1); Person person2 = new Person("Brice", "France", 190, 82, "brown"); personArrayList.add(person2); Person person3 = new Person("Alexandre", "France", 175, 73, "green"); personArrayList.add(person3); - Person person4 = new Person("Sophia", "Brasil", 155, 57, "blue"); + Person person4 = new Person("Sophia", "Brazil", 155, 57, "blue"); personArrayList.add(person4); - Person person5 = new Person("Sylvain", "Italie", 181, 75, "brown"); + Person person5 = new Person("Sylvain", "Italy", 181, 75, "brown"); personArrayList.add(person5); - Person person6 = new Person("Merlin", "United States of America", 210, 88, "blue"); + Person person6 = new Person("Merlin", "United States", 210, 88, "blue"); personArrayList.add(person6); - Person person7 = new Person("Bob", "United States of America", 162, 75, "green"); + Person person7 = new Person("Bob", "United States", 162, 75, "green"); personArrayList.add(person7); Person person8 = new Person("John", "United Kingdom", 176, 102, "brown"); personArrayList.add(person8); @@ -40,7 +40,7 @@ public class Main { personArrayList.add(person16); Person person17 = new Person("Aïfa", "Mali", 149, 49, "black"); personArrayList.add(person17); - Person person18 = new Person("Roberto", "Brasil", 168, 56, "brown"); + Person person18 = new Person("Roberto", "Brazil", 168, 56, "brown"); personArrayList.add(person18); Person person19 = new Person("Batista", "France", 158, 52, "black"); personArrayList.add(person19); diff --git a/src/Person.java b/src/Person.java index f1409f5..f843250 100644 --- a/src/Person.java +++ b/src/Person.java @@ -19,10 +19,10 @@ public class Person implements Comparator { setDistanceFromReference(0); } - Person(String firstname, String country, Integer size, Integer weight, String colorEye) { + Person(String firstname, String countryName, Integer size, Integer weight, String colorEye) { setFirstname(new Firstname(firstname)); - setOrigin(new Origin(country)); - setCountry(new Country(country)); + setOrigin(new Origin(countryName)); + setCountry(new Country(countryName)); setPersonSize(new Size(size)); setWeight(new Weight(weight)); setEye(new Eye(colorEye)); diff --git a/src/Region.java b/src/Region.java index c97b5ff..2233e55 100644 --- a/src/Region.java +++ b/src/Region.java @@ -55,19 +55,19 @@ public class Region { String latLng = line.get(16); String[] latLngArray = latLng.split(","); if (line.get(12).equals("Europe")) { - Country c = new Country(countryArray[1], line.get(12), Double.parseDouble(latLngArray[0]), Double.parseDouble(latLngArray[1]), line.get(21)); + Country c = new Country(countryArray[0], line.get(12), Double.parseDouble(latLngArray[0]), Double.parseDouble(latLngArray[1]), line.get(21)); countryArrayListEurope.add(c); } else if (line.get(12).equals("Africa")) { - Country c = new Country(countryArray[1], line.get(12), Double.parseDouble(latLngArray[0]), Double.parseDouble(latLngArray[1]), line.get(21)); + Country c = new Country(countryArray[0], line.get(12), Double.parseDouble(latLngArray[0]), Double.parseDouble(latLngArray[1]), line.get(21)); countryArrayListAfrica.add(c); } else if (line.get(12).equals("Americas")) { - Country c = new Country(countryArray[1], line.get(12), Double.parseDouble(latLngArray[0]), Double.parseDouble(latLngArray[1]), line.get(21)); + Country c = new Country(countryArray[0], line.get(12), Double.parseDouble(latLngArray[0]), Double.parseDouble(latLngArray[1]), line.get(21)); countryArrayListAmericas.add(c); } else if (line.get(12).equals("Asia")) { - Country c = new Country(countryArray[1], line.get(12), Double.parseDouble(latLngArray[0]), Double.parseDouble(latLngArray[1]), line.get(21)); + Country c = new Country(countryArray[0], line.get(12), Double.parseDouble(latLngArray[0]), Double.parseDouble(latLngArray[1]), line.get(21)); countryArrayListAsia.add(c); } else if (line.get(12).equals("Oceania")) { - Country c = new Country(countryArray[1], line.get(12), Double.parseDouble(latLngArray[0]), Double.parseDouble(latLngArray[1]), line.get(21)); + Country c = new Country(countryArray[0], line.get(12), Double.parseDouble(latLngArray[0]), Double.parseDouble(latLngArray[1]), line.get(21)); countryArrayListOceania.add(c); } } -- 2.34.1