Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 16 additions & 0 deletions Report.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# 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.

## 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.
3 changes: 3 additions & 0 deletions build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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'


}

Expand Down
41 changes: 31 additions & 10 deletions src/main/java/WeatherApp.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,25 @@
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) {

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)));
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);
Expand All @@ -38,15 +43,31 @@ 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);
answer = tem.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 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;
}

}