From 14f8c38a7052bad8b0f4b4967132841c62007fc5 Mon Sep 17 00:00:00 2001 From: Selim Eser <2211482@stud.hs-mannheim.de> Date: Sun, 9 Jun 2024 16:39:05 +0200 Subject: [PATCH 1/4] distance method implemented --- .../java/de/hs_mannheim/domain/System.java | 29 +++++++++++++++++-- 1 file changed, 27 insertions(+), 2 deletions(-) diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index 128203d..ae22776 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -65,7 +65,7 @@ public class System { zip_set.add(line); } } - } catch (IOException e) {} + } catch (Exception e) {} return new ArrayList<>(zip_set); } @@ -208,7 +208,32 @@ public class System { } public String distance(String destination_zip){ - return ""; + + double lon1 = 1; + double lon2 = 1; + double lat1 = 1; + double lat2 = 1; + + 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(line.split(";")[0].equals(destination_zip)){ + lon2 = Double.parseDouble(line.split(";")[2]); + lat2 = Double.parseDouble(line.split(";")[3]); + } + + if(line.split(";")[0].equals(current_user.getZip())){ + lon1 = Double.parseDouble(line.split(";")[2]); + lat1 = Double.parseDouble(line.split(";")[3]); + } + } + } catch (Exception e) {} + + return "" + 6378.388 * Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)); } public String[] travel_time(String destination_zip){ From 39cfdc4d438d8e0b3ae522c9749a138d3eb5789e Mon Sep 17 00:00:00 2001 From: Selim Eser <2211482@stud.hs-mannheim.de> Date: Sun, 9 Jun 2024 17:01:39 +0200 Subject: [PATCH 2/4] Fixed second if statement and corrected return formula --- src/main/java/de/hs_mannheim/domain/System.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index ae22776..5d8c180 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -226,14 +226,15 @@ public class System { lat2 = Double.parseDouble(line.split(";")[3]); } - if(line.split(";")[0].equals(current_user.getZip())){ + if(line.split(";")[0].equals(""+current_user.getZip())){ lon1 = Double.parseDouble(line.split(";")[2]); lat1 = Double.parseDouble(line.split(";")[3]); } } } catch (Exception e) {} - - return "" + 6378.388 * Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)); + + return "" + (Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)) * 111.324) + " km"; + } public String[] travel_time(String destination_zip){ From 523ff6a4a6f0040c2623a414ac9f61293615bf10 Mon Sep 17 00:00:00 2001 From: Selim Eser <2211482@stud.hs-mannheim.de> Date: Sun, 9 Jun 2024 17:54:58 +0200 Subject: [PATCH 3/4] Changed destination formula to Haversine and multiplied return distance by 1.25 for distance using the streets --- src/main/java/de/hs_mannheim/domain/System.java | 9 ++++++++- 1 file changed, 8 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 5d8c180..897cf8e 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -233,7 +233,14 @@ public class System { } } catch (Exception e) {} - return "" + (Math.acos(Math.sin(lat1) * Math.sin(lat2) + Math.cos(lat1) * Math.cos(lat2) * Math.cos(lon2 - lon1)) * 111.324) + " km"; + double dLat = lat2-lat1; + double dLon = lon2-lon1; + + double 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)); + + double distance = 6378.388 * 2.0 * Math.atan2(Math.sqrt(a), Math.sqrt(1.0-a)); + + return "" + (distance * 1.25) + " km"; } From 067617e8c0e8fc473c67ab3ce23b66e390e3286a Mon Sep 17 00:00:00 2001 From: Selim Eser <2211482@stud.hs-mannheim.de> Date: Sun, 9 Jun 2024 18:00:26 +0200 Subject: [PATCH 4/4] test method implemented and build successful --- src/test/java/de/hs_mannheim/domain/SystemTest.java | 13 +++++++++++++ 1 file changed, 13 insertions(+) diff --git a/src/test/java/de/hs_mannheim/domain/SystemTest.java b/src/test/java/de/hs_mannheim/domain/SystemTest.java index bc8acac..4a546a0 100644 --- a/src/test/java/de/hs_mannheim/domain/SystemTest.java +++ b/src/test/java/de/hs_mannheim/domain/SystemTest.java @@ -39,4 +39,17 @@ public class SystemTest { } + @Test + public void distance(){ + + System current_system = new System("35a75437476f12302f72e55d368485db"); + + 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 + + } + }