diff --git a/src/main/java/de/hs_mannheim/domain/Destination.java b/src/main/java/de/hs_mannheim/domain/Destination.java new file mode 100644 index 0000000..37a95b7 --- /dev/null +++ b/src/main/java/de/hs_mannheim/domain/Destination.java @@ -0,0 +1,36 @@ +package de.hs_mannheim.domain; + +public class Destination { + + private String town; + private String zip; + private double distance_from_user; + + public Destination(String town,String zip, double distance_from_user) { + this.town = town; + this.zip = zip; + this.distance_from_user = distance_from_user; + } + + public String getTown() { + return town; + } + + public void setTown(String town) { + this.town = town; + } + + public String getZip() { + return zip; + } + public void setZip(String zip) { + this.zip = zip; + } + public double getDistance_from_user() { + return distance_from_user; + } + public void setDistance_from_user(double distance_from_user) { + this.distance_from_user = distance_from_user; + } + +} diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index 376d237..ca4b468 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -7,6 +7,7 @@ import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.net.http.HttpResponse.BodyHandlers; import java.util.ArrayList; +import java.util.HashMap; import java.util.TreeSet; import org.apache.commons.codec.binary.Base64; @@ -17,7 +18,7 @@ public class System { private User current_user = new User(); private ArrayList all_user = new ArrayList<>(); private String api_key; - private ArrayList distances = new ArrayList<>(); + private HashMap all_destinations = new HashMap(); public System(String api_key) { this.api_key = api_key; @@ -25,7 +26,7 @@ public class System { } public void set_current_user_zip(String zip) { - this.current_user.setZip(Integer.parseInt(zip)); + this.current_user.setZip(zip); } public void set_current_user_car_l_100km(double car_l_100km) { @@ -56,13 +57,6 @@ public class System { return Base64.encodeBase64String(binary_data); } - public static int parseInt(String s) throws NumberFormatException{ - if(s.equals("")) - return 0; - else - return Integer.parseInt(s); - } - public static double parseDouble(String s) throws NumberFormatException { if(s.equals("")) return 0; @@ -81,7 +75,7 @@ public class System { fileString = path.split(";"); fileString[1] = decoding(fileString[1]); this.all_user.add(new User(fileString[0], fileString[1], fileString[2], - Integer.parseInt(fileString[3]), fileString[4], + fileString[3], fileString[4], Double.parseDouble(fileString[5]), Double.parseDouble(fileString[6]), Double.parseDouble(fileString[7]))); @@ -108,22 +102,22 @@ public class System { current_user = new User(user.getUsername(), user.getPassword(), user.getHometown(), user.getZip(), user.getCar_name(), user.getCar_l_100km(), user.getCar_avg_kmh(), user.getBike_avg_kmh()); + + all_distances(); return true; } } return false; } - public boolean sign_up_user(String username, String password, String hometown, String zipS, + public boolean sign_up_user(String username, String password, String hometown, String zip, String car_name, String car_l_100kmS, String car_avg_kmhS, String bike_avg_kmhS){ - int zip; double car_l_100km; double car_avg_kmh; double bike_avg_kmh; try{ - zip = parseInt(zipS); car_l_100km = parseDouble(car_l_100kmS); car_avg_kmh = parseDouble(car_avg_kmhS); bike_avg_kmh = parseDouble(bike_avg_kmhS); @@ -131,14 +125,14 @@ public class System { return false; } - if(username.equals("")||password.equals("")||hometown.equals("")||zipS.equals("")) + if(username.equals("")||password.equals("")||hometown.equals("")||zip.equals("")) return false; for(User user: this.all_user) if(user.getUsername().equals(username)) return false; - ArrayList mem = search(zipS); + ArrayList mem = search(zip); boolean bool = false; for (String line: mem) @@ -157,19 +151,18 @@ public class System { write_to_file(all_user_toString(), "src/main/resources/user_data.csv"); write_to_file(all_user_toString(), "src/test/resources/user_data.csv"); + all_distances(); return true; } - public boolean change_user_details(String username, String password, String hometown, String zipS, + public boolean change_user_details(String username, String password, String hometown, String zip, String car_name, String car_l_100kmS, String car_avg_kmhS, String bike_avg_kmhS){ - int zip; double car_l_100km; double car_avg_kmh; double bike_avg_kmh; try{ - zip = parseInt(zipS); car_l_100km = parseDouble(car_l_100kmS); car_avg_kmh = parseDouble(car_avg_kmhS); bike_avg_kmh = parseDouble(bike_avg_kmhS); @@ -177,14 +170,14 @@ public class System { return false; } - if(username.equals("")||password.equals("")||hometown.equals("")||zipS.equals("")) + if(username.equals("")||password.equals("")||hometown.equals("")||zip.equals("")) return false; for(User user: this.all_user) if(user.getUsername().equals(username)) return false; - ArrayList mem = search(zipS); + ArrayList mem = search(zip); boolean bool = false; for (String line: mem) @@ -210,6 +203,7 @@ public class System { write_to_file(all_user_toString(), "src/main/resources/user_data.csv"); write_to_file(all_user_toString(), "src/test/resources/user_data.csv"); + all_distances(); return true; } @@ -225,16 +219,12 @@ public class System { } public void sign_out_user() { - this.distances = new ArrayList<>(); - current_user = new User(); } public String[] getDetails(){ return new String[]{current_user.getUsername(), current_user.getPassword(), - current_user.getHometown(), - current_user.getZip()==0?"":String.valueOf(current_user.getZip()), - current_user.getCar_name(), + current_user.getHometown(), current_user.getZip(), current_user.getCar_name(), current_user.getCar_l_100km()==0?"":String.valueOf(current_user.getCar_l_100km()), current_user.getCar_avg_kmh()==0?"":String.valueOf(current_user.getCar_avg_kmh()), current_user.getBike_avg_kmh()==0?"":String.valueOf(current_user.getBike_avg_kmh())}; @@ -249,10 +239,9 @@ public class System { try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { String line; while ((line = reader.readLine()) != null && zip_set.size() < 200) { - if (line.contains(hometown_or_zip)) { - line = line.replace("\"", ""); + line = line.replace("\"", ""); + if (line.split(";")[0].contains(hometown_or_zip)||line.split(";")[1].contains(hometown_or_zip)) zip_set.add(line); - } } } catch (Exception e) { } @@ -262,37 +251,37 @@ public class System { public ArrayList random_destinations_car() { - calc_all_distances(); - - ArrayList mem = new ArrayList<>(); + ArrayList mem = new ArrayList<>(); ArrayList result = new ArrayList<>(); + int random = 0; - for (int i = 0; i < this.distances.size(); i++) { - if (Double.parseDouble(this.distances.get(i).split(";")[2]) > 150) - mem.add(this.distances.get(i)); + for(Destination destination: this.all_destinations.values()){ + if(destination.getDistance_from_user()>150) + mem.add(destination); } - for (int i = 0; i < 3; i++) - result.add(mem.get((int) (Math.random() * mem.size()))); - + for (int i = 0; i < 3; i++){ + random = (int) (Math.random() * mem.size()); + result.add(mem.get(random).getZip() + ";" + mem.get(random).getTown()); + } return result; } public ArrayList random_destinations_bike() { - calc_all_distances(); - - ArrayList mem = new ArrayList<>(); + ArrayList mem = new ArrayList<>(); ArrayList result = new ArrayList<>(); + int random = 0; - for (int i = 0; i < this.distances.size(); i++) { - if (Double.parseDouble(this.distances.get(i).split(";")[2]) < 100) - mem.add(this.distances.get(i)); + for(Destination destination: this.all_destinations.values()){ + if(destination.getDistance_from_user()<100) + mem.add(destination); } - for (int i = 0; i < 3; i++) - result.add(mem.get((int) (Math.random() * mem.size()))); - + for (int i = 0; i < 3; i++){ + random = (int) (Math.random() * mem.size()); + result.add(mem.get(random).getZip() + ";" + mem.get(random).getTown()); + } return result; } @@ -434,13 +423,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"); @@ -449,12 +443,7 @@ 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())) { + if (line.split(";")[0].equals(current_user.getZip())) { lon1 = Double.parseDouble(line.split(";")[2]); lat1 = Double.parseDouble(line.split(";")[3]); } @@ -462,50 +451,47 @@ public class System { } catch (Exception e) { } - if (lon1 == -1 || lon2 == -1 || lat1 == -1 || lat2 == -1) - return "Es ist ein Fehler aufgetreten!"; + InputStream inputStream2 = System.class.getResourceAsStream("/zip.csv"); - 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))) { + try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream2))) { 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(";")[1],line.split(";")[0], result)); } } catch (Exception e) { } + + return true; } + public String distance(String destination_zip){ + + return "" + this.all_destinations.get(destination_zip).getDistance_from_user() + " km"; + + } + public String[] travel_time(String destination_zip) { String[] result = new String[2]; - if (distance(destination_zip).equals("Es ist ein Fehler aufgetreten!")) { - result[0] = "Es ist ein Fehler aufgetreten!"; - result[1] = "Es ist ein Fehler aufgetreten!"; - return result; - } - 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"; @@ -514,9 +500,6 @@ public class System { public String calc_l_consumption(String destination_zip) { - if (distance(destination_zip).equals("Es ist ein Fehler aufgetreten!")) - return "Es ist ein Fehler aufgetreten!"; - return "" + (Math.round((Double.parseDouble(distance(destination_zip).replace(" km", "")) * (current_user.getCar_l_100km() / 100.0)) * 1000) / 1000.0) + " l"; } diff --git a/src/main/java/de/hs_mannheim/domain/User.java b/src/main/java/de/hs_mannheim/domain/User.java index b1f311b..5a6daeb 100644 --- a/src/main/java/de/hs_mannheim/domain/User.java +++ b/src/main/java/de/hs_mannheim/domain/User.java @@ -5,7 +5,7 @@ public class User { private String username = ""; private String password = ""; private String hometown = ""; - private int zip = 0; + private String zip = ""; private String car_name = ""; private double car_l_100km = 0; private double car_avg_kmh = 0; @@ -13,7 +13,7 @@ public class User { public User(){} - public User(String username, String password, String hometown, int zip + public User(String username, String password, String hometown, String zip , String car_name, double car_l_100km, double car_avg_kmh, double bike_avg_kmh){ this.username = username; this.password = password; @@ -49,11 +49,11 @@ public class User { this.hometown = hometown; } - public int getZip() { + public String getZip() { return zip; } - public void setZip(int zip) { + public void setZip(String zip) { this.zip = zip; } diff --git a/src/main/resources/user_data.csv b/src/main/resources/user_data.csv index d73791d..912c767 100644 --- a/src/main/resources/user_data.csv +++ b/src/main/resources/user_data.csv @@ -1,2 +1,2 @@ Daniel;MTQwMURhbmllbA==;Mannheim;68305;BMW;1.5;50.4;40.2 -David;MTIzRXNlbA==;Mannheim;68161;AMG;10.0;300.0;20.0 \ No newline at end of file +David;MTIzRXNlbA==;Mannheim;68161;AMG;10.0;300.0;20.0 \ No newline at end of file diff --git a/src/test/java/de/hs_mannheim/domain/SystemTest.java b/src/test/java/de/hs_mannheim/domain/SystemTest.java index 53f4e8b..3059507 100644 --- a/src/test/java/de/hs_mannheim/domain/SystemTest.java +++ b/src/test/java/de/hs_mannheim/domain/SystemTest.java @@ -20,7 +20,7 @@ public class SystemTest { @Test public void current_weather(){ - current_system.set_current_user_zip("68161"); + current_system.sign_in_user("David","123Esel"); assertNotEquals("Es ist ein Fehler aufgetreten!",current_system.current_weather()); } @@ -37,8 +37,8 @@ public class SystemTest { @Test public void distance(){ - - current_system.set_current_user_zip("68161"); + + current_system.sign_in_user("David","123Esel"); assertEquals("88.46 km", current_system.distance("60306")); // Frankfurt assertEquals("581.109 km", current_system.distance("20095")); // Hamburg @@ -49,7 +49,8 @@ public class SystemTest { @Test public void travel_time(){ - current_system.set_current_user_zip("68161"); + current_system.sign_in_user("David","123Esel"); + current_system.set_current_user_car_avg_kmh(100); current_system.set_current_user_bike_avg_kmh(20); @@ -63,7 +64,8 @@ public class SystemTest { @Test public void calc_l_consumption(){ - current_system.set_current_user_zip("68161"); + current_system.sign_in_user("David","123Esel"); + current_system.set_current_user_car_avg_kmh(100); current_system.set_current_user_car_l_100km(10); @@ -75,15 +77,17 @@ public class SystemTest { @Test public void random_destinations(){ - current_system.set_current_user_zip("68161"); - - assertEquals(3, current_system.random_destinations_car().size()); // random_destinations_car gibt genau 3 destinations zurück - assertEquals(3, current_system.random_destinations_bike().size()); // random destinations_bike gibt genau 3 destinations zurück + current_system.sign_in_user("David","123Esel"); + ArrayList random_destination_car = current_system.random_destinations_car(); + ArrayList random_destination_bike = current_system.random_destinations_bike(); + + assertEquals(3, random_destination_car.size()); // random_destinations_car gibt genau 3 destinations zurück + assertEquals(3, random_destination_bike.size()); // random destinations_bike gibt genau 3 destinations zurück // random_destinations_car gibt nur destinations mit mindestens 150 km Entfernung zurück - assertEquals(true, Double.parseDouble(current_system.distance(current_system.random_destinations_car().get(0).split(";")[0]).replace(" km", "")) > 150); + assertEquals(true, Double.parseDouble(current_system.distance(random_destination_car.get(0).split(";")[0]).replace(" km", "")) > 150); // random_destinations_bike gibt nur destinations mit maximal 100 km Entfernung zurück - assertEquals(true, Double.parseDouble(current_system.distance(current_system.random_destinations_bike().get(0).split(";")[0]).replace(" km", "")) < 100); + assertEquals(true, Double.parseDouble(current_system.distance(random_destination_bike.get(0).split(";")[0]).replace(" km", "")) < 100); } @Test @@ -144,10 +148,11 @@ public class SystemTest { assertEquals("Enes", current_system.getDetails()[0]); } - /* - Tests auf Basis von user_data.csv: Daniel;MTQwMURhbmllbA==;Mannheim;68305;BMW;1.5;50.4;40.2 - David;MTIzRXNlbA==;Mannheim;68161;AMG;10.0;300.0;20.0 - */ +/* +Tests auf Basis von user_data.csv: +Daniel;MTQwMURhbmllbA==;Mannheim;68305;BMW;1.5;50.4;40.2 +David;MTIzRXNlbA==;Mannheim;68161;AMG;10.0;300.0;20.0 +*/ } \ No newline at end of file diff --git a/src/test/resources/user_data.csv b/src/test/resources/user_data.csv index d73791d..912c767 100644 --- a/src/test/resources/user_data.csv +++ b/src/test/resources/user_data.csv @@ -1,2 +1,2 @@ Daniel;MTQwMURhbmllbA==;Mannheim;68305;BMW;1.5;50.4;40.2 -David;MTIzRXNlbA==;Mannheim;68161;AMG;10.0;300.0;20.0 \ No newline at end of file +David;MTIzRXNlbA==;Mannheim;68161;AMG;10.0;300.0;20.0 \ No newline at end of file