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] 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