2024-06-06 11:22:54 +02:00
|
|
|
package de.hs_mannheim.domain;
|
|
|
|
|
2024-06-09 15:33:13 +02:00
|
|
|
import java.io.BufferedReader;
|
2024-06-09 15:58:25 +02:00
|
|
|
import java.io.IOException;
|
2024-06-09 15:33:13 +02:00
|
|
|
import java.io.InputStream;
|
|
|
|
import java.io.InputStreamReader;
|
2024-06-08 22:02:40 +02:00
|
|
|
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;
|
2024-06-09 15:33:13 +02:00
|
|
|
import java.nio.charset.StandardCharsets;
|
2024-06-09 15:17:47 +02:00
|
|
|
import java.util.ArrayList;
|
2024-06-06 11:22:54 +02:00
|
|
|
import java.util.HashSet;
|
2024-06-09 18:38:38 +02:00
|
|
|
import java.util.List;
|
2024-06-06 11:29:09 +02:00
|
|
|
import java.util.TreeSet;
|
2024-06-06 11:22:54 +02:00
|
|
|
|
2024-06-08 22:02:40 +02:00
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2024-06-09 15:33:13 +02:00
|
|
|
import de.hs_mannheim.ui.Main;
|
|
|
|
|
2024-06-06 11:22:54 +02:00
|
|
|
public class System {
|
|
|
|
|
2024-06-09 04:45:31 +02:00
|
|
|
private User current_user = new User();
|
2024-06-08 15:44:40 +02:00
|
|
|
private String api_key;
|
|
|
|
|
2024-06-09 02:52:50 +02:00
|
|
|
public System(String api_key) {
|
2024-06-08 15:44:40 +02:00
|
|
|
this.api_key = api_key;
|
|
|
|
}
|
2024-06-06 11:22:54 +02:00
|
|
|
|
2024-06-09 04:45:31 +02:00
|
|
|
public void set_current_user_zip(String zip){
|
|
|
|
this.current_user.setZip(Integer.parseInt(zip));
|
|
|
|
}
|
|
|
|
|
2024-06-06 11:22:54 +02:00
|
|
|
public HashSet<User> get_all_user(){
|
|
|
|
return new HashSet<User>();
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean sign_in_user(String username, String password){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean sign_up_user(String username, String password, String hometown, int zip,
|
|
|
|
String car_name, double car_co2_km, double car_avg_kmh, double bike_avg_kmh){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void sign_out_user(){}
|
|
|
|
|
|
|
|
public boolean change_user_details(String username, String password, String hometown, int zip,
|
|
|
|
String car_name, double car_co2_km, double car_avg_kmh, double bike_avg_kmh){
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2024-06-09 15:17:47 +02:00
|
|
|
public ArrayList<String> search(String hometown_or_zip){
|
2024-06-09 15:33:13 +02:00
|
|
|
|
|
|
|
TreeSet<String> zip_set = new TreeSet<>();
|
|
|
|
|
2024-06-09 15:58:25 +02:00
|
|
|
InputStream inputStream = Main.class.getResourceAsStream("/zip.csv");
|
2024-06-09 15:33:13 +02:00
|
|
|
|
2024-06-09 15:58:25 +02:00
|
|
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
|
2024-06-09 15:33:13 +02:00
|
|
|
String line;
|
|
|
|
while ((line = reader.readLine()) != null) {
|
2024-06-09 15:58:25 +02:00
|
|
|
if(line.contains(hometown_or_zip)&&zip_set.size()<200){
|
|
|
|
line = line.replace("\"", "");
|
2024-06-09 15:33:13 +02:00
|
|
|
zip_set.add(line);
|
2024-06-09 15:58:25 +02:00
|
|
|
}
|
2024-06-09 15:33:13 +02:00
|
|
|
}
|
2024-06-09 16:39:05 +02:00
|
|
|
} catch (Exception e) {}
|
2024-06-09 15:33:13 +02:00
|
|
|
|
|
|
|
return new ArrayList<>(zip_set);
|
2024-06-06 11:29:09 +02:00
|
|
|
}
|
|
|
|
|
2024-06-09 16:06:15 +02:00
|
|
|
public ArrayList<String> random_destinations_car(){
|
2024-06-09 18:38:38 +02:00
|
|
|
|
|
|
|
ArrayList<String> result = new ArrayList<>();
|
|
|
|
|
|
|
|
InputStream inputStream = Main.class.getResourceAsStream("/zip.csv");
|
|
|
|
|
|
|
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
|
|
|
|
String line;
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
|
|
|
|
line = line.replace("\"", "");
|
|
|
|
|
|
|
|
if(Double.parseDouble(distance(line.split(";")[0]).replace(" km", "")) > 150)
|
|
|
|
result.add(line);
|
|
|
|
|
|
|
|
}
|
|
|
|
} catch (Exception e) {}
|
|
|
|
|
|
|
|
return result;
|
2024-06-09 16:06:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public ArrayList<String> random_destinations_bike(){
|
2024-06-09 15:17:47 +02:00
|
|
|
return new ArrayList<String>();
|
2024-06-06 11:29:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public String[] destination_details(String destination_zip){
|
|
|
|
return new String[1];
|
|
|
|
}
|
|
|
|
|
2024-06-09 02:15:45 +02:00
|
|
|
public String current_weather(){
|
2024-06-08 22:02:40 +02:00
|
|
|
|
2024-06-09 02:15:45 +02:00
|
|
|
String weather = "";
|
2024-06-09 04:45:31 +02:00
|
|
|
double temperature = 1;
|
|
|
|
|
2024-06-09 02:15:45 +02:00
|
|
|
try{
|
2024-06-08 22:02:40 +02:00
|
|
|
HttpClient http_client = HttpClient.newHttpClient();
|
|
|
|
|
|
|
|
HttpRequest get_request = HttpRequest.newBuilder()
|
2024-06-09 04:45:31 +02:00
|
|
|
.uri(new URI("https://api.openweathermap.org/data/2.5/weather?zip="+current_user.getZip()+",de&appid="+api_key+"&units=metric&lang=de"))
|
2024-06-08 22:02:40 +02:00
|
|
|
.GET()
|
|
|
|
.build();
|
|
|
|
|
|
|
|
HttpResponse get_response = http_client.send(get_request, BodyHandlers.ofString());
|
|
|
|
|
2024-06-09 04:45:31 +02:00
|
|
|
JSONObject json = new JSONObject(((String)get_response.body()).substring(0,((String) get_response.body()).length()));
|
2024-06-08 22:02:40 +02:00
|
|
|
weather = json.getJSONArray("weather").getJSONObject(0).getString("description");
|
|
|
|
temperature = json.getJSONObject("main").getDouble("temp");
|
2024-06-09 02:15:45 +02:00
|
|
|
} catch (Exception e){}
|
2024-06-08 22:02:40 +02:00
|
|
|
|
2024-06-09 02:15:45 +02:00
|
|
|
if(weather.equals(""))
|
2024-06-08 22:02:40 +02:00
|
|
|
return "Es ist ein Fehler aufgetreten!";
|
|
|
|
else
|
|
|
|
return weather + ": " + temperature + " °C";
|
|
|
|
|
2024-06-06 11:29:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public String weather_forecast(String destination_zip){
|
2024-06-09 02:15:45 +02:00
|
|
|
|
|
|
|
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()
|
2024-06-09 04:45:31 +02:00
|
|
|
.uri(new URI("https://api.openweathermap.org/data/2.5/forecast?zip="+destination_zip+",de&appid="+api_key+"&units=metric&lang=de"))
|
2024-06-09 02:15:45 +02:00
|
|
|
.GET()
|
|
|
|
.build();
|
|
|
|
|
|
|
|
HttpResponse get_response = http_client.send(get_request, BodyHandlers.ofString());
|
2024-06-09 04:45:31 +02:00
|
|
|
|
|
|
|
JSONObject json = new JSONObject(((String)get_response.body()).substring(0,((String) get_response.body()).length()));
|
2024-06-09 02:15:45 +02:00
|
|
|
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";
|
2024-06-06 11:29:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public String distance(String destination_zip){
|
2024-06-09 16:39:05 +02:00
|
|
|
|
|
|
|
double lon1 = 1;
|
|
|
|
double lon2 = 1;
|
|
|
|
double lat1 = 1;
|
|
|
|
double lat2 = 1;
|
|
|
|
|
|
|
|
InputStream inputStream = Main.class.getResourceAsStream("/zip.csv");
|
|
|
|
|
|
|
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
|
|
|
|
String line;
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
line = line.replace("\"", "");
|
|
|
|
|
|
|
|
if(line.split(";")[0].equals(destination_zip)){
|
|
|
|
lon2 = Double.parseDouble(line.split(";")[2]);
|
|
|
|
lat2 = Double.parseDouble(line.split(";")[3]);
|
|
|
|
}
|
|
|
|
|
2024-06-09 17:01:39 +02:00
|
|
|
if(line.split(";")[0].equals(""+current_user.getZip())){
|
2024-06-09 16:39:05 +02:00
|
|
|
lon1 = Double.parseDouble(line.split(";")[2]);
|
|
|
|
lat1 = Double.parseDouble(line.split(";")[3]);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} catch (Exception e) {}
|
2024-06-09 17:01:39 +02:00
|
|
|
|
2024-06-09 17:54:58 +02:00
|
|
|
double dLat = lat2-lat1;
|
|
|
|
double dLon = lon2-lon1;
|
|
|
|
|
|
|
|
double a = Math.pow(Math.sin(Math.toRadians(dLat/2.0)), 2) + Math.pow(Math.sin(Math.toRadians(dLon/2.0)), 2) * Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));
|
|
|
|
|
|
|
|
double distance = 6378.388 * 2.0 * Math.atan2(Math.sqrt(a), Math.sqrt(1.0-a));
|
|
|
|
|
|
|
|
return "" + (distance * 1.25) + " km";
|
2024-06-09 17:01:39 +02:00
|
|
|
|
2024-06-06 11:29:09 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
public String[] travel_time(String destination_zip){
|
2024-06-09 18:12:54 +02:00
|
|
|
|
|
|
|
String[] result = new String[2];
|
|
|
|
result[0] = "" + (Double.parseDouble(distance(destination_zip).replace(" km", "")) / current_user.getCar_avg_kmh()) + " h";
|
|
|
|
result[1] = "" + (Double.parseDouble(distance(destination_zip).replace(" km", "")) / current_user.getBike_avg_kmh()) + " h";
|
|
|
|
|
|
|
|
return result;
|
2024-06-06 11:29:09 +02:00
|
|
|
}
|
|
|
|
|
2024-06-09 18:25:57 +02:00
|
|
|
public String calc_l_consumption(String destination_zip){
|
|
|
|
|
|
|
|
return "" + (Double.parseDouble(distance(destination_zip).replace(" km", "")) * (current_user.getCar_l_100km() / 100)) + " l";
|
|
|
|
|
2024-06-06 11:29:09 +02:00
|
|
|
}
|
2024-06-06 11:22:54 +02:00
|
|
|
}
|
|
|
|
|