-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWeather.java
More file actions
289 lines (175 loc) · 8.6 KB
/
Weather.java
File metadata and controls
289 lines (175 loc) · 8.6 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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
package weather;
import java.util.ArrayList;
import java.util.Scanner;
public class Weather {
private double temperature;
private double chanceOfPrecipitation;
private double humidity;
private int sunRiseTimeInHours;
private int sunSetTimeInHours;
private int sunRiseTimeInMinutes;
private int sunSetTimeInMinutes;
private double windSpeed;
private ArrayList <String> alerts;
public Weather(double temperature, double chanceOfPrecipitation, double humidity, int sunRiseTimeInHours, int sunRiseTimeInMinutes,
int sunSetTimeInHours, int sunSetTimeInMinutes, double windSpeed) {
this.temperature = temperature;
this.chanceOfPrecipitation = chanceOfPrecipitation;
this.humidity = humidity;
this.sunRiseTimeInHours = sunRiseTimeInHours;
this.sunRiseTimeInMinutes = sunRiseTimeInMinutes;
this.sunSetTimeInHours = sunSetTimeInHours;
this.sunSetTimeInMinutes = sunSetTimeInMinutes;
this.windSpeed = windSpeed;
this.alerts = new ArrayList<String>();
}
public static boolean isSunRiseSunSetValid (int timeInHours) {
if (timeInHours >= 5 && timeInHours <= 8) {
return true;
}
else {
return false;
}
}
public static boolean isPercentDataValid (double percentData) {
if (percentData >= 0 && percentData <= 100) {
return true;
}
else {
return false;
}
}
public void checkForAlerts() {
/*If temperature is above 90 it is a heat advisory,
if above 105 it is a heat warning,
if below 32 it is a freeze advisory,
if below 20 it is a freeze warning. */
if(this.temperature > 90) {
if(this.temperature > 105) {
this.alerts.add("Heat Warning");
}
else {
this.alerts.add("Heat Advisory");
}
}
if (this.temperature < 32) {
if(this.temperature < 20) {
this.alerts.add("Freeze Warning");
}
else {
this.alerts.add("Freeze advisory");
}
}
/*If precipitation is < 25 % than it is a low chance,
if it is between 25 % and 75 % chance then it is a decent chance and
if it is greater than 75 % then it is a likely chance of rain. (Pair this with temperature though.
If its below freezing it won’t be rain but snow that comes down) */
if (this.chanceOfPrecipitation < 25) {
this.alerts.add("Low Chance of Precipitation");
}
if(this.chanceOfPrecipitation > 25 && this.chanceOfPrecipitation < 75) {
this.alerts.add("Decent Chance of Precipitation");
}
if (this.chanceOfPrecipitation > 75) {
if(this.temperature < 20){
this.alerts.add("Likely Chance of snow");
}
else {
this.alerts.add("Likely chance of rain");
}
}
/* If about 80 degrees with low humidity and no precipitation then it is a fire warning.
If wind speed is greater than 40MPH then it is a wind advisory. If above 60MPH then it is a wind warning. */
if(this.temperature >= 80 && this.humidity <= 30) {
this.alerts.add("Fire Warning");
}
if (this.windSpeed > 40) {
if(this.windSpeed > 60) {
this.alerts.add("Wind Advisory");
}
else {
this.alerts.add("Wind warning");
}
}
}
public void printData() {
System.out.println("========================");
System.out.println("DATA GIVEN");
System.out.println("Sunrise: " + this.sunRiseTimeInHours + ":" + this.sunRiseTimeInMinutes + " AM");
System.out.println("Sunset: " + this.sunSetTimeInHours + ":" + this.sunSetTimeInMinutes + " PM");
System.out.println("Temperature: " + this.temperature);
System.out.println("Precipitation: " + this.chanceOfPrecipitation);
System.out.println("Humidity: " + this.humidity);
System.out.println("Wind Speed: " + this.windSpeed);
}
public void printAlerts() {
if (this.alerts.size() > 0) {
System.out.println("Watch out for following alerts: ");
for (int i =0; i < this.alerts.size();i++) {
System.out.println(this.alerts.get(i));
}
}
else {
System.out.println("There are no alerts given the weather data. " );
}
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
System.out.println("Please enter temperature in Farhenheit");
double temperature = sc.nextDouble();
System.out.println("Please enter percent chance of precipitation: (0-100)");
double precipitation = sc.nextDouble();
while(true){
if(!Weather.isPercentDataValid(precipitation)){
System.out.println("The percent data should be 0-100. Please try again.");
precipitation = sc.nextDouble();
}
else {
break;
}
}
System.out.println("Please enter humidity percent");
double humidity = sc.nextDouble();
while(true){
if(!Weather.isPercentDataValid(humidity)){
System.out.println("The percent data should be 0-100. Please try again.");
humidity = sc.nextDouble();
}
else {
break;
}
}
System.out.println("Please enter hour of sunrise in AM (5-8)");
int hour = sc.nextInt();
System.out.println("Please enter minute of sunrise (0-59)");
int minute = sc.nextInt();
while (true) {
if(!Weather.isSunRiseSunSetValid(hour)){
System.out.println("Invalid input. Hours of Sunrise should be between 5am - 8 am. Try again");
hour = sc.nextInt();
}
else {
break;
}
}
System.out.println("Please enter hour of sunset in PM (5-8)");
int sunsetHour = sc.nextInt();
System.out.println("Please enter minute of sunset in PM (0-59)");
int sunsetMinute = sc.nextInt();
while (true) {
if(!Weather.isSunRiseSunSetValid(hour)){
System.out.println("Invalid input. Hours of Sunset should be between 5pm - 8 pm. Try again");
sunsetHour = sc.nextInt();
}
else {
break;
}
}
System.out.println("Please enter wind speed in MPH");
double windSpeed = sc.nextDouble();
Weather weather = new Weather(temperature, precipitation, humidity, hour, minute, sunsetHour, sunsetMinute, windSpeed);
weather.checkForAlerts();
weather.printData();
weather.printAlerts();
}
}