generated from hummel/Bank-System
Persistenz hinzugefügt.
parent
4e969f611f
commit
296180d37b
|
@ -5,10 +5,9 @@ import de.hs_mannheim.informatik.bank.ui.UI;
|
|||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) {
|
||||
public static void main(String[] args) throws Exception {
|
||||
Banksystem bs = new Banksystem("Spaßkasse Mannheim");
|
||||
UI ui = new UI(bs);
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -1,9 +1,10 @@
|
|||
package de.hs_mannheim.informatik.bank.domain;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Collection;
|
||||
import java.util.HashMap;
|
||||
|
||||
public class Bank {
|
||||
public class Bank implements Serializable {
|
||||
private String name;
|
||||
private HashMap<Integer, Konto> konten = new HashMap<>();
|
||||
|
||||
|
|
|
@ -1,6 +1,8 @@
|
|||
package de.hs_mannheim.informatik.bank.domain;
|
||||
|
||||
public class Girokonto extends Konto {
|
||||
import java.io.Serializable;
|
||||
|
||||
public class Girokonto extends Konto implements Serializable {
|
||||
|
||||
public Girokonto(String inhaber) {
|
||||
super(inhaber);
|
||||
|
|
|
@ -1,10 +1,23 @@
|
|||
package de.hs_mannheim.informatik.bank.domain;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
|
||||
public class Konto {
|
||||
import de.hs_mannheim.informatik.bank.infrastructure.Persistenz;
|
||||
|
||||
public class Konto implements Serializable {
|
||||
private static int kontozähler = 0;
|
||||
|
||||
static { // die bislang eleganteste Lösung, die mir eingefallen ist
|
||||
try {
|
||||
if (Persistenz.sindDatenGespeichert())
|
||||
kontozähler = Persistenz.ladeKontozähler();
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
private int nummer;
|
||||
private long stand = 0;
|
||||
private String inhaber;
|
||||
|
@ -64,4 +77,8 @@ public class Konto {
|
|||
return auflistung;
|
||||
}
|
||||
|
||||
public int getKontozähler() {
|
||||
return kontozähler;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,8 +1,9 @@
|
|||
package de.hs_mannheim.informatik.bank.domain;
|
||||
|
||||
import java.io.Serializable;
|
||||
import java.util.Date;
|
||||
|
||||
public class Kontobewegung {
|
||||
public class Kontobewegung implements Serializable {
|
||||
private long betrag;
|
||||
private Date datum;
|
||||
private String betreff;
|
||||
|
|
|
@ -5,15 +5,19 @@ import java.util.Collection;
|
|||
import de.hs_mannheim.informatik.bank.domain.Bank;
|
||||
import de.hs_mannheim.informatik.bank.domain.Girokonto;
|
||||
import de.hs_mannheim.informatik.bank.domain.Konto;
|
||||
import de.hs_mannheim.informatik.bank.infrastructure.Persistenz;
|
||||
|
||||
public class Banksystem {
|
||||
private Bank bank;
|
||||
|
||||
public Banksystem(String bankname) {
|
||||
this.bank = new Bank(bankname);
|
||||
public Banksystem(String bankname) throws Exception {
|
||||
if (Persistenz.sindDatenGespeichert())
|
||||
this.bank = (Bank) Persistenz.ladeBankDaten();
|
||||
else
|
||||
this.bank = new Bank(bankname);
|
||||
}
|
||||
|
||||
public int kontoAnlegen(String name, int auswahl) {
|
||||
public int kontoAnlegen(String name, int auswahl) throws Exception {
|
||||
Konto k;
|
||||
|
||||
if (auswahl == 1)
|
||||
|
@ -23,7 +27,8 @@ public class Banksystem {
|
|||
|
||||
bank.addKonto(k);
|
||||
|
||||
// System.out.println(k instanceof Girokonto);
|
||||
Persistenz.speichereBankDaten(this.bank);
|
||||
Persistenz.speichereKontozähler(k.getKontozähler());
|
||||
|
||||
return k.getKontonummer();
|
||||
}
|
||||
|
@ -44,16 +49,20 @@ public class Banksystem {
|
|||
return bank.getName();
|
||||
}
|
||||
|
||||
public long geldEinzahlen(int kontonummer, long betrag) {
|
||||
public long geldEinzahlen(int kontonummer, long betrag) throws Exception {
|
||||
Konto konto = bank.findeKonto(kontonummer);
|
||||
konto.einzahlen(betrag, "Einzahlung am Schalter", "Einzahlung", konto.getInhaber());
|
||||
|
||||
Persistenz.speichereBankDaten(this.bank);
|
||||
|
||||
return konto.getKontostand();
|
||||
}
|
||||
|
||||
public boolean geldAuszahlen(int kontonummer, long betrag) {
|
||||
public boolean geldAuszahlen(int kontonummer, long betrag) throws Exception {
|
||||
Konto konto = bank.findeKonto(kontonummer);
|
||||
|
||||
Persistenz.speichereBankDaten(this.bank);
|
||||
|
||||
return konto.auszahlen(betrag, "Auszahlung am Schalter", "Auszahlung", konto.getInhaber());
|
||||
}
|
||||
|
||||
|
|
|
@ -0,0 +1,47 @@
|
|||
package de.hs_mannheim.informatik.bank.infrastructure;
|
||||
|
||||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
public class Persistenz {
|
||||
private static final String BANK_DATEI = "bank.ser";
|
||||
private static final String ZÄHLER_DATEI = "zähler.ser";
|
||||
|
||||
public static boolean sindDatenGespeichert() {
|
||||
return new File(BANK_DATEI).exists();
|
||||
}
|
||||
|
||||
public static void speichereBankDaten(Object bank) throws Exception {
|
||||
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(BANK_DATEI));
|
||||
oos.writeObject(bank);
|
||||
oos.close();
|
||||
}
|
||||
|
||||
public static void speichereKontozähler(int kontozähler) throws IOException {
|
||||
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(ZÄHLER_DATEI));
|
||||
oos.writeInt(kontozähler);
|
||||
oos.close();
|
||||
}
|
||||
|
||||
public static Object ladeBankDaten() throws Exception {
|
||||
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(BANK_DATEI));
|
||||
Object bank = ois.readObject();
|
||||
ois.close();
|
||||
|
||||
return bank;
|
||||
}
|
||||
|
||||
public static int ladeKontozähler() throws FileNotFoundException, IOException {
|
||||
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(ZÄHLER_DATEI));
|
||||
int zähler = ois.readInt();
|
||||
ois.close();
|
||||
|
||||
return zähler;
|
||||
}
|
||||
|
||||
}
|
|
@ -35,16 +35,22 @@ public class UI {
|
|||
int input = Integer.parseInt(sc.nextLine());
|
||||
System.out.println();
|
||||
|
||||
switch(input) {
|
||||
try {
|
||||
switch(input) {
|
||||
case 1: kontenAnzeigen(); break;
|
||||
case 2: kontoAnlegen(); break;
|
||||
case 2:
|
||||
kontoAnlegen();
|
||||
break;
|
||||
case 3: geldEinzahlen(); break;
|
||||
case 4: geldAuszahlen(); break;
|
||||
case 5: kontoauszugDrucken(); break;
|
||||
case 6: überweisungBeauftragen(); break;
|
||||
case 9: break mainloop;
|
||||
}
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println(e.getLocalizedMessage());
|
||||
}
|
||||
System.out.println();
|
||||
}
|
||||
|
||||
|
@ -64,7 +70,7 @@ public class UI {
|
|||
}
|
||||
}
|
||||
|
||||
private void kontoAnlegen() {
|
||||
private void kontoAnlegen() throws Exception {
|
||||
System.out.println("Bitte den Namen des Kontoinhabers angeben: ");
|
||||
String name = sc.nextLine();
|
||||
|
||||
|
@ -75,7 +81,7 @@ public class UI {
|
|||
System.out.println("Konto mit der Nummer " + kontonummer + " neu angelegt.");
|
||||
}
|
||||
|
||||
private void geldEinzahlen() {
|
||||
private void geldEinzahlen() throws Exception {
|
||||
System.out.println("Geld einzahlen");
|
||||
System.out.print("Bitte die gewünschte Kontonummer eingeben: ");
|
||||
int kontonummer = Integer.parseInt(sc.nextLine());
|
||||
|
@ -90,7 +96,7 @@ public class UI {
|
|||
System.out.printf("Einzahlung erfolgreich, neuer Kontostand = %.2f Euro", (neuerKontostand / 100.0));
|
||||
}
|
||||
|
||||
private void geldAuszahlen() {
|
||||
private void geldAuszahlen() throws Exception {
|
||||
System.out.println("Geld auszahlen");
|
||||
System.out.print("Bitte die gewünschte Kontonummer eingeben: ");
|
||||
int kontonummer = Integer.parseInt(sc.nextLine());
|
||||
|
|
Loading…
Reference in New Issue