-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathController.java
More file actions
117 lines (90 loc) · 3.87 KB
/
Controller.java
File metadata and controls
117 lines (90 loc) · 3.87 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
package com.company;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.net.URL;
import java.net.URLConnection;
import java.util.ResourceBundle;
import javafx.fxml.FXML;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
import javafx.scene.layout.AnchorPane;
import javafx.scene.text.Text;
import org.json.JSONObject;
public class Controller {
@FXML
public ResourceBundle resources;
@FXML
public URL location;
@FXML
public AnchorPane getBg1;
@FXML
public AnchorPane getBg2;
@FXML
public Text getName;
@FXML
public TextField getCity;
@FXML
public Text getTemp;
@FXML
public Text getRF;
@FXML
public Text getCoR;
@FXML
public Text getWS;
@FXML
public Text getHum;
@FXML
public Text getPressure;
@FXML
public Text getAQI;
@FXML
public Button getFindButton;
@FXML
void initialize() {
getFindButton.setOnAction(e -> {
String getUserCity = getCity.getText().trim();
String output = getURLInf("https://api.openweathermap.org/data/2.5/weather?q=" + getUserCity + "&appid=094f441c7787614890194110a0b15952&units=metric");
if(!output.isEmpty()){
//get data from the first URL
JSONObject object = new JSONObject(output);
getTemp.setText("Temperature: " + object.getJSONObject("main").getDouble("temp") + "℃");
getRF.setText("Real feel: " + object.getJSONObject("main").getDouble("feels_like") + "℃");
getCoR.setText("Clouds: " + object.getJSONObject("clouds").getDouble("all") + "%");
getWS.setText("Wind speed: " + object.getJSONObject("wind").getDouble("speed") + "m/s");
getHum.setText("Humidity: " + object.getJSONObject("main").getDouble("humidity") + "%");
getPressure.setText("Pressure: " + object.getJSONObject("main").getDouble("pressure") + " mmHg");
double lat,lon,aqi; //variables for air quality index
lat = object.getJSONObject("coord").getDouble("lat"); //get latitude data from the first URL
lon = object.getJSONObject("coord").getDouble("lon"); //get longitude data from the first URL
//get data from the second URL
String output_aqi = getURLInf("http://api.openweathermap.org/data/2.5/air_pollution?lat=" + lat + "&lon=" + lon + "&appid=094f441c7787614890194110a0b15952");
JSONObject object_aqi = new JSONObject(output_aqi);
aqi = object_aqi.getJSONArray("list").getJSONObject(0).getJSONObject("main").getDouble("aqi");
switch ((int) aqi) {
case 1 -> getAQI.setText("Air Quality Index: " + (int) aqi + " (Good)");
case 2 -> getAQI.setText("Air Quality Index: " + (int) aqi + " (Fair)");
case 3 -> getAQI.setText("Air Quality Index: " + (int) aqi + " (Moderate)");
case 4 -> getAQI.setText("Air Quality Index: " + (int) aqi + " (Poor)");
case 5 -> getAQI.setText("Air Quality Index: " + (int) aqi + " (Very Poor)");
}
}
});
}
//access URL
private String getURLInf(String urlAddress){
StringBuffer information = new StringBuffer();
try{
URL url = new URL(urlAddress);
URLConnection urlConnection = url.openConnection();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(urlConnection.getInputStream()));
String line;
while((line = bufferedReader.readLine()) != null){
information.append(line + "\n");
}
bufferedReader.close();
} catch (Exception e){
System.out.println("City was not found");
}
return information.toString();
}
}