Vollständige User Klasse und UserFacade
parent
09a7b543a5
commit
829e875409
|
@ -1,12 +1,19 @@
|
|||
package domain;
|
||||
|
||||
import java.io.BufferedReader;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.FileReader;
|
||||
import java.io.FileWriter;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
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 password;
|
||||
|
@ -73,14 +80,12 @@ public class User {
|
|||
this.durchschnittsgeschwindigkeitFahrrad = durchschnittsgeschwindigkeitFahrrad;
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void registrieren() {
|
||||
|
||||
try (PrintWriter writer = new PrintWriter(new FileWriter("users.txt", true))) {
|
||||
writer.println(username + "," + password + "," + heimatstandort.getPLZ() + "," + heimatstandort.getOrtName() + ","
|
||||
+ auto.getName() + "," + auto.getCO2AusstossProKm() + "," + durchschnittsgeschwindigkeitPKW + ","
|
||||
+ durchschnittsgeschwindigkeitFahrrad);
|
||||
writer.println(username + "," + password + "," + heimatstandort.getPLZ() + "," + heimatstandort.getOrtName()
|
||||
+ "," + auto.getName() + "," + auto.getCO2AusstossProKm() + "," + durchschnittsgeschwindigkeitPKW
|
||||
+ "," + durchschnittsgeschwindigkeitFahrrad);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
|
@ -109,13 +114,39 @@ public class User {
|
|||
}
|
||||
|
||||
|
||||
|
||||
public void zeigeWetterHeimatstandort() {
|
||||
|
||||
|
||||
|
||||
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) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
private List<User> loadUsers() {
|
||||
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;
|
||||
|
||||
import domain.User;
|
||||
import domain.Ort;
|
||||
import domain.Auto;
|
||||
|
||||
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