From 353797a7a295f0936b0925774ab590eb064bf13e Mon Sep 17 00:00:00 2001 From: Oliver Hummel Date: Tue, 19 Dec 2023 17:06:45 +0100 Subject: [PATCH] =?UTF-8?q?Einfache=20Persistenz=20hinzugef=C3=BCgt.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Banksystem/src/bank/Bank.java | 37 +++++++++++++++++++++++++++++- Banksystem/src/bank/BankUI.java | 4 +++- Banksystem/src/bank/Bankkonto.java | 3 ++- 3 files changed, 41 insertions(+), 3 deletions(-) diff --git a/Banksystem/src/bank/Bank.java b/Banksystem/src/bank/Bank.java index ae0ae92..e08b1f8 100644 --- a/Banksystem/src/bank/Bank.java +++ b/Banksystem/src/bank/Bank.java @@ -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 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"); + } + } diff --git a/Banksystem/src/bank/BankUI.java b/Banksystem/src/bank/BankUI.java index 52afb62..d8eaeed 100644 --- a/Banksystem/src/bank/BankUI.java +++ b/Banksystem/src/bank/BankUI.java @@ -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ü(); } diff --git a/Banksystem/src/bank/Bankkonto.java b/Banksystem/src/bank/Bankkonto.java index 55f7231..978c330 100644 --- a/Banksystem/src/bank/Bankkonto.java +++ b/Banksystem/src/bank/Bankkonto.java @@ -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;