Vollständige User Klasse und UserFacade
parent
09a7b543a5
commit
829e875409
|
@ -1,12 +1,19 @@
|
||||||
package domain;
|
package domain;
|
||||||
|
|
||||||
import java.io.BufferedReader;
|
import java.io.BufferedReader;
|
||||||
|
import java.io.FileInputStream;
|
||||||
|
import java.io.FileOutputStream;
|
||||||
import java.io.FileReader;
|
import java.io.FileReader;
|
||||||
import java.io.FileWriter;
|
import java.io.FileWriter;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
|
import java.io.ObjectInputStream;
|
||||||
|
import java.io.ObjectOutputStream;
|
||||||
import java.io.PrintWriter;
|
import java.io.PrintWriter;
|
||||||
|
import java.io.Serializable;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
public class User {
|
public class User implements Serializable{
|
||||||
|
|
||||||
private String username;
|
private String username;
|
||||||
private String password;
|
private String password;
|
||||||
|
@ -28,94 +35,118 @@ public class User {
|
||||||
public String getUsername() {
|
public String getUsername() {
|
||||||
return username;
|
return username;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setUsername(String username) {
|
public void setUsername(String username) {
|
||||||
this.username = username;
|
this.username = username;
|
||||||
}
|
}
|
||||||
|
|
||||||
public String getPassword() {
|
public String getPassword() {
|
||||||
return password;
|
return password;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setPassword(String password) {
|
public void setPassword(String password) {
|
||||||
this.password = password;
|
this.password = password;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Ort getHeimatstandort() {
|
public Ort getHeimatstandort() {
|
||||||
return heimatstandort;
|
return heimatstandort;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setHeimatstandort(Ort heimatstandort) {
|
public void setHeimatstandort(Ort heimatstandort) {
|
||||||
this.heimatstandort = heimatstandort;
|
this.heimatstandort = heimatstandort;
|
||||||
}
|
}
|
||||||
|
|
||||||
public Auto getAuto() {
|
public Auto getAuto() {
|
||||||
return auto;
|
return auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setAuto(Auto auto) {
|
public void setAuto(Auto auto) {
|
||||||
this.auto = auto;
|
this.auto = auto;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getDurchschnittsgeschwindigkeitPKW() {
|
public double getDurchschnittsgeschwindigkeitPKW() {
|
||||||
return durchschnittsgeschwindigkeitPKW;
|
return durchschnittsgeschwindigkeitPKW;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void setDurchschnittsgeschwindigkeitPKW(double durchschnittsgeschwindigkeitPKW) {
|
public void setDurchschnittsgeschwindigkeitPKW(double durchschnittsgeschwindigkeitPKW) {
|
||||||
this.durchschnittsgeschwindigkeitPKW = durchschnittsgeschwindigkeitPKW;
|
this.durchschnittsgeschwindigkeitPKW = durchschnittsgeschwindigkeitPKW;
|
||||||
}
|
}
|
||||||
|
|
||||||
public double getDurchschnittsgeschwindigkeitFahrrad() {
|
public double getDurchschnittsgeschwindigkeitFahrrad() {
|
||||||
return durchschnittsgeschwindigkeitFahrrad;
|
return durchschnittsgeschwindigkeitFahrrad;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public void setDurchschnittsgeschwindigkeitFahrrad(double durchschnittsgeschwindigkeitFahrrad) {
|
||||||
|
this.durchschnittsgeschwindigkeitFahrrad = durchschnittsgeschwindigkeitFahrrad;
|
||||||
|
}
|
||||||
|
|
||||||
public void setDurchschnittsgeschwindigkeitFahrrad(double durchschnittsgeschwindigkeitFahrrad) {
|
|
||||||
this.durchschnittsgeschwindigkeitFahrrad = durchschnittsgeschwindigkeitFahrrad;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
public void registrieren() {
|
public void registrieren() {
|
||||||
|
|
||||||
try (PrintWriter writer = new PrintWriter(new FileWriter("users.txt", true))) {
|
try (PrintWriter writer = new PrintWriter(new FileWriter("users.txt", true))) {
|
||||||
writer.println(username + "," + password + "," + heimatstandort.getPLZ() + "," + heimatstandort.getOrtName() + ","
|
writer.println(username + "," + password + "," + heimatstandort.getPLZ() + "," + heimatstandort.getOrtName()
|
||||||
+ auto.getName() + "," + auto.getCO2AusstossProKm() + "," + durchschnittsgeschwindigkeitPKW + ","
|
+ "," + auto.getName() + "," + auto.getCO2AusstossProKm() + "," + durchschnittsgeschwindigkeitPKW
|
||||||
+ durchschnittsgeschwindigkeitFahrrad);
|
+ "," + durchschnittsgeschwindigkeitFahrrad);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
public boolean einloggen() {
|
public boolean einloggen() {
|
||||||
|
|
||||||
try (BufferedReader reader = new BufferedReader(new FileReader("users.txt"))) {
|
try (BufferedReader reader = new BufferedReader(new FileReader("users.txt"))) {
|
||||||
String line;
|
String line;
|
||||||
while ((line = reader.readLine()) != null) {
|
while ((line = reader.readLine()) != null) {
|
||||||
String[] parts = line.split(",");
|
String[] parts = line.split(",");
|
||||||
if (parts[0].equals(username) && parts[1].equals(password)) {
|
if (parts[0].equals(username) && parts[1].equals(password)) {
|
||||||
// Benutzerdaten laden
|
// Benutzerdaten laden
|
||||||
this.heimatstandort = new Ort(parts[2], parts[3]);
|
this.heimatstandort = new Ort(parts[2], parts[3]);
|
||||||
this.auto = new Auto(parts[4], Double.parseDouble(parts[5]));
|
this.auto = new Auto(parts[4], Double.parseDouble(parts[5]));
|
||||||
this.durchschnittsgeschwindigkeitPKW = Double.parseDouble(parts[6]);
|
this.durchschnittsgeschwindigkeitPKW = Double.parseDouble(parts[6]);
|
||||||
this.durchschnittsgeschwindigkeitFahrrad = Double.parseDouble(parts[7]);
|
this.durchschnittsgeschwindigkeitFahrrad = Double.parseDouble(parts[7]);
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
} catch (IOException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String zeigeWetterHeimatstandort() {
|
||||||
|
try {
|
||||||
|
return WetterService.getAktuellesWetter(heimatstandort.getOrtName());
|
||||||
|
} catch (IOException | InterruptedException e) {
|
||||||
|
return "Fehler beim Abrufen des Wetters: " + e.getMessage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public String zeigeWettervorhersageHeimatstandort() {
|
||||||
|
try {
|
||||||
|
return WetterService.getWettervorhersage(heimatstandort.getOrtName());
|
||||||
|
} catch (IOException | InterruptedException e) {
|
||||||
|
return "Fehler beim Abrufen der Wettervorhersage: " + e.getMessage();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
private void saveUsers(List<User> users) {
|
||||||
|
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("users.ser"))) {
|
||||||
|
oos.writeObject(users);
|
||||||
} catch (IOException e) {
|
} catch (IOException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
return false;
|
}
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
|
private List<User> loadUsers() {
|
||||||
public void zeigeWetterHeimatstandort() {
|
try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("users.ser"))) {
|
||||||
|
return (List<User>) ois.readObject();
|
||||||
|
} catch (IOException | ClassNotFoundException e) {
|
||||||
|
return new ArrayList<>();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,46 @@
|
||||||
package fassade;
|
package fassade;
|
||||||
|
|
||||||
|
import domain.User;
|
||||||
|
import domain.Ort;
|
||||||
|
import domain.Auto;
|
||||||
|
|
||||||
public class UserFassade {
|
public class UserFassade {
|
||||||
|
|
||||||
|
public User registrieren(String username, String password, String plz, String ortName, String autoName,
|
||||||
|
double co2AusstossProKm, double durchschnittsgeschwindigkeitPKW,
|
||||||
|
double durchschnittsgeschwindigkeitFahrrad) {
|
||||||
|
Ort heimatstandort = new Ort(plz, ortName);
|
||||||
|
Auto auto = new Auto(autoName, co2AusstossProKm);
|
||||||
|
User user = new User(username, password, heimatstandort, auto, durchschnittsgeschwindigkeitPKW,
|
||||||
|
durchschnittsgeschwindigkeitFahrrad);
|
||||||
|
user.registrieren();
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
|
||||||
|
public User einloggen(String username, String password) {
|
||||||
|
User user = new User(username, password, null, null, 0, 0);
|
||||||
|
if (user.einloggen()) {
|
||||||
|
return user;
|
||||||
|
}
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void zeigeWetterHeimatstandort(User user) {
|
||||||
|
try {
|
||||||
|
String aktuellesWetter = user.zeigeWetterHeimatstandort();
|
||||||
|
System.out.println(aktuellesWetter);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("Fehler beim Abrufen des aktuellen Wetters: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void zeigeWettervorhersageHeimatstandort(User user) {
|
||||||
|
try {
|
||||||
|
String wettervorhersage = user.zeigeWettervorhersageHeimatstandort();
|
||||||
|
System.out.println(wettervorhersage);
|
||||||
|
} catch (Exception e) {
|
||||||
|
System.err.println("Fehler beim Abrufen der Wettervorhersage: " + e.getMessage());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue