From 9f1e899962a92c9c1b8d3713eca0524964465014 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 18 Jan 2019 13:06:31 +0100 Subject: [PATCH] Switch person class usage of Origin class to Country class and use it for distance computation. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/Country.java | 30 ++++++++++++------------------ src/Origin.java | 3 --- src/Person.java | 4 +++- src/PersonView.java | 18 ++++++++++++------ src/RegionView.java | 6 +++++- 5 files changed, 32 insertions(+), 29 deletions(-) diff --git a/src/Country.java b/src/Country.java index 5a57362..8a41df6 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,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{" + diff --git a/src/Origin.java b/src/Origin.java index 7ea4e53..c02dd28 100644 --- a/src/Origin.java +++ b/src/Origin.java @@ -13,7 +13,6 @@ public class Origin { {"Australia"} /* Oceania */ }; - Origin() { } @@ -64,8 +63,6 @@ public class Origin { } public int distanceTo(Origin origin) { - - return 0; } } diff --git a/src/Person.java b/src/Person.java index 6fbdf6a..f1409f5 100644 --- a/src/Person.java +++ b/src/Person.java @@ -12,6 +12,7 @@ public class Person implements Comparator { 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(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 { 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() + ")" + diff --git a/src/PersonView.java b/src/PersonView.java index 803fcf5..901ce90 100644 --- a/src/PersonView.java +++ b/src/PersonView.java @@ -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)); diff --git a/src/RegionView.java b/src/RegionView.java index 17a2949..42941ac 100644 --- a/src/RegionView.java +++ b/src/RegionView.java @@ -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; } -- 2.34.1