Merge branch 'distance_related_methods'

facade_class
Selim Eser 2024-06-09 20:01:53 +02:00
commit de917666c7
3 changed files with 142 additions and 22 deletions

View File

@ -12,6 +12,7 @@ import java.net.http.HttpResponse.BodyHandlers;
import java.nio.charset.StandardCharsets; import java.nio.charset.StandardCharsets;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.HashSet; import java.util.HashSet;
import java.util.List;
import java.util.TreeSet; import java.util.TreeSet;
import org.json.JSONObject; import org.json.JSONObject;
@ -31,6 +32,18 @@ public class System {
this.current_user.setZip(Integer.parseInt(zip)); this.current_user.setZip(Integer.parseInt(zip));
} }
public void set_current_user_car_l_100km(double car_l_100km){
this.current_user.setCar_l_100km(car_l_100km);
}
public void set_current_user_car_avg_kmh(double car_avg_kmh){
this.current_user.setCar_avg_kmh(car_avg_kmh);
}
public void set_current_user_bike_avg_kmh(double bike_avg_kmh){
this.current_user.setBike_avg_kmh(bike_avg_kmh);
}
public HashSet<User> get_all_user(){ public HashSet<User> get_all_user(){
return new HashSet<User>(); return new HashSet<User>();
} }
@ -59,8 +72,8 @@ public class System {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
String line; String line;
while ((line = reader.readLine()) != null) { while ((line = reader.readLine()) != null && zip_set.size()<200) {
if(line.contains(hometown_or_zip)&&zip_set.size()<200){ if(line.contains(hometown_or_zip)){
line = line.replace("\"", ""); line = line.replace("\"", "");
zip_set.add(line); zip_set.add(line);
} }
@ -71,15 +84,56 @@ public class System {
} }
public ArrayList<String> random_destinations_car(){ public ArrayList<String> random_destinations_car(){
return new ArrayList<String>();
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 && result.size()<3) {
line = line.replace("\"", "");
if(Double.parseDouble(distance(line.split(";")[0]).replace(" km", "")) > 150)
result.add(line);
}
} catch (Exception e) {}
return result;
} }
public ArrayList<String> random_destinations_bike(){ public ArrayList<String> random_destinations_bike(){
return new ArrayList<String>(); 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 && result.size()<3) {
line = line.replace("\"", "");
if(Double.parseDouble(distance(line.split(";")[0]).replace(" km", "")) < 100)
result.add(line);
}
} catch (Exception e) {}
return result;
} }
public String[] destination_details(String destination_zip){ public String[] destination_details(String destination_zip){
return new String[1]; String[] result = new String[5];
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;
} }
public String current_weather(){ public String current_weather(){
@ -209,10 +263,10 @@ public class System {
public String distance(String destination_zip){ public String distance(String destination_zip){
double lon1 = 1; double lon1 = -1;
double lon2 = 1; double lon2 = -1;
double lat1 = 1; double lat1 = -1;
double lat2 = 1; double lat2 = -1;
InputStream inputStream = Main.class.getResourceAsStream("/zip.csv"); InputStream inputStream = Main.class.getResourceAsStream("/zip.csv");
@ -233,6 +287,9 @@ public class System {
} }
} catch (Exception e) {} } catch (Exception e) {}
if(lon1==-1||lon2==-1||lat1==-1||lat2==-1)
return "Es ist ein Fehler aufgetreten!";
double dLat = lat2-lat1; double dLat = lat2-lat1;
double dLon = lon2-lon1; double dLon = lon2-lon1;
@ -240,16 +297,33 @@ public class System {
double distance = 6378.388 * 2.0 * Math.atan2(Math.sqrt(a), Math.sqrt(1.0-a)); double distance = 6378.388 * 2.0 * Math.atan2(Math.sqrt(a), Math.sqrt(1.0-a));
return "" + (distance * 1.25) + " km"; return "" + (Math.round((distance * 1.25)*1000)/1000.0) + " km";
} }
public String[] travel_time(String destination_zip){ public String[] travel_time(String destination_zip){
return new String[1];
String[] result = new String[2];
if(distance(destination_zip).equals("Es ist ein Fehler aufgetreten!")){
result[0] = "Es ist ein Fehler aufgetreten!";
result[1] = "Es ist ein Fehler aufgetreten!";
return result;
} }
public String calc_co2(String destination_zip){ result[0] = "" + (Math.round(((Double.parseDouble(distance(destination_zip).replace(" km", "")) / current_user.getCar_avg_kmh()))*1000) / 1000.0) + " h";
return ""; result[1] = "" + (Math.round(((Double.parseDouble(distance(destination_zip).replace(" km", "")) / current_user.getBike_avg_kmh()))*1000) / 1000.0) + " h";
return result;
}
public String calc_l_consumption(String destination_zip){
if(distance(destination_zip).equals("Es ist ein Fehler aufgetreten!"))
return "Es ist ein Fehler aufgetreten!";
return "" + (Math.round((Double.parseDouble(distance(destination_zip).replace(" km", "")) * (current_user.getCar_l_100km() / 100.0))*1000)/1000.0) + " l";
} }
} }

View File

@ -7,7 +7,7 @@ public class User {
private String hometown = ""; private String hometown = "";
private int zip; private int zip;
private String car_name = ""; private String car_name = "";
private double car_co2_kmh; private double car_l_100km;
private double car_avg_kmh; private double car_avg_kmh;
private double bike_avg_kmh; private double bike_avg_kmh;
@ -51,12 +51,12 @@ public class User {
this.car_name = car_name; this.car_name = car_name;
} }
public double getCar_co2_kmh() { public double getCar_l_100km() {
return car_co2_kmh; return car_l_100km;
} }
public void setCar_co2_kmh(double car_co2_kmh) { public void setCar_l_100km(double car_l_100km) {
this.car_co2_kmh = car_co2_kmh; this.car_l_100km = car_l_100km;
} }
public double getCar_avg_kmh() { public double getCar_avg_kmh() {

View File

@ -46,10 +46,56 @@ public class SystemTest {
current_system.set_current_user_zip("68161"); current_system.set_current_user_zip("68161");
assertEquals("88.4596509227594 km", current_system.distance("60306")); // Frankfurt assertEquals("88.46 km", current_system.distance("60306")); // Frankfurt
assertEquals("581.1091061333296 km", current_system.distance("20095")); // Hamburg assertEquals("581.109 km", current_system.distance("20095")); // Hamburg
assertEquals("603.6077163174941 km", current_system.distance("10115")); // Berlin assertEquals("603.608 km", current_system.distance("10115")); // Berlin
} }
@Test
public void travel_time(){
System current_system = new System("35a75437476f12302f72e55d368485db");
current_system.set_current_user_zip("68161");
current_system.set_current_user_car_avg_kmh(100);
current_system.set_current_user_bike_avg_kmh(20);
assertEquals("0.885 h", current_system.travel_time("60306")[0]); // Frankfurt mit Auto
assertEquals("4.423 h", current_system.travel_time("60306")[1]); // Frankfurt mit Fahrrad
assertEquals("6.036 h", current_system.travel_time("10115")[0]); // Berlin mit Auto
assertEquals("30.18 h", current_system.travel_time("10115")[1]); // Berlin mit Fahrrad
}
@Test
public void calc_l_consumption(){
System current_system = new System("35a75437476f12302f72e55d368485db");
current_system.set_current_user_zip("68161");
current_system.set_current_user_car_avg_kmh(100);
current_system.set_current_user_car_l_100km(10);
assertEquals("8.846 l", current_system.calc_l_consumption("60306")); // Kraftstoffverbrauch nach Frankfurt
assertEquals("58.111 l", current_system.calc_l_consumption("20095")); // Kraftstoffverbrauch nach Hamburg
assertEquals("60.361 l", current_system.calc_l_consumption("10115")); // Kraftstoffverbrauch nach Berlin
}
@Test
public void random_destinations(){
System current_system = new System("35a75437476f12302f72e55d368485db");
current_system.set_current_user_zip("68161");
assertEquals(3, current_system.random_destinations_car().size()); // random_destinations_car gibt genau 3 destinations zurück
assertEquals(3, current_system.random_destinations_bike().size()); // random destinations_bike gibt genau 3 destinations zurück
// random_destinations_car gibt nur destinations mit mindestens 150 km Entfernung zurück
assertEquals(true, Double.parseDouble(current_system.distance(current_system.random_destinations_car().get(0).split(";")[0]).replace(" km", "")) > 150);
// random_destinations_bike gibt nur destinations mit maximal 100 km Entfernung zurück
assertEquals(true, Double.parseDouble(current_system.distance(current_system.random_destinations_bike().get(0).split(";")[0]).replace(" km", "")) < 100);
}
} }