diff --git a/build.gradle b/build.gradle index cfc21e9..a6e550c 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: '20220924' } diff --git a/report.txt b/report.txt new file mode 100644 index 0000000..cbc8d3a --- /dev/null +++ b/report.txt @@ -0,0 +1,14 @@ +apikey ro az site gofte shode dar file readme bardashtim va dar string apiKey gozashtim +yek string be nam e city tarif kardim va un ro az karbar daryaft mikonim +string city ro be method getWeatherData pass midim va khuruji in method ro dakhele string weatherData mirizim +method getWeatherData az targih api etelaati daryaft mikone va be ma khuruji mide ke json hastesh va ma bayad ba estefade az tavabe mojud dar library json in etelaat ro estekhraj konim ke inkar tavasot method haye getTemperature , getHumidity , getWindSpeed va getWindDirection anjam mishe +json mesle hashmap daraye key va value hastesh +ma dar hameye tavabe getTemperature , getHumidity , getWindSpeed va getWindDirection miaim ba estefade az tavabe ketabkhune json etelaate morede nazar ro daryaft mikonim +dar method getTemperature ma yek answer tarif mikonim ke az jense double hast +dar vaghe in answer ke khurujie tabe hastesh temperatue shahre morede nazare karbar hastesh +ma yek jsonobject be nam e data tarif mikonim va ba estefade az tavabe ketabkhune json temperature shahre morede nazare karbar ro dakhele answer mirizim va dar nahayat answer ro return mikonim +baraye baghie tavabe getHumidiy , getWindSpeed va getWindDirection ham be hamin surat amal mikonim +dar hameye tavabe getTemperature , getHumidity , getWindSpeed va getWindDirection az data.getJSONObject().getStirng() estefade kardim +ke ebarat haye dakhele parantez key hastand va code [ data.getJSONObject().getStirng()().getStirng() ] value morede nazare ma ro return mikone +ma khurujie tavabe neveshte shode ro dar moteghayer hayi ba esm moshabeh be tabe nazir khodeshun rikhtim va dar nahayat chap kardim + diff --git a/src/main/java/WeatherApp.java b/src/main/java/WeatherApp.java index 8cecccd..59f0466 100644 --- a/src/main/java/WeatherApp.java +++ b/src/main/java/WeatherApp.java @@ -6,11 +6,24 @@ import java.util.Scanner; public class WeatherApp { - // Copy your API-KEY here - public final static String apiKey = "API-KEY"; + public final static String apiKey = "ac6b2d9d5637408890e32801232802"; + // TODO: Write main function public static void main(String[] args) { + Scanner in = new Scanner(System.in); + System.out.print("Please enter the city name :"); + String city = in.next(); + + String weatherData = getWeatherData(city); + double temperature = getTemperature(weatherData); + int humidity = getHumidity(weatherData); + double windMph = getWindSpeed(weatherData); + String windDir = getWindDirection(weatherData); + System.out.println("The temperature in " + city + " is " + temperature); + System.out.println("The humidity percentage in " + city + " is " + humidity); + System.out.println("The wind mph in " + city + " is " + windMph); + System.out.println("The wind direction in " + city + " is " + windDir); } /** @@ -41,12 +54,28 @@ 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 getWindDirection(String weatherJson){ + String answer = ""; + JSONObject data = new JSONObject(weatherJson); + answer = data.getJSONObject("current").getString("wind_dir"); return answer; } -} +} \ No newline at end of file