add method in Country
[Persons_Comparator.git] / src / Country.java
1 import java.io.File;
2 import java.io.FileNotFoundException;
3 import java.util.List;
4 import java.util.Scanner;
5
6 public class Country {
7 private String name;
8 private String region;
9 private double lat;
10 private double lng;
11 private String flag;
12 private String csvFile = "data/countries.csv";
13
14 public Country(String name, String region, double lat, double lng, String flag) {
15 setName(name);
16 setRegion(region);
17 setLat(lat);
18 setLng(lng);
19 setFlag(flag);
20 }
21
22 public Country(String name) {
23 setName(name);
24 loadCSVOneCountry(this.name);
25
26 }
27
28 public void setName(String name) {
29 this.name = name;
30 }
31
32 public String getName() {
33 return name;
34 }
35
36 public void setRegion(String region) {
37 this.region = region;
38 }
39
40 public void setLat(double lat) {
41 this.lat = lat;
42 }
43
44 public double getLat() {
45 return lat;
46 }
47
48 public void setLng(double lng) {
49 this.lng = lng;
50 }
51
52 public double getLng() {
53 return lng;
54 }
55
56 public void setFlag(String flag) {
57 this.flag = flag;
58 }
59
60 public String getFlag() {
61 return flag;
62 }
63
64 public int distanceTo(Country country) {
65 if ((this.lat == country.lat) && (this.lng == country.lng)) {
66 return 0;
67 } else {
68 double theta = this.lng - country.lng;
69 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));
70 dist = Math.acos(dist);
71 dist = Math.toDegrees(dist);
72 dist = dist * 60 * 1.1515;
73 // Kilometers
74 dist = dist * 1.609344;
75 return dist.intValue();
76 }
77 }
78
79 public void loadCSVOneCountry(String name) {
80 Scanner scanner = null;
81 try {
82 scanner = new Scanner(new File(csvFile));
83 } catch (FileNotFoundException e) {
84 e.printStackTrace();
85 }
86 while (scanner.hasNext()) {
87 List<String> line = CSVUtils.parseLine(scanner.nextLine());
88 String countryList = line.get(0);
89 String[] countryArray = countryList.split(",");
90 String latLng = line.get(16);
91 String[] latLngArray = latLng.split(",");
92
93
94 if (countryArray[1].equals(name)) {
95 setRegion(line.get(12));
96 setLat(Double.parseDouble(latLngArray[0]));
97 setLng(Double.parseDouble(latLngArray[1]));
98 }
99 }
100 scanner.close();
101 /*Utils.displayArrayList(countryArrayListOceania);
102 Utils.displayArrayList(countryArrayListAfrica);
103 Utils.displayArrayList(countryArrayListAmericas);
104 Utils.displayArrayList(countryArrayListAsia);
105 Utils.displayArrayList(countryArrayListEurope);*/
106 }
107
108
109 @Override
110 public String toString() {
111 return "Country{" +
112 "name='" + name + '\'' +
113 ", region='" + region + '\'' +
114 ", lat=" + lat +
115 ", lng=" + lng +
116 ", flag=" + flag +
117 '}';
118 }
119 }