Added Rounding in double methods for 3 decimal places, updated method tests, implemented travel_time method test and all tests run successful

distance_related_methods
Selim Eser 2024-06-09 19:30:50 +02:00
parent ba7ba72a87
commit 3b49d9f2e2
2 changed files with 35 additions and 7 deletions

View File

@ -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<User> get_all_user(){
return new HashSet<User>();
}
@ -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";
}
}

View File

@ -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
}
}