Merge branch 'selims_dev_branch' into sign_in_up_out

sign_in_up_out
Selim Eser 2024-06-11 15:55:42 +02:00
commit 122dfe0b64
5 changed files with 136 additions and 185 deletions

View File

@ -6,13 +6,7 @@ import java.net.http.HttpClient;
import java.net.http.HttpRequest; import java.net.http.HttpRequest;
import java.net.http.HttpResponse; import java.net.http.HttpResponse;
import java.net.http.HttpResponse.BodyHandlers; 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.ArrayList;
import java.util.Arrays;
import java.util.HashSet;
import java.util.TreeSet; import java.util.TreeSet;
import org.apache.commons.codec.binary.Base64; import org.apache.commons.codec.binary.Base64;
@ -21,11 +15,13 @@ import org.json.JSONObject;
public class System { public class System {
private User current_user = new User(); private User current_user = new User();
private ArrayList<User> all_user = new ArrayList<>();
private String api_key; private String api_key;
private ArrayList<String> distances = new ArrayList<>(); private ArrayList<String> distances = new ArrayList<>();
public System(String api_key) { public System(String api_key) {
this.api_key = api_key; this.api_key = api_key;
get_all_user();
} }
public void set_current_user_zip(String zip) { public void set_current_user_zip(String zip) {
@ -44,7 +40,7 @@ public class System {
this.current_user.setBike_avg_kmh(bike_avg_kmh); 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()]; 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); binary_data[i] = (byte)string.charAt(i);
@ -52,7 +48,7 @@ public class System {
return new String(Base64.decodeBase64(binary_data)); 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()]; 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); binary_data[i] = (byte) string.charAt(i);
@ -60,8 +56,21 @@ public class System {
return Base64.encodeBase64String(binary_data); return Base64.encodeBase64String(binary_data);
} }
public ArrayList<User> get_all_user() { public static int parseInt(String s) throws NumberFormatException{
ArrayList<User> all_users = new ArrayList<>(); 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]; String[] fileString = new String[8];
InputStream inputStream = System.class.getResourceAsStream("/user_data.csv"); InputStream inputStream = System.class.getResourceAsStream("/user_data.csv");
@ -70,8 +79,8 @@ public class System {
try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) { try (BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
while ((path = reader.readLine()) != null) { while ((path = reader.readLine()) != null) {
fileString = path.split(";"); fileString = path.split(";");
fileString[1] = encoding(fileString[1]); fileString[1] = decoding(fileString[1]);
all_users.add(new User(fileString[0], fileString[1], fileString[2], this.all_user.add(new User(fileString[0], fileString[1], fileString[2],
Integer.parseInt(fileString[3]), fileString[4], Integer.parseInt(fileString[3]), fileString[4],
Double.parseDouble(fileString[5]), Double.parseDouble(fileString[5]),
Double.parseDouble(fileString[6]), Double.parseDouble(fileString[6]),
@ -79,30 +88,23 @@ public class System {
} }
} catch (Exception e) { } catch (Exception e) {
} }
all_users.add(current_user);
return all_users;
} }
public ArrayList<String> all_user_toString(){ public ArrayList<String> all_user_toString(){
ArrayList<String> result = new ArrayList<>(); ArrayList<String> result = new ArrayList<>();
ArrayList<User> all_user = get_all_user();
for(User user : all_user){ for(User user : this.all_user){
result.add(getDetails()[0] + ";" + getDetails()[1] + ";" + getDetails()[2] + ";" + getDetails()[3] result.add(user.toString());
+ ";" + getDetails()[4] + ";" + getDetails()[5] + ";" + getDetails()[6] + ";" + getDetails()[7]);
} }
return result; return result;
} }
public boolean sign_in_user(String username, String password) { public boolean sign_in_user(String username, String password) {
ArrayList<User> mem = get_all_user();
for (User user : mem) { for (User user : this.all_user) {
if (user.getUsername().equals(username) && decoding(user.getPassword()).equals(password)) { if (user.getUsername().equals(username) && user.getPassword().equals(password)) {
current_user = new User(user.getUsername(), user.getPassword(), current_user = new User(user.getUsername(), user.getPassword(),
user.getHometown(), user.getZip(), user.getCar_name(), user.getHometown(), user.getZip(), user.getCar_name(),
user.getCar_l_100km(), user.getCar_avg_kmh(), user.getBike_avg_kmh()); user.getCar_l_100km(), user.getCar_avg_kmh(), user.getBike_avg_kmh());
@ -115,14 +117,24 @@ public class System {
public boolean sign_up_user(String username, String password, String hometown, String zipS, public boolean sign_up_user(String username, String password, String hometown, String zipS,
String car_name, String car_l_100kmS, String car_avg_kmhS, String bike_avg_kmhS){ String car_name, String car_l_100kmS, String car_avg_kmhS, String bike_avg_kmhS){
int zip = Integer.parseInt(zipS); int zip;
double car_l_100km = Double.parseDouble(car_l_100kmS); double car_l_100km;
double car_avg_kmh = Double.parseDouble(car_avg_kmhS); double car_avg_kmh;
double bike_avg_kmh = Double.parseDouble(bike_avg_kmhS); double bike_avg_kmh;
ArrayList<User> all_user = get_all_user(); try{
zip = parseInt(zipS);
car_l_100km = parseDouble(car_l_100kmS);
car_avg_kmh = parseDouble(car_avg_kmhS);
bike_avg_kmh = parseDouble(bike_avg_kmhS);
} catch (NumberFormatException n){
return false;
}
for(User user: all_user) if(username.equals("")||password.equals("")||hometown.equals("")||zipS.equals(""))
return false;
for(User user: this.all_user)
if(user.getUsername().equals(username)) if(user.getUsername().equals(username))
return false; return false;
@ -140,11 +152,68 @@ public class System {
current_user = new User(username, password, hometown, zip, car_name, car_l_100km, car_avg_kmh, bike_avg_kmh); 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; 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 = 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;
}
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<String> mem = search(zipS);
boolean bool = false;
for (String line: mem)
if(line.split(";")[1].equals(hometown)) {
bool = true;
break;
}
if(!bool)
return false;
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");
write_to_file(all_user_toString(), "src/test/resources/user_data.csv");
return true;
}
public void write_to_file(ArrayList<String> lines, String file) { public void write_to_file(ArrayList<String> lines, String file) {
try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) { try (BufferedWriter writer = new BufferedWriter(new FileWriter(file))) {
for (int i = 0; i < lines.size() - 1; i++) { for (int i = 0; i < lines.size() - 1; i++) {
@ -155,144 +224,20 @@ public class System {
} catch (IOException e) {} } 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<User> mem = get_all_user();
ArrayList<String> 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<String> 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() { public void sign_out_user() {
this.distances = new ArrayList<>(); this.distances = new ArrayList<>();
current_user = new User(); 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(){ public String[] getDetails(){
return new String[]{current_user.getUsername(), current_user.getPassword(), return new String[]{current_user.getUsername(), current_user.getPassword(),
current_user.getHometown(), String.valueOf(current_user.getZip()), current_user.getHometown(),
current_user.getCar_name(), String.valueOf(current_user.getCar_l_100km()), current_user.getZip()==0?"":String.valueOf(current_user.getZip()),
String.valueOf(current_user.getCar_avg_kmh()), String.valueOf(current_user.getCar_avg_kmh())}; 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<String> search(String hometown_or_zip) { public ArrayList<String> search(String hometown_or_zip) {

View File

@ -5,11 +5,11 @@ public class User {
private String username = ""; private String username = "";
private String password = ""; private String password = "";
private String hometown = ""; private String hometown = "";
private int zip; private int zip = 0;
private String car_name = ""; private String car_name = "";
private double car_l_100km; private double car_l_100km = 0;
private double car_avg_kmh; private double car_avg_kmh = 0;
private double bike_avg_kmh; private double bike_avg_kmh = 0;
public User(){} public User(){}
@ -88,4 +88,9 @@ public class User {
public void setBike_avg_kmh(double bike_avg_kmh) { public void setBike_avg_kmh(double bike_avg_kmh) {
this.bike_avg_kmh = 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;
}
} }

View File

@ -1,2 +1,2 @@
Daniel;1401Daniel;Mannheim;68305;BMW;1.5;50.4;40.2 Daniel;MTQwMURhbmllbA==;Mannheim;68305;BMW;1.5;50.4;40.2
David;123Esel;Mannheim;68305;BMW;1.5;50.4;40.2 David;MTIzRXNlbA==;Mannheim;68161;AMG;10.0;300.0;20.0
1 Daniel 1401Daniel MTQwMURhbmllbA== Mannheim 68305 BMW 1.5 50.4 40.2
2 David 123Esel MTIzRXNlbA== Mannheim 68305 68161 BMW AMG 1.5 10.0 50.4 300.0 40.2 20.0

View File

@ -3,7 +3,6 @@ package de.hs_mannheim.domain;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotEquals; import static org.junit.jupiter.api.Assertions.assertNotEquals;
import java.io.IOException;
import java.util.ArrayList; import java.util.ArrayList;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -73,7 +72,7 @@ public class SystemTest {
assertEquals("60.361 l", current_system.calc_l_consumption("10115")); // Kraftstoffverbrauch nach Berlin assertEquals("60.361 l", current_system.calc_l_consumption("10115")); // Kraftstoffverbrauch nach Berlin
} }
/*@Test @Test
public void random_destinations(){ public void random_destinations(){
current_system.set_current_user_zip("68161"); current_system.set_current_user_zip("68161");
@ -85,14 +84,14 @@ public class SystemTest {
assertEquals(true, Double.parseDouble(current_system.distance(current_system.random_destinations_car().get(0).split(";")[0]).replace(" km", "")) > 150); assertEquals(true, Double.parseDouble(current_system.distance(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 // random_destinations_bike gibt nur destinations mit maximal 100 km Entfernung zurück
assertEquals(true, Double.parseDouble(current_system.distance(current_system.random_destinations_bike().get(0).split(";")[0]).replace(" km", "")) < 100); assertEquals(true, Double.parseDouble(current_system.distance(current_system.random_destinations_bike().get(0).split(";")[0]).replace(" km", "")) < 100);
}*/ }
@Test @Test
public void encoding(){ public void encoding(){
String test_password = "123Esel"; String test_password = "123Esel";
assertEquals("MTIzRXNlbA==",current_system.encoding(test_password)); assertEquals("MTIzRXNlbA==",System.encoding(test_password));
} }
@ -101,18 +100,10 @@ public class SystemTest {
String test_password = "MTIzRXNlbA=="; 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 @Test
public void sign_in_user() { public void sign_in_user() {
@ -135,7 +126,7 @@ public class SystemTest {
public void sign_up_user(){ public void sign_up_user(){
// Username darf nicht doppelt vorkommen! // Username darf nicht doppelt vorkommen!
assertEquals(false, current_system.sign_up_user("David","123Esel","Mannheim","68161","AMG","10","300","20")); 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 // PLZ muss mit Stadt übereinstimmen
assertEquals(false, current_system.sign_up_user("Lukas","123Esel","Mannheim","11105","AMG","10","300","20")); 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(true, current_system.sign_up_user("Lukas","123Esel","Mannheim","68305","AMG","10","300","20"));
@ -145,8 +136,18 @@ public class SystemTest {
assertEquals("",current_system.getDetails()[0]); assertEquals("",current_system.getDetails()[0]);
} }
@Test
public void change_user_details(){
current_system.sign_in_user("David", "123Esel");
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
*/
} }

View File

@ -1,2 +1,2 @@
Daniel;1401Daniel;Mannheim;68305;BMW;1.5;50.4;40.2 Daniel;MTQwMURhbmllbA==;Mannheim;68305;BMW;1.5;50.4;40.2
David;123Esel;Mannheim;68305;BMW;1.5;50.4;40.2 David;MTIzRXNlbA==;Mannheim;68161;AMG;10.0;300.0;20.0
1 Daniel 1401Daniel MTQwMURhbmllbA== Mannheim 68305 BMW 1.5 50.4 40.2
2 David 123Esel MTIzRXNlbA== Mannheim 68305 68161 BMW AMG 1.5 10.0 50.4 300.0 40.2 20.0