Add listener to specific events like country selection.
[Persons_Comparator.git] / src / Origin.java
1 import java.util.ArrayList;
2 import java.util.Arrays;
3
4 public class Origin implements Comparable<Origin> {
5 private String continent = "Unknown";
6 private String country;
7 private String[] continents = {"Africa", "Americas", "Asia", "Europe", "Oceania"};
8 private String[][] content2DArray = {
9 {"Mali"}, /* Africa */
10 {"United States of America", "Brasil", "Mexico"}, /* Americas */
11 {"India"}, /* Asia */
12 {"France", "Germany", "United Kingdom", "Belgium", "Norway"}, /* Europe */
13 {"Australia"} /* Oceania */
14 };
15
16 Origin() {
17 }
18
19 Origin(String country) {
20 setCountry(country);
21 setContinent(this.getContinentFromCountry(country));
22 }
23
24 public String getContinent() {
25 return continent;
26 }
27
28 public void setContinent(String continent) {
29 this.continent = continent;
30 }
31
32 public String getCountry() {
33 return country;
34 }
35
36 public void setCountry(String country) {
37 this.country = country;
38 }
39
40 public String[] getCountriesArray() {
41 ArrayList<String> stringArrayList = new ArrayList<>();
42 for (int i = 0; i < this.content2DArray.length; i++) {
43 for (int j = 0; j < this.content2DArray[i].length; j++) {
44 stringArrayList.add(this.content2DArray[i][j]);
45 }
46 }
47 stringArrayList.sort(String::compareToIgnoreCase);
48 String[] rtArray = new String[stringArrayList.size()];
49 return stringArrayList.toArray(rtArray);
50 }
51
52 public String getContinentFromCountry(String country) {
53 int continentKey = -1;
54 for (int i = 0; i < content2DArray.length; i++) {
55 if (Arrays.asList(content2DArray[i]).indexOf(country) != -1) {
56 continentKey = i;
57 }
58 }
59 if (continentKey != -1)
60 return this.continents[continentKey];
61 else
62 return "Unknown";
63 }
64
65 @Override
66 public int compareTo(Origin origin) {
67 return 0;
68 }
69 }