diff --git a/build.gradle b/build.gradle index cfc21e9..2cf926e 100644 --- a/build.gradle +++ b/build.gradle @@ -12,6 +12,8 @@ repositories { dependencies { testImplementation 'org.junit.jupiter:junit-jupiter-api:5.8.1' testRuntimeOnly 'org.junit.jupiter:junit-jupiter-engine:5.8.1' + // https://mvnrepository.com/artifact/org.json/json + implementation group: 'org.json', name: 'json', version: '20230227' } diff --git a/report.md b/report.md new file mode 100644 index 0000000..ce60c7b --- /dev/null +++ b/report.md @@ -0,0 +1,9 @@ +# Weather app assignment +## Introduction +In this assignment, we built a simple Java application that connects to a weather API and retrieves information about the current weather conditions of a given city. +## Design and Implemention +At first the city's name is passed to the getWeatherData method and it returns a string.Then the string is passed to getTemperature(or getHumidity, etc) method and we can get the information we want. +## Testing and Evaluation +To make sure the program works properly, I checked the results with the correct weather information of that city. +## Conclusion +This assignment helped me to learn how to use JSON objects. diff --git a/src/main/java/WeatherApp.java b/src/main/java/WeatherApp.java index 8cecccd..5cb4cc2 100644 --- a/src/main/java/WeatherApp.java +++ b/src/main/java/WeatherApp.java @@ -7,10 +7,16 @@ public class WeatherApp { // Copy your API-KEY here - public final static String apiKey = "API-KEY"; + public final static String apiKey = "67a6c2c3591e4e10839152337232502"; // TODO: Write main function public static void main(String[] args) { + Scanner input = new Scanner(System.in); + String city = input.nextLine(); + System.out.println("Temperature = " + getTemperature(getWeatherData(city))); + System.out.println("Humidity = " + getHumidity(getWeatherData(city))); + System.out.println("Wind speed = " + getWindSpeed(getWeatherData(city))); + System.out.println("Wind direction = " + getWindDir(getWeatherData(city))); } /** @@ -41,12 +47,32 @@ public static String getWeatherData(String city) { // TODO: Write getTemperature function returns celsius temperature of city by given json string public static double getTemperature(String weatherJson){ double answer = 0.0; + JSONObject data = new JSONObject(weatherJson); + answer = data.getJSONObject("current").getDouble("temp_c"); return answer; } // TODO: Write getHumidity function returns humidity percentage of city by given json string public static int getHumidity(String weatherJson){ int answer = 0; + JSONObject data = new JSONObject(weatherJson); + answer = data.getJSONObject("current").getInt("humidity"); return answer; } + + public static double getWindSpeed(String weatherJson){ + double answer = 0.0; + JSONObject data = new JSONObject(weatherJson); + answer = data.getJSONObject("current").getDouble("wind_mph"); + return answer; + } + + public static String getWindDir(String weatherJson){ + String answer; + JSONObject data = new JSONObject(weatherJson); + answer = data.getJSONObject("current").getString("wind_dir"); + return answer; + } + + }