-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeatherTest.java
More file actions
184 lines (175 loc) · 6.88 KB
/
Copy pathWeatherTest.java
File metadata and controls
184 lines (175 loc) · 6.88 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
// Ema Ikeda
// ID: 950607512
// 11/08/21
// Driver code to test Weather API. Requests the API to retrieve the specific information about the weather in the city selected by the user.
import java.util.*;
public class WeatherTest {
public static String codeToAPICategory(int code) { // convert category into format in JSON file
String apiCategory = "";
switch (code) {
case 1:
apiCategory = "main";
break;
case 2:
apiCategory = "temp";
break;
case 3:
apiCategory = "pressure";
break;
case 4:
apiCategory = "humidity";
break;
case 5:
apiCategory = "deg";
break;
case 6:
apiCategory = "speed";
break;
case 7:
apiCategory = "gust";
break;
default:
System.out.println("Invalid input. Type a number 1-7.");
break;
}
return apiCategory;
}
public static String APItoCategory(String apiCategory) {
String category = "";
switch (apiCategory) {
case "main":
category = "Overall Weather";
break;
case "temp":
category = "Temperature";
break;
case "pressure":
category = "Pressure";
break;
case "humidity":
category = "Humidity";
break;
case "deg":
category = "Wind Degree";
break;
case "speed":
category = "Wind Speed";
break;
case "gust":
category = "Wind Gust";
break;
default:
break;
}
return category;
}
public static void main(String[] args) throws Exception {
Scanner input = new Scanner(System.in);
String result = "";
while (true) {
System.out.println("Select a city:\n 1: Bellevue \n 2: Tehran \n 3: Hong Kong \n 4: New Delhi \n 5: Paris \n 6: Cairo \n 7: Moscow \n 8: Tokyo \n 9: Sydney \n 10: Cape Town \n 0: exit");
String cityName = "";
int cityCode;
String city = input.next();
// Select city: Set cityName to specific city based on code inputted by user.
// If user typed 0, exit the loop and close the program
try{
cityCode = Integer.parseInt(city);
} catch (NumberFormatException e){ // if input error happened
System.out.println("Invalid input. Type a number 0-10.");
continue;
}
if (cityCode == 0) {
break;
}
boolean validCityCode = false;
while (!validCityCode) {
switch (cityCode) {
case 1:
cityName = "Bellevue";
break;
case 2:
cityName = "Tehran";
break;
case 3:
cityName = "Hong Kong";
break;
case 4:
cityName = "New Delhi";
break;
case 5:
cityName = "Paris";
break;
case 6:
cityName = "Cairo";
break;
case 7:
cityName = "Moscow";
break;
case 8:
cityName = "Tokyo";
break;
case 9:
cityName = "Sydney";
break;
case 10:
cityName = "Cape Town";
break;
default:
System.out.println("Invalid number. Type numbers 0-10.");
break;
}
if (cityName != "") {
validCityCode = true;
} else {
cityCode = input.nextInt();
}
}
// Select weather category: Set category to specific city based on code inputted by user.
System.out.println("Select a weather category:\n 1: Overall Weather \n 2: Temperature \n 3: Pressure \n 4: Humidity \n 5: Wind Degree \n 6: Wind Speed \n 7: Wind Gust");
int categoryCode;
String code = input.next();
try{
categoryCode = Integer.parseInt(code);
String category = codeToAPICategory(categoryCode);
if (category == "temp" || category == "speed" || category == "gust") {
// Select units: User can change to metric or imperial. Standard if invalid input or no input
System.out.println("Optional: Select unit measurement: \n 1: Metric \n 2: Imperial");
int unitCode;
String unit = input.next(); // User can also press enter to move onto standard units
try {
unitCode = Integer.parseInt(unit);
switch (unitCode) {
case 1:
unit = "metric";
result = WeatherAPI.getCityWeather(cityName, category, unit);
break;
case 2:
unit = "imperial";
result = WeatherAPI.getCityWeather(cityName, category, unit);
break;
default:
unit = "";
result = WeatherAPI.getCityWeather(cityName, category); // call vanilla version
break;
}
}
catch (NumberFormatException e) {
System.out.println("Using standard measurements.");
result = WeatherAPI.getCityWeather(cityName, category);
}
} else {
result = WeatherAPI.getCityWeather(cityName, category);
}
System.out.println();
System.out.println("=== Weather Information ===");
System.out.println("City: " + cityName);
System.out.println(APItoCategory(category) +": "+ result);
}
catch(NumberFormatException e) {
System.out.println("Invalid input. Type a number 1-7.");
}
}
input.close();
}
}