diff --git a/src/de/hs_mannheim/informatik/bank/Main.java b/src/de/hs_mannheim/informatik/bank/Main.java index 4b54e6f..511f3ec 100644 --- a/src/de/hs_mannheim/informatik/bank/Main.java +++ b/src/de/hs_mannheim/informatik/bank/Main.java @@ -3,10 +3,12 @@ package de.hs_mannheim.informatik.bank; import de.hs_mannheim.informatik.bank.UI.UI; import de.hs_mannheim.informatik.bank.facade.Banksystem; +import persistance.Read; public class Main { public static void main(String[] args) { + Read r = new Read(); Banksystem bs = new Banksystem("Spaßkasse Mannheim"); UI ui = new UI(bs); } diff --git a/src/de/hs_mannheim/informatik/bank/UI/UI.java b/src/de/hs_mannheim/informatik/bank/UI/UI.java index 9b52c3c..3ad4939 100644 --- a/src/de/hs_mannheim/informatik/bank/UI/UI.java +++ b/src/de/hs_mannheim/informatik/bank/UI/UI.java @@ -17,7 +17,7 @@ public class UI { hauptmenu(); } - private void hauptmenu() { + private void hauptmenu() { System.out.println("Willkommen bei der " + bs.getBankname() + "!"); mainloop: @@ -31,6 +31,8 @@ public class UI { System.out.println("4 -> Auszahlen"); System.out.println("5 -> Kontostand Suchen"); System.out.println("6 -> Verlauf Zeigen"); + System.out.println("7 -> Speichern"); + System.out.println("8 -> laden"); System.out.println("9 -> Beenden"); System.out.println(); @@ -43,8 +45,13 @@ public class UI { case 2: kontoAnlegen(); break; case 3: einzahlen(); break; case 4: ausziehen(); break; - case 5: standSuchen(); break; + case 5: standSuchen(); break; case 6: zeigeVerlauf(); break; + case 7: speichern(); break; + case 8: laden(); break; + + + case 9: break mainloop; } @@ -128,5 +135,32 @@ public class UI { } + private void speichern() + { + if(bs.speichern()) + { + System.out.println("Date gespeichert"); + return; + }else + { + System.out.println("Ein Fehler ist aufgetreten"); + } + + } + private void laden() + { + try { + if(bs.laden()) + { + System.out.println("Data gespeichert"); + return; + } + } + catch(ClassNotFoundException a) + { + System.out.println("Ein Fehler ist aufgetreten"); + + } + } } diff --git a/src/de/hs_mannheim/informatik/bank/domain/Bank.java b/src/de/hs_mannheim/informatik/bank/domain/Bank.java index fa1f6c1..7f810d3 100644 --- a/src/de/hs_mannheim/informatik/bank/domain/Bank.java +++ b/src/de/hs_mannheim/informatik/bank/domain/Bank.java @@ -5,10 +5,23 @@ package de.hs_mannheim.informatik.bank.domain; import java.util.Collection; import java.util.HashMap; +import persistance.Read; +import persistance.Write; + public class Bank { private String name; private HashMap konten = new HashMap<>(); + public HashMap getKonten() { + return konten; + } + + + public void setKonten(HashMap konten) { + this.konten = konten; + } + + public Bank(String name) { this.name = name; } @@ -25,6 +38,13 @@ public class Bank { return konten.values(); } + public boolean save() + { + Write w = new Write(); + return w.saveFile(konten); + + } + } diff --git a/src/de/hs_mannheim/informatik/bank/domain/Konto.java b/src/de/hs_mannheim/informatik/bank/domain/Konto.java index 65502cd..3baab30 100644 --- a/src/de/hs_mannheim/informatik/bank/domain/Konto.java +++ b/src/de/hs_mannheim/informatik/bank/domain/Konto.java @@ -1,9 +1,10 @@ package de.hs_mannheim.informatik.bank.domain; +import java.io.Serializable; import java.util.ArrayList; import java.util.List; -public class Konto { +public class Konto implements Serializable{ private static int kontozähler = 0; private List history = new ArrayList(); diff --git a/src/de/hs_mannheim/informatik/bank/facade/Banksystem.java b/src/de/hs_mannheim/informatik/bank/facade/Banksystem.java index 9fef3ea..513ae65 100644 --- a/src/de/hs_mannheim/informatik/bank/facade/Banksystem.java +++ b/src/de/hs_mannheim/informatik/bank/facade/Banksystem.java @@ -2,12 +2,15 @@ package de.hs_mannheim.informatik.bank.facade; -import java.util.ArrayList; +import java.io.IOException; import java.util.Collection; +import java.util.HashMap; import java.util.List; import de.hs_mannheim.informatik.bank.domain.Bank; import de.hs_mannheim.informatik.bank.domain.Konto; +import persistance.Read; +import persistance.Write; public class Banksystem { private Bank bank; @@ -100,5 +103,21 @@ public class Banksystem { return null; } + public boolean laden() throws ClassNotFoundException + { + Read r = new Read(); + HashMap x = r.load(); + if(x == null) return false; + bank.setKonten(r.load()); + return true; + + } + public boolean speichern() + { + Write w = new Write(); + return w.saveFile(bank.getKonten()); + + + } } diff --git a/src/persistance/Read.java b/src/persistance/Read.java new file mode 100644 index 0000000..a64407c --- /dev/null +++ b/src/persistance/Read.java @@ -0,0 +1,29 @@ +package persistance; + +import java.io.FileInputStream; +import java.io.FileNotFoundException; +import java.io.IOException; +import java.io.ObjectInputStream; +import java.util.HashMap; + +import de.hs_mannheim.informatik.bank.domain.Konto; + +public class Read { + + public HashMap load() throws ClassNotFoundException + { + try { + ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.ser")); + HashMap map = (HashMap) ois.readObject(); + ois.close(); + return map; + } catch(IOException e ) + { + return null; + } + + + + } + +} diff --git a/src/persistance/Write.java b/src/persistance/Write.java new file mode 100644 index 0000000..8b1602b --- /dev/null +++ b/src/persistance/Write.java @@ -0,0 +1,35 @@ +package persistance; + +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.ObjectOutputStream; +import java.util.HashMap; + +import de.hs_mannheim.informatik.bank.domain.Konto; + +public class Write { + + public boolean saveFile(HashMap data) + { + + try{ + FileOutputStream fos =new FileOutputStream("object.ser"); + ObjectOutputStream oos = new ObjectOutputStream(fos); + oos.writeObject(data); + fos.close(); + oos.close(); + + return true; + } + catch(IOException a) + { + System.out.println("tff"); + return false; + + } + + + + } +}