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;
|
||||
|
@ -28,94 +35,118 @@ public class User {
|
|||
public String getUsername() {
|
||||
return username;
|
||||
}
|
||||
|
||||
|
||||
public void setUsername(String username) {
|
||||
this.username = username;
|
||||
}
|
||||
|
||||
|
||||
public String getPassword() {
|
||||
return password;
|
||||
}
|
||||
return password;
|
||||
}
|
||||
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
public void setPassword(String password) {
|
||||
this.password = password;
|
||||
}
|
||||
|
||||
public Ort getHeimatstandort() {
|
||||
return heimatstandort;
|
||||
}
|
||||
public Ort getHeimatstandort() {
|
||||
return heimatstandort;
|
||||
}
|
||||
|
||||
public void setHeimatstandort(Ort heimatstandort) {
|
||||
this.heimatstandort = heimatstandort;
|
||||
}
|
||||
public void setHeimatstandort(Ort heimatstandort) {
|
||||
this.heimatstandort = heimatstandort;
|
||||
}
|
||||
|
||||
public Auto getAuto() {
|
||||
return auto;
|
||||
}
|
||||
public Auto getAuto() {
|
||||
return auto;
|
||||
}
|
||||
|
||||
public void setAuto(Auto auto) {
|
||||
this.auto = auto;
|
||||
}
|
||||
public void setAuto(Auto auto) {
|
||||
this.auto = auto;
|
||||
}
|
||||
|
||||
public double getDurchschnittsgeschwindigkeitPKW() {
|
||||
return durchschnittsgeschwindigkeitPKW;
|
||||
}
|
||||
public double getDurchschnittsgeschwindigkeitPKW() {
|
||||
return durchschnittsgeschwindigkeitPKW;
|
||||
}
|
||||
|
||||
public void setDurchschnittsgeschwindigkeitPKW(double durchschnittsgeschwindigkeitPKW) {
|
||||
this.durchschnittsgeschwindigkeitPKW = durchschnittsgeschwindigkeitPKW;
|
||||
}
|
||||
public void setDurchschnittsgeschwindigkeitPKW(double durchschnittsgeschwindigkeitPKW) {
|
||||
this.durchschnittsgeschwindigkeitPKW = durchschnittsgeschwindigkeitPKW;
|
||||
}
|
||||
|
||||
public double getDurchschnittsgeschwindigkeitFahrrad() {
|
||||
return durchschnittsgeschwindigkeitFahrrad;
|
||||
}
|
||||
public double getDurchschnittsgeschwindigkeitFahrrad() {
|
||||
return durchschnittsgeschwindigkeitFahrrad;
|
||||
}
|
||||
|
||||
public void setDurchschnittsgeschwindigkeitFahrrad(double durchschnittsgeschwindigkeitFahrrad) {
|
||||
this.durchschnittsgeschwindigkeitFahrrad = 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 + "," + password + "," + heimatstandort.getPLZ() + "," + heimatstandort.getOrtName() + ","
|
||||
+ auto.getName() + "," + auto.getCO2AusstossProKm() + "," + durchschnittsgeschwindigkeitPKW + ","
|
||||
+ durchschnittsgeschwindigkeitFahrrad);
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
writer.println(username + "," + password + "," + heimatstandort.getPLZ() + "," + heimatstandort.getOrtName()
|
||||
+ "," + 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(password)) {
|
||||
// Benutzerdaten laden
|
||||
this.heimatstandort = new Ort(parts[2], 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;
|
||||
}
|
||||
}
|
||||
String line;
|
||||
while ((line = reader.readLine()) != null) {
|
||||
String[] parts = line.split(",");
|
||||
if (parts[0].equals(username) && parts[1].equals(password)) {
|
||||
// Benutzerdaten laden
|
||||
this.heimatstandort = new Ort(parts[2], 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.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();
|
||||
}
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
|
||||
public void zeigeWetterHeimatstandort() {
|
||||
|
||||
|
||||
|
||||
}
|
||||
|
||||
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