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