From 531407dcf75865756f370c3b14a3d96a1784a67c Mon Sep 17 00:00:00 2001 From: Selim Eser <2211482@stud.hs-mannheim.de> Date: Tue, 11 Jun 2024 22:39:07 +0200 Subject: [PATCH] Added calculation method for all Destinations from current_user zip --- .DS_Store | Bin 6148 -> 0 bytes .../de/hs_mannheim/domain/Destination.java | 4 ++ .../java/de/hs_mannheim/domain/System.java | 65 +++++++++--------- 3 files changed, 35 insertions(+), 34 deletions(-) delete mode 100644 .DS_Store diff --git a/.DS_Store b/.DS_Store deleted file mode 100644 index 33584f90c2eb03423002dd4a65e7aac253e1829f..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 6148 zcmeHKOHKnZ41J~*RASR58>F0|5;q7{S+MQ}papd^bgI%;EayfX5uRgHF_0|?AzN~u z6MrUo(&Pg`ru&C0U<_bHRTMQUqQh0!i3bmfQfj>53C(TOE*DLu&|e(Vxu0Q!mNTE= z)BM{7Jxa?pb@En(7BIjwwpiLKk5RAnsKb1_Zrb&VUNVGJ>~PQQ6&|tcSCGpu71#|4 z27-ZLAQ%V+et-czktm~c!)SwnU?3PcGNAiIqAF&`&aiA9Y|0aWsMqKswDr=gImt0Q zc82sQctD*nU}OXqm#3kHILf5w0eXY<*Vi}JJe+w%0RP1HN8ipF(nP-yoq0qp2La#5V-PiiwR XJ9dUzMaN4zF)jjykZ6K|Utr)JY4bL9 diff --git a/src/main/java/de/hs_mannheim/domain/Destination.java b/src/main/java/de/hs_mannheim/domain/Destination.java index 23f03a6..e64984e 100644 --- a/src/main/java/de/hs_mannheim/domain/Destination.java +++ b/src/main/java/de/hs_mannheim/domain/Destination.java @@ -5,6 +5,10 @@ public class Destination { private String zip; private double distance_from_user; + public Destination(String zip, double distance_from_user) { + this.zip = zip; + this.distance_from_user = distance_from_user; + } public String getZip() { return zip; } diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index 5b241f8..4a3f08b 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -250,8 +250,6 @@ public class System { public ArrayList random_destinations_car() { - calc_all_distances(); - ArrayList mem = new ArrayList<>(); ArrayList result = new ArrayList<>(); @@ -268,8 +266,6 @@ public class System { public ArrayList random_destinations_bike() { - calc_all_distances(); - ArrayList mem = new ArrayList<>(); ArrayList result = new ArrayList<>(); @@ -422,13 +418,18 @@ public class System { + "Übermorgen: " + weather_day_2 + ": Minimum: " + temperature_day_2_low + " °C" + "; Maximum: " + temperature_day_2_high + " °C\n" + "Überübermorgen: " + weather_day_3 + ": Minimum: " + temperature_day_3_low + " °C" + "; Maximum: " + temperature_day_3_high + " °C"; } - - public String distance(String destination_zip) { + + public boolean all_distances() { double lon1 = -1; double lon2 = -1; double lat1 = -1; double lat2 = -1; + double dLat; + double dLon; + double a; + double distance; + double result; InputStream inputStream = System.class.getResourceAsStream("/zip.csv"); @@ -437,11 +438,6 @@ public class System { 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]); @@ -450,40 +446,41 @@ 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; - - 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 "" + (Math.round((distance * 1.25) * 1000) / 1000.0) + " km"; - - } - - public void calc_all_distances() { - - if (!this.distances.isEmpty()) - return; - - InputStream inputStream = System.class.getResourceAsStream("/zip.csv"); - try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { String line; while ((line = reader.readLine()) != null) { - line = line.replace("\"", ""); - this.distances.add(line.split(";")[0] + ";" + line.split(";")[1] + ";" + Double.parseDouble(distance(line.split(";")[0]).replace(" km", ""))); + lon2 = Double.parseDouble(line.split(";")[2]); + lat2 = Double.parseDouble(line.split(";")[3]); + + if (lon1 == -1 || lon2 == -1 || lat1 == -1 || lat2 == -1) + return false; + + dLat = lat2 - lat1; + dLon = lon2 - lon1; + + 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)); + + distance = 6378.388 * 2.0 * Math.atan2(Math.sqrt(a), Math.sqrt(1.0 - a)); + + result = Math.round((distance * 1.25) * 1000) / 1000.0; + + this.all_destinations.put(line.split(";")[0],new Destination(line.split(";")[0], result)); } } catch (Exception e) { } + + return true; } + public String distance(String destination_zip){ + + return "" + this.all_destinations.get(destination_zip) + " km"; + + } + public String[] travel_time(String destination_zip) { String[] result = new String[2];