forked from hummel/BankSystemWS23
Compare commits
1 Commits
main
...
persistenc
Author | SHA1 | Date |
---|---|---|
Oliver Hummel | 353797a7a2 |
|
@ -1,8 +1,15 @@
|
|||
package bank;
|
||||
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Bank {
|
||||
public class Bank implements Serializable {
|
||||
private String name;
|
||||
private ArrayList<Bankkonto> kontenliste;
|
||||
|
||||
|
@ -16,6 +23,8 @@ public class Bank {
|
|||
kontenliste.add(bk);
|
||||
|
||||
// Speichern Methode 2: geänderte Daten speichern
|
||||
saveBankData(); // bzw. der Einfachheit halber: alle Daten ;-)
|
||||
|
||||
return bk.getKontonummer();
|
||||
}
|
||||
|
||||
|
@ -43,4 +52,30 @@ public class Bank {
|
|||
return null;
|
||||
}
|
||||
|
||||
// Die folgenden Persistenz-Methoden würde man in der Praxis in eine eigene
|
||||
// Klasse auslagern.
|
||||
private void saveBankData() {
|
||||
ObjectOutputStream oos;
|
||||
try {
|
||||
oos = new ObjectOutputStream(new FileOutputStream("BankData.ser"));
|
||||
oos.writeObject(this);
|
||||
oos.close();
|
||||
} catch (FileNotFoundException e) {
|
||||
e.printStackTrace();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public static Bank loadBankData() {
|
||||
try {
|
||||
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("BankData.ser"));
|
||||
return (Bank) ois.readObject();
|
||||
} catch (IOException | ClassNotFoundException e) {
|
||||
System.err.println("Keine Daten gespeichert, initialisiere alle Daten neu.");
|
||||
}
|
||||
|
||||
return new Bank("Spaßkasse");
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,10 +3,12 @@ package bank;
|
|||
import java.util.Scanner;
|
||||
|
||||
public class BankUI {
|
||||
private static Bank bank = new Bank("Spaßkasse");
|
||||
private static Bank bank;
|
||||
private static Scanner kb = new Scanner(System.in);
|
||||
|
||||
public static void main(String[] args) {
|
||||
bank = Bank.loadBankData();
|
||||
|
||||
willkommen();
|
||||
hauptmenü();
|
||||
}
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
package bank;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Bankkonto {
|
||||
public class Bankkonto implements Serializable {
|
||||
// sog. Geheimnisprinzip, Attribute bleiben
|
||||
// privat, Zugriff erfolgt nur über Methoden
|
||||
private String name;
|
||||
|
|
Loading…
Reference in New Issue