forked from hummel/Bank-System
Aufräumarbeiten (Refactorings)
* Kontozähler und Persistenz aufgeräumt * Domänenfunktionalität aus dem Banksystem in die Bank geschobenmain
parent
28158e7515
commit
666fd4909a
|
@ -7,13 +7,24 @@ import java.util.HashMap;
|
|||
public class Bank implements Serializable {
|
||||
private String name;
|
||||
private HashMap<Integer, Konto> konten = new HashMap<>();
|
||||
private int kontozähler;
|
||||
|
||||
public Bank(String name) {
|
||||
this.name = name;
|
||||
this.kontozähler = -1;
|
||||
}
|
||||
|
||||
public void addKonto(Konto k) {
|
||||
public int addKonto(String name, int auswahl) {
|
||||
Konto k;
|
||||
|
||||
if (auswahl == 1)
|
||||
k = new Konto(name, ++kontozähler);
|
||||
else
|
||||
k = new Girokonto(name, ++kontozähler);
|
||||
|
||||
konten.put(k.getKontonummer(), k);
|
||||
|
||||
return k.getKontonummer();
|
||||
}
|
||||
|
||||
public String getName() {
|
||||
|
|
|
@ -4,8 +4,8 @@ import java.io.Serializable;
|
|||
|
||||
public class Girokonto extends Konto implements Serializable {
|
||||
|
||||
public Girokonto(String inhaber) {
|
||||
super(inhaber);
|
||||
public Girokonto(String inhaber, int kontozähler) {
|
||||
super(inhaber, kontozähler);
|
||||
}
|
||||
|
||||
public boolean überweise(Girokonto ziel, long betrag, String zweck) {
|
||||
|
|
|
@ -1,31 +1,17 @@
|
|||
package de.hs_mannheim.informatik.bank.domain;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.util.ArrayList;
|
||||
|
||||
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;
|
||||
|
||||
private ArrayList<Kontobewegung> kontobewegungen;
|
||||
|
||||
public Konto(String inhaber) {
|
||||
nummer = 1000 + kontozähler++;
|
||||
public Konto(String inhaber, int kontozähler) {
|
||||
nummer = 1000 + kontozähler;
|
||||
this.inhaber = inhaber;
|
||||
|
||||
this.kontobewegungen = new ArrayList<>();
|
||||
|
@ -77,8 +63,4 @@ public class Konto implements Serializable {
|
|||
return auflistung;
|
||||
}
|
||||
|
||||
public int getKontozähler() {
|
||||
return kontozähler;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -7,23 +7,28 @@ import org.junit.jupiter.api.Test;
|
|||
class KontoTest {
|
||||
|
||||
@Test
|
||||
void testKonto() {
|
||||
Konto k = new Konto("Müller");
|
||||
void testKontoBasics() {
|
||||
Konto k = new Konto("Müller", 0);
|
||||
assertEquals("Müller", k.getInhaber());
|
||||
assertEquals(1000, k.getKontonummer());
|
||||
assertEquals(0, k.getKontostand());
|
||||
}
|
||||
|
||||
@Test
|
||||
void testKontoEinUndAuszahlung() {
|
||||
Konto k = new Konto("Müller", 0);
|
||||
Konto k2 = new Konto("Mayer", 1);
|
||||
|
||||
Konto k2 = new Konto("Mayer");
|
||||
assertEquals("Mayer", k2.getInhaber());
|
||||
assertNotEquals(k.getKontonummer(), k2.getKontonummer());
|
||||
assertEquals(1001, k2.getKontonummer());
|
||||
|
||||
k2.einzahlen(100, "Test", "Einzahlung", "JUnit");
|
||||
assertEquals(100, k2.getKontostand());
|
||||
assertTrue(k2.auszahlen(50, "Test", "Einzahlung", "JUnit"));
|
||||
|
||||
assertTrue(k2.auszahlen(50, "Test", "Auszahlung", "JUnit"));
|
||||
assertEquals(50, k2.getKontostand());
|
||||
|
||||
assertFalse(k2.auszahlen(500, "Test", "Einzahlung", "JUnit"));
|
||||
assertFalse(k2.auszahlen(500, "Test", "Auszahlung", "JUnit"));
|
||||
assertEquals(50, k2.getKontostand());
|
||||
}
|
||||
|
||||
|
|
|
@ -11,26 +11,18 @@ public class Banksystem {
|
|||
private Bank bank;
|
||||
|
||||
public Banksystem(String bankname) throws Exception {
|
||||
if (Persistenz.sindDatenGespeichert())
|
||||
this.bank = (Bank) Persistenz.ladeBankDaten();
|
||||
if (Persistenz.sindDatenGespeichert(bankname))
|
||||
this.bank = (Bank) Persistenz.ladeBankDaten(bankname);
|
||||
else
|
||||
this.bank = new Bank(bankname);
|
||||
}
|
||||
|
||||
public int kontoAnlegen(String name, int auswahl) throws Exception {
|
||||
Konto k;
|
||||
int kontonummer = bank.addKonto(name, auswahl);
|
||||
|
||||
if (auswahl == 1)
|
||||
k = new Konto(name);
|
||||
else
|
||||
k = new Girokonto(name);
|
||||
Persistenz.speichereBankDaten(this.bank, bank.getName());
|
||||
|
||||
bank.addKonto(k);
|
||||
|
||||
Persistenz.speichereBankDaten(this.bank);
|
||||
Persistenz.speichereKontozähler(k.getKontozähler());
|
||||
|
||||
return k.getKontonummer();
|
||||
return kontonummer;
|
||||
}
|
||||
|
||||
public String[] getKontenliste() {
|
||||
|
@ -53,7 +45,7 @@ public class Banksystem {
|
|||
Konto konto = bank.findeKonto(kontonummer);
|
||||
konto.einzahlen(betrag, "Einzahlung am Schalter", "Einzahlung", konto.getInhaber());
|
||||
|
||||
Persistenz.speichereBankDaten(this.bank);
|
||||
Persistenz.speichereBankDaten(this.bank, bank.getName());
|
||||
|
||||
return konto.getKontostand();
|
||||
}
|
||||
|
@ -61,7 +53,7 @@ public class Banksystem {
|
|||
public boolean geldAuszahlen(int kontonummer, long betrag) throws Exception {
|
||||
Konto konto = bank.findeKonto(kontonummer);
|
||||
|
||||
Persistenz.speichereBankDaten(this.bank);
|
||||
Persistenz.speichereBankDaten(this.bank, bank.getName());
|
||||
|
||||
return konto.auszahlen(betrag, "Auszahlung am Schalter", "Auszahlung", konto.getInhaber());
|
||||
}
|
||||
|
|
|
@ -0,0 +1,19 @@
|
|||
package de.hs_mannheim.informatik.bank.facade;
|
||||
|
||||
import static org.junit.jupiter.api.Assertions.assertEquals;
|
||||
import static org.junit.jupiter.api.Assertions.assertNotNull;
|
||||
|
||||
import org.junit.jupiter.api.Test;
|
||||
|
||||
class SystemTest {
|
||||
|
||||
@Test
|
||||
void smokeTest() throws Exception {
|
||||
Banksystem bs = new Banksystem("Testsystem");
|
||||
|
||||
assertNotNull(bs);
|
||||
assertEquals(0, bs.getKontenliste().length);
|
||||
assertEquals("Testsystem", bs.getBankname());
|
||||
}
|
||||
|
||||
}
|
|
@ -2,46 +2,29 @@ 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";
|
||||
private static final String BANK_DATEI = "-bank-data.ser";
|
||||
|
||||
public static boolean sindDatenGespeichert() {
|
||||
return new File(BANK_DATEI).exists();
|
||||
public static boolean sindDatenGespeichert(String name) {
|
||||
return new File(name + BANK_DATEI).exists();
|
||||
}
|
||||
|
||||
public static void speichereBankDaten(Object bank) throws Exception {
|
||||
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(BANK_DATEI));
|
||||
public static void speichereBankDaten(Object bank, String name) throws Exception {
|
||||
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(name + 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));
|
||||
public static Object ladeBankDaten(String name) throws Exception {
|
||||
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(name + 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;
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue