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] 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 + } + }