diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index 128203d..897cf8e 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,40 @@ 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) {} + + 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"; + } public String[] travel_time(String destination_zip){ 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 + + } + }