From 1aed134131fdb2341b38f2eee28f850af791295a Mon Sep 17 00:00:00 2001 From: Selim Eser <2211482@stud.hs-mannheim.de> Date: Sun, 9 Jun 2024 21:15:39 +0200 Subject: [PATCH 1/3] Corrected the method logic --- .../java/de/hs_mannheim/domain/System.java | 22 ++++++++++++++----- 1 file changed, 16 insertions(+), 6 deletions(-) diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index 3b47d0f..d59c693 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -82,42 +82,52 @@ public class System { public ArrayList random_destinations_car(){ - ArrayList result = new ArrayList<>(); + ArrayList mem = new ArrayList<>(); InputStream inputStream = Main.class.getResourceAsStream("/zip.csv"); try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { String line; - while ((line = reader.readLine()) != null && result.size()<3) { + while ((line = reader.readLine()) != null) { line = line.replace("\"", ""); if(Double.parseDouble(distance(line.split(";")[0]).replace(" km", "")) > 150) - result.add(line); + mem.add(line); } } catch (Exception e) {} + ArrayList result = new ArrayList<>(); + + for(int i = 0; i<3; i++) + result.add(mem.get((int) (Math.random()*mem.size()))); + return result; } public ArrayList random_destinations_bike(){ - ArrayList result = new ArrayList<>(); + ArrayList mem = new ArrayList<>(); InputStream inputStream = Main.class.getResourceAsStream("/zip.csv"); try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { String line; - while ((line = reader.readLine()) != null && result.size()<3) { + while ((line = reader.readLine()) != null) { line = line.replace("\"", ""); if(Double.parseDouble(distance(line.split(";")[0]).replace(" km", "")) < 100) - result.add(line); + mem.add(line); } } catch (Exception e) {} + ArrayList result = new ArrayList<>(); + + for(int i = 0; i<3; i++) + result.add(mem.get((int) (Math.random()*mem.size()))); + return result; } From e2923ae8383cd892c2e0e04b067ef5902c641f33 Mon Sep 17 00:00:00 2001 From: Selim Eser <2211482@stud.hs-mannheim.de> Date: Sun, 9 Jun 2024 21:51:37 +0200 Subject: [PATCH 2/3] System.java now saves all distances after one calculation to fasten random_destinations methods up --- .../java/de/hs_mannheim/domain/System.java | 63 ++++++++++--------- .../de/hs_mannheim/domain/SystemTest.java | 16 +---- 2 files changed, 36 insertions(+), 43 deletions(-) diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index d59c693..2e56cfc 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -20,6 +20,7 @@ public class System { private User current_user = new User(); private String api_key; + private ArrayList distances = new ArrayList<>(); public System(String api_key) { this.api_key = api_key; @@ -82,23 +83,15 @@ public class System { public ArrayList random_destinations_car(){ - ArrayList mem = new ArrayList<>(); - - InputStream inputStream = Main.class.getResourceAsStream("/zip.csv"); + calc_all_distances(); - try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { - String line; - while ((line = reader.readLine()) != null) { - - line = line.replace("\"", ""); - - if(Double.parseDouble(distance(line.split(";")[0]).replace(" km", "")) > 150) - mem.add(line); - - } - } catch (Exception e) {} - + ArrayList mem = new ArrayList<>(); ArrayList result = new ArrayList<>(); + + for(int i = 0; i150) + mem.add(this.distances.get(i)); + } for(int i = 0; i<3; i++) result.add(mem.get((int) (Math.random()*mem.size()))); @@ -107,23 +100,16 @@ public class System { } public ArrayList random_destinations_bike(){ - ArrayList mem = new ArrayList<>(); - InputStream inputStream = Main.class.getResourceAsStream("/zip.csv"); + calc_all_distances(); - try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { - String line; - while ((line = reader.readLine()) != null) { - - line = line.replace("\"", ""); - - if(Double.parseDouble(distance(line.split(";")[0]).replace(" km", "")) < 100) - mem.add(line); - - } - } catch (Exception e) {} - + ArrayList mem = new ArrayList<>(); ArrayList result = new ArrayList<>(); + + for(int i = 0; i list = current_system.search("Mannheim"); assertEquals("68159", list.get(0).split(";")[0]); @@ -42,8 +38,6 @@ public class SystemTest { @Test public void distance(){ - System current_system = new System("35a75437476f12302f72e55d368485db"); - current_system.set_current_user_zip("68161"); assertEquals("88.46 km", current_system.distance("60306")); // Frankfurt @@ -55,8 +49,6 @@ public class SystemTest { @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); @@ -71,8 +63,6 @@ public class SystemTest { @Test public void calc_l_consumption(){ - 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_car_l_100km(10); @@ -85,8 +75,6 @@ public class SystemTest { @Test public void random_destinations(){ - System current_system = new System("35a75437476f12302f72e55d368485db"); - current_system.set_current_user_zip("68161"); assertEquals(3, current_system.random_destinations_car().size()); // random_destinations_car gibt genau 3 destinations zurück From 4fbcfb4bf5b423198cccb2173ef923bb72c7251c Mon Sep 17 00:00:00 2001 From: Selim Eser <2211482@stud.hs-mannheim.de> Date: Sun, 9 Jun 2024 21:54:50 +0200 Subject: [PATCH 3/3] If user signs out the saved distances get deleted --- src/main/java/de/hs_mannheim/domain/System.java | 4 +++- 1 file changed, 3 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 2e56cfc..ca38761 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -55,7 +55,9 @@ public class System { return true; } - public void sign_out_user(){} + public void sign_out_user(){ + this.distances = new ArrayList<>(); + } public boolean change_user_details(String username, String password, String hometown, int zip, String car_name, double car_co2_km, double car_avg_kmh, double bike_avg_kmh){