2 import java
.io
.FileNotFoundException
;
4 import java
.util
.Scanner
;
12 private String csvFile
= "data/countries.csv";
17 public Country(String name
) {
19 initCountryFromCSV(name
);
22 public Country(String name
, String region
, double lat
, double lng
, String flag
) {
30 public String
getName() {
34 public void setName(String name
) {
38 public void setRegion(String region
) {
42 public double getLat() {
46 public void setLat(double lat
) {
50 public double getLng() {
54 public void setLng(double lng
) {
58 public String
getFlag() {
62 public void setFlag(String flag
) {
66 public int distanceTo(Country country
) {
67 if ((this.getLat() == country
.getLat()) && (this.getLng() == country
.getLng())) {
70 double theta
= this.getLng() - country
.getLng();
71 Double dist
= Math
.sin(Math
.toRadians(this.getLat())) * Math
.sin(Math
.toRadians(country
.getLat())) +
72 Math
.cos(Math
.toRadians(this.getLat())) * Math
.cos(Math
.toRadians(country
.getLat())) *
73 Math
.cos(Math
.toRadians(theta
));
74 dist
= Math
.acos(dist
);
75 dist
= Math
.toDegrees(dist
);
76 dist
= dist
* 60 * 1.1515;
78 dist
= dist
* 1.609344;
79 return dist
.intValue();
83 private void initCountryFromCSV(String name
) {
84 Scanner scanner
= null;
86 scanner
= new Scanner(new File(csvFile
));
87 } catch (FileNotFoundException e
) {
90 boolean found
= false;
91 while (scanner
.hasNext()) {
92 List
<String
> line
= CSVUtils
.parseLine(scanner
.nextLine());
93 String countryList
= line
.get(0);
94 String
[] countryArray
= countryList
.split(",");
95 String latLng
= line
.get(16);
96 String
[] latLngArray
= latLng
.split(",");
97 if (countryArray
[0].equals(name
)) {
99 setRegion(line
.get(12));
100 setLat(Double
.parseDouble(latLngArray
[0]));
101 setLng(Double
.parseDouble(latLngArray
[1]));
102 setFlag(line
.get(21));
106 System
.out
.println("Country " + name
+ " not found");
111 public String
toString() {
113 "name='" + name
+ '\'' +
114 ", region='" + region
+ '\'' +