Switch person class usage of Origin class to Country class and use it for distance...
authorJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 18 Jan 2019 12:06:31 +0000 (13:06 +0100)
committerJérôme Benoit <jerome.benoit@piment-noir.org>
Fri, 18 Jan 2019 12:06:31 +0000 (13:06 +0100)
Signed-off-by: Jérôme Benoit <jerome.benoit@piment-noir.org>
src/Country.java
src/Origin.java
src/Person.java
src/PersonView.java
src/RegionView.java

index 5a57362f07d6a2006ba07b75436d13cd426ac133..8a41df6ad2281a454b63af19135e2c97aea4a3c4 100644 (file)
@@ -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,12 +27,6 @@ public class Country {
         setFlag(flag);
     }
 
-    public Country(String name) {
-        setName(name);
-        loadCSVOneCountry(this.name);
-
-    }
-
     public void setName(String name) {
         this.name = name;
     }
@@ -62,11 +64,11 @@ public class Country {
     }
 
     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,7 +78,7 @@ public class Country {
         }
     }
 
-    public void loadCSVOneCountry(String name) {
+    private void initCountryFromCSV(String name) {
         Scanner scanner = null;
         try {
             scanner = new Scanner(new File(csvFile));
@@ -89,8 +91,6 @@ public class Country {
             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]));
@@ -98,14 +98,8 @@ public class Country {
             }
         }
         scanner.close();
-          /*Utils.displayArrayList(countryArrayListOceania);
-        Utils.displayArrayList(countryArrayListAfrica);
-        Utils.displayArrayList(countryArrayListAmericas);
-        Utils.displayArrayList(countryArrayListAsia);
-        Utils.displayArrayList(countryArrayListEurope);*/
     }
 
-
     @Override
     public String toString() {
         return "Country{" +
index 7ea4e536a487efde36ad6451076636698a25b43a..c02dd28a1f4b777de433777a5e5452b8a6702f31 100644 (file)
@@ -13,7 +13,6 @@ public class Origin {
             {"Australia"} /* Oceania */
     };
 
-
     Origin() {
     }
 
@@ -64,8 +63,6 @@ public class Origin {
     }
 
     public int distanceTo(Origin origin) {
-
-
         return 0;
     }
 }
index 6fbdf6ac55a644af4baf0043d23f89c40b240d75..f1409f52760d91eb57e59e7624a3af2a7304cbfa 100644 (file)
@@ -12,6 +12,7 @@ public class Person implements Comparator<Person> {
     Person() {
         setFirstname(new Firstname());
         setOrigin(new Origin());
+        setCountry(new Country());
         setPersonSize(new Size());
         setWeight(new Weight());
         setEye(new Eye());
@@ -21,6 +22,7 @@ public class Person implements Comparator<Person> {
     Person(String firstname, String country, Integer size, Integer weight, String colorEye) {
         setFirstname(new Firstname(firstname));
         setOrigin(new Origin(country));
+        setCountry(new Country(country));
         setPersonSize(new Size(size));
         setWeight(new Weight(weight));
         setEye(new Eye(colorEye));
@@ -88,7 +90,7 @@ public class Person implements Comparator<Person> {
         return "Person{" +
                 "firstname=" + firstname.getFirstname() +
                 ", origin=" + origin.getCountry() +
-                ", country=" + country +
+                ", country=" + country.getName() +
                 ", size=" + size.getSize() +
                 ", weight=" + weight.getWeight() +
                 ", eye=" + eye.getStrColor() + "(" + this.getEye().getColor().getRed() + "," + this.getEye().getColor().getGreen() + "," + this.getEye().getColor().getBlue() + ")" +
index 803fcf50498e42fbaa70e723e8faffbc38010578..901ce90158131b4f237ed5ca2c96a23a6152235e 100644 (file)
@@ -84,11 +84,14 @@ public class PersonView extends JComponent implements ActionListener {
         this.originView = originView;
     }
 
+    public RegionView getRegionView() {
+        return regionView;
+    }
+
     public void setRegionView(RegionView regionView) {
         this.regionView = regionView;
     }
 
-
     /**
      * @return
      */
@@ -167,20 +170,23 @@ public class PersonView extends JComponent implements ActionListener {
     public void actionPerformed(ActionEvent actionEvent) {
         //TODO: one can implement a smarter way of getting all inputs values inside the main panel.
         String firstname = this.getFirstnameView().getTextField().getText();
-        String country = null;
-        if (this.getOriginView().getComboBox().getSelectedIndex() != -1)
-            country = (String) this.getOriginView().getComboBox().getSelectedObjects()[0];
+        String countryName = null;
+        if (this.getRegionView().getTree().getLastSelectedPathComponent() != null)
+            countryName = this.getRegionView().getTree().getLastSelectedPathComponent().toString();
+        /*if (this.getOriginView().getComboBox().getSelectedIndex() != -1)
+            countryName = (String) this.getOriginView().getComboBox().getSelectedObjects()[0];*/
         Integer size = (Integer) this.getSizeView().getSpinner().getValue();
         Integer weight = (Integer) this.getWeightView().getComboBox().getSelectedItem();
         String eye_color = (String) this.getEyeView().getColorsList().getSelectedValue();
-        if (firstname.equals("") || country == null || eye_color == null) {
+        if (firstname.equals("") || countryName == null || eye_color == null) {
             JOptionPane.showMessageDialog(this,
                     "Some required fields are missing.",
                     "Error",
                     JOptionPane.ERROR_MESSAGE);
         } else {
             this.getPersonObj().setFirstname(new Firstname(firstname));
-            this.getPersonObj().setOrigin(new Origin(country));
+            //this.getPersonObj().setOrigin(new Origin(countryName));
+            this.getPersonObj().setCountry(new Country(countryName));
             this.getPersonObj().setPersonSize(new Size(size));
             this.getPersonObj().setWeight(new Weight(weight));
             this.getPersonObj().setEye(new Eye(eye_color));
index 17a294908f6933a8915d19a5da672a923c9a2c51..42941ac2d6f27247f749a309994f98fdd3854172 100644 (file)
@@ -72,13 +72,17 @@ public class RegionView extends JPanel {
                     }
                 }
                 if (node.getChildCount() > 0) {
-                    node = sortTree(node);
+                    sortTree(node);
                 }
             }
             return root;
         }
     }
 
+    public JTree getTree() {
+        return tree;
+    }
+
     public void setRegionObj(Region regionObj) {
         this.regionObj = regionObj;
     }