From 5a5f86db98b3d88641f908da30cbc51583b5c86d Mon Sep 17 00:00:00 2001 From: Selim Eser <2211482@stud.hs-mannheim.de> Date: Sun, 9 Jun 2024 18:12:54 +0200 Subject: [PATCH 1/9] travel_time method implemented --- src/main/java/de/hs_mannheim/domain/System.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index 897cf8e..d087e45 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -245,7 +245,12 @@ public class System { } public String[] travel_time(String destination_zip){ - return new String[1]; + + 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; } public String calc_co2(String destination_zip){ From 5413a184350013896eb456640524b6999e4e7489 Mon Sep 17 00:00:00 2001 From: Selim Eser <2211482@stud.hs-mannheim.de> Date: Sun, 9 Jun 2024 18:25:57 +0200 Subject: [PATCH 2/9] Changed car_co2_km to car_l_100km because its more important for user and implemented calc_l_consumption --- src/main/java/de/hs_mannheim/domain/System.java | 6 ++++-- src/main/java/de/hs_mannheim/domain/User.java | 12 ++++++------ 2 files changed, 10 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index d087e45..aace573 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -253,8 +253,10 @@ public class System { return result; } - public String calc_co2(String destination_zip){ - return ""; + public String calc_l_consumption(String destination_zip){ + + return "" + (Double.parseDouble(distance(destination_zip).replace(" km", "")) * (current_user.getCar_l_100km() / 100)) + " l"; + } } diff --git a/src/main/java/de/hs_mannheim/domain/User.java b/src/main/java/de/hs_mannheim/domain/User.java index b0cd545..54eacac 100644 --- a/src/main/java/de/hs_mannheim/domain/User.java +++ b/src/main/java/de/hs_mannheim/domain/User.java @@ -7,7 +7,7 @@ public class User { private String hometown = ""; private int zip; private String car_name = ""; - private double car_co2_kmh; + private double car_l_100km; private double car_avg_kmh; private double bike_avg_kmh; @@ -51,14 +51,14 @@ public class User { this.car_name = car_name; } - public double getCar_co2_kmh() { - return car_co2_kmh; + public double getCar_l_100km() { + return car_l_100km; } - public void setCar_co2_kmh(double car_co2_kmh) { - this.car_co2_kmh = car_co2_kmh; + public void setCar_l_100km(double car_l_100km) { + this.car_l_100km = car_l_100km; } - + public double getCar_avg_kmh() { return car_avg_kmh; } From 44b385bf3d6b155c36b21a8948de280bd38210c5 Mon Sep 17 00:00:00 2001 From: Selim Eser <2211482@stud.hs-mannheim.de> Date: Sun, 9 Jun 2024 18:38:38 +0200 Subject: [PATCH 3/9] random_destinations_car method implemented --- .../java/de/hs_mannheim/domain/System.java | 20 ++++++++++++++++++- 1 file changed, 19 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index aace573..02847ab 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -12,6 +12,7 @@ import java.net.http.HttpResponse.BodyHandlers; import java.nio.charset.StandardCharsets; import java.util.ArrayList; import java.util.HashSet; +import java.util.List; import java.util.TreeSet; import org.json.JSONObject; @@ -71,7 +72,24 @@ public class System { } public ArrayList random_destinations_car(){ - return new ArrayList(); + + ArrayList 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; } public ArrayList random_destinations_bike(){ From 9deda6bf76e888017129004dc13e5217f8ca9d40 Mon Sep 17 00:00:00 2001 From: Selim Eser <2211482@stud.hs-mannheim.de> Date: Sun, 9 Jun 2024 18:39:48 +0200 Subject: [PATCH 4/9] random_destinations_bike method implemented --- .../java/de/hs_mannheim/domain/System.java | 18 +++++++++++++++++- 1 file changed, 17 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index 02847ab..28a32be 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -93,7 +93,23 @@ public class System { } public ArrayList random_destinations_bike(){ - return new ArrayList(); + ArrayList 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", "")) < 100) + result.add(line); + + } + } catch (Exception e) {} + + return result; } public String[] destination_details(String destination_zip){ From e3ae9c6599cdec1aa4e91c4d36f83b97fc660fc2 Mon Sep 17 00:00:00 2001 From: Selim Eser <2211482@stud.hs-mannheim.de> Date: Sun, 9 Jun 2024 18:45:42 +0200 Subject: [PATCH 5/9] destination_details method implemented --- src/main/java/de/hs_mannheim/domain/System.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index 28a32be..ea70f0f 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -113,7 +113,15 @@ public class System { } 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(){ From ba7ba72a877ea31b39fdac53f381d90dd8f2f886 Mon Sep 17 00:00:00 2001 From: Selim Eser <2211482@stud.hs-mannheim.de> Date: Sun, 9 Jun 2024 18:58:41 +0200 Subject: [PATCH 6/9] Added some Exception Handling --- .../java/de/hs_mannheim/domain/System.java | 21 +++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index ea70f0f..3ca2d1b 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -251,10 +251,10 @@ public class System { public String distance(String destination_zip){ - double lon1 = 1; - double lon2 = 1; - double lat1 = 1; - double lat2 = 1; + double lon1 = -1; + double lon2 = -1; + double lat1 = -1; + double lat2 = -1; InputStream inputStream = Main.class.getResourceAsStream("/zip.csv"); @@ -275,6 +275,9 @@ public class System { } } catch (Exception e) {} + if(lon1==-1||lon2==-1||lat1==-1||lat2==-1) + return "Es ist ein Fehler aufgetreten!"; + double dLat = lat2-lat1; double dLon = lon2-lon1; @@ -289,6 +292,13 @@ public class System { public String[] travel_time(String destination_zip){ 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; + } + 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"; @@ -297,6 +307,9 @@ public class System { 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 "" + (Double.parseDouble(distance(destination_zip).replace(" km", "")) * (current_user.getCar_l_100km() / 100)) + " l"; } From 3b49d9f2e27398033aa7812d1aa89c18e8845228 Mon Sep 17 00:00:00 2001 From: Selim Eser <2211482@stud.hs-mannheim.de> Date: Sun, 9 Jun 2024 19:30:50 +0200 Subject: [PATCH 7/9] Added Rounding in double methods for 3 decimal places, updated method tests, implemented travel_time method test and all tests run successful --- .../java/de/hs_mannheim/domain/System.java | 20 +++++++++++++---- .../de/hs_mannheim/domain/SystemTest.java | 22 ++++++++++++++++--- 2 files changed, 35 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index 3ca2d1b..be85240 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -32,6 +32,18 @@ public class System { 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 get_all_user(){ return new HashSet(); } @@ -285,7 +297,7 @@ public class System { 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"; } @@ -299,8 +311,8 @@ public class System { return result; } - 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"; + 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"; return result; } @@ -310,7 +322,7 @@ public class System { if(distance(destination_zip).equals("Es ist ein Fehler aufgetreten!")) return "Es ist ein Fehler aufgetreten!"; - return "" + (Double.parseDouble(distance(destination_zip).replace(" km", "")) * (current_user.getCar_l_100km() / 100)) + " l"; + return "" + (Math.round((Double.parseDouble(distance(destination_zip).replace(" km", "")) * (current_user.getCar_l_100km() / 100.0))*1000)/1000.0) + " l"; } } diff --git a/src/test/java/de/hs_mannheim/domain/SystemTest.java b/src/test/java/de/hs_mannheim/domain/SystemTest.java index 4a546a0..ab7124b 100644 --- a/src/test/java/de/hs_mannheim/domain/SystemTest.java +++ b/src/test/java/de/hs_mannheim/domain/SystemTest.java @@ -46,10 +46,26 @@ public class SystemTest { current_system.set_current_user_zip("68161"); - assertEquals("88.4596509227594 km", current_system.distance("60306")); // Frankfurt - assertEquals("581.1091061333296 km", current_system.distance("20095")); // Hamburg - assertEquals("603.6077163174941 km", current_system.distance("10115")); // Berlin + assertEquals("88.46 km", current_system.distance("60306")); // Frankfurt + assertEquals("581.109 km", current_system.distance("20095")); // Hamburg + 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 + } + } From db04ad8201e308f7e59b93152b816160aecaa590 Mon Sep 17 00:00:00 2001 From: Selim Eser <2211482@stud.hs-mannheim.de> Date: Sun, 9 Jun 2024 19:36:53 +0200 Subject: [PATCH 8/9] calc_l_consumption method test implemented and successful --- .../java/de/hs_mannheim/domain/SystemTest.java | 14 ++++++++++++++ 1 file changed, 14 insertions(+) diff --git a/src/test/java/de/hs_mannheim/domain/SystemTest.java b/src/test/java/de/hs_mannheim/domain/SystemTest.java index ab7124b..d84d42b 100644 --- a/src/test/java/de/hs_mannheim/domain/SystemTest.java +++ b/src/test/java/de/hs_mannheim/domain/SystemTest.java @@ -68,4 +68,18 @@ public class SystemTest { 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 + } + } From 0ac411e9d3794ae6487afafbfe362061158fb99a Mon Sep 17 00:00:00 2001 From: Selim Eser <2211482@stud.hs-mannheim.de> Date: Sun, 9 Jun 2024 19:59:22 +0200 Subject: [PATCH 9/9] Fixed search, random_destination method return ArrayList sizes, implemented random_destination tests and build ran successful --- src/main/java/de/hs_mannheim/domain/System.java | 8 ++++---- .../java/de/hs_mannheim/domain/SystemTest.java | 16 ++++++++++++++++ 2 files changed, 20 insertions(+), 4 deletions(-) diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index be85240..37e298a 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -72,8 +72,8 @@ public class System { try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { String line; - while ((line = reader.readLine()) != null) { - if(line.contains(hometown_or_zip)&&zip_set.size()<200){ + while ((line = reader.readLine()) != null && zip_set.size()<200) { + if(line.contains(hometown_or_zip)){ line = line.replace("\"", ""); zip_set.add(line); } @@ -91,7 +91,7 @@ public class System { try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { String line; - while ((line = reader.readLine()) != null) { + while ((line = reader.readLine()) != null && result.size()<3) { line = line.replace("\"", ""); @@ -111,7 +111,7 @@ public class System { try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { String line; - while ((line = reader.readLine()) != null) { + while ((line = reader.readLine()) != null && result.size()<3) { line = line.replace("\"", ""); diff --git a/src/test/java/de/hs_mannheim/domain/SystemTest.java b/src/test/java/de/hs_mannheim/domain/SystemTest.java index d84d42b..3ac1f7e 100644 --- a/src/test/java/de/hs_mannheim/domain/SystemTest.java +++ b/src/test/java/de/hs_mannheim/domain/SystemTest.java @@ -82,4 +82,20 @@ public class SystemTest { 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); + } + }