From a41384a1165ef1022366e492fbe9178e4ef42222 Mon Sep 17 00:00:00 2001 From: Daniel Zdravkovic <3012893@stud.hs-mannheim.de> Date: Mon, 10 Jun 2024 11:59:35 +0200 Subject: [PATCH 01/23] get_all_users method (complete but without encoding method) --- .../java/de/hs_mannheim/domain/System.java | 35 ++++++++++++++++--- src/main/java/de/hs_mannheim/domain/User.java | 14 ++++++++ 2 files changed, 44 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index cf8c246..b91c89e 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -1,14 +1,13 @@ package de.hs_mannheim.domain; -import java.io.BufferedReader; -import java.io.InputStream; -import java.io.InputStreamReader; +import java.io.*; import java.net.URI; import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.net.http.HttpResponse.BodyHandlers; import java.util.ArrayList; +import java.util.Arrays; import java.util.HashSet; import java.util.TreeSet; @@ -39,9 +38,35 @@ public class System { public void set_current_user_bike_avg_kmh(double bike_avg_kmh){ this.current_user.setBike_avg_kmh(bike_avg_kmh); } + + public void encoding(String string){ + + } - public HashSet get_all_user(){ - return new HashSet(); + public HashSet get_all_user() throws FileNotFoundException, IOException{ + HashSet all_users = new HashSet<>(); + File file = new File("user_information.csv"); + String[] fileString = new String[8]; + + if(file.exists() && file.isFile()){ + FileReader fileReader = new FileReader(file); + BufferedReader bufferedReader = new BufferedReader(fileReader); + String path; + + while((path = bufferedReader.readLine()) != null){ + fileString = path.split(";"); + fileString = (String[]) Arrays.stream(fileString) + .map(string -> string.substring(0, string.length()-1)) + .toArray(); + encoding(fileString[1]); + all_users.add(new User(fileString[0], fileString[1], fileString[2], + Integer.parseInt(fileString[3]), fileString[4], + Double.parseDouble(fileString[5]), + Double.parseDouble(fileString[6]), + Double.parseDouble(fileString[7]))); + } + } + return all_users; } public boolean sign_in_user(String username, String password){ diff --git a/src/main/java/de/hs_mannheim/domain/User.java b/src/main/java/de/hs_mannheim/domain/User.java index 54eacac..cd9b6f9 100644 --- a/src/main/java/de/hs_mannheim/domain/User.java +++ b/src/main/java/de/hs_mannheim/domain/User.java @@ -11,6 +11,20 @@ public class User { private double car_avg_kmh; private double bike_avg_kmh; + public User(){} + + public User(String username, String password, String hometown, int zip + , String car_name, double car_l_100km, double car_avg_kmh, double bike_avg_kmh){ + this.username = username; + this.password = password; + this.hometown = hometown; + this.zip = zip; + this.car_name = car_name; + this.car_l_100km = car_l_100km; + this.car_avg_kmh = car_avg_kmh; + this.bike_avg_kmh = bike_avg_kmh; + } + public String getUsername() { return username; } From 5120dd94be3b7229c9e7ac51081f8d6c98d5d2c5 Mon Sep 17 00:00:00 2001 From: Daniel Zdravkovic <3012893@stud.hs-mannheim.de> Date: Mon, 10 Jun 2024 13:34:52 +0200 Subject: [PATCH 02/23] encoding und decoding methods mit Base64 implementiert --- src/main/java/de/hs_mannheim/domain/System.java | 12 +++++++++++- 1 file changed, 11 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 b91c89e..0b4ea1d 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -11,6 +11,7 @@ import java.util.Arrays; import java.util.HashSet; import java.util.TreeSet; +import org.apache.commons.codec.binary.Base64; import org.json.JSONObject; public class System { @@ -39,8 +40,17 @@ public class System { this.current_user.setBike_avg_kmh(bike_avg_kmh); } - public void encoding(String string){ + public void decoding(String string){ + byte[] binary_data = Base64.decodeBase64(string); + string = new String(binary_data); + } + public void encoding(String string){ + byte[] binary_data = new byte[string.length()]; + for(int i = 0; i < string.length(); i++){ + binary_data[i] = (byte) string.charAt(i); + } + string = Base64.encodeBase64String(binary_data); } public HashSet get_all_user() throws FileNotFoundException, IOException{ From 41676607adca78f2b563bd37a9f7ece3054cdb06 Mon Sep 17 00:00:00 2001 From: Daniel Zdravkovic <3012893@stud.hs-mannheim.de> Date: Mon, 10 Jun 2024 14:17:14 +0200 Subject: [PATCH 03/23] createSheet Methode welche eine Tabelle erstellt mit User Informationen --- .../java/de/hs_mannheim/domain/System.java | 21 ++++++++++++++++--- 1 file changed, 18 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 0b4ea1d..c5513e7 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -12,16 +12,20 @@ import java.util.HashSet; import java.util.TreeSet; import org.apache.commons.codec.binary.Base64; +import org.apache.poi.xssf.usermodel.XSSFSheet; +import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.json.JSONObject; public class System { - + private User current_user = new User(); private String api_key; private ArrayList distances = new ArrayList<>(); + private XSSFWorkbook workbook = new XSSFWorkbook(); public System(String api_key) { this.api_key = api_key; + createSheet(); } public void set_current_user_zip(String zip){ @@ -52,10 +56,21 @@ public class System { } string = Base64.encodeBase64String(binary_data); } - + + public void createSheet(){ + try + { + XSSFSheet sheet1 = workbook.createSheet("user_information"); + FileOutputStream fileOut = new FileOutputStream("workbook.xlsx"); + workbook.write(fileOut); + fileOut.close(); + } + catch(Exception ex) {} + } + public HashSet get_all_user() throws FileNotFoundException, IOException{ HashSet all_users = new HashSet<>(); - File file = new File("user_information.csv"); + File file = new File("workbook.xlsx"); String[] fileString = new String[8]; if(file.exists() && file.isFile()){ From 88493290fd5ea30be96f478053bd551ba07a90f9 Mon Sep 17 00:00:00 2001 From: Daniel Zdravkovic <3012893@stud.hs-mannheim.de> Date: Mon, 10 Jun 2024 14:40:53 +0200 Subject: [PATCH 04/23] sign_in_user method implemented --- pom.xml | 25 +++++++++++++++++++ .../java/de/hs_mannheim/domain/System.java | 14 +++++++++-- 2 files changed, 37 insertions(+), 2 deletions(-) diff --git a/pom.xml b/pom.xml index 1a81f1b..e1f5961 100644 --- a/pom.xml +++ b/pom.xml @@ -28,6 +28,31 @@ json 20210307 + + commons-codec + commons-codec + 1.17.0 + + + org.apache.poi + poi + 3.9 + + + org.apache.poi + poi-ooxml + 5.2.3 + + + commons-io + commons-io + 2.7 + + + org.apache.logging.log4j + log4j-core + 2.20.0 + diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index c5513e7..e9613a6 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -94,8 +94,18 @@ public class System { return all_users; } - public boolean sign_in_user(String username, String password){ - return true; + public boolean sign_in_user(String username, String password) throws IOException { + ArrayList mem = new ArrayList<>(get_all_user()); + + for(User user : mem){ + if(user.getUsername().equals(username) && user.getPassword().equals(password)){ + 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()); + return true; + } + } + return false; } public boolean sign_up_user(String username, String password, String hometown, int zip, From 52ed99fc1dd9063348b3f35c6432b9c525e0e774 Mon Sep 17 00:00:00 2001 From: Daniel Zdravkovic <3012893@stud.hs-mannheim.de> Date: Mon, 10 Jun 2024 14:44:55 +0200 Subject: [PATCH 05/23] sign_out_user method implemented --- src/main/java/de/hs_mannheim/domain/System.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index e9613a6..52b9cff 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -115,6 +115,8 @@ public class System { public void sign_out_user(){ this.distances = new ArrayList<>(); + + current_user = new User(); } public boolean change_user_details(String username, String password, String hometown, int zip, From f5e74fbbe5b7539bec5f9a71d810a98c349e7011 Mon Sep 17 00:00:00 2001 From: Daniel Zdravkovic <3012893@stud.hs-mannheim.de> Date: Mon, 10 Jun 2024 15:05:01 +0200 Subject: [PATCH 06/23] discard some useless dependencies --- pom.xml | 20 ----------- .../java/de/hs_mannheim/domain/System.java | 36 +++++++++---------- 2 files changed, 17 insertions(+), 39 deletions(-) diff --git a/pom.xml b/pom.xml index e1f5961..78cbbe7 100644 --- a/pom.xml +++ b/pom.xml @@ -33,26 +33,6 @@ commons-codec 1.17.0 - - org.apache.poi - poi - 3.9 - - - org.apache.poi - poi-ooxml - 5.2.3 - - - commons-io - commons-io - 2.7 - - - org.apache.logging.log4j - log4j-core - 2.20.0 - diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index 52b9cff..e495511 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -12,8 +12,6 @@ import java.util.HashSet; import java.util.TreeSet; import org.apache.commons.codec.binary.Base64; -import org.apache.poi.xssf.usermodel.XSSFSheet; -import org.apache.poi.xssf.usermodel.XSSFWorkbook; import org.json.JSONObject; public class System { @@ -21,11 +19,9 @@ public class System { private User current_user = new User(); private String api_key; private ArrayList distances = new ArrayList<>(); - private XSSFWorkbook workbook = new XSSFWorkbook(); public System(String api_key) { this.api_key = api_key; - createSheet(); } public void set_current_user_zip(String zip){ @@ -57,20 +53,9 @@ public class System { string = Base64.encodeBase64String(binary_data); } - public void createSheet(){ - try - { - XSSFSheet sheet1 = workbook.createSheet("user_information"); - FileOutputStream fileOut = new FileOutputStream("workbook.xlsx"); - workbook.write(fileOut); - fileOut.close(); - } - catch(Exception ex) {} - } - public HashSet get_all_user() throws FileNotFoundException, IOException{ HashSet all_users = new HashSet<>(); - File file = new File("workbook.xlsx"); + File file = new File("/user_data.csv"); String[] fileString = new String[8]; if(file.exists() && file.isFile()){ @@ -109,8 +94,21 @@ public class System { } public boolean sign_up_user(String username, String password, String hometown, int zip, - String car_name, double car_co2_km, double car_avg_kmh, double bike_avg_kmh){ - return true; + String car_name, double car_l_100km, double car_avg_kmh, double bike_avg_kmh) throws IOException { + ArrayList mem = new ArrayList<>(get_all_user()); + ArrayList user_names = new ArrayList<>(); + + for(User user : mem){ + user_names.add(user.getUsername()); + } + if(!user_names.contains(username)){ + current_user = new User(username, password, + hometown, zip, car_name, + car_l_100km, car_avg_kmh, bike_avg_kmh); + + return true; + } + return false; } public void sign_out_user(){ @@ -120,7 +118,7 @@ public class System { } 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){ + String car_name, double car_l_100km, double car_avg_kmh, double bike_avg_kmh){ return true; } From 96e7dad23ffab624120ec06b707780cef328c6ee Mon Sep 17 00:00:00 2001 From: Daniel Zdravkovic <3012893@stud.hs-mannheim.de> Date: Mon, 10 Jun 2024 15:17:36 +0200 Subject: [PATCH 07/23] sign_up_user implemented and new logic for csv data --- .../java/de/hs_mannheim/domain/System.java | 24 ++++++++++++++++--- 1 file changed, 21 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 e495511..2a738c5 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -65,9 +65,6 @@ public class System { while((path = bufferedReader.readLine()) != null){ fileString = path.split(";"); - fileString = (String[]) Arrays.stream(fileString) - .map(string -> string.substring(0, string.length()-1)) - .toArray(); encoding(fileString[1]); all_users.add(new User(fileString[0], fileString[1], fileString[2], Integer.parseInt(fileString[3]), fileString[4], @@ -106,6 +103,27 @@ public class System { hometown, zip, car_name, car_l_100km, car_avg_kmh, bike_avg_kmh); + ArrayList content = new ArrayList<>(); + for(User user : mem){ + content.add(user.getUsername() + ";" + user.getPassword() + ";" + user.getHometown() + + ";" + user.getZip() + ";" + user.getCar_name() + + ";" + user.getCar_l_100km() + ";" + user.getCar_avg_kmh() + + ";" + user.getBike_avg_kmh()); + } + + File file = new File("/user_data.csv"); + + try { + FileWriter fileWriter = new FileWriter(file, false); + BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); + + for (String line : content) { + bufferedWriter.write(line); + bufferedWriter.newLine(); + } + bufferedWriter.close(); + } catch (IOException e) {} + return true; } return false; From 0a1482451fcd7ccbf42cccc8d8709d6eac02b3fe Mon Sep 17 00:00:00 2001 From: Daniel Zdravkovic <3012893@stud.hs-mannheim.de> Date: Mon, 10 Jun 2024 15:19:24 +0200 Subject: [PATCH 08/23] encoding / decoding method fix --- src/main/java/de/hs_mannheim/domain/System.java | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index 2a738c5..055ccf3 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -40,17 +40,17 @@ public class System { this.current_user.setBike_avg_kmh(bike_avg_kmh); } - public void decoding(String string){ + public String decoding(String string){ byte[] binary_data = Base64.decodeBase64(string); - string = new String(binary_data); + return new String(binary_data); } - public void encoding(String string){ + public String encoding(String string){ byte[] binary_data = new byte[string.length()]; for(int i = 0; i < string.length(); i++){ binary_data[i] = (byte) string.charAt(i); } - string = Base64.encodeBase64String(binary_data); + return Base64.encodeBase64String(binary_data); } public HashSet get_all_user() throws FileNotFoundException, IOException{ @@ -65,7 +65,7 @@ public class System { while((path = bufferedReader.readLine()) != null){ fileString = path.split(";"); - encoding(fileString[1]); + fileString[1] = encoding(fileString[1]); all_users.add(new User(fileString[0], fileString[1], fileString[2], Integer.parseInt(fileString[3]), fileString[4], Double.parseDouble(fileString[5]), From 8016e9848e6716863155562907c6c397c7b00535 Mon Sep 17 00:00:00 2001 From: Daniel Zdravkovic <3012893@stud.hs-mannheim.de> Date: Mon, 10 Jun 2024 15:21:54 +0200 Subject: [PATCH 09/23] sign_up method implemented --- src/main/java/de/hs_mannheim/domain/System.java | 1 + 1 file changed, 1 insertion(+) diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index 055ccf3..637972a 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -105,6 +105,7 @@ public class System { ArrayList content = new ArrayList<>(); for(User user : mem){ + user.setPassword(decoding(user.getPassword())); content.add(user.getUsername() + ";" + user.getPassword() + ";" + user.getHometown() + ";" + user.getZip() + ";" + user.getCar_name() + ";" + user.getCar_l_100km() + ";" + user.getCar_avg_kmh() From 75b3b597c16f91de05845057d286d049989919d2 Mon Sep 17 00:00:00 2001 From: Daniel Zdravkovic <3012893@stud.hs-mannheim.de> Date: Mon, 10 Jun 2024 15:58:06 +0200 Subject: [PATCH 10/23] get_all_user method, sign_in_up methods fixed and get_all_user method successfully tested --- .../java/de/hs_mannheim/domain/System.java | 245 +++++++++--------- src/main/resources/user_data.csv | 1 + .../de/hs_mannheim/domain/SystemTest.java | 10 + src/test/resources/user_data.csv | 1 + 4 files changed, 137 insertions(+), 120 deletions(-) create mode 100644 src/main/resources/user_data.csv create mode 100644 src/test/resources/user_data.csv diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index 637972a..a4a02d5 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -24,46 +24,44 @@ public class System { this.api_key = api_key; } - public void set_current_user_zip(String zip){ + public void set_current_user_zip(String zip) { this.current_user.setZip(Integer.parseInt(zip)); } - public void set_current_user_car_l_100km(double car_l_100km){ + public void set_current_user_car_l_100km(double car_l_100km) { this.current_user.setCar_l_100km(car_l_100km); } - public void set_current_user_car_avg_kmh(double car_avg_kmh){ + public void set_current_user_car_avg_kmh(double car_avg_kmh) { this.current_user.setCar_avg_kmh(car_avg_kmh); } - public void set_current_user_bike_avg_kmh(double bike_avg_kmh){ + public void set_current_user_bike_avg_kmh(double bike_avg_kmh) { this.current_user.setBike_avg_kmh(bike_avg_kmh); } - public String decoding(String string){ + public String decoding(String string) { byte[] binary_data = Base64.decodeBase64(string); return new String(binary_data); } - public String encoding(String string){ + public String encoding(String string) { byte[] binary_data = new byte[string.length()]; - for(int i = 0; i < string.length(); i++){ + for (int i = 0; i < string.length(); i++) { binary_data[i] = (byte) string.charAt(i); } return Base64.encodeBase64String(binary_data); } - public HashSet get_all_user() throws FileNotFoundException, IOException{ + public HashSet get_all_user() { HashSet all_users = new HashSet<>(); - File file = new File("/user_data.csv"); String[] fileString = new String[8]; - if(file.exists() && file.isFile()){ - FileReader fileReader = new FileReader(file); - BufferedReader bufferedReader = new BufferedReader(fileReader); - String path; + InputStream inputStream = System.class.getResourceAsStream("/user_data.csv"); + String path; - while((path = bufferedReader.readLine()) != null){ + try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { + while ((path = reader.readLine()) != null) { fileString = path.split(";"); fileString[1] = encoding(fileString[1]); all_users.add(new User(fileString[0], fileString[1], fileString[2], @@ -72,6 +70,7 @@ public class System { Double.parseDouble(fileString[6]), Double.parseDouble(fileString[7]))); } + } catch (Exception e) { } return all_users; } @@ -79,8 +78,8 @@ public class System { public boolean sign_in_user(String username, String password) throws IOException { ArrayList mem = new ArrayList<>(get_all_user()); - for(User user : mem){ - if(user.getUsername().equals(username) && user.getPassword().equals(password)){ + for (User user : mem) { + if (user.getUsername().equals(username) && decoding(user.getPassword()).equals(password)) { 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()); @@ -90,21 +89,21 @@ public class System { return false; } - public boolean sign_up_user(String username, String password, String hometown, int zip, + public boolean sign_up_user(String username, String password, String hometown, int zip, String car_name, double car_l_100km, double car_avg_kmh, double bike_avg_kmh) throws IOException { ArrayList mem = new ArrayList<>(get_all_user()); ArrayList user_names = new ArrayList<>(); - for(User user : mem){ + for (User user : mem) { user_names.add(user.getUsername()); } - if(!user_names.contains(username)){ - current_user = new User(username, password, - hometown, zip, car_name, - car_l_100km, car_avg_kmh, bike_avg_kmh); + if (!user_names.contains(username)) { + current_user = new User(username, password, + hometown, zip, car_name, + car_l_100km, car_avg_kmh, bike_avg_kmh); ArrayList content = new ArrayList<>(); - for(User user : mem){ + for (User user : mem) { user.setPassword(decoding(user.getPassword())); content.add(user.getUsername() + ";" + user.getPassword() + ";" + user.getHometown() + ";" + user.getZip() + ";" + user.getCar_name() @@ -123,82 +122,84 @@ public class System { bufferedWriter.newLine(); } bufferedWriter.close(); - } catch (IOException e) {} + } catch (IOException e) { + } - return true; + return true; } return false; } - public void sign_out_user(){ + public void sign_out_user() { this.distances = new ArrayList<>(); current_user = new User(); } - public boolean change_user_details(String username, String password, String hometown, int zip, - String car_name, double car_l_100km, double car_avg_kmh, double bike_avg_kmh){ + public boolean change_user_details(String username, String password, String hometown, int zip, + String car_name, double car_l_100km, double car_avg_kmh, double bike_avg_kmh) { return true; } - public ArrayList search(String hometown_or_zip){ - + public ArrayList search(String hometown_or_zip) { + TreeSet zip_set = new TreeSet<>(); InputStream inputStream = System.class.getResourceAsStream("/zip.csv"); 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)){ + while ((line = reader.readLine()) != null && zip_set.size() < 200) { + if (line.contains(hometown_or_zip)) { line = line.replace("\"", ""); zip_set.add(line); } } - } catch (Exception e) {} - + } catch (Exception e) { + } + return new ArrayList<>(zip_set); } - public ArrayList random_destinations_car(){ - + public ArrayList random_destinations_car() { + calc_all_distances(); ArrayList mem = new ArrayList<>(); ArrayList result = new ArrayList<>(); - for(int i = 0; i150) + 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(int i = 0; i<3; i++) - result.add(mem.get((int) (Math.random()*mem.size()))); + + for (int i = 0; i < 3; i++) + result.add(mem.get((int) (Math.random() * mem.size()))); return result; } - public ArrayList random_destinations_bike(){ - + public ArrayList random_destinations_bike() { + calc_all_distances(); ArrayList mem = new ArrayList<>(); ArrayList result = new ArrayList<>(); - for(int i = 0; i get_response = http_client.send(get_request, BodyHandlers.ofString()); + try { + HttpClient http_client = HttpClient.newHttpClient(); - JSONObject json = new JSONObject(((String)get_response.body()).substring(0,((String) get_response.body()).length())); - weather = json.getJSONArray("weather").getJSONObject(0).getString("description"); - temperature = json.getJSONObject("main").getDouble("temp"); - } catch (Exception e){} - - if(weather.equals("")) + HttpRequest get_request = HttpRequest.newBuilder() + .uri(new URI("https://api.openweathermap.org/data/2.5/weather?zip=" + current_user.getZip() + ",de&appid=" + api_key + "&units=metric&lang=de")) + .GET() + .build(); + + HttpResponse get_response = http_client.send(get_request, BodyHandlers.ofString()); + + JSONObject json = new JSONObject(((String) get_response.body()).substring(0, ((String) get_response.body()).length())); + weather = json.getJSONArray("weather").getJSONObject(0).getString("description"); + temperature = json.getJSONObject("main").getDouble("temp"); + } catch (Exception e) { + } + + if (weather.equals("")) return "Es ist ein Fehler aufgetreten!"; else return weather + ": " + temperature + " °C"; } - public String weather_forecast(String destination_zip){ - + public String weather_forecast(String destination_zip) { + String weather_day_1 = ""; String weather_day_2 = ""; String weather_day_3 = ""; @@ -269,44 +271,45 @@ public class System { double temperature_day_3_low = 1; - try{ + try { HttpClient http_client = HttpClient.newHttpClient(); - + HttpRequest get_request = HttpRequest.newBuilder() - .uri(new URI("https://api.openweathermap.org/data/2.5/forecast?zip="+destination_zip+",de&appid="+api_key+"&units=metric&lang=de")) - .GET() - .build(); - + .uri(new URI("https://api.openweathermap.org/data/2.5/forecast?zip=" + destination_zip + ",de&appid=" + api_key + "&units=metric&lang=de")) + .GET() + .build(); + HttpResponse get_response = http_client.send(get_request, BodyHandlers.ofString()); - JSONObject json = new JSONObject(((String)get_response.body()).substring(0,((String) get_response.body()).length())); + JSONObject json = new JSONObject(((String) get_response.body()).substring(0, ((String) get_response.body()).length())); weather_day_1 = json.getJSONArray("list").getJSONObject(11).getJSONArray("weather").getJSONObject(0).getString("description"); weather_day_2 = json.getJSONArray("list").getJSONObject(19).getJSONArray("weather").getJSONObject(0).getString("description"); weather_day_3 = json.getJSONArray("list").getJSONObject(27).getJSONArray("weather").getJSONObject(0).getString("description"); - + temperature_day_1_1 = json.getJSONArray("list").getJSONObject(8).getJSONObject("main").getDouble("temp"); temperature_day_1_2 = json.getJSONArray("list").getJSONObject(10).getJSONObject("main").getDouble("temp"); temperature_day_1_3 = json.getJSONArray("list").getJSONObject(12).getJSONObject("main").getDouble("temp"); temperature_day_1_4 = json.getJSONArray("list").getJSONObject(14).getJSONObject("main").getDouble("temp"); - + temperature_day_2_1 = json.getJSONArray("list").getJSONObject(16).getJSONObject("main").getDouble("temp"); temperature_day_2_2 = json.getJSONArray("list").getJSONObject(18).getJSONObject("main").getDouble("temp"); temperature_day_2_3 = json.getJSONArray("list").getJSONObject(20).getJSONObject("main").getDouble("temp"); temperature_day_2_4 = json.getJSONArray("list").getJSONObject(22).getJSONObject("main").getDouble("temp"); - + temperature_day_3_1 = json.getJSONArray("list").getJSONObject(24).getJSONObject("main").getDouble("temp"); temperature_day_3_2 = json.getJSONArray("list").getJSONObject(26).getJSONObject("main").getDouble("temp"); temperature_day_3_3 = json.getJSONArray("list").getJSONObject(28).getJSONObject("main").getDouble("temp"); temperature_day_3_4 = json.getJSONArray("list").getJSONObject(30).getJSONObject("main").getDouble("temp"); - - } catch (Exception e){} - + + } catch (Exception e) { + } + temperature_day_1.add(temperature_day_1_1); temperature_day_1.add(temperature_day_1_2); temperature_day_1.add(temperature_day_1_3); temperature_day_1.add(temperature_day_1_4); - temperature_day_1_high = (double) temperature_day_1.toArray()[temperature_day_1.size()-1]; + temperature_day_1_high = (double) temperature_day_1.toArray()[temperature_day_1.size() - 1]; temperature_day_1_low = (double) temperature_day_1.toArray()[0]; temperature_day_2.add(temperature_day_2_1); @@ -314,7 +317,7 @@ public class System { temperature_day_2.add(temperature_day_2_3); temperature_day_2.add(temperature_day_2_4); - temperature_day_2_high = (double) temperature_day_2.toArray()[temperature_day_1.size()-1]; + temperature_day_2_high = (double) temperature_day_2.toArray()[temperature_day_1.size() - 1]; temperature_day_2_low = (double) temperature_day_2.toArray()[0]; temperature_day_3.add(temperature_day_3_1); @@ -322,19 +325,19 @@ public class System { temperature_day_3.add(temperature_day_3_3); temperature_day_3.add(temperature_day_3_4); - temperature_day_3_high = (double) temperature_day_3.toArray()[temperature_day_1.size()-1]; + temperature_day_3_high = (double) temperature_day_3.toArray()[temperature_day_1.size() - 1]; temperature_day_3_low = (double) temperature_day_3.toArray()[0]; - if(weather_day_1.equals("")||weather_day_2.equals("")||weather_day_3.equals("")) + if (weather_day_1.equals("") || weather_day_2.equals("") || weather_day_3.equals("")) return "Es ist ein Fehler aufgetreten!"; else return "Morgen: " + weather_day_1 + ": Minimum: " + temperature_day_1_low + " °C" + "; Maximum: " + temperature_day_1_high + " °C\n" - + "Ü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"; + + "Ü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 String distance(String destination_zip) { + double lon1 = -1; double lon2 = -1; double lat1 = -1; @@ -346,36 +349,37 @@ public class System { String line; while ((line = reader.readLine()) != null) { line = line.replace("\"", ""); - - if(line.split(";")[0].equals(destination_zip)){ + + 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]); } } - } catch (Exception e) {} + } catch (Exception e) { + } - if(lon1==-1||lon2==-1||lat1==-1||lat2==-1) + if (lon1 == -1 || lon2 == -1 || lat1 == -1 || lat2 == -1) return "Es ist ein Fehler aufgetreten!"; - double dLat = lat2-lat1; - double dLon = lon2-lon1; + 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 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)); + 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"; - return "" + (Math.round((distance * 1.25)*1000)/1000.0) + " km"; - } - public void calc_all_distances(){ + public void calc_all_distances() { - if(!this.distances.isEmpty()) + if (!this.distances.isEmpty()) return; InputStream inputStream = System.class.getResourceAsStream("/zip.csv"); @@ -383,38 +387,39 @@ public class System { 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", ""))); - + } - } catch (Exception e) {} + } catch (Exception e) { + } } - - public String[] travel_time(String destination_zip){ - + + public String[] travel_time(String destination_zip) { + String[] result = new String[2]; - - if(distance(destination_zip).equals("Es ist ein Fehler aufgetreten!")){ + + 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"; - + 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"; + return result; } - public String calc_l_consumption(String destination_zip){ - - if(distance(destination_zip).equals("Es ist ein Fehler aufgetreten!")) + 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"; - + 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/resources/user_data.csv b/src/main/resources/user_data.csv new file mode 100644 index 0000000..f55d378 --- /dev/null +++ b/src/main/resources/user_data.csv @@ -0,0 +1 @@ +Daniel;1401Daniel;Mannheim;68305;BMW;1.5;50.4;40.2 \ 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 bf443c5..8cba9f7 100644 --- a/src/test/java/de/hs_mannheim/domain/SystemTest.java +++ b/src/test/java/de/hs_mannheim/domain/SystemTest.java @@ -3,6 +3,7 @@ package de.hs_mannheim.domain; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; +import java.io.IOException; import java.util.ArrayList; import org.junit.jupiter.api.Test; @@ -86,4 +87,13 @@ public class SystemTest { assertEquals(true, Double.parseDouble(current_system.distance(current_system.random_destinations_bike().get(0).split(";")[0]).replace(" km", "")) < 100); } + @Test + public void get_all_user() { + assertEquals(1, current_system.get_all_user().size()); + + ArrayList list = new ArrayList<>(current_system.get_all_user()); + assertEquals(true, list.get(0).getPassword().equals("MTQwMURhbmllbA==")); + //Passwort unkodiert: 1401Daniel + } + } diff --git a/src/test/resources/user_data.csv b/src/test/resources/user_data.csv new file mode 100644 index 0000000..f55d378 --- /dev/null +++ b/src/test/resources/user_data.csv @@ -0,0 +1 @@ +Daniel;1401Daniel;Mannheim;68305;BMW;1.5;50.4;40.2 \ No newline at end of file From 33ff251c20e2c577f0aa22b986dd24f058b99f26 Mon Sep 17 00:00:00 2001 From: Daniel Zdravkovic <3012893@stud.hs-mannheim.de> Date: Tue, 11 Jun 2024 10:50:46 +0200 Subject: [PATCH 11/23] decoding encoding methods fixed and tests successful/ sign_up some other minor needs to be worked over --- .../java/de/hs_mannheim/domain/System.java | 129 +++++++++++++++--- .../de/hs_mannheim/domain/SystemTest.java | 23 +++- 2 files changed, 133 insertions(+), 19 deletions(-) diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index a4a02d5..69c2b4e 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -6,6 +6,10 @@ import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.net.http.HttpResponse.BodyHandlers; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; import java.util.ArrayList; import java.util.Arrays; import java.util.HashSet; @@ -41,8 +45,11 @@ public class System { } public String decoding(String string) { - byte[] binary_data = Base64.decodeBase64(string); - return new String(binary_data); + byte[] binary_data = new byte[string.length()]; + for(int i = 0; i < string.length(); i++) { + binary_data[i] = (byte)string.charAt(i); + } + return new String(Base64.decodeBase64(binary_data)); } public String encoding(String string) { @@ -53,8 +60,8 @@ public class System { return Base64.encodeBase64String(binary_data); } - public HashSet get_all_user() { - HashSet all_users = new HashSet<>(); + public ArrayList get_all_user() { + ArrayList all_users = new ArrayList<>(); String[] fileString = new String[8]; InputStream inputStream = System.class.getResourceAsStream("/user_data.csv"); @@ -89,9 +96,14 @@ public class System { return false; } - public boolean sign_up_user(String username, String password, String hometown, int zip, - String car_name, double car_l_100km, double car_avg_kmh, double bike_avg_kmh) throws IOException { - ArrayList mem = new ArrayList<>(get_all_user()); + /*public boolean sign_up_user(String username, String password, String hometown, String zipS, + String car_name, String car_l_100kmS, String car_avg_kmhS, String bike_avg_kmhS) throws IOException { + int zip = Integer.parseInt(zipS); + double car_l_100km = Double.parseDouble(car_l_100kmS); + double car_avg_kmh = Double.parseDouble(car_avg_kmhS); + double bike_avg_kmh = Double.parseDouble(bike_avg_kmhS); + + ArrayList mem = get_all_user(); ArrayList user_names = new ArrayList<>(); for (User user : mem) { @@ -101,6 +113,8 @@ public class System { current_user = new User(username, password, hometown, zip, car_name, car_l_100km, car_avg_kmh, bike_avg_kmh); + mem.add(current_user); + user_names.add(current_user.getUsername()); ArrayList content = new ArrayList<>(); for (User user : mem) { @@ -111,34 +125,115 @@ public class System { + ";" + user.getBike_avg_kmh()); } - File file = new File("/user_data.csv"); + Path resourcePath = Paths.get("src/main/resources/user_data.csv"); + Path testResourcePath = Paths.get("src/test/resources/user_data.csv"); + try { - FileWriter fileWriter = new FileWriter(file, false); - BufferedWriter bufferedWriter = new BufferedWriter(fileWriter); + Files.createDirectories(resourcePath.getParent()); + } catch (IOException e) { + e.printStackTrace(); + } + try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(resourcePath.toFile(),false), StandardCharsets.UTF_8)){ - for (String line : content) { - bufferedWriter.write(line); - bufferedWriter.newLine(); + for (int i = 0; i < content.size()-1; i++) { + writer.write(content.get(i)); + writer.write("\n"); } - bufferedWriter.close(); + writer.write(content.getLast()); + } catch (IOException e) { } + try { + Files.createDirectories(testResourcePath.getParent()); + } catch (IOException e) { + e.printStackTrace(); + } + try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(testResourcePath.toFile()), StandardCharsets.UTF_8)){ + + for (int i = 0; i < content.size()-1; i++) { + writer.write(content.get(i)); + writer.write("\n"); + } + writer.write(content.getLast()); + + } catch (IOException e) { + } + + return true; } return false; } + */ + public void sign_out_user() { this.distances = new ArrayList<>(); current_user = new User(); } - public boolean change_user_details(String username, String password, String hometown, int zip, - String car_name, double car_l_100km, double car_avg_kmh, double bike_avg_kmh) { - return true; + /*public void change_user_details(String username, String password, String hometown, String zipS, + String car_name, String car_l_100kmS, String car_avg_kmhS, String bike_avg_kmhS) throws IOException { + + int zip = Integer.parseInt(zipS); + double car_l_100km = Double.parseDouble(car_l_100kmS); + double car_avg_kmh = Double.parseDouble(car_avg_kmhS); + double bike_avg_kmh = Double.parseDouble(bike_avg_kmhS); + + String new_user_details = username + ";" + password + ";" + hometown + ";" + zipS + + ";" + car_name + ";" + car_l_100kmS + ";" + car_avg_kmhS + ";" + bike_avg_kmhS; + + File mainFile = new File("src/main/resources/user_data.csv"); + File testFile = new File("src/test/resources/user_data.csv"); + + BufferedWriter writer = new BufferedWriter(new FileWriter(mainFile)); + BufferedWriter writerTest = new BufferedWriter(new FileWriter(testFile)); + + InputStream inputStream = System.class.getResourceAsStream("/user_data.csv"); + + try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { + String line; + while ((line = reader.readLine()) != null) { + String[] row = line.split(";"); + + if (row[0].equals(current_user.getUsername())) { + writer.write(new_user_details); + writerTest.write(new_user_details); + } else { + writer.write(line); + writerTest.write(line); + } + if(reader.readLine() != null){ + writer.write("\n"); + writer.write("\n"); + } + writer.write("\n"); + } + + } catch (Exception e) {} + + writer.close(); + + current_user.setUsername(username); + current_user.setPassword(password); + current_user.setHometown(hometown); + current_user.setZip(zip); + current_user.setCar_name(car_name); + current_user.setCar_l_100km(car_l_100km); + current_user.setCar_avg_kmh(car_avg_kmh); + current_user.setBike_avg_kmh(bike_avg_kmh); + } + + */ + + public String[] getDetails(){ + return new String[]{current_user.getUsername(), current_user.getPassword(), + current_user.getHometown(), String.valueOf(current_user.getZip()), + current_user.getCar_name(), String.valueOf(current_user.getCar_l_100km()), + String.valueOf(current_user.getCar_avg_kmh()), String.valueOf(current_user.getCar_avg_kmh())}; } public ArrayList search(String hometown_or_zip) { diff --git a/src/test/java/de/hs_mannheim/domain/SystemTest.java b/src/test/java/de/hs_mannheim/domain/SystemTest.java index 8cba9f7..05fbcdf 100644 --- a/src/test/java/de/hs_mannheim/domain/SystemTest.java +++ b/src/test/java/de/hs_mannheim/domain/SystemTest.java @@ -73,7 +73,7 @@ public class SystemTest { assertEquals("60.361 l", current_system.calc_l_consumption("10115")); // Kraftstoffverbrauch nach Berlin } - @Test + /*@Test public void random_destinations(){ current_system.set_current_user_zip("68161"); @@ -85,10 +85,29 @@ public class SystemTest { assertEquals(true, Double.parseDouble(current_system.distance(current_system.random_destinations_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); + }*/ + + @Test + public void encoding(){ + + String test_password = "123Esel"; + + assertEquals("MTIzRXNlbA==",current_system.encoding(test_password)); + + } + + @Test + public void decoding(){ + + String test_password = "MTIzRXNlbA=="; + + assertEquals("123Esel",current_system.decoding(test_password)); + } @Test public void get_all_user() { + assertEquals(1, current_system.get_all_user().size()); ArrayList list = new ArrayList<>(current_system.get_all_user()); @@ -96,4 +115,4 @@ public class SystemTest { //Passwort unkodiert: 1401Daniel } -} +} \ No newline at end of file From 5d087518e967dc3b6193e59a17f1440281daf26e Mon Sep 17 00:00:00 2001 From: Daniel Zdravkovic <3012893@stud.hs-mannheim.de> Date: Tue, 11 Jun 2024 10:57:28 +0200 Subject: [PATCH 12/23] get_all_user tests successful --- src/main/java/de/hs_mannheim/domain/System.java | 1 + src/main/resources/user_data.csv | 3 ++- src/test/java/de/hs_mannheim/domain/SystemTest.java | 8 +++----- src/test/resources/user_data.csv | 3 ++- 4 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index 69c2b4e..2a817b6 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -96,6 +96,7 @@ public class System { return false; } + /*public boolean sign_up_user(String username, String password, String hometown, String zipS, String car_name, String car_l_100kmS, String car_avg_kmhS, String bike_avg_kmhS) throws IOException { int zip = Integer.parseInt(zipS); diff --git a/src/main/resources/user_data.csv b/src/main/resources/user_data.csv index f55d378..b4eeae8 100644 --- a/src/main/resources/user_data.csv +++ b/src/main/resources/user_data.csv @@ -1 +1,2 @@ -Daniel;1401Daniel;Mannheim;68305;BMW;1.5;50.4;40.2 \ No newline at end of file +Daniel;1401Daniel;Mannheim;68305;BMW;1.5;50.4;40.2 +David;123Esel;Mannheim;68305;BMW;1.5;50.4;40.2 \ 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 05fbcdf..992760b 100644 --- a/src/test/java/de/hs_mannheim/domain/SystemTest.java +++ b/src/test/java/de/hs_mannheim/domain/SystemTest.java @@ -108,11 +108,9 @@ public class SystemTest { @Test public void get_all_user() { - assertEquals(1, current_system.get_all_user().size()); - - ArrayList list = new ArrayList<>(current_system.get_all_user()); - assertEquals(true, list.get(0).getPassword().equals("MTQwMURhbmllbA==")); - //Passwort unkodiert: 1401Daniel + assertEquals(2, current_system.get_all_user().size()); + assertEquals(true, current_system.get_all_user().get(0).getPassword().equals("MTQwMURhbmllbA==")); + assertEquals(true, current_system.get_all_user().get(1).getPassword().equals("MTIzRXNlbA==")); } } \ No newline at end of file diff --git a/src/test/resources/user_data.csv b/src/test/resources/user_data.csv index f55d378..b4eeae8 100644 --- a/src/test/resources/user_data.csv +++ b/src/test/resources/user_data.csv @@ -1 +1,2 @@ -Daniel;1401Daniel;Mannheim;68305;BMW;1.5;50.4;40.2 \ No newline at end of file +Daniel;1401Daniel;Mannheim;68305;BMW;1.5;50.4;40.2 +David;123Esel;Mannheim;68305;BMW;1.5;50.4;40.2 \ No newline at end of file From 5141d8ec0393426ca7b1edce9771a25a1874a9e0 Mon Sep 17 00:00:00 2001 From: Daniel Zdravkovic <3012893@stud.hs-mannheim.de> Date: Tue, 11 Jun 2024 11:00:28 +0200 Subject: [PATCH 13/23] sign_in_user method minor change --- src/main/java/de/hs_mannheim/domain/System.java | 4 ++-- 1 file changed, 2 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 2a817b6..ab7c799 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -82,8 +82,8 @@ public class System { return all_users; } - public boolean sign_in_user(String username, String password) throws IOException { - ArrayList mem = new ArrayList<>(get_all_user()); + public boolean sign_in_user(String username, String password) { + ArrayList mem = get_all_user(); for (User user : mem) { if (user.getUsername().equals(username) && decoding(user.getPassword()).equals(password)) { From 6252c3d686204f0ea30fda067d29b20c0cc1575c Mon Sep 17 00:00:00 2001 From: Daniel Zdravkovic <3012893@stud.hs-mannheim.de> Date: Tue, 11 Jun 2024 11:05:48 +0200 Subject: [PATCH 14/23] sign_in_user method another minor decoding fix and tests successful --- src/test/java/de/hs_mannheim/domain/SystemTest.java | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/test/java/de/hs_mannheim/domain/SystemTest.java b/src/test/java/de/hs_mannheim/domain/SystemTest.java index 992760b..adfac1d 100644 --- a/src/test/java/de/hs_mannheim/domain/SystemTest.java +++ b/src/test/java/de/hs_mannheim/domain/SystemTest.java @@ -113,4 +113,12 @@ public class SystemTest { assertEquals(true, current_system.get_all_user().get(1).getPassword().equals("MTIzRXNlbA==")); } + @Test + public void sign_in_user() { + + assertEquals(true, current_system.sign_in_user("Daniel", "1401Daniel")); + assertEquals("Daniel",current_system.getDetails()[0]); + + } + } \ No newline at end of file From 8bf5c4a0b873367fe77a1dcc2aab10f632a44ba3 Mon Sep 17 00:00:00 2001 From: Daniel Zdravkovic <3012893@stud.hs-mannheim.de> Date: Tue, 11 Jun 2024 11:09:29 +0200 Subject: [PATCH 15/23] sign_out_method tests successful --- src/test/java/de/hs_mannheim/domain/SystemTest.java | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/src/test/java/de/hs_mannheim/domain/SystemTest.java b/src/test/java/de/hs_mannheim/domain/SystemTest.java index adfac1d..8fac097 100644 --- a/src/test/java/de/hs_mannheim/domain/SystemTest.java +++ b/src/test/java/de/hs_mannheim/domain/SystemTest.java @@ -121,4 +121,13 @@ public class SystemTest { } + @Test + public void sign_out_user() { + + current_system.sign_in_user("Daniel", "1401Daniel"); + current_system.sign_out_user(); + assertEquals("",current_system.getDetails()[0]); + + } + } \ No newline at end of file From 675a1bd35e3a682a89e44a0176975be65eaea514 Mon Sep 17 00:00:00 2001 From: Daniel Zdravkovic <3012893@stud.hs-mannheim.de> Date: Tue, 11 Jun 2024 11:11:59 +0200 Subject: [PATCH 16/23] more sign_out tests --- src/test/java/de/hs_mannheim/domain/SystemTest.java | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/test/java/de/hs_mannheim/domain/SystemTest.java b/src/test/java/de/hs_mannheim/domain/SystemTest.java index 8fac097..a804e42 100644 --- a/src/test/java/de/hs_mannheim/domain/SystemTest.java +++ b/src/test/java/de/hs_mannheim/domain/SystemTest.java @@ -124,10 +124,15 @@ public class SystemTest { @Test public void sign_out_user() { - current_system.sign_in_user("Daniel", "1401Daniel"); + assertEquals(true, current_system.sign_in_user("David", "123Esel")); + assertEquals("David",current_system.getDetails()[0]); current_system.sign_out_user(); assertEquals("",current_system.getDetails()[0]); } + + + + } \ No newline at end of file From cd86769cd0aa54211eb95377007cac5b60bab84e Mon Sep 17 00:00:00 2001 From: Daniel Zdravkovic <3012893@stud.hs-mannheim.de> Date: Tue, 11 Jun 2024 11:34:42 +0200 Subject: [PATCH 17/23] first sign_up_method logic implemented and tested successful --- .../java/de/hs_mannheim/domain/System.java | 32 +++++++++++++++++++ .../de/hs_mannheim/domain/SystemTest.java | 11 +++++++ 2 files changed, 43 insertions(+) diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index ab7c799..fc3c47c 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -96,6 +96,38 @@ public class System { return false; } + public boolean sign_up_user(String username, String password, String hometown, String zipS, + String car_name, String car_l_100kmS, String car_avg_kmhS, String bike_avg_kmhS){ + + int zip = Integer.parseInt(zipS); + double car_l_100km = Double.parseDouble(car_l_100kmS); + double car_avg_kmh = Double.parseDouble(car_avg_kmhS); + double bike_avg_kmh = Double.parseDouble(bike_avg_kmhS); + + ArrayList all_user = get_all_user(); + + for(User user: all_user) + if(user.getUsername().equals(username)) + return false; + + ArrayList mem = search(zipS); + boolean bool = false; + + for (String line: mem) + if(line.split(";")[1].equals(hometown)) { + bool = true; + break; + } + + if(!bool) + return false; + + current_user = new User(username, password, hometown, zip, car_name, car_l_100km, car_avg_kmh, bike_avg_kmh); + + + + return true; + } /*public boolean sign_up_user(String username, String password, String hometown, String zipS, String car_name, String car_l_100kmS, String car_avg_kmhS, String bike_avg_kmhS) throws IOException { diff --git a/src/test/java/de/hs_mannheim/domain/SystemTest.java b/src/test/java/de/hs_mannheim/domain/SystemTest.java index a804e42..4d9100f 100644 --- a/src/test/java/de/hs_mannheim/domain/SystemTest.java +++ b/src/test/java/de/hs_mannheim/domain/SystemTest.java @@ -131,6 +131,17 @@ public class SystemTest { } + @Test + public void sign_up_user(){ + // Username darf nicht doppelt vorkommen! + assertEquals(false, current_system.sign_up_user("David","123Esel","Mannheim","68161","AMG","10","300","20")); + assertEquals(true, current_system.sign_up_user("Daavid","123Esel","Mannheim","68161","AMG","10","300","20")); + // PLZ muss mit Stadt übereinstimmen + assertEquals(false, current_system.sign_up_user("Lukas","123Esel","Mannheim","11105","AMG","10","300","20")); + assertEquals(true, current_system.sign_up_user("Lukas","123Esel","Mannheim","68305","AMG","10","300","20")); + + } + From 735c1f38ea87850e4422eb57e329683b2b6bcc56 Mon Sep 17 00:00:00 2001 From: Daniel Zdravkovic <3012893@stud.hs-mannheim.de> Date: Tue, 11 Jun 2024 11:37:06 +0200 Subject: [PATCH 18/23] more tests for sign_up successful --- src/test/java/de/hs_mannheim/domain/SystemTest.java | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/test/java/de/hs_mannheim/domain/SystemTest.java b/src/test/java/de/hs_mannheim/domain/SystemTest.java index 4d9100f..c3adc94 100644 --- a/src/test/java/de/hs_mannheim/domain/SystemTest.java +++ b/src/test/java/de/hs_mannheim/domain/SystemTest.java @@ -140,6 +140,9 @@ public class SystemTest { assertEquals(false, current_system.sign_up_user("Lukas","123Esel","Mannheim","11105","AMG","10","300","20")); assertEquals(true, current_system.sign_up_user("Lukas","123Esel","Mannheim","68305","AMG","10","300","20")); + assertEquals("Lukas",current_system.getDetails()[0]); + current_system.sign_out_user(); + assertEquals("",current_system.getDetails()[0]); } From bc8c19bc66c63e697a318fc3403d0ef284b07fe6 Mon Sep 17 00:00:00 2001 From: Daniel Zdravkovic <3012893@stud.hs-mannheim.de> Date: Tue, 11 Jun 2024 11:54:07 +0200 Subject: [PATCH 19/23] all_user_toString method and write_to_file method implemented to help with sign_up_user --- .../java/de/hs_mannheim/domain/System.java | 26 +++++++++++++++++++ 1 file changed, 26 insertions(+) diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index fc3c47c..3b18178 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -79,9 +79,25 @@ public class System { } } catch (Exception e) { } + + all_users.add(current_user); + return all_users; } + public ArrayList all_user_toString(){ + + ArrayList result = new ArrayList<>(); + ArrayList all_user = get_all_user(); + + for(User user : all_user){ + result.add(getDetails()[0] + ";" + getDetails()[1] + ";" + getDetails()[2] + ";" + getDetails()[3] + + ";" + getDetails()[4] + ";" + getDetails()[5] + ";" + getDetails()[6] + ";" + getDetails()[7]); + } + + return result; + } + public boolean sign_in_user(String username, String password) { ArrayList mem = get_all_user(); @@ -129,6 +145,16 @@ public class System { return true; } + public void write_to_file(ArrayList lines, String file) { + try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) { + for (int i = 0; i < lines.size() - 1; i++) { + writer.write(lines.get(i)); + writer.newLine(); + } + writer.write(lines.getLast()); + } catch (IOException e) {} + } + /*public boolean sign_up_user(String username, String password, String hometown, String zipS, String car_name, String car_l_100kmS, String car_avg_kmhS, String bike_avg_kmhS) throws IOException { int zip = Integer.parseInt(zipS); From f669e9166838884f5e746d76391ed60b8cb5d406 Mon Sep 17 00:00:00 2001 From: Selim Eser <2211482@stud.hs-mannheim.de> Date: Tue, 11 Jun 2024 14:32:24 +0200 Subject: [PATCH 20/23] sign_out_method fixed all tests successful --- .../java/de/hs_mannheim/domain/System.java | 170 ++---------------- src/main/java/de/hs_mannheim/domain/User.java | 5 + src/main/resources/user_data.csv | 4 +- .../de/hs_mannheim/domain/SystemTest.java | 20 +-- src/test/resources/user_data.csv | 4 +- 5 files changed, 31 insertions(+), 172 deletions(-) diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index 3b18178..7805f5c 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -6,13 +6,7 @@ import java.net.http.HttpClient; import java.net.http.HttpRequest; import java.net.http.HttpResponse; import java.net.http.HttpResponse.BodyHandlers; -import java.nio.charset.StandardCharsets; -import java.nio.file.Files; -import java.nio.file.Path; -import java.nio.file.Paths; import java.util.ArrayList; -import java.util.Arrays; -import java.util.HashSet; import java.util.TreeSet; import org.apache.commons.codec.binary.Base64; @@ -21,11 +15,13 @@ import org.json.JSONObject; public class System { private User current_user = new User(); + private ArrayList all_user = new ArrayList<>(); private String api_key; private ArrayList distances = new ArrayList<>(); public System(String api_key) { this.api_key = api_key; + get_all_user(); } public void set_current_user_zip(String zip) { @@ -44,7 +40,7 @@ public class System { this.current_user.setBike_avg_kmh(bike_avg_kmh); } - public String decoding(String string) { + public static String decoding(String string) { byte[] binary_data = new byte[string.length()]; for(int i = 0; i < string.length(); i++) { binary_data[i] = (byte)string.charAt(i); @@ -52,7 +48,7 @@ public class System { return new String(Base64.decodeBase64(binary_data)); } - public String encoding(String string) { + public static String encoding(String string) { byte[] binary_data = new byte[string.length()]; for (int i = 0; i < string.length(); i++) { binary_data[i] = (byte) string.charAt(i); @@ -60,8 +56,7 @@ public class System { return Base64.encodeBase64String(binary_data); } - public ArrayList get_all_user() { - ArrayList all_users = new ArrayList<>(); + public void get_all_user() { String[] fileString = new String[8]; InputStream inputStream = System.class.getResourceAsStream("/user_data.csv"); @@ -70,8 +65,8 @@ public class System { try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { while ((path = reader.readLine()) != null) { fileString = path.split(";"); - fileString[1] = encoding(fileString[1]); - all_users.add(new User(fileString[0], fileString[1], fileString[2], + fileString[1] = decoding(fileString[1]); + this.all_user.add(new User(fileString[0], fileString[1], fileString[2], Integer.parseInt(fileString[3]), fileString[4], Double.parseDouble(fileString[5]), Double.parseDouble(fileString[6]), @@ -79,30 +74,23 @@ public class System { } } catch (Exception e) { } - - all_users.add(current_user); - - return all_users; } public ArrayList all_user_toString(){ ArrayList result = new ArrayList<>(); - ArrayList all_user = get_all_user(); - for(User user : all_user){ - result.add(getDetails()[0] + ";" + getDetails()[1] + ";" + getDetails()[2] + ";" + getDetails()[3] - + ";" + getDetails()[4] + ";" + getDetails()[5] + ";" + getDetails()[6] + ";" + getDetails()[7]); + for(User user : this.all_user){ + result.add(user.toString()); } return result; } - + public boolean sign_in_user(String username, String password) { - ArrayList mem = get_all_user(); - for (User user : mem) { - if (user.getUsername().equals(username) && decoding(user.getPassword()).equals(password)) { + for (User user : this.all_user) { + if (user.getUsername().equals(username) && user.getPassword().equals(password)) { 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()); @@ -120,8 +108,6 @@ public class System { double car_avg_kmh = Double.parseDouble(car_avg_kmhS); double bike_avg_kmh = Double.parseDouble(bike_avg_kmhS); - ArrayList all_user = get_all_user(); - for(User user: all_user) if(user.getUsername().equals(username)) return false; @@ -139,8 +125,11 @@ public class System { return false; current_user = new User(username, password, hometown, zip, car_name, car_l_100km, car_avg_kmh, bike_avg_kmh); + + this.all_user.add(current_user); - + write_to_file(all_user_toString(), "src/main/resources/user_data.csv"); + write_to_file(all_user_toString(), "src/test/resources/user_data.csv"); return true; } @@ -155,139 +144,12 @@ public class System { } catch (IOException e) {} } - /*public boolean sign_up_user(String username, String password, String hometown, String zipS, - String car_name, String car_l_100kmS, String car_avg_kmhS, String bike_avg_kmhS) throws IOException { - int zip = Integer.parseInt(zipS); - double car_l_100km = Double.parseDouble(car_l_100kmS); - double car_avg_kmh = Double.parseDouble(car_avg_kmhS); - double bike_avg_kmh = Double.parseDouble(bike_avg_kmhS); - - ArrayList mem = get_all_user(); - ArrayList user_names = new ArrayList<>(); - - for (User user : mem) { - user_names.add(user.getUsername()); - } - if (!user_names.contains(username)) { - current_user = new User(username, password, - hometown, zip, car_name, - car_l_100km, car_avg_kmh, bike_avg_kmh); - mem.add(current_user); - user_names.add(current_user.getUsername()); - - ArrayList content = new ArrayList<>(); - for (User user : mem) { - user.setPassword(decoding(user.getPassword())); - content.add(user.getUsername() + ";" + user.getPassword() + ";" + user.getHometown() - + ";" + user.getZip() + ";" + user.getCar_name() - + ";" + user.getCar_l_100km() + ";" + user.getCar_avg_kmh() - + ";" + user.getBike_avg_kmh()); - } - - Path resourcePath = Paths.get("src/main/resources/user_data.csv"); - Path testResourcePath = Paths.get("src/test/resources/user_data.csv"); - - - try { - Files.createDirectories(resourcePath.getParent()); - } catch (IOException e) { - e.printStackTrace(); - } - try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(resourcePath.toFile(),false), StandardCharsets.UTF_8)){ - - for (int i = 0; i < content.size()-1; i++) { - writer.write(content.get(i)); - writer.write("\n"); - } - writer.write(content.getLast()); - - } catch (IOException e) { - } - - try { - Files.createDirectories(testResourcePath.getParent()); - } catch (IOException e) { - e.printStackTrace(); - } - try (OutputStreamWriter writer = new OutputStreamWriter(new FileOutputStream(testResourcePath.toFile()), StandardCharsets.UTF_8)){ - - for (int i = 0; i < content.size()-1; i++) { - writer.write(content.get(i)); - writer.write("\n"); - } - writer.write(content.getLast()); - - } catch (IOException e) { - } - - - return true; - } - return false; - } - - */ - public void sign_out_user() { this.distances = new ArrayList<>(); current_user = new User(); } - /*public void change_user_details(String username, String password, String hometown, String zipS, - String car_name, String car_l_100kmS, String car_avg_kmhS, String bike_avg_kmhS) throws IOException { - - int zip = Integer.parseInt(zipS); - double car_l_100km = Double.parseDouble(car_l_100kmS); - double car_avg_kmh = Double.parseDouble(car_avg_kmhS); - double bike_avg_kmh = Double.parseDouble(bike_avg_kmhS); - - String new_user_details = username + ";" + password + ";" + hometown + ";" + zipS - + ";" + car_name + ";" + car_l_100kmS + ";" + car_avg_kmhS + ";" + bike_avg_kmhS; - - File mainFile = new File("src/main/resources/user_data.csv"); - File testFile = new File("src/test/resources/user_data.csv"); - - BufferedWriter writer = new BufferedWriter(new FileWriter(mainFile)); - BufferedWriter writerTest = new BufferedWriter(new FileWriter(testFile)); - - InputStream inputStream = System.class.getResourceAsStream("/user_data.csv"); - - try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { - String line; - while ((line = reader.readLine()) != null) { - String[] row = line.split(";"); - - if (row[0].equals(current_user.getUsername())) { - writer.write(new_user_details); - writerTest.write(new_user_details); - } else { - writer.write(line); - writerTest.write(line); - } - if(reader.readLine() != null){ - writer.write("\n"); - writer.write("\n"); - } - writer.write("\n"); - } - - } catch (Exception e) {} - - writer.close(); - - current_user.setUsername(username); - current_user.setPassword(password); - current_user.setHometown(hometown); - current_user.setZip(zip); - current_user.setCar_name(car_name); - current_user.setCar_l_100km(car_l_100km); - current_user.setCar_avg_kmh(car_avg_kmh); - current_user.setBike_avg_kmh(bike_avg_kmh); - } - - */ - public String[] getDetails(){ return new String[]{current_user.getUsername(), current_user.getPassword(), current_user.getHometown(), String.valueOf(current_user.getZip()), diff --git a/src/main/java/de/hs_mannheim/domain/User.java b/src/main/java/de/hs_mannheim/domain/User.java index cd9b6f9..a1d202e 100644 --- a/src/main/java/de/hs_mannheim/domain/User.java +++ b/src/main/java/de/hs_mannheim/domain/User.java @@ -88,4 +88,9 @@ public class User { public void setBike_avg_kmh(double bike_avg_kmh) { this.bike_avg_kmh = bike_avg_kmh; } + + public String toString(){ + return this.username + ";" + System.encoding(this.password) + ";" + this.hometown + ";" + this.zip + ";" + this.car_name + ";" + + this.car_l_100km + ";" + this.car_avg_kmh + ";" + this.bike_avg_kmh; + } } diff --git a/src/main/resources/user_data.csv b/src/main/resources/user_data.csv index b4eeae8..2a4e5cb 100644 --- a/src/main/resources/user_data.csv +++ b/src/main/resources/user_data.csv @@ -1,2 +1,2 @@ -Daniel;1401Daniel;Mannheim;68305;BMW;1.5;50.4;40.2 -David;123Esel;Mannheim;68305;BMW;1.5;50.4;40.2 \ No newline at end of file +Daniel;MTQwMURhbmllbA==;Mannheim;68305;BMW;1.5;50.4;40.2 +David;MTIzRXNlbA==;Mannheim;68305;BMW;1.5;50.4;40.2 \ 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 c3adc94..aa5a7a6 100644 --- a/src/test/java/de/hs_mannheim/domain/SystemTest.java +++ b/src/test/java/de/hs_mannheim/domain/SystemTest.java @@ -73,7 +73,7 @@ public class SystemTest { assertEquals("60.361 l", current_system.calc_l_consumption("10115")); // Kraftstoffverbrauch nach Berlin } - /*@Test + @Test public void random_destinations(){ current_system.set_current_user_zip("68161"); @@ -85,14 +85,14 @@ public class SystemTest { assertEquals(true, Double.parseDouble(current_system.distance(current_system.random_destinations_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); - }*/ + } @Test public void encoding(){ String test_password = "123Esel"; - assertEquals("MTIzRXNlbA==",current_system.encoding(test_password)); + assertEquals("MTIzRXNlbA==",System.encoding(test_password)); } @@ -101,18 +101,10 @@ public class SystemTest { String test_password = "MTIzRXNlbA=="; - assertEquals("123Esel",current_system.decoding(test_password)); + assertEquals("123Esel",System.decoding(test_password)); } - - @Test - public void get_all_user() { - - assertEquals(2, current_system.get_all_user().size()); - assertEquals(true, current_system.get_all_user().get(0).getPassword().equals("MTQwMURhbmllbA==")); - assertEquals(true, current_system.get_all_user().get(1).getPassword().equals("MTIzRXNlbA==")); - } - + @Test public void sign_in_user() { @@ -135,7 +127,7 @@ public class SystemTest { public void sign_up_user(){ // Username darf nicht doppelt vorkommen! assertEquals(false, current_system.sign_up_user("David","123Esel","Mannheim","68161","AMG","10","300","20")); - assertEquals(true, current_system.sign_up_user("Daavid","123Esel","Mannheim","68161","AMG","10","300","20")); + assertEquals(true, current_system.sign_up_user("Selim","Penis69","Mannheim","68161","AMG","10","300","20")); // PLZ muss mit Stadt übereinstimmen assertEquals(false, current_system.sign_up_user("Lukas","123Esel","Mannheim","11105","AMG","10","300","20")); assertEquals(true, current_system.sign_up_user("Lukas","123Esel","Mannheim","68305","AMG","10","300","20")); diff --git a/src/test/resources/user_data.csv b/src/test/resources/user_data.csv index b4eeae8..2a4e5cb 100644 --- a/src/test/resources/user_data.csv +++ b/src/test/resources/user_data.csv @@ -1,2 +1,2 @@ -Daniel;1401Daniel;Mannheim;68305;BMW;1.5;50.4;40.2 -David;123Esel;Mannheim;68305;BMW;1.5;50.4;40.2 \ No newline at end of file +Daniel;MTQwMURhbmllbA==;Mannheim;68305;BMW;1.5;50.4;40.2 +David;MTIzRXNlbA==;Mannheim;68305;BMW;1.5;50.4;40.2 \ No newline at end of file From 0e4f7d35b7264990f3daca67337109b210029f85 Mon Sep 17 00:00:00 2001 From: Selim Eser <2211482@stud.hs-mannheim.de> Date: Tue, 11 Jun 2024 15:07:35 +0200 Subject: [PATCH 21/23] change_user_details method implemented and getDetails Exception Handling --- .../java/de/hs_mannheim/domain/System.java | 62 +++++++++++++++++-- .../de/hs_mannheim/domain/SystemTest.java | 12 +++- 2 files changed, 68 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 7805f5c..36ed716 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -108,7 +108,7 @@ public class System { double car_avg_kmh = Double.parseDouble(car_avg_kmhS); double bike_avg_kmh = Double.parseDouble(bike_avg_kmhS); - for(User user: all_user) + for(User user: this.all_user) if(user.getUsername().equals(username)) return false; @@ -134,6 +134,57 @@ public class System { return true; } + public boolean change_user_details(String username, String password, String hometown, String zipS, + 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 = Integer.parseInt(zipS); + car_l_100km = Double.parseDouble(car_l_100kmS); + car_avg_kmh = Double.parseDouble(car_avg_kmhS); + bike_avg_kmh = Double.parseDouble(bike_avg_kmhS); + } catch (NumberFormatException n){ + return false; + } + + if(username.equals("")||password.equals("")||hometown.equals("")||zipS.equals("")) + return false; + + for(User user: this.all_user) + if(user.getUsername().equals(username)) + return false; + + ArrayList mem = search(zipS); + boolean bool = false; + + for (String line: mem) + if(line.split(";")[1].equals(hometown)) { + bool = true; + break; + } + + if(!bool) + return false; + + this.all_user.remove(this.current_user); + this.current_user = new User(username, password, hometown, zip, car_name, car_l_100km, car_avg_kmh, bike_avg_kmh); + + write_to_file(all_user_toString(), "src/main/resources/user_data.csv"); + write_to_file(all_user_toString(), "src/test/resources/user_data.csv"); + + this.all_user.add(current_user); + + write_to_file(all_user_toString(), "src/main/resources/user_data.csv"); + write_to_file(all_user_toString(), "src/test/resources/user_data.csv"); + + return true; + + } + public void write_to_file(ArrayList lines, String file) { try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) { for (int i = 0; i < lines.size() - 1; i++) { @@ -152,9 +203,12 @@ public class System { public String[] getDetails(){ return new String[]{current_user.getUsername(), current_user.getPassword(), - current_user.getHometown(), String.valueOf(current_user.getZip()), - current_user.getCar_name(), String.valueOf(current_user.getCar_l_100km()), - String.valueOf(current_user.getCar_avg_kmh()), String.valueOf(current_user.getCar_avg_kmh())}; + current_user.getHometown(), + current_user.getZip()==0?"":String.valueOf(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())}; } public ArrayList search(String hometown_or_zip) { diff --git a/src/test/java/de/hs_mannheim/domain/SystemTest.java b/src/test/java/de/hs_mannheim/domain/SystemTest.java index aa5a7a6..666c72b 100644 --- a/src/test/java/de/hs_mannheim/domain/SystemTest.java +++ b/src/test/java/de/hs_mannheim/domain/SystemTest.java @@ -3,7 +3,6 @@ package de.hs_mannheim.domain; import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals; -import java.io.IOException; import java.util.ArrayList; import org.junit.jupiter.api.Test; @@ -123,7 +122,7 @@ public class SystemTest { } - @Test + /*@Test public void sign_up_user(){ // Username darf nicht doppelt vorkommen! assertEquals(false, current_system.sign_up_user("David","123Esel","Mannheim","68161","AMG","10","300","20")); @@ -135,6 +134,15 @@ public class SystemTest { assertEquals("Lukas",current_system.getDetails()[0]); current_system.sign_out_user(); assertEquals("",current_system.getDetails()[0]); + }*/ + + @Test + public void change_user_details(){ + + current_system.sign_in_user("David", "123Esel"); + current_system.change_user_details(null, null, null, null, null, null, null, null); + assertEquals(current_system, STR); + } From 4a811935d2ceeb7e74084e1bb2babd4f0bcc7342 Mon Sep 17 00:00:00 2001 From: Selim Eser <2211482@stud.hs-mannheim.de> Date: Tue, 11 Jun 2024 15:13:37 +0200 Subject: [PATCH 22/23] More Exception Handling --- .../java/de/hs_mannheim/domain/System.java | 20 +++++++++++++++---- src/main/java/de/hs_mannheim/domain/User.java | 8 ++++---- 2 files changed, 20 insertions(+), 8 deletions(-) diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index 36ed716..e99da42 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -103,10 +103,22 @@ public class System { public boolean sign_up_user(String username, String password, String hometown, String zipS, String car_name, String car_l_100kmS, String car_avg_kmhS, String bike_avg_kmhS){ - int zip = Integer.parseInt(zipS); - double car_l_100km = Double.parseDouble(car_l_100kmS); - double car_avg_kmh = Double.parseDouble(car_avg_kmhS); - double bike_avg_kmh = Double.parseDouble(bike_avg_kmhS); + int zip; + double car_l_100km; + double car_avg_kmh; + double bike_avg_kmh; + + try{ + zip = Integer.parseInt(zipS); + car_l_100km = Double.parseDouble(car_l_100kmS); + car_avg_kmh = Double.parseDouble(car_avg_kmhS); + bike_avg_kmh = Double.parseDouble(bike_avg_kmhS); + } catch (NumberFormatException n){ + return false; + } + + if(username.equals("")||password.equals("")||hometown.equals("")||zipS.equals("")) + return false; for(User user: this.all_user) if(user.getUsername().equals(username)) diff --git a/src/main/java/de/hs_mannheim/domain/User.java b/src/main/java/de/hs_mannheim/domain/User.java index a1d202e..b1f311b 100644 --- a/src/main/java/de/hs_mannheim/domain/User.java +++ b/src/main/java/de/hs_mannheim/domain/User.java @@ -5,11 +5,11 @@ public class User { private String username = ""; private String password = ""; private String hometown = ""; - private int zip; + private int zip = 0; private String car_name = ""; - private double car_l_100km; - private double car_avg_kmh; - private double bike_avg_kmh; + private double car_l_100km = 0; + private double car_avg_kmh = 0; + private double bike_avg_kmh = 0; public User(){} From 0cd0be5fe92bf8e9f6c526090660419b944f6e7c Mon Sep 17 00:00:00 2001 From: Selim Eser <2211482@stud.hs-mannheim.de> Date: Tue, 11 Jun 2024 15:53:54 +0200 Subject: [PATCH 23/23] Everything works fine for now tests all successful --- .../java/de/hs_mannheim/domain/System.java | 39 +++++++++++++------ src/main/resources/user_data.csv | 2 +- .../de/hs_mannheim/domain/SystemTest.java | 15 +++---- src/test/resources/user_data.csv | 2 +- 4 files changed, 38 insertions(+), 20 deletions(-) diff --git a/src/main/java/de/hs_mannheim/domain/System.java b/src/main/java/de/hs_mannheim/domain/System.java index e99da42..376d237 100644 --- a/src/main/java/de/hs_mannheim/domain/System.java +++ b/src/main/java/de/hs_mannheim/domain/System.java @@ -56,6 +56,20 @@ 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; + else + return Double.parseDouble(s); + } + public void get_all_user() { String[] fileString = new String[8]; @@ -109,10 +123,10 @@ public class System { double bike_avg_kmh; try{ - zip = Integer.parseInt(zipS); - car_l_100km = Double.parseDouble(car_l_100kmS); - car_avg_kmh = Double.parseDouble(car_avg_kmhS); - bike_avg_kmh = Double.parseDouble(bike_avg_kmhS); + zip = parseInt(zipS); + car_l_100km = parseDouble(car_l_100kmS); + car_avg_kmh = parseDouble(car_avg_kmhS); + bike_avg_kmh = parseDouble(bike_avg_kmhS); } catch (NumberFormatException n){ return false; } @@ -145,7 +159,7 @@ public class System { return true; } - + public boolean change_user_details(String username, String password, String hometown, String zipS, String car_name, String car_l_100kmS, String car_avg_kmhS, String bike_avg_kmhS){ @@ -155,10 +169,10 @@ public class System { double bike_avg_kmh; try{ - zip = Integer.parseInt(zipS); - car_l_100km = Double.parseDouble(car_l_100kmS); - car_avg_kmh = Double.parseDouble(car_avg_kmhS); - bike_avg_kmh = Double.parseDouble(bike_avg_kmhS); + zip = parseInt(zipS); + car_l_100km = parseDouble(car_l_100kmS); + car_avg_kmh = parseDouble(car_avg_kmhS); + bike_avg_kmh = parseDouble(bike_avg_kmhS); } catch (NumberFormatException n){ return false; } @@ -182,12 +196,15 @@ public class System { if(!bool) return false; - this.all_user.remove(this.current_user); - this.current_user = new User(username, password, hometown, zip, car_name, car_l_100km, car_avg_kmh, bike_avg_kmh); + + for(int i = 0; i< this.all_user.size(); i++) + if(this.all_user.get(i).getUsername().equals(current_user.getUsername())) + this.all_user.remove(i); write_to_file(all_user_toString(), "src/main/resources/user_data.csv"); write_to_file(all_user_toString(), "src/test/resources/user_data.csv"); + this.current_user = new User(username, password, hometown, zip, car_name, car_l_100km, car_avg_kmh, bike_avg_kmh); this.all_user.add(current_user); write_to_file(all_user_toString(), "src/main/resources/user_data.csv"); diff --git a/src/main/resources/user_data.csv b/src/main/resources/user_data.csv index 2a4e5cb..d73791d 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;68305;BMW;1.5;50.4;40.2 \ 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 666c72b..53f4e8b 100644 --- a/src/test/java/de/hs_mannheim/domain/SystemTest.java +++ b/src/test/java/de/hs_mannheim/domain/SystemTest.java @@ -122,7 +122,7 @@ public class SystemTest { } - /*@Test + @Test public void sign_up_user(){ // Username darf nicht doppelt vorkommen! assertEquals(false, current_system.sign_up_user("David","123Esel","Mannheim","68161","AMG","10","300","20")); @@ -134,19 +134,20 @@ public class SystemTest { assertEquals("Lukas",current_system.getDetails()[0]); current_system.sign_out_user(); assertEquals("",current_system.getDetails()[0]); - }*/ + } @Test public void change_user_details(){ current_system.sign_in_user("David", "123Esel"); - current_system.change_user_details(null, null, null, null, null, null, null, null); - assertEquals(current_system, STR); + current_system.change_user_details("Enes", "Penis123", "Mannheim", "68161", "", "", "", ""); + 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 + */ } \ No newline at end of file diff --git a/src/test/resources/user_data.csv b/src/test/resources/user_data.csv index 2a4e5cb..d73791d 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;68305;BMW;1.5;50.4;40.2 \ No newline at end of file +David;MTIzRXNlbA==;Mannheim;68161;AMG;10.0;300.0;20.0 \ No newline at end of file