Merge branch 'api_setup'

distance_method
Selim Eser 2024-06-09 04:46:11 +02:00
commit 5a4cc07d9e
6 changed files with 16750 additions and 6 deletions

View File

@ -30,6 +30,11 @@
</dependency> </dependency>
</dependencies> </dependencies>
<build> <build>
<resources>
<resource>
<directory>src/main/resources</directory>
</resource>
</resources>
<plugins> <plugins>
<plugin> <plugin>
<groupId>org.apache.maven.plugins</groupId> <groupId>org.apache.maven.plugins</groupId>

View File

@ -1,11 +1,27 @@
package de.hs_mannheim.domain; package de.hs_mannheim.domain;
import java.net.URI;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers;
import java.util.HashSet; import java.util.HashSet;
import java.util.TreeSet; import java.util.TreeSet;
import org.json.JSONObject;
public class System { public class System {
private User current_user; private User current_user = new User();
private String api_key;
public System(String api_key) {
this.api_key = api_key;
}
public void set_current_user_zip(String zip){
this.current_user.setZip(Integer.parseInt(zip));
}
public HashSet<User> get_all_user(){ public HashSet<User> get_all_user(){
return new HashSet<User>(); return new HashSet<User>();
@ -40,11 +56,128 @@ public class System {
} }
public String current_weather(){ public String current_weather(){
return "";
String weather = "";
double temperature = 1;
try{
HttpClient http_client = HttpClient.newHttpClient();
HttpRequest get_request = HttpRequest.newBuilder()
.uri(new URI("https://api.openweathermap.org/data/2.5/weather?zip="+current_user.getZip()+",de&appid="+api_key+"&units=metric&lang=de"))
.GET()
.build();
HttpResponse get_response = http_client.send(get_request, BodyHandlers.ofString());
JSONObject json = new JSONObject(((String)get_response.body()).substring(0,((String) get_response.body()).length()));
weather = json.getJSONArray("weather").getJSONObject(0).getString("description");
temperature = json.getJSONObject("main").getDouble("temp");
} catch (Exception e){}
if(weather.equals(""))
return "Es ist ein Fehler aufgetreten!";
else
return weather + ": " + temperature + " °C";
} }
public String weather_forecast(String destination_zip){ public String weather_forecast(String destination_zip){
return "";
String weather_day_1 = "";
String weather_day_2 = "";
String weather_day_3 = "";
double temperature_day_1_1 = 1;
double temperature_day_1_2 = 1;
double temperature_day_1_3 = 1;
double temperature_day_1_4 = 1;
TreeSet<Double> temperature_day_1 = new TreeSet<>();
double temperature_day_1_high = 1;
double temperature_day_1_low = 1;
double temperature_day_2_1 = 1;
double temperature_day_2_2 = 1;
double temperature_day_2_3 = 1;
double temperature_day_2_4 = 1;
TreeSet<Double> temperature_day_2 = new TreeSet<>();
double temperature_day_2_high = 1;
double temperature_day_2_low = 1;
double temperature_day_3_1 = 1;
double temperature_day_3_2 = 1;
double temperature_day_3_3 = 1;
double temperature_day_3_4 = 1;
TreeSet<Double> temperature_day_3 = new TreeSet<>();
double temperature_day_3_high = 1;
double temperature_day_3_low = 1;
try{
HttpClient http_client = HttpClient.newHttpClient();
HttpRequest get_request = HttpRequest.newBuilder()
.uri(new URI("https://api.openweathermap.org/data/2.5/forecast?zip="+destination_zip+",de&appid="+api_key+"&units=metric&lang=de"))
.GET()
.build();
HttpResponse get_response = http_client.send(get_request, BodyHandlers.ofString());
JSONObject json = new JSONObject(((String)get_response.body()).substring(0,((String) get_response.body()).length()));
weather_day_1 = json.getJSONArray("list").getJSONObject(11).getJSONArray("weather").getJSONObject(0).getString("description");
weather_day_2 = json.getJSONArray("list").getJSONObject(19).getJSONArray("weather").getJSONObject(0).getString("description");
weather_day_3 = json.getJSONArray("list").getJSONObject(27).getJSONArray("weather").getJSONObject(0).getString("description");
temperature_day_1_1 = json.getJSONArray("list").getJSONObject(8).getJSONObject("main").getDouble("temp");
temperature_day_1_2 = json.getJSONArray("list").getJSONObject(10).getJSONObject("main").getDouble("temp");
temperature_day_1_3 = json.getJSONArray("list").getJSONObject(12).getJSONObject("main").getDouble("temp");
temperature_day_1_4 = json.getJSONArray("list").getJSONObject(14).getJSONObject("main").getDouble("temp");
temperature_day_2_1 = json.getJSONArray("list").getJSONObject(16).getJSONObject("main").getDouble("temp");
temperature_day_2_2 = json.getJSONArray("list").getJSONObject(18).getJSONObject("main").getDouble("temp");
temperature_day_2_3 = json.getJSONArray("list").getJSONObject(20).getJSONObject("main").getDouble("temp");
temperature_day_2_4 = json.getJSONArray("list").getJSONObject(22).getJSONObject("main").getDouble("temp");
temperature_day_3_1 = json.getJSONArray("list").getJSONObject(24).getJSONObject("main").getDouble("temp");
temperature_day_3_2 = json.getJSONArray("list").getJSONObject(26).getJSONObject("main").getDouble("temp");
temperature_day_3_3 = json.getJSONArray("list").getJSONObject(28).getJSONObject("main").getDouble("temp");
temperature_day_3_4 = json.getJSONArray("list").getJSONObject(30).getJSONObject("main").getDouble("temp");
} catch (Exception e){}
temperature_day_1.add(temperature_day_1_1);
temperature_day_1.add(temperature_day_1_2);
temperature_day_1.add(temperature_day_1_3);
temperature_day_1.add(temperature_day_1_4);
temperature_day_1_high = (double) temperature_day_1.toArray()[temperature_day_1.size()-1];
temperature_day_1_low = (double) temperature_day_1.toArray()[0];
temperature_day_2.add(temperature_day_2_1);
temperature_day_2.add(temperature_day_2_2);
temperature_day_2.add(temperature_day_2_3);
temperature_day_2.add(temperature_day_2_4);
temperature_day_2_high = (double) temperature_day_2.toArray()[temperature_day_1.size()-1];
temperature_day_2_low = (double) temperature_day_2.toArray()[0];
temperature_day_3.add(temperature_day_3_1);
temperature_day_3.add(temperature_day_3_2);
temperature_day_3.add(temperature_day_3_3);
temperature_day_3.add(temperature_day_3_4);
temperature_day_3_high = (double) temperature_day_3.toArray()[temperature_day_1.size()-1];
temperature_day_3_low = (double) temperature_day_3.toArray()[0];
if(weather_day_1.equals("")||weather_day_2.equals("")||weather_day_3.equals(""))
return "Es ist ein Fehler aufgetreten!";
else
return "Morgen: " + weather_day_1 + ": Minimum: " + temperature_day_1_low + " °C" + "; Maximum: " + temperature_day_1_high + " °C\n"
+ "Übermorgen: " + weather_day_2 + ": Minimum: " + temperature_day_2_low + " °C" + "; Maximum: " + temperature_day_2_high + " °C\n"
+ "Überübermorgen: " + weather_day_3 + ": Minimum: " + temperature_day_3_low + " °C" + "; Maximum: " + temperature_day_3_high + " °C";
} }
public String distance(String destination_zip){ public String distance(String destination_zip){

View File

@ -8,8 +8,8 @@ public class Application {
private System running_system; private System running_system;
public Application(){ public Application(String api_key){
this.running_system = new System(); this.running_system = new System(api_key);
} }
public boolean sign_in_user(String username, String password){ public boolean sign_in_user(String username, String password){

File diff suppressed because it is too large Load Diff

View File

@ -0,0 +1,28 @@
package de.hs_mannheim.domain;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals;
import org.junit.jupiter.api.Test;
public class SystemTest {
@Test
public void weather_forecast(){
System current_system = new System("35a75437476f12302f72e55d368485db");
assertNotEquals("Es ist ein Fehler aufgetreten!",current_system.weather_forecast("68161"));
}
@Test
public void current_weather(){
System current_system = new System("35a75437476f12302f72e55d368485db");
current_system.set_current_user_zip("68161");
assertNotEquals("Es ist ein Fehler aufgetreten!",current_system.current_weather());
}
}

File diff suppressed because it is too large Load Diff