From bbd66e209dfef5ca1d6551c7789b630e674dd349 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 1 Mar 2023 21:04:29 +0330 Subject: [PATCH 1/6] added dependencies section --- build.gradle | 3 +++ 1 file changed, 3 insertions(+) diff --git a/build.gradle b/build.gradle index cfc21e9..92b0ad7 100644 --- a/build.gradle +++ b/build.gradle @@ -12,6 +12,9 @@ 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' + } From 5c0a9bb9b2be2e7ff0c8839ed8ccd531ae347946 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 1 Mar 2023 21:17:58 +0330 Subject: [PATCH 2/6] updated --- src/main/java/WeatherApp.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/src/main/java/WeatherApp.java b/src/main/java/WeatherApp.java index 8cecccd..8f648c5 100644 --- a/src/main/java/WeatherApp.java +++ b/src/main/java/WeatherApp.java @@ -5,12 +5,16 @@ import org.json.JSONObject; import java.util.Scanner; + + public class WeatherApp { // Copy your API-KEY here - public final static String apiKey = "API-KEY"; + public final static String apiKey = "b36dc40757fb415fa35192654232602"; // TODO: Write main function public static void main(String[] args) { + + System.out.println("temperature : " + getTemperature(getWeatherData(city))); } /** @@ -41,6 +45,8 @@ 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 tem = new JSONObject(String weatherJson); + answer = tem.getJSONObject("current").getDouble("temp_c"); return answer; } From 0e816564b2e88e0ac2b1b8430b9bf206ccc0ca18 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 1 Mar 2023 22:34:29 +0330 Subject: [PATCH 3/6] added main function --- Report.md | 1 + src/main/java/WeatherApp.java | 26 ++++++++++++++++++++++---- 2 files changed, 23 insertions(+), 4 deletions(-) create mode 100644 Report.md diff --git a/Report.md b/Report.md new file mode 100644 index 0000000..f6ecaa6 --- /dev/null +++ b/Report.md @@ -0,0 +1 @@ +# Introduction \ No newline at end of file diff --git a/src/main/java/WeatherApp.java b/src/main/java/WeatherApp.java index 8f648c5..97a28c4 100644 --- a/src/main/java/WeatherApp.java +++ b/src/main/java/WeatherApp.java @@ -12,9 +12,12 @@ public class WeatherApp { public final static String apiKey = "b36dc40757fb415fa35192654232602"; // TODO: Write main function public static void main(String[] args) { - - - System.out.println("temperature : " + getTemperature(getWeatherData(city))); + Scanner scanner = new Scanner(System.in); + String city = scanner.next(); + System.out.println("Temperature in C is : " + getTemperature(getWeatherData(city)) + "degree"); + System.out.println("Humidity is : "+ getHumidity(getWeatherData(city))); + System.out.println("Wind speeed in kph is :" + getWind(getWeatherData(city))); + System.out.println("Wind direction is : " + getWindDirection(getWeatherData(city))); } /** @@ -45,7 +48,7 @@ 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 tem = new JSONObject(String weatherJson); + JSONObject tem = new JSONObject(weatherJson); answer = tem.getJSONObject("current").getDouble("temp_c"); return answer; } @@ -53,6 +56,21 @@ public static double getTemperature(String weatherJson){ // TODO: Write getHumidity function returns humidity percentage of city by given json string public static int getHumidity(String weatherJson){ int answer = 0; + JSONObject humidity = new JSONObject(weatherJson); + answer = humidity.getJSONObject("current").getInt("humidity"); + return answer; + } + public static double getWind(String weatherJson){ + double answer = 0.0; + JSONObject wind = new JSONObject(weatherJson); + answer = wind.getJSONObject("current").getDouble("wind_kph"); + return answer; + } + public static String getWindDirection(String weatherJson){ + String answer = ""; + JSONObject windDir = new JSONObject(weatherJson); + answer = windDir.getJSONObject("current").getString("wind_dir"); return answer; } + } From 31b3efa5f20642dc3c71f7ce0e4c55f257893112 Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 1 Mar 2023 23:36:48 +0330 Subject: [PATCH 4/6] Report added --- Report.md | 15 ++++++++++++++- src/main/java/WeatherApp.java | 13 +++++-------- 2 files changed, 19 insertions(+), 9 deletions(-) diff --git a/Report.md b/Report.md index f6ecaa6..cbb4ef6 100644 --- a/Report.md +++ b/Report.md @@ -1 +1,14 @@ -# Introduction \ No newline at end of file +# Weather App (Secend Assignment) + +## introduction : +in this assignment we write a program to first get a city name from the user and then show him/her the temprature, humidity, wind speed and direction of that city. + +## Design and Implementation : +- We get an API key and store it in string api. +- and use JSONObject to get the online data. +- There is a getWeather function that get the data we want from online website with the name of the city. +- I write 4 function which any of them show the temperature, humidity, wind speed and wind direction of the city. +- In the main function we use Scanner function to get the city from the user. +- and then show the data that we get on the next 4 lines. + + diff --git a/src/main/java/WeatherApp.java b/src/main/java/WeatherApp.java index 97a28c4..4a12195 100644 --- a/src/main/java/WeatherApp.java +++ b/src/main/java/WeatherApp.java @@ -18,14 +18,12 @@ public static void main(String[] args) { System.out.println("Humidity is : "+ getHumidity(getWeatherData(city))); System.out.println("Wind speeed in kph is :" + getWind(getWeatherData(city))); System.out.println("Wind direction is : " + getWindDirection(getWeatherData(city))); + if (getWeatherData(city) == null) { + System.out.println("wrong name!"); + } } - /** - * Retrieves weather data for the specified city. - * - * @param city the name of the city for which weather data should be retrieved - * @return a string representation of the weather data, or null if an error occurred - */ + public static String getWeatherData(String city) { try { URL url = new URL("http://api.weatherapi.com/v1/current.json?key=" + apiKey + "&q=" + city); @@ -45,7 +43,7 @@ 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 tem = new JSONObject(weatherJson); @@ -53,7 +51,6 @@ public static double getTemperature(String weatherJson){ 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 humidity = new JSONObject(weatherJson); From ef80068693a0dd79e9e69592623963fbba00fe8f Mon Sep 17 00:00:00 2001 From: unknown Date: Wed, 1 Mar 2023 23:49:47 +0330 Subject: [PATCH 5/6] last update --- Report.md | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/Report.md b/Report.md index cbb4ef6..4b94917 100644 --- a/Report.md +++ b/Report.md @@ -11,4 +11,6 @@ in this assignment we write a program to first get a city name from the user and - In the main function we use Scanner function to get the city from the user. - and then show the data that we get on the next 4 lines. - +## Conlusion +We used Json and API in this program to get data throw website and show them on the terminal +User give us a name of a city and we show some weather data of that city. From a19cdd7be12b4236d6563a94842e26c536a4ff60 Mon Sep 17 00:00:00 2001 From: unknown Date: Mon, 6 Mar 2023 18:55:44 +0330 Subject: [PATCH 6/6] git add . --- src/main/java/WeatherApp.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/java/WeatherApp.java b/src/main/java/WeatherApp.java index 4a12195..141d3db 100644 --- a/src/main/java/WeatherApp.java +++ b/src/main/java/WeatherApp.java @@ -19,7 +19,7 @@ public static void main(String[] args) { System.out.println("Wind speeed in kph is :" + getWind(getWeatherData(city))); System.out.println("Wind direction is : " + getWindDirection(getWeatherData(city))); if (getWeatherData(city) == null) { - System.out.println("wrong name!"); + System.out.println("wrong name!!"); } }