Compare commits
2 Commits
main
...
guiExample
Author | SHA1 | Date |
---|---|---|
Oliver Hummel | 3e676fa6d0 | |
Oliver Hummel | 506e832323 |
|
@ -1,13 +1,21 @@
|
|||
package de.hs_mannheim.informatik.bank;
|
||||
|
||||
import de.hs_mannheim.informatik.bank.facade.Banksystem;
|
||||
import de.hs_mannheim.informatik.bank.ui.UI;
|
||||
import de.hs_mannheim.informatik.bank.gui.KontoAnlegenFrame;
|
||||
import de.hs_mannheim.informatik.bank.gui.KontoListingFrame;
|
||||
|
||||
public class Main {
|
||||
|
||||
public static void main(String[] args) throws Exception {
|
||||
Banksystem bs = new Banksystem("Spaßkasse Mannheim");
|
||||
UI ui = new UI(bs);
|
||||
// UI ui = new UI(bs);
|
||||
|
||||
KontoAnlegenFrame kaf = new KontoAnlegenFrame(bs);
|
||||
kaf.setVisible(true);
|
||||
|
||||
|
||||
KontoListingFrame klf = new KontoListingFrame(bs);
|
||||
klf.setVisible(true);
|
||||
}
|
||||
|
||||
}
|
|
@ -73,4 +73,10 @@ public class Konto implements Serializable {
|
|||
return saldo;
|
||||
}
|
||||
|
||||
public void rollback() {
|
||||
Kontobewegung kbw = kontobewegungen.get(kontobewegungen.size()-1);
|
||||
stand += kbw.getBetrag() *-1;
|
||||
kontobewegungen.remove(kontobewegungen.size()-1);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package de.hs_mannheim.informatik.bank.facade;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Collection;
|
||||
|
||||
import de.hs_mannheim.informatik.bank.domain.Bank;
|
||||
|
@ -10,14 +11,21 @@ import de.hs_mannheim.informatik.bank.infrastructure.Persistenz;
|
|||
public class Banksystem {
|
||||
private Bank bank;
|
||||
|
||||
public Banksystem(String bankname) throws Exception {
|
||||
public Banksystem(String bankname) {
|
||||
if (Persistenz.sindDatenGespeichert(bankname))
|
||||
try {
|
||||
this.bank = (Bank) Persistenz.ladeBankDaten(bankname);
|
||||
else
|
||||
|
||||
return;
|
||||
} catch (ClassNotFoundException | IOException e) {
|
||||
// Hier loggen wir die Exception und starten einfach mit einem leeren System.
|
||||
System.err.println("Persistierte Daten konnten nicht geladen werden.");
|
||||
}
|
||||
|
||||
this.bank = new Bank(bankname);
|
||||
}
|
||||
|
||||
public int kontoAnlegen(String name, int auswahl) throws Exception {
|
||||
public int kontoAnlegen(String name, int auswahl) throws IOException {
|
||||
int kontonummer = bank.addKonto(name, auswahl);
|
||||
|
||||
Persistenz.speichereBankDaten(this.bank, bank.getName());
|
||||
|
@ -41,16 +49,21 @@ public class Banksystem {
|
|||
return bank.getName();
|
||||
}
|
||||
|
||||
public long geldEinzahlen(int kontonummer, long betrag) throws Exception {
|
||||
public long geldEinzahlen(int kontonummer, long betrag) throws IOException {
|
||||
Konto konto = bank.findeKonto(kontonummer);
|
||||
konto.einzahlen(betrag, "Einzahlung am Schalter", "Einzahlung", konto.getInhaber());
|
||||
|
||||
try {
|
||||
Persistenz.speichereBankDaten(this.bank, bank.getName());
|
||||
} catch (IOException ioe) {
|
||||
konto.rollback();
|
||||
throw ioe;
|
||||
}
|
||||
|
||||
return konto.getKontostand();
|
||||
}
|
||||
|
||||
public boolean geldAuszahlen(int kontonummer, long betrag) throws Exception {
|
||||
public boolean geldAuszahlen(int kontonummer, long betrag) throws IOException {
|
||||
Konto konto = bank.findeKonto(kontonummer);
|
||||
|
||||
boolean erg = konto.auszahlen(betrag, "Auszahlung am Schalter", "Auszahlung", konto.getInhaber());
|
||||
|
|
|
@ -0,0 +1,62 @@
|
|||
package de.hs_mannheim.informatik.bank.gui;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
import de.hs_mannheim.informatik.bank.facade.Banksystem;
|
||||
|
||||
public class KontoAnlegenFrame extends JFrame implements ActionListener {
|
||||
private JTextField eingabe;
|
||||
private JTextArea ausgabe;
|
||||
private JButton ok;
|
||||
|
||||
private Banksystem bs;
|
||||
|
||||
public KontoAnlegenFrame(Banksystem bs) {
|
||||
this.bs = bs;
|
||||
|
||||
this.setTitle(bs.getBankname());
|
||||
this.setSize(300, 300);
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
eingabe = new JTextField(20);
|
||||
|
||||
ausgabe = new JTextArea(5, 20);
|
||||
ausgabe.setLineWrap(true);
|
||||
ausgabe.setWrapStyleWord(true);
|
||||
|
||||
// Die Ausgabe automatisch scrollbar machen, wenn Sie nicht
|
||||
// mehr auf den Bildschirm passen sollte.
|
||||
JScrollPane sp = new JScrollPane(ausgabe);
|
||||
|
||||
ok = new JButton("Konto anlegen");
|
||||
ok.addActionListener(this);
|
||||
|
||||
// Container cp = this.getContentPane();
|
||||
this.add(eingabe, "North");
|
||||
this.add(sp, "Center");
|
||||
this.add(ok, "South");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
System.out.println("Konto anlegen...");
|
||||
|
||||
try {
|
||||
int knr = bs.kontoAnlegen(eingabe.getText(), 1);
|
||||
|
||||
this.ausgabe.append("Neues Konto angelegt: " + knr + "\n");
|
||||
} catch (IOException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -0,0 +1,62 @@
|
|||
package de.hs_mannheim.informatik.bank.gui;
|
||||
|
||||
import java.awt.event.ActionEvent;
|
||||
import java.awt.event.ActionListener;
|
||||
import java.io.IOException;
|
||||
|
||||
import javax.swing.JButton;
|
||||
import javax.swing.JFrame;
|
||||
import javax.swing.JScrollPane;
|
||||
import javax.swing.JTextArea;
|
||||
import javax.swing.JTextField;
|
||||
|
||||
import de.hs_mannheim.informatik.bank.facade.Banksystem;
|
||||
|
||||
public class KontoListingFrame extends JFrame implements ActionListener {
|
||||
private JTextField eingabe;
|
||||
private JTextArea ausgabe;
|
||||
private JButton ok;
|
||||
|
||||
private Banksystem bs;
|
||||
|
||||
public KontoListingFrame(Banksystem bs) {
|
||||
this.bs = bs;
|
||||
|
||||
this.setTitle(bs.getBankname());
|
||||
this.setSize(300, 300);
|
||||
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||
|
||||
eingabe = new JTextField(20);
|
||||
|
||||
ausgabe = new JTextArea(5, 20);
|
||||
ausgabe.setLineWrap(true);
|
||||
ausgabe.setWrapStyleWord(true);
|
||||
|
||||
// Die Ausgabe automatisch scrollbar machen, wenn Sie nicht
|
||||
// mehr auf den Bildschirm passen sollte.
|
||||
JScrollPane sp = new JScrollPane(ausgabe);
|
||||
|
||||
ok = new JButton("Kontenliste abrufen");
|
||||
ok.addActionListener(this);
|
||||
|
||||
// Container cp = this.getContentPane();
|
||||
// this.add(eingabe, "North");
|
||||
this.add(sp, "Center");
|
||||
this.add(ok, "South");
|
||||
}
|
||||
|
||||
@Override
|
||||
public void actionPerformed(ActionEvent e) {
|
||||
System.out.println("Kontenliste abrufen...");
|
||||
|
||||
ausgabe.setText("");
|
||||
String[] konten = bs.getKontenliste();
|
||||
|
||||
for (String konto : konten) {
|
||||
ausgabe.append(konto + "\n");
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
}
|
|
@ -3,6 +3,7 @@ package de.hs_mannheim.informatik.bank.infrastructure;
|
|||
import java.io.File;
|
||||
import java.io.FileInputStream;
|
||||
import java.io.FileOutputStream;
|
||||
import java.io.IOException;
|
||||
import java.io.ObjectInputStream;
|
||||
import java.io.ObjectOutputStream;
|
||||
|
||||
|
@ -13,13 +14,13 @@ public class Persistenz {
|
|||
return new File(name + BANK_DATEI).exists();
|
||||
}
|
||||
|
||||
public static void speichereBankDaten(Object bank, String name) throws Exception {
|
||||
public static void speichereBankDaten(Object bank, String name) throws IOException {
|
||||
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(name + BANK_DATEI));
|
||||
oos.writeObject(bank);
|
||||
oos.close();
|
||||
}
|
||||
|
||||
public static Object ladeBankDaten(String name) throws Exception {
|
||||
public static Object ladeBankDaten(String name) throws IOException, ClassNotFoundException {
|
||||
ObjectInputStream ois = new ObjectInputStream(new FileInputStream(name + BANK_DATEI));
|
||||
Object bank = ois.readObject();
|
||||
ois.close();
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
package de.hs_mannheim.informatik.bank.ui;
|
||||
package de.hs_mannheim.informatik.bank.tui;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.util.Scanner;
|
||||
|
||||
import de.hs_mannheim.informatik.bank.facade.Banksystem;
|
||||
|
@ -49,7 +50,6 @@ public class UI {
|
|||
case 7: saldoAbfragen(); break;
|
||||
case 9: break mainloop;
|
||||
}
|
||||
|
||||
} catch (Exception e) {
|
||||
System.err.println(e.getLocalizedMessage());
|
||||
}
|
||||
|
@ -72,7 +72,7 @@ public class UI {
|
|||
}
|
||||
}
|
||||
|
||||
private void kontoAnlegen() throws Exception {
|
||||
private void kontoAnlegen() throws IOException {
|
||||
System.out.println("Bitte den Namen des Kontoinhabers angeben: ");
|
||||
String name = sc.nextLine();
|
||||
|
||||
|
@ -83,22 +83,40 @@ public class UI {
|
|||
System.out.println("Konto mit der Nummer " + kontonummer + " neu angelegt.");
|
||||
}
|
||||
|
||||
private void geldEinzahlen() throws Exception {
|
||||
private void geldEinzahlen() throws IOException {
|
||||
int kontonummer = 0;
|
||||
double betrag = 0;
|
||||
boolean ok = false;
|
||||
|
||||
do {
|
||||
System.out.println("Geld einzahlen");
|
||||
System.out.print("Bitte die gewünschte Kontonummer eingeben: ");
|
||||
int kontonummer = Integer.parseInt(sc.nextLine());
|
||||
|
||||
try {
|
||||
kontonummer = Integer.parseInt(sc.nextLine());
|
||||
// optional prüfen, ob Konto existiert
|
||||
} catch (NumberFormatException nfe) {
|
||||
System.err.println("Kontonummer muss eine Zahl sein, bitte Eingabe wiederholen!");
|
||||
continue;
|
||||
}
|
||||
|
||||
System.out.print("Bitte den gewünschten Betrag eingeben: ");
|
||||
double betrag = Double.parseDouble(sc.nextLine());
|
||||
try {
|
||||
betrag = Double.parseDouble(sc.nextLine());
|
||||
} catch (NumberFormatException nfe) {
|
||||
System.err.println("Betrag muss eine Kommazahl sein, bitte Eingabe wiederholen!");
|
||||
continue;
|
||||
}
|
||||
|
||||
ok = true;
|
||||
} while(!ok);
|
||||
|
||||
long neuerKontostand = bs.geldEinzahlen(kontonummer, (long)betrag * 100);
|
||||
|
||||
System.out.printf("Einzahlung erfolgreich, neuer Kontostand = %.2f Euro", (neuerKontostand / 100.0));
|
||||
}
|
||||
|
||||
private void geldAuszahlen() throws Exception {
|
||||
private void geldAuszahlen() throws IOException {
|
||||
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