String programName = "Person Comparator";
ArrayList<Person> 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);
+import java.util.ArrayList;
+import java.util.Arrays;
+
public class Origin implements Comparable<Origin> {
- 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() {
this.country = country;
}
+ public String[] getCountriesArray() {
+ ArrayList<String> 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;
public class OriginView extends JPanel {
private Origin originObj;
private JLabel label;
+ private JComboBox<String> 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() {