From 1a2ddb852431259fab5dc522d8caf5b74bb3acd8 Mon Sep 17 00:00:00 2001 From: =?utf8?q?J=C3=A9r=C3=B4me=20Benoit?= Date: Fri, 11 Jan 2019 20:54:23 +0100 Subject: [PATCH] Add some basic data for the Origin class and implement the associated view to allow future complete inputs treatment testing. MIME-Version: 1.0 Content-Type: text/plain; charset=utf8 Content-Transfer-Encoding: 8bit Signed-off-by: Jérôme Benoit --- src/Main.java | 2 +- src/MainWindowsView.java | 2 +- src/Origin.java | 41 +++++++++++++++++++++++++++++++++++++--- src/OriginView.java | 9 +++++++++ 4 files changed, 49 insertions(+), 5 deletions(-) diff --git a/src/Main.java b/src/Main.java index 5bac73e..f455313 100644 --- a/src/Main.java +++ b/src/Main.java @@ -8,7 +8,7 @@ public class Main { String programName = "Person Comparator"; ArrayList personArrayList = new ArrayList<>(); - Person person1 = new Person("Alan", "USA", 180, 95, "black"); + Person person1 = new Person("Alan", "United States of America", 180, 95, "black"); personArrayList.add(person1); Person person2 = new Person("Brice", "France", 190, 82, "brown"); personArrayList.add(person2); diff --git a/src/MainWindowsView.java b/src/MainWindowsView.java index 406fcd5..58a37fb 100644 --- a/src/MainWindowsView.java +++ b/src/MainWindowsView.java @@ -11,7 +11,7 @@ public class MainWindowsView extends JFrame { MainWindowsView(String title, JPanel panel) { //Create and set up the window. setTitle(title); - setSize(300, 300); + setSize(400, 300); setLocationRelativeTo(null); setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); diff --git a/src/Origin.java b/src/Origin.java index 6e89fb9..aa63762 100644 --- a/src/Origin.java +++ b/src/Origin.java @@ -1,14 +1,24 @@ +import java.util.ArrayList; +import java.util.Arrays; + public class Origin implements Comparable { - private String continent; + private String continent = "Unknown"; private String country; + private String continents[] = {"Africa", "Americas", "Asia", "Europe", "Oceania"}; + private String[][] content2DArray = { + {"Mali"}, /* Africa */ + {"United States of America", "Brasil", "Mexico"}, /* Americas */ + {"India"}, /* Asia */ + {"France", "Germany", "United Kingdom", "Belgium", "Norway"} /* Europe */, + {"Australia"} /* Oceania */ + }; Origin() { } Origin(String country) { setCountry(country); - //TODO: properly set the continent from the country. - setContinent("Europe"); + setContinent(this.getContinentFromCountry(country)); } public String getContinent() { @@ -27,6 +37,31 @@ public class Origin implements Comparable { this.country = country; } + public String[] getCountriesArray() { + ArrayList stringArrayList = new ArrayList<>(); + for (int i = 0; i < this.content2DArray.length; i++) { + for (int j = 0; j < this.content2DArray[i].length; j++) { + stringArrayList.add(this.content2DArray[i][j]); + } + } + stringArrayList.sort(String::compareToIgnoreCase); + String[] rtArray = new String[stringArrayList.size()]; + return stringArrayList.toArray(rtArray); + } + + private String getContinentFromCountry(String country) { + int continentKey = -1; + for (int i = 0; i < content2DArray.length; i++) { + if (Arrays.asList(content2DArray[i]).indexOf(country) != -1) { + continentKey = i; + } + } + if (continentKey != -1) + return this.continents[continentKey]; + else + return "Unknown"; + } + @Override public int compareTo(Origin origin) { return 0; diff --git a/src/OriginView.java b/src/OriginView.java index 3c3b518..a5df1ad 100644 --- a/src/OriginView.java +++ b/src/OriginView.java @@ -3,12 +3,21 @@ import javax.swing.*; public class OriginView extends JPanel { private Origin originObj; private JLabel label; + private JComboBox comboBox; + //FIXME: this label should be refreshed on country selection. + private JLabel continentLabel; OriginView(Origin originObj) { setOriginObj(originObj); this.label = new JLabel(); this.label.setText("Origin"); + this.comboBox = new JComboBox<>(this.getOriginObj().getCountriesArray()); + this.comboBox.setSelectedItem(this.getOriginObj().getCountry()); + this.continentLabel = new JLabel(); + this.continentLabel.setText("(" + this.getOriginObj().getContinent() + ")"); add(label); + add(comboBox); + add(continentLabel); } public Origin getOriginObj() { -- 2.34.1