2024-06-06 11:22:54 +02:00
|
|
|
package de.hs_mannheim.domain;
|
|
|
|
|
2024-06-10 11:59:35 +02:00
|
|
|
import java.io.*;
|
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:17:47 +02:00
|
|
|
import java.util.ArrayList;
|
2024-06-11 22:11:11 +02:00
|
|
|
import java.util.HashMap;
|
2024-06-06 11:29:09 +02:00
|
|
|
import java.util.TreeSet;
|
2024-06-06 11:22:54 +02:00
|
|
|
|
2024-06-10 13:34:52 +02:00
|
|
|
import org.apache.commons.codec.binary.Base64;
|
2024-06-08 22:02:40 +02:00
|
|
|
import org.json.JSONObject;
|
|
|
|
|
2024-06-06 11:22:54 +02:00
|
|
|
public class System {
|
2024-06-10 14:17:14 +02:00
|
|
|
|
2024-06-09 04:45:31 +02:00
|
|
|
private User current_user = new User();
|
2024-06-11 14:32:24 +02:00
|
|
|
private ArrayList<User> all_user = new ArrayList<>();
|
2024-06-08 15:44:40 +02:00
|
|
|
private String api_key;
|
2024-06-11 22:11:11 +02:00
|
|
|
private HashMap<String, Destination> all_destinations = new HashMap<String, Destination>();
|
2024-06-08 15:44:40 +02:00
|
|
|
|
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-11 14:32:24 +02:00
|
|
|
get_all_user();
|
2024-06-08 15:44:40 +02:00
|
|
|
}
|
2024-06-06 11:22:54 +02:00
|
|
|
|
2024-06-10 15:58:06 +02:00
|
|
|
public void set_current_user_zip(String zip) {
|
2024-06-11 22:11:11 +02:00
|
|
|
this.current_user.setZip(zip);
|
2024-06-09 04:45:31 +02:00
|
|
|
}
|
|
|
|
|
2024-06-10 15:58:06 +02:00
|
|
|
public void set_current_user_car_l_100km(double car_l_100km) {
|
2024-06-09 19:30:50 +02:00
|
|
|
this.current_user.setCar_l_100km(car_l_100km);
|
|
|
|
}
|
|
|
|
|
2024-06-10 15:58:06 +02:00
|
|
|
public void set_current_user_car_avg_kmh(double car_avg_kmh) {
|
2024-06-09 19:30:50 +02:00
|
|
|
this.current_user.setCar_avg_kmh(car_avg_kmh);
|
|
|
|
}
|
|
|
|
|
2024-06-10 15:58:06 +02:00
|
|
|
public void set_current_user_bike_avg_kmh(double bike_avg_kmh) {
|
2024-06-09 19:30:50 +02:00
|
|
|
this.current_user.setBike_avg_kmh(bike_avg_kmh);
|
|
|
|
}
|
2024-06-10 11:59:35 +02:00
|
|
|
|
2024-06-11 14:32:24 +02:00
|
|
|
public static String decoding(String string) {
|
2024-06-11 10:50:46 +02:00
|
|
|
byte[] binary_data = new byte[string.length()];
|
|
|
|
for(int i = 0; i < string.length(); i++) {
|
|
|
|
binary_data[i] = (byte)string.charAt(i);
|
|
|
|
}
|
|
|
|
return new String(Base64.decodeBase64(binary_data));
|
2024-06-10 13:34:52 +02:00
|
|
|
}
|
2024-06-10 11:59:35 +02:00
|
|
|
|
2024-06-11 14:32:24 +02:00
|
|
|
public static String encoding(String string) {
|
2024-06-10 13:34:52 +02:00
|
|
|
byte[] binary_data = new byte[string.length()];
|
2024-06-10 15:58:06 +02:00
|
|
|
for (int i = 0; i < string.length(); i++) {
|
2024-06-10 13:34:52 +02:00
|
|
|
binary_data[i] = (byte) string.charAt(i);
|
|
|
|
}
|
2024-06-10 15:19:24 +02:00
|
|
|
return Base64.encodeBase64String(binary_data);
|
2024-06-10 11:59:35 +02:00
|
|
|
}
|
2024-06-10 14:17:14 +02:00
|
|
|
|
2024-06-11 15:53:54 +02:00
|
|
|
public static double parseDouble(String s) throws NumberFormatException {
|
|
|
|
if(s.equals(""))
|
|
|
|
return 0;
|
|
|
|
else
|
|
|
|
return Double.parseDouble(s);
|
|
|
|
}
|
|
|
|
|
2024-06-11 14:32:24 +02:00
|
|
|
public void get_all_user() {
|
2024-06-10 11:59:35 +02:00
|
|
|
String[] fileString = new String[8];
|
|
|
|
|
2024-06-10 15:58:06 +02:00
|
|
|
InputStream inputStream = System.class.getResourceAsStream("/user_data.csv");
|
|
|
|
String path;
|
2024-06-10 11:59:35 +02:00
|
|
|
|
2024-06-10 15:58:06 +02:00
|
|
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
|
|
|
|
while ((path = reader.readLine()) != null) {
|
2024-06-10 11:59:35 +02:00
|
|
|
fileString = path.split(";");
|
2024-06-11 14:32:24 +02:00
|
|
|
fileString[1] = decoding(fileString[1]);
|
|
|
|
this.all_user.add(new User(fileString[0], fileString[1], fileString[2],
|
2024-06-11 22:11:11 +02:00
|
|
|
fileString[3], fileString[4],
|
2024-06-10 11:59:35 +02:00
|
|
|
Double.parseDouble(fileString[5]),
|
|
|
|
Double.parseDouble(fileString[6]),
|
|
|
|
Double.parseDouble(fileString[7])));
|
|
|
|
}
|
2024-06-10 15:58:06 +02:00
|
|
|
} catch (Exception e) {
|
2024-06-12 01:30:09 +02:00
|
|
|
//
|
2024-06-10 11:59:35 +02:00
|
|
|
}
|
2024-06-06 11:22:54 +02:00
|
|
|
}
|
|
|
|
|
2024-06-11 11:54:07 +02:00
|
|
|
public ArrayList<String> all_user_toString(){
|
|
|
|
|
|
|
|
ArrayList<String> result = new ArrayList<>();
|
|
|
|
|
2024-06-11 14:32:24 +02:00
|
|
|
for(User user : this.all_user){
|
|
|
|
result.add(user.toString());
|
2024-06-11 11:54:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
2024-06-11 14:32:24 +02:00
|
|
|
|
2024-06-11 11:00:28 +02:00
|
|
|
public boolean sign_in_user(String username, String password) {
|
2024-06-10 14:40:53 +02:00
|
|
|
|
2024-06-11 14:32:24 +02:00
|
|
|
for (User user : this.all_user) {
|
|
|
|
if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
|
2024-06-10 14:40:53 +02:00
|
|
|
current_user = new User(user.getUsername(), user.getPassword(),
|
|
|
|
user.getHometown(), user.getZip(), user.getCar_name(),
|
|
|
|
user.getCar_l_100km(), user.getCar_avg_kmh(), user.getBike_avg_kmh());
|
2024-06-11 23:04:38 +02:00
|
|
|
|
|
|
|
all_distances();
|
2024-06-10 14:40:53 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
2024-06-06 11:22:54 +02:00
|
|
|
}
|
|
|
|
|
2024-06-11 22:11:11 +02:00
|
|
|
public boolean sign_up_user(String username, String password, String hometown, String zip,
|
2024-06-11 11:34:42 +02:00
|
|
|
String car_name, String car_l_100kmS, String car_avg_kmhS, String bike_avg_kmhS){
|
|
|
|
|
2024-06-11 15:13:37 +02:00
|
|
|
double car_l_100km;
|
|
|
|
double car_avg_kmh;
|
|
|
|
double bike_avg_kmh;
|
|
|
|
|
|
|
|
try{
|
2024-06-11 15:53:54 +02:00
|
|
|
car_l_100km = parseDouble(car_l_100kmS);
|
|
|
|
car_avg_kmh = parseDouble(car_avg_kmhS);
|
|
|
|
bike_avg_kmh = parseDouble(bike_avg_kmhS);
|
2024-06-11 15:13:37 +02:00
|
|
|
} catch (NumberFormatException n){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-06-11 22:11:11 +02:00
|
|
|
if(username.equals("")||password.equals("")||hometown.equals("")||zip.equals(""))
|
2024-06-11 15:13:37 +02:00
|
|
|
return false;
|
2024-06-11 11:34:42 +02:00
|
|
|
|
2024-06-11 15:07:35 +02:00
|
|
|
for(User user: this.all_user)
|
2024-06-11 11:34:42 +02:00
|
|
|
if(user.getUsername().equals(username))
|
|
|
|
return false;
|
|
|
|
|
2024-06-11 22:11:11 +02:00
|
|
|
ArrayList<String> mem = search(zip);
|
2024-06-11 11:34:42 +02:00
|
|
|
boolean bool = false;
|
|
|
|
|
|
|
|
for (String line: mem)
|
|
|
|
if(line.split(";")[1].equals(hometown)) {
|
|
|
|
bool = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!bool)
|
|
|
|
return false;
|
|
|
|
|
|
|
|
current_user = new User(username, password, hometown, zip, car_name, car_l_100km, car_avg_kmh, bike_avg_kmh);
|
2024-06-11 14:32:24 +02:00
|
|
|
|
|
|
|
this.all_user.add(current_user);
|
2024-06-11 11:34:42 +02:00
|
|
|
|
2024-06-11 14:32:24 +02:00
|
|
|
write_to_file(all_user_toString(), "src/main/resources/user_data.csv");
|
|
|
|
write_to_file(all_user_toString(), "src/test/resources/user_data.csv");
|
2024-06-11 11:34:42 +02:00
|
|
|
|
2024-06-11 23:04:38 +02:00
|
|
|
all_distances();
|
2024-06-11 11:34:42 +02:00
|
|
|
return true;
|
|
|
|
}
|
2024-06-11 15:53:54 +02:00
|
|
|
|
2024-06-11 22:11:11 +02:00
|
|
|
public boolean change_user_details(String username, String password, String hometown, String zip,
|
2024-06-11 15:07:35 +02:00
|
|
|
String car_name, String car_l_100kmS, String car_avg_kmhS, String bike_avg_kmhS){
|
|
|
|
|
|
|
|
double car_l_100km;
|
|
|
|
double car_avg_kmh;
|
|
|
|
double bike_avg_kmh;
|
|
|
|
|
|
|
|
try{
|
2024-06-11 15:53:54 +02:00
|
|
|
car_l_100km = parseDouble(car_l_100kmS);
|
|
|
|
car_avg_kmh = parseDouble(car_avg_kmhS);
|
|
|
|
bike_avg_kmh = parseDouble(bike_avg_kmhS);
|
2024-06-11 15:07:35 +02:00
|
|
|
} catch (NumberFormatException n){
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2024-06-13 12:23:56 +02:00
|
|
|
if(!current_user.getPassword().equals(password))
|
|
|
|
return false;
|
|
|
|
|
|
|
|
if(username.equals("")||hometown.equals("")||zip.equals(""))
|
2024-06-11 15:07:35 +02:00
|
|
|
return false;
|
|
|
|
|
|
|
|
for(User user: this.all_user)
|
|
|
|
if(user.getUsername().equals(username))
|
|
|
|
return false;
|
|
|
|
|
2024-06-11 22:11:11 +02:00
|
|
|
ArrayList<String> mem = search(zip);
|
2024-06-11 15:07:35 +02:00
|
|
|
boolean bool = false;
|
|
|
|
|
|
|
|
for (String line: mem)
|
|
|
|
if(line.split(";")[1].equals(hometown)) {
|
|
|
|
bool = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!bool)
|
|
|
|
return false;
|
|
|
|
|
2024-06-11 15:53:54 +02:00
|
|
|
|
|
|
|
for(int i = 0; i< this.all_user.size(); i++)
|
|
|
|
if(this.all_user.get(i).getUsername().equals(current_user.getUsername()))
|
|
|
|
this.all_user.remove(i);
|
2024-06-11 15:07:35 +02:00
|
|
|
|
|
|
|
write_to_file(all_user_toString(), "src/main/resources/user_data.csv");
|
|
|
|
write_to_file(all_user_toString(), "src/test/resources/user_data.csv");
|
|
|
|
|
2024-06-11 15:53:54 +02:00
|
|
|
this.current_user = new User(username, password, hometown, zip, car_name, car_l_100km, car_avg_kmh, bike_avg_kmh);
|
2024-06-11 15:07:35 +02:00
|
|
|
this.all_user.add(current_user);
|
|
|
|
|
|
|
|
write_to_file(all_user_toString(), "src/main/resources/user_data.csv");
|
|
|
|
write_to_file(all_user_toString(), "src/test/resources/user_data.csv");
|
|
|
|
|
2024-06-11 23:04:38 +02:00
|
|
|
all_distances();
|
2024-06-11 15:07:35 +02:00
|
|
|
return true;
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-06-11 11:54:07 +02:00
|
|
|
public void write_to_file(ArrayList<String> lines, String file) {
|
|
|
|
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
|
|
|
|
for (int i = 0; i < lines.size() - 1; i++) {
|
|
|
|
writer.write(lines.get(i));
|
|
|
|
writer.newLine();
|
|
|
|
}
|
|
|
|
writer.write(lines.getLast());
|
2024-06-12 01:30:09 +02:00
|
|
|
} catch (IOException e) {
|
|
|
|
//
|
|
|
|
}
|
2024-06-11 11:54:07 +02:00
|
|
|
}
|
|
|
|
|
2024-06-10 15:58:06 +02:00
|
|
|
public void sign_out_user() {
|
2024-06-10 14:44:55 +02:00
|
|
|
current_user = new User();
|
2024-06-09 21:54:50 +02:00
|
|
|
}
|
2024-06-06 11:22:54 +02:00
|
|
|
|
2024-06-11 10:50:46 +02:00
|
|
|
public String[] getDetails(){
|
2024-06-13 12:21:08 +02:00
|
|
|
return new String[]{current_user.getUsername(), current_user.getHometown(),
|
|
|
|
current_user.getZip(), current_user.getCar_name(),
|
2024-06-11 15:07:35 +02:00
|
|
|
current_user.getCar_l_100km()==0?"":String.valueOf(current_user.getCar_l_100km()),
|
|
|
|
current_user.getCar_avg_kmh()==0?"":String.valueOf(current_user.getCar_avg_kmh()),
|
|
|
|
current_user.getBike_avg_kmh()==0?"":String.valueOf(current_user.getBike_avg_kmh())};
|
2024-06-06 11:22:54 +02:00
|
|
|
}
|
|
|
|
|
2024-06-10 15:58:06 +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-10 14:01:37 +02:00
|
|
|
InputStream inputStream = System.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;
|
2024-06-10 15:58:06 +02:00
|
|
|
while ((line = reader.readLine()) != null && zip_set.size() < 200) {
|
2024-06-11 22:11:11 +02:00
|
|
|
line = line.replace("\"", "");
|
|
|
|
if (line.split(";")[0].contains(hometown_or_zip)||line.split(";")[1].contains(hometown_or_zip))
|
2024-06-09 15:33:13 +02:00
|
|
|
zip_set.add(line);
|
|
|
|
}
|
2024-06-10 15:58:06 +02:00
|
|
|
} catch (Exception e) {
|
2024-06-12 01:30:09 +02:00
|
|
|
//
|
2024-06-10 15:58:06 +02:00
|
|
|
}
|
|
|
|
|
2024-06-09 15:33:13 +02:00
|
|
|
return new ArrayList<>(zip_set);
|
2024-06-06 11:29:09 +02:00
|
|
|
}
|
|
|
|
|
2024-06-10 15:58:06 +02:00
|
|
|
public ArrayList<String> random_destinations_car() {
|
|
|
|
|
2024-06-11 23:04:38 +02:00
|
|
|
ArrayList<Destination> mem = new ArrayList<>();
|
2024-06-09 21:15:39 +02:00
|
|
|
ArrayList<String> result = new ArrayList<>();
|
2024-06-11 23:04:38 +02:00
|
|
|
int random = 0;
|
2024-06-09 21:51:37 +02:00
|
|
|
|
2024-06-11 23:04:38 +02:00
|
|
|
for(Destination destination: this.all_destinations.values()){
|
|
|
|
if(destination.getDistance_from_user()>150)
|
|
|
|
mem.add(destination);
|
2024-06-09 21:51:37 +02:00
|
|
|
}
|
2024-06-10 15:58:06 +02:00
|
|
|
|
2024-06-11 23:04:38 +02:00
|
|
|
for (int i = 0; i < 3; i++){
|
|
|
|
random = (int) (Math.random() * mem.size());
|
|
|
|
result.add(mem.get(random).getZip() + ";" + mem.get(random).getTown());
|
|
|
|
}
|
2024-06-09 18:38:38 +02:00
|
|
|
return result;
|
2024-06-09 16:06:15 +02:00
|
|
|
}
|
|
|
|
|
2024-06-10 15:58:06 +02:00
|
|
|
public ArrayList<String> random_destinations_bike() {
|
|
|
|
|
2024-06-11 23:04:38 +02:00
|
|
|
ArrayList<Destination> mem = new ArrayList<>();
|
2024-06-09 21:15:39 +02:00
|
|
|
ArrayList<String> result = new ArrayList<>();
|
2024-06-11 23:04:38 +02:00
|
|
|
int random = 0;
|
2024-06-09 21:51:37 +02:00
|
|
|
|
2024-06-11 23:04:38 +02:00
|
|
|
for(Destination destination: this.all_destinations.values()){
|
|
|
|
if(destination.getDistance_from_user()<100)
|
|
|
|
mem.add(destination);
|
2024-06-09 21:51:37 +02:00
|
|
|
}
|
2024-06-10 15:58:06 +02:00
|
|
|
|
2024-06-11 23:04:38 +02:00
|
|
|
for (int i = 0; i < 3; i++){
|
|
|
|
random = (int) (Math.random() * mem.size());
|
|
|
|
result.add(mem.get(random).getZip() + ";" + mem.get(random).getTown());
|
|
|
|
}
|
2024-06-09 18:39:48 +02:00
|
|
|
return result;
|
2024-06-06 11:29:09 +02:00
|
|
|
}
|
|
|
|
|
2024-06-10 15:58:06 +02:00
|
|
|
public String[] destination_details(String destination_zip) {
|
2024-06-09 18:45:42 +02:00
|
|
|
String[] result = new String[5];
|
2024-06-10 15:58:06 +02:00
|
|
|
|
2024-06-09 18:45:42 +02:00
|
|
|
result[0] = distance(destination_zip); // Entfernung
|
|
|
|
result[1] = travel_time(destination_zip)[0]; // Reisedauer Auto
|
|
|
|
result[2] = travel_time(destination_zip)[1]; // Reisedauer Fahrrad
|
|
|
|
result[3] = calc_l_consumption(destination_zip); // Kraftstoffverbrauch Auto
|
|
|
|
result[4] = weather_forecast(destination_zip); // Wettervorhersage für die nächsten 3 Tage
|
|
|
|
|
|
|
|
return result;
|
2024-06-06 11:29:09 +02:00
|
|
|
}
|
|
|
|
|
2024-06-10 15:58:06 +02:00
|
|
|
public String current_weather() {
|
|
|
|
|
2024-06-09 02:15:45 +02:00
|
|
|
String weather = "";
|
2024-06-09 04:45:31 +02:00
|
|
|
double temperature = 1;
|
|
|
|
|
2024-06-10 15:58:06 +02:00
|
|
|
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<String> 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) {
|
2024-06-12 01:30:09 +02:00
|
|
|
//
|
2024-06-10 15:58:06 +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
|
|
|
}
|
|
|
|
|
2024-06-10 15:58:06 +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;
|
|
|
|
|
|
|
|
|
2024-06-10 15:58:06 +02:00
|
|
|
try {
|
2024-06-09 02:15:45 +02:00
|
|
|
HttpClient http_client = HttpClient.newHttpClient();
|
2024-06-10 15:58:06 +02:00
|
|
|
|
2024-06-09 02:15:45 +02:00
|
|
|
HttpRequest get_request = HttpRequest.newBuilder()
|
2024-06-10 15:58:06 +02:00
|
|
|
.uri(new URI("https://api.openweathermap.org/data/2.5/forecast?zip=" + destination_zip + ",de&appid=" + api_key + "&units=metric&lang=de"))
|
|
|
|
.GET()
|
|
|
|
.build();
|
|
|
|
|
2024-06-09 20:17:02 +02:00
|
|
|
HttpResponse<String> get_response = http_client.send(get_request, BodyHandlers.ofString());
|
2024-06-09 04:45:31 +02:00
|
|
|
|
2024-06-10 15:58:06 +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");
|
2024-06-10 15:58:06 +02:00
|
|
|
|
2024-06-09 02:15:45 +02:00
|
|
|
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");
|
2024-06-10 15:58:06 +02:00
|
|
|
|
2024-06-09 02:15:45 +02:00
|
|
|
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");
|
2024-06-10 15:58:06 +02:00
|
|
|
|
2024-06-09 02:15:45 +02:00
|
|
|
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");
|
2024-06-10 15:58:06 +02:00
|
|
|
|
|
|
|
} catch (Exception e) {
|
2024-06-12 01:30:09 +02:00
|
|
|
//
|
2024-06-10 15:58:06 +02:00
|
|
|
}
|
|
|
|
|
2024-06-09 02:15:45 +02:00
|
|
|
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);
|
|
|
|
|
2024-06-10 15:58:06 +02:00
|
|
|
temperature_day_1_high = (double) temperature_day_1.toArray()[temperature_day_1.size() - 1];
|
2024-06-09 02:15:45 +02:00
|
|
|
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);
|
|
|
|
|
2024-06-10 15:58:06 +02:00
|
|
|
temperature_day_2_high = (double) temperature_day_2.toArray()[temperature_day_1.size() - 1];
|
2024-06-09 02:15:45 +02:00
|
|
|
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);
|
|
|
|
|
2024-06-10 15:58:06 +02:00
|
|
|
temperature_day_3_high = (double) temperature_day_3.toArray()[temperature_day_1.size() - 1];
|
2024-06-09 02:15:45 +02:00
|
|
|
temperature_day_3_low = (double) temperature_day_3.toArray()[0];
|
|
|
|
|
2024-06-10 15:58:06 +02:00
|
|
|
if (weather_day_1.equals("") || weather_day_2.equals("") || weather_day_3.equals(""))
|
2024-06-09 02:15:45 +02:00
|
|
|
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"
|
2024-06-10 15:58:06 +02:00
|
|
|
+ "Ü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
|
|
|
}
|
2024-06-11 22:39:07 +02:00
|
|
|
|
|
|
|
public boolean all_distances() {
|
2024-06-10 15:58:06 +02:00
|
|
|
|
2024-06-09 18:58:41 +02:00
|
|
|
double lon1 = -1;
|
|
|
|
double lon2 = -1;
|
|
|
|
double lat1 = -1;
|
|
|
|
double lat2 = -1;
|
2024-06-11 22:39:07 +02:00
|
|
|
double dLat;
|
|
|
|
double dLon;
|
|
|
|
double a;
|
|
|
|
double distance;
|
|
|
|
double result;
|
2024-06-09 16:39:05 +02:00
|
|
|
|
2024-06-10 14:01:37 +02:00
|
|
|
InputStream inputStream = System.class.getResourceAsStream("/zip.csv");
|
2024-06-09 16:39:05 +02:00
|
|
|
|
|
|
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
|
|
|
|
String line;
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
line = line.replace("\"", "");
|
2024-06-10 15:58:06 +02:00
|
|
|
|
2024-06-11 23:04:38 +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]);
|
|
|
|
}
|
|
|
|
}
|
2024-06-10 15:58:06 +02:00
|
|
|
} catch (Exception e) {
|
2024-06-12 01:30:09 +02:00
|
|
|
//
|
2024-06-10 15:58:06 +02:00
|
|
|
}
|
2024-06-09 17:01:39 +02:00
|
|
|
|
2024-06-11 23:56:48 +02:00
|
|
|
InputStream inputStream2 = System.class.getResourceAsStream("/zip.csv");
|
|
|
|
|
|
|
|
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream2))) {
|
2024-06-11 22:39:07 +02:00
|
|
|
String line;
|
|
|
|
while ((line = reader.readLine()) != null) {
|
|
|
|
line = line.replace("\"", "");
|
2024-06-06 11:29:09 +02:00
|
|
|
|
2024-06-11 22:39:07 +02:00
|
|
|
lon2 = Double.parseDouble(line.split(";")[2]);
|
|
|
|
lat2 = Double.parseDouble(line.split(";")[3]);
|
|
|
|
|
|
|
|
if (lon1 == -1 || lon2 == -1 || lat1 == -1 || lat2 == -1)
|
|
|
|
return false;
|
2024-06-09 21:51:37 +02:00
|
|
|
|
2024-06-11 22:39:07 +02:00
|
|
|
dLat = lat2 - lat1;
|
|
|
|
dLon = lon2 - lon1;
|
2024-06-09 21:51:37 +02:00
|
|
|
|
2024-06-11 22:39:07 +02:00
|
|
|
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));
|
2024-06-09 21:51:37 +02:00
|
|
|
|
2024-06-11 22:39:07 +02:00
|
|
|
distance = 6378.388 * 2.0 * Math.atan2(Math.sqrt(a), Math.sqrt(1.0 - a));
|
2024-06-10 15:58:06 +02:00
|
|
|
|
2024-06-12 01:30:09 +02:00
|
|
|
result = Math.round(distance * 1.25 * 1000) / 1000.0;
|
2024-06-10 15:58:06 +02:00
|
|
|
|
2024-06-11 23:04:38 +02:00
|
|
|
this.all_destinations.put(line.split(";")[0],new Destination(line.split(";")[1],line.split(";")[0], result));
|
2024-06-10 15:58:06 +02:00
|
|
|
|
2024-06-09 21:51:37 +02:00
|
|
|
}
|
2024-06-10 15:58:06 +02:00
|
|
|
} catch (Exception e) {
|
2024-06-12 01:30:09 +02:00
|
|
|
//
|
2024-06-10 15:58:06 +02:00
|
|
|
}
|
2024-06-11 22:39:07 +02:00
|
|
|
|
|
|
|
return true;
|
2024-06-09 21:51:37 +02:00
|
|
|
}
|
2024-06-10 15:58:06 +02:00
|
|
|
|
2024-06-11 22:39:07 +02:00
|
|
|
public String distance(String destination_zip){
|
|
|
|
|
2024-06-11 23:04:38 +02:00
|
|
|
return "" + this.all_destinations.get(destination_zip).getDistance_from_user() + " km";
|
2024-06-11 22:39:07 +02:00
|
|
|
|
|
|
|
}
|
|
|
|
|
2024-06-10 15:58:06 +02:00
|
|
|
public String[] travel_time(String destination_zip) {
|
|
|
|
|
2024-06-09 18:12:54 +02:00
|
|
|
String[] result = new String[2];
|
2024-06-10 15:58:06 +02:00
|
|
|
|
2024-06-12 01:30:09 +02:00
|
|
|
result[0] = "" + (Math.round(Double.parseDouble(distance(destination_zip).replace(" km", "")) / current_user.getCar_avg_kmh() * 1000) / 1000.0) + " h";
|
|
|
|
result[1] = "" + (Math.round(Double.parseDouble(distance(destination_zip).replace(" km", "")) / current_user.getBike_avg_kmh() * 1000) / 1000.0) + " h";
|
2024-06-10 15:58:06 +02:00
|
|
|
|
2024-06-09 18:12:54 +02:00
|
|
|
return result;
|
2024-06-06 11:29:09 +02:00
|
|
|
}
|
|
|
|
|
2024-06-10 15:58:06 +02:00
|
|
|
public String calc_l_consumption(String destination_zip) {
|
|
|
|
|
2024-06-12 01:30:09 +02:00
|
|
|
return "" + (Math.round(Double.parseDouble(distance(destination_zip).replace(" km", "")) * current_user.getCar_l_100km() / 100.0 * 1000) / 1000.0) + " l";
|
2024-06-10 15:58:06 +02:00
|
|
|
|
2024-06-06 11:29:09 +02:00
|
|
|
}
|
2024-06-06 11:22:54 +02:00
|
|
|
}
|
|
|
|
|