diff --git a/pom.xml b/pom.xml index fc1157b2a9d2264fc490249b9781570a3214d4e4..b8f479eb6db211944ee324f66c8180736ff0323d 100644 --- a/pom.xml +++ b/pom.xml @@ -63,6 +63,12 @@ <artifactId>mysql-connector-java</artifactId> <version>8.0.33</version> </dependency> + <dependency> + <groupId>org.json</groupId> + <artifactId>json</artifactId> + <version>20231013</version> + </dependency> + </dependencies> diff --git a/src/main/java/module-info.java b/src/main/java/module-info.java index 5ca397f5be5df0ba05f86c97ab84d64ac31cf989..b6cdf4659c7ec31f0cd9920fc57e693c8d04bc80 100644 --- a/src/main/java/module-info.java +++ b/src/main/java/module-info.java @@ -6,6 +6,8 @@ module sk.kasv.schmidt.javafx.worldx_gui { requires com.dlsc.formsfx; requires org.kordamp.bootstrapfx.core; requires java.sql; + requires java.net.http; + requires org.json; opens sk.kasv.schmidt.javafx.worldx_gui.Controller to javafx.fxml; exports sk.kasv.schmidt.javafx.worldx_gui.Controller; diff --git a/src/main/java/sk/kasv/schmidt/javafx/worldx_gui/API/ApiCaller.java b/src/main/java/sk/kasv/schmidt/javafx/worldx_gui/API/ApiCaller.java new file mode 100644 index 0000000000000000000000000000000000000000..e85f1117719ef54d50e13f32b258a25362d7cf12 --- /dev/null +++ b/src/main/java/sk/kasv/schmidt/javafx/worldx_gui/API/ApiCaller.java @@ -0,0 +1,41 @@ +package sk.kasv.schmidt.javafx.worldx_gui.API; + +import java.io.IOException; +import java.net.URI; +import java.net.http.HttpClient; +import java.net.http.HttpRequest; +import java.net.http.HttpResponse; + +import org.json.JSONObject; + +public class ApiCaller { + public static double getTemperature(String city, String countryCode3) { + if (city.contains(" ")) { + city = city.replace(" ", "%20"); + } + var uri = URI.create("https://api.openweathermap.org/data/2.5/weather?q="+city+","+countryCode3+"&units=metric&appid=e87d429814426dd37aaf988ea289b362"); + var client = HttpClient.newHttpClient(); + var request = HttpRequest + .newBuilder() + .uri(uri) + .header("accept", "application/json") + .GET() + .build(); + HttpResponse<String> response; + try { + response = client.send(request, HttpResponse.BodyHandlers.ofString()); + int statusCode = response.statusCode(); + if (statusCode == 200) { + JSONObject jsonResponse = new JSONObject(response.body()); + JSONObject main = jsonResponse.getJSONObject("main"); + double temperature = main.getDouble("temp"); + return temperature; + } else { + System.out.println("Request failed with status code: " + statusCode); + } + } catch (IOException | InterruptedException e) { + e.printStackTrace(); + } + return 0; + } +} \ No newline at end of file diff --git a/src/main/java/sk/kasv/schmidt/javafx/worldx_gui/Controller/WorldXController.java b/src/main/java/sk/kasv/schmidt/javafx/worldx_gui/Controller/WorldXController.java index a91dcd1f75771b7da0b142174590fe20990f9d30..7e56063ab2c1067543b6e34fed8afec7f7d4ed31 100644 --- a/src/main/java/sk/kasv/schmidt/javafx/worldx_gui/Controller/WorldXController.java +++ b/src/main/java/sk/kasv/schmidt/javafx/worldx_gui/Controller/WorldXController.java @@ -3,16 +3,23 @@ package sk.kasv.schmidt.javafx.worldx_gui.Controller; import javafx.fxml.FXML; import javafx.scene.control.ChoiceBox; import javafx.scene.control.Label; +import sk.kasv.schmidt.javafx.worldx_gui.API.ApiCaller; import sk.kasv.schmidt.javafx.worldx_gui.Database.DB; import java.sql.SQLException; import java.util.List; public class WorldXController { + @FXML public ChoiceBox<String> cities; + + @FXML public Label cityLabel; + public Label tempOutput; + public Label population; + @FXML private ChoiceBox<String> countries; @@ -24,11 +31,25 @@ public class WorldXController { countries.getItems().addAll(" "); countries.getItems().addAll(countryList); - countries.setOnAction(e -> { - List<String> cityList = DB.getCitiesByCountry(countries.getValue()); - cities.getItems().clear(); - cities.getItems().addAll(cityList); - }); + countries.setOnAction(e -> { + List<String> cityList = DB.getCitiesByCountry(countries.getValue()); + cities.getItems().clear(); + cities.getItems().addAll(cityList); + cities.setValue(null); + }); + cities.setOnAction(e -> { + if (cities.getValue() == null) { + tempOutput.setVisible(false); + population.setVisible(false); + } + else { + tempOutput.setVisible(true); + population.setVisible(true); + tempOutput.setText(ApiCaller.getTemperature(cities.getValue(), + DB.get3LetterCode(countries.getValue())) + "°C"); + population.setText(""+DB.getPopulation(cities.getValue())); + }}); + } catch (SQLException e) { e.printStackTrace(); } diff --git a/src/main/java/sk/kasv/schmidt/javafx/worldx_gui/Database/DB.java b/src/main/java/sk/kasv/schmidt/javafx/worldx_gui/Database/DB.java index ed133c6cc772818c1e48bc5d5c589273fc968849..594c1c2b0f7f90893d486d82ad0bf7e741245fb5 100644 --- a/src/main/java/sk/kasv/schmidt/javafx/worldx_gui/Database/DB.java +++ b/src/main/java/sk/kasv/schmidt/javafx/worldx_gui/Database/DB.java @@ -2,14 +2,18 @@ package sk.kasv.schmidt.javafx.worldx_gui.Database; import java.sql.*; import java.util.ArrayList; + import java.util.List; +import org.json.JSONObject; public class DB { private static final String URL = "jdbc:mysql://localhost:3306/world_x"; private static final String USER = "root"; private static final String PASS = ""; private static final String SELECT_ALL_COUNTRIES = "SELECT Name FROM country"; - private static final String SELECT_ALL_CITIES = "SELECT Name FROM city"; + private static final String SELECT_3LETTER_CODE = "SELECT Code FROM country WHERE Name = ?"; + private static final String SELECT_CITIES_BY_COUNTRY = + "SELECT city.Name FROM city JOIN country ON city.CountryCode = country.Code WHERE country.Name = ?"; public static void DBConnection() { try { @@ -33,23 +37,10 @@ public class DB { return countries; } - public static List<String> getAllCities() throws SQLException { - List<String> cities = new ArrayList<>(); - Connection connection = DriverManager.getConnection(URL, USER, PASS); - PreparedStatement preparedStatement = connection.prepareStatement(SELECT_ALL_CITIES); - ResultSet resultSet = preparedStatement.executeQuery(); - - while (resultSet.next()) { - cities.add(resultSet.getString("Name")); - } - return cities; - } - public static List<String> getCitiesByCountry(String selectedCountry) { List<String> cities = new ArrayList<>(); try { Connection connection = DriverManager.getConnection(URL, USER, PASS); - String SELECT_CITIES_BY_COUNTRY = "SELECT city.Name FROM city JOIN country ON city.CountryCode = country.Code WHERE country.Name = ?"; PreparedStatement preparedStatement = connection.prepareStatement(SELECT_CITIES_BY_COUNTRY); preparedStatement.setString(1, selectedCountry); ResultSet resultSet = preparedStatement.executeQuery(); @@ -67,4 +58,48 @@ public class DB { return cities; } + public static String get3LetterCode(String countryName) { + String countryCode3 = ""; + try { + Connection connection = DriverManager.getConnection(URL, USER, PASS); + PreparedStatement preparedStatement = connection.prepareStatement(SELECT_3LETTER_CODE); + preparedStatement.setString(1, countryName); + ResultSet resultSet = preparedStatement.executeQuery(); + + while (resultSet.next()) { + countryCode3 = resultSet.getString("Code"); + } + + resultSet.close(); + preparedStatement.close(); + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + + } + return countryCode3; + } + + public static int getPopulation(String city) { + int population = 0; + try { + Connection connection = DriverManager.getConnection(URL, USER, PASS); + PreparedStatement preparedStatement = connection.prepareStatement("SELECT Info FROM city WHERE Name = ?"); + preparedStatement.setString(1, city); + ResultSet resultSet = preparedStatement.executeQuery(); + + while (resultSet.next()) { + String info = resultSet.getString("Info"); + JSONObject json = new JSONObject(info); + population = json.getInt("Population"); + } + + resultSet.close(); + preparedStatement.close(); + connection.close(); + } catch (SQLException e) { + e.printStackTrace(); + } + return population; + } } diff --git a/src/main/java/sk/kasv/schmidt/javafx/worldx_gui/Database/DatabaseTest/DBTest.java b/src/main/java/sk/kasv/schmidt/javafx/worldx_gui/Database/DatabaseTest/DBTest.java index 17b3fcc74c75e2078eb9eceba3d8b7e695a6e4ee..21f8a2600bf72331e10b05dd20f44dcd99c29c5b 100644 --- a/src/main/java/sk/kasv/schmidt/javafx/worldx_gui/Database/DatabaseTest/DBTest.java +++ b/src/main/java/sk/kasv/schmidt/javafx/worldx_gui/Database/DatabaseTest/DBTest.java @@ -1,5 +1,7 @@ package sk.kasv.schmidt.javafx.worldx_gui.Database.DatabaseTest; +import sk.kasv.schmidt.javafx.worldx_gui.Database.DB; + import java.sql.DriverManager; import java.sql.SQLException; @@ -16,5 +18,6 @@ public class DBTest { System.out.println("Connection failed!"); e.printStackTrace(); } + System.out.println(DB.getPopulation("Bratislava")); } } \ No newline at end of file diff --git a/src/main/resources/sk/kasv/schmidt/javafx/worldx_gui/hello-view.fxml b/src/main/resources/sk/kasv/schmidt/javafx/worldx_gui/hello-view.fxml index bc9d63428113546b9e861718adf296f86588b52d..f7a47cfa752baff5b13e90f091ae72194d79d911 100644 --- a/src/main/resources/sk/kasv/schmidt/javafx/worldx_gui/hello-view.fxml +++ b/src/main/resources/sk/kasv/schmidt/javafx/worldx_gui/hello-view.fxml @@ -14,27 +14,48 @@ <font> <Font size="20.0" /> </font></Label> - <GridPane alignment="CENTER"> + <GridPane alignment="CENTER" prefHeight="113.0" prefWidth="378.0"> <columnConstraints> - <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/> - <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0"/> + <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> + <ColumnConstraints hgrow="SOMETIMES" minWidth="10.0" prefWidth="100.0" /> </columnConstraints> <rowConstraints> - <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/> - <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/> - <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES"/> + <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> + <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> + <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> + <RowConstraints minHeight="10.0" prefHeight="30.0" vgrow="SOMETIMES" /> </rowConstraints> <Label alignment="CENTER" contentDisplay="CENTER" text="Countries:"> <font> - <Font size="18.0"/> + <Font size="18.0" /> </font> </Label> - <ChoiceBox fx:id="countries" prefWidth="150.0" GridPane.columnIndex="1"/> - <ChoiceBox fx:id="cities" prefWidth="150.0" GridPane.columnIndex="1" GridPane.rowIndex="1"/> + <ChoiceBox fx:id="countries" prefWidth="150.0" GridPane.columnIndex="1" /> + <ChoiceBox fx:id="cities" prefWidth="150.0" GridPane.columnIndex="1" GridPane.rowIndex="1" /> <Label fx:id="cityLabel" text="Cities:" GridPane.rowIndex="1"> <font> - <Font size="18.0"/> + <Font size="18.0" /> </font> </Label> + <Label text="Temperature:" textAlignment="CENTER" GridPane.rowIndex="3"> + <font> + <Font size="18.0" /> + </font> + </Label> + <Label fx:id="tempOutput" prefHeight="27.0" prefWidth="172.0" GridPane.columnIndex="1" GridPane.rowIndex="3"> + <font> + <Font size="18.0" /> + </font> + </Label> + <Label text="Population:" GridPane.rowIndex="2"> + <font> + <Font size="18.0" /> + </font> + </Label> + <Label fx:id="population" prefHeight="27.0" prefWidth="190.0" GridPane.columnIndex="1" GridPane.rowIndex="2"> + <font> + <Font size="18.0" /> + </font> + </Label> </GridPane> </VBox>