132 lines
4.0 KiB
Java
132 lines
4.0 KiB
Java
package domain;
|
|
|
|
import java.io.BufferedReader;
|
|
import java.io.FileReader;
|
|
import java.io.FileWriter;
|
|
import java.io.IOException;
|
|
import java.io.PrintWriter;
|
|
import org.apache.commons.codec.digest.DigestUtils;
|
|
|
|
public class User {
|
|
|
|
private String username;
|
|
private String hashedPassword;
|
|
private Ort heimatstandort;
|
|
public Auto auto;
|
|
private double durchschnittsgeschwindigkeitPKW;
|
|
private double durchschnittsgeschwindigkeitFahrrad;
|
|
|
|
public User(String username, String password, Ort heimatstandort, Auto auto, double durchschnittsgeschwindigkeitPKW,
|
|
double durchschnittsgeschwindigkeitFahrrad) {
|
|
|
|
this.username = username;
|
|
this.hashedPassword = hashPassword(password);
|
|
this.heimatstandort = heimatstandort;
|
|
this.auto = auto;
|
|
this.durchschnittsgeschwindigkeitPKW = durchschnittsgeschwindigkeitPKW;
|
|
this.durchschnittsgeschwindigkeitFahrrad = durchschnittsgeschwindigkeitFahrrad;
|
|
}
|
|
|
|
private String hashPassword(String password) {
|
|
return DigestUtils.sha256Hex(password); // Hash-Funktion verwenden
|
|
}
|
|
|
|
public String getUsername() {
|
|
return username;
|
|
}
|
|
|
|
public void setUsername(String username) {
|
|
this.username = username;
|
|
}
|
|
|
|
public String getHashedPassword() {
|
|
return hashedPassword;
|
|
}
|
|
|
|
public void setPassword(String password) {
|
|
this.hashedPassword = hashPassword(password); // Passwort hashen beim Setzen
|
|
}
|
|
|
|
public Ort getHeimatstandort() {
|
|
return heimatstandort;
|
|
}
|
|
|
|
public void setHeimatstandort(Ort heimatstandort) {
|
|
this.heimatstandort = heimatstandort;
|
|
}
|
|
|
|
public Auto getAuto() {
|
|
return auto;
|
|
}
|
|
|
|
public void setAuto(Auto auto) {
|
|
this.auto = auto;
|
|
}
|
|
|
|
public double getDurchschnittsgeschwindigkeitPKW() {
|
|
return durchschnittsgeschwindigkeitPKW;
|
|
}
|
|
|
|
public void setDurchschnittsgeschwindigkeitPKW(double durchschnittsgeschwindigkeitPKW) {
|
|
this.durchschnittsgeschwindigkeitPKW = durchschnittsgeschwindigkeitPKW;
|
|
}
|
|
|
|
public double getDurchschnittsgeschwindigkeitFahrrad() {
|
|
return durchschnittsgeschwindigkeitFahrrad;
|
|
}
|
|
|
|
public void setDurchschnittsgeschwindigkeitFahrrad(double durchschnittsgeschwindigkeitFahrrad) {
|
|
this.durchschnittsgeschwindigkeitFahrrad = durchschnittsgeschwindigkeitFahrrad;
|
|
}
|
|
|
|
public void registrieren() {
|
|
|
|
try (PrintWriter writer = new PrintWriter(new FileWriter("users.txt", true))) {
|
|
writer.println(username + "," + hashedPassword + "," + heimatstandort.getPLZ() + ","
|
|
+ heimatstandort.getName() + "," + auto.getName() + "," + auto.getCO2AusstossProKm() + ","
|
|
+ durchschnittsgeschwindigkeitPKW + "," + durchschnittsgeschwindigkeitFahrrad);
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
|
|
}
|
|
|
|
public boolean einloggen() {
|
|
try (BufferedReader reader = new BufferedReader(new FileReader("users.txt"))) {
|
|
String line;
|
|
while ((line = reader.readLine()) != null) {
|
|
String[] parts = line.split(",");
|
|
if (parts[0].equals(username) && parts[1].equals(hashedPassword)) {
|
|
int plz = Integer.parseInt(parts[2]);
|
|
this.heimatstandort = new Ort(plz, parts[3]);
|
|
this.auto = new Auto(parts[4], Double.parseDouble(parts[5]));
|
|
this.durchschnittsgeschwindigkeitPKW = Double.parseDouble(parts[6]);
|
|
this.durchschnittsgeschwindigkeitFahrrad = Double.parseDouble(parts[7]);
|
|
return true;
|
|
}
|
|
}
|
|
} catch (IOException e) {
|
|
e.printStackTrace();
|
|
}
|
|
return false;
|
|
}
|
|
|
|
|
|
public String zeigeWetterHeimatstandort() {
|
|
try {
|
|
return WetterService.getAktuellesWetter(heimatstandort.getName());
|
|
} catch (IOException | InterruptedException e) {
|
|
return "Fehler beim Abrufen des Wetters: " + e.getMessage();
|
|
}
|
|
}
|
|
|
|
public String zeigeWettervorhersageHeimatstandort() {
|
|
try {
|
|
return WetterService.getWettervorhersage(heimatstandort.getName());
|
|
} catch (IOException | InterruptedException e) {
|
|
return "Fehler beim Abrufen der Wettervorhersage: " + e.getMessage();
|
|
}
|
|
}
|
|
|
|
}
|