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