Sehr geehrtes Forum,
Ich habe ein Programm geschrieben und möchte euch bitten mir Verbesserungsvorschläge/Kritik zu geben.
Hier ist das Programm:
Deutsch und Englisch ist gemischt, da ich die BannkKonto Klasse aus einem anderen Projekt übernommen habe.
Mit freundlichen Grüßen,
thelegend27
Ich habe ein Programm geschrieben und möchte euch bitten mir Verbesserungsvorschläge/Kritik zu geben.
Hier ist das Programm:
Java:
/*
* To change this license header, choose License Headers in Project Properties.
* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package finalgui;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Toolkit;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.nio.file.Paths;
import javax.swing.BoxLayout;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;
import java.io.*;
import java.util.Scanner;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.swing.JOptionPane;
/**
*
* @author Ahmet
*/
class MainFrame extends JFrame implements ActionListener {
String name;
String iban;
String pin;
int currentAccount;
int currentBalance;
int regIndex = 0;
String fileName;
BankKonto[] accounts = new BankKonto[10000];
JPanel homePan;
JButton close;
JLabel homeHeader;
JButton login;
JButton register;
JPanel registerPan;
JButton leaveReg;
JLabel registerHeader;
JTextField nameIn;
JButton tryRegister;
JPanel loginPan;
JButton leaveLog;
JLabel loginHeader;
JLabel ibanPrompt;
JTextField ibanIn;
JLabel pinPrompt;
JTextField pinIn;
JButton tryLogin;
JPanel loggedInPan;
JButton leaveLogged;
JLabel loggedHeader;
JButton openCheck;
JButton openWithdraw;
JButton openDeposit;
JButton openTransfer;
JPanel checkPan;
JButton leaveCheck;
JLabel checkHeader;
JTextField balanceOut;
JPanel withdrawPan;
JButton leaveWithdraw;
JLabel withdrawPrompt;
JTextField withdrawIn;
JButton tryWithdraw;
JPanel depositPan;
JButton leaveDeposit;
JLabel depositHeader;
JTextField depositIn;
JButton tryDeposit;
JPanel transferPan;
JButton leaveTransfer;
JLabel transferHeader;
JLabel transIbanPrompt;
JTextField transIbanIn;
JLabel transAmountPrompt;
JTextField transAmount;
JButton tryTransfer;
JPanel mainPan;
MainFrame() throws IOException {
regIndex = initializeRegIndex();
accounts = initializeAccounts(accounts);
setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setTitle("Die Bank");
addWindowListener(new WindowAdapter() {
public void windowClosing(WindowEvent e) {
try {
logAccount(accounts);
logRegIndex(regIndex);
} catch (IOException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
}
});
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
setSize(screenSize);
close = new JButton("Beenden");
close.addActionListener(this);
close.setActionCommand("close");
homeHeader = new JLabel("Wilkommen in der Bank");
login = new JButton("Einloggen");
login.addActionListener(this);
login.setActionCommand("login");
register = new JButton("Registrieren");
register.addActionListener(this);
register.setActionCommand("register");
homePan = new JPanel();
homePan.add(close);
homePan.add(homeHeader);
homePan.add(login);
homePan.add(register);
homePan.setVisible(true);
leaveReg = new JButton("<=");
leaveReg.addActionListener(this);
leaveReg.setActionCommand("leaveReg");
registerHeader = new JLabel("Geben sie Ihren Namen ein, um sich zu registrieren");
nameIn = new JTextField(15);
tryRegister = new JButton("registrieren");
tryRegister.addActionListener(this);
tryRegister.setActionCommand("tryRegister");
registerPan = new JPanel();
registerPan.add(leaveReg);
registerPan.add(registerHeader);
registerPan.add(nameIn);
registerPan.add(tryRegister);
registerPan.setVisible(false);
leaveLog = new JButton("<=");
leaveLog.addActionListener(this);
leaveLog.setActionCommand("leaveLog");
loginHeader = new JLabel("Geben Sie ihre Kontonummer und PIN ein um sich anzumelden");
ibanPrompt = new JLabel("Kontonummer:");
ibanIn = new JTextField(6);
pinPrompt = new JLabel("PIN: ");
pinIn = new JTextField(6);
tryLogin = new JButton("einloggen");
tryLogin.addActionListener(this);
tryLogin.setActionCommand("tryLogin");
loginPan = new JPanel();
loginPan.add(leaveLog);
loginPan.add(loginHeader);
loginPan.add(ibanPrompt);
loginPan.add(ibanIn);
loginPan.add(pinPrompt);
loginPan.add(pinIn);
loginPan.add(tryLogin);
loginPan.setVisible(false);
leaveLogged = new JButton("<=");
leaveLogged.addActionListener(this);
leaveLogged.setActionCommand("leaveLogged");
loggedHeader = new JLabel("Wilkommen");
openCheck = new JButton("Kontostand einsehen");
openCheck.addActionListener(this);
openCheck.setActionCommand("openCheck");
openWithdraw = new JButton("Betrag auszahlen");
openWithdraw.addActionListener(this);
openWithdraw.setActionCommand("openWithdraw");
openDeposit = new JButton("Betrag einzahlen");
openDeposit.addActionListener(this);
openDeposit.setActionCommand("openDeposit");
openTransfer = new JButton("Betrag überweisen");
openTransfer.addActionListener(this);
openTransfer.setActionCommand("openTransfer");
loggedInPan = new JPanel();
loggedInPan.add(leaveLogged);
loggedInPan.add(loggedHeader);
loggedInPan.add(openCheck);
loggedInPan.add(openWithdraw);
loggedInPan.add(openDeposit);
loggedInPan.add(openTransfer);
loggedInPan.setVisible(false);
leaveCheck = new JButton("<=");
leaveCheck.addActionListener(this);
leaveCheck.setActionCommand("leaveCheck");
checkHeader = new JLabel("Ihr Kontostand beträgt: ");
balanceOut = new JTextField(15);
balanceOut.setEditable(false);
checkPan = new JPanel();
checkPan.add(leaveCheck);
checkPan.add(checkHeader);
checkPan.add(balanceOut);
checkPan.setVisible(false);
leaveWithdraw = new JButton("<=");
leaveWithdraw.addActionListener(this);
leaveWithdraw.setActionCommand("leaveWithdraw");
withdrawPrompt = new JLabel("Geben Sie den Betrag an den Sie abheben möchten!");
withdrawIn = new JTextField(10);
tryWithdraw = new JButton("abheben");
tryWithdraw.addActionListener(this);
tryWithdraw.setActionCommand("tryWithdraw");
withdrawPan = new JPanel();
withdrawPan.add(leaveWithdraw);
withdrawPan.add(withdrawPrompt);
withdrawPan.add(withdrawIn);
withdrawPan.add(tryWithdraw);
withdrawPan.setVisible(false);
leaveDeposit = new JButton("<=");
leaveDeposit.addActionListener(this);
leaveDeposit.setActionCommand("leaveDeposit");
depositHeader = new JLabel("Geben Sie den Betrag an, den Sie einzahlen möchten!");
depositIn = new JTextField(10);
tryDeposit = new JButton("überweisen");
tryDeposit.addActionListener(this);
tryDeposit.setActionCommand("tryDeposit");
depositPan = new JPanel();
depositPan.add(leaveDeposit);
depositPan.add(depositHeader);
depositPan.add(depositIn);
depositPan.add(tryDeposit);
depositPan.setVisible(false);
leaveTransfer = new JButton("<=");
leaveTransfer.addActionListener(this);
leaveTransfer.setActionCommand("leaveTransfer");
transferHeader = new JLabel("Geben sie den Empfänger und den Betrag der Überweisung an!");
transIbanPrompt = new JLabel("Kontonummer des Empfängers: ");
transIbanIn = new JTextField(6);
transAmountPrompt = new JLabel("Betrag: ");
transAmount = new JTextField(6);
tryTransfer = new JButton("überweisen");
tryTransfer.addActionListener(this);
tryTransfer.setActionCommand("tryTransfer");
transferPan = new JPanel();
transferPan.add(leaveTransfer);
transferPan.add(transferHeader);
transferPan.add(transIbanPrompt);
transferPan.add(transIbanIn);
transferPan.add(transAmountPrompt);
transferPan.add(transAmount);
transferPan.add(tryTransfer);
transferPan.setVisible(false);
mainPan = new JPanel();
mainPan.add(homePan);
mainPan.add(registerPan);
mainPan.add(loginPan);
mainPan.add(loggedInPan);
mainPan.add(checkPan);
mainPan.add(withdrawPan);
mainPan.add(depositPan);
mainPan.add(transferPan);
add(mainPan);
}
public void actionPerformed(ActionEvent evt) {
if (evt.getActionCommand().equals("close")) {
try {
logAccount(accounts);
} catch (IOException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
try {
logRegIndex(regIndex);
} catch (IOException ex) {
Logger.getLogger(MainFrame.class.getName()).log(Level.SEVERE, null, ex);
}
System.exit(0);
} else if (evt.getActionCommand().equals("login")) {
homePan.setVisible(false);
loginPan.setVisible(true);
validate();
} else if (evt.getActionCommand().equals("register")) {
homePan.setVisible(false);
registerPan.setVisible(true);
validate();
} else if (evt.getActionCommand().equals("openCheck")) {
balanceOut.setText(Integer.toString(accounts[currentAccount].gibKontoStand()));
loggedInPan.setVisible(false);
checkPan.setVisible(true);
validate();
} else if (evt.getActionCommand().equals("openWithdraw")) {
loggedInPan.setVisible(false);
withdrawPan.setVisible(true);
validate();
} else if (evt.getActionCommand().equals("openDeposit")) {
loggedInPan.setVisible(false);
withdrawPan.setVisible(true);
validate();
} else if (evt.getActionCommand().equals("openTransfer")) {
loggedInPan.setVisible(false);
transferPan.setVisible(true);
validate();
} else if (evt.getActionCommand().equals("tryRegister")) {
if (nameIn.getText().isEmpty()) {
int input = JOptionPane.showOptionDialog(null, "Geben Sie bitte Ihren Namen ein!", "The title", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);
} else if (!(nameIn.getText().isEmpty())) {
iban = String.valueOf((int) (Math.random() * 10000 + 1000));
pin = String.valueOf((int) (Math.random() * 10000 + 1000));
name = nameIn.getText().trim();
accounts[regIndex] = createAccount();
int input = JOptionPane.showOptionDialog(null, "Ihr Konto wurde erfolgreich erstellt \n Ihre Kontonummer: " + accounts[regIndex].gibKontoNummer() + "\n Ihre PIN: " + accounts[regIndex].gibKontoPIN(), "The title", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);
regIndex++;
if (input == JOptionPane.OK_OPTION) {
registerPan.setVisible(false);
homePan.setVisible(true);
nameIn.setText("");
validate();
}
}
} else if (evt.getActionCommand().equals("tryLogin")) {
if (checkInput(ibanIn.getText()) && checkInput(pinIn.getText())) {
if (ibanIn.getText().isEmpty() || pinIn.getText().isEmpty()) {
int input = JOptionPane.showOptionDialog(null, "Geben Sie bitte Ihre Daten ein!", "The title", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);
}
boolean checkLogin = false;
for (int i = 0; i <= accounts.length - 1; i++) {
if (accounts[i] == null) {
checkLogin = false;
} else if (((ibanIn.getText().trim().equals(accounts[i].gibKontoNummer())) && (pinIn.getText().trim().equals(accounts[i].gibKontoPIN())))) {
currentAccount = i;
checkLogin = true;
currentBalance = accounts[currentAccount].gibKontoStand();
break;
}
}
if (checkLogin) {
loginPan.setVisible(false);
loggedInPan.setVisible(true);
validate();
}
}
} else if (evt.getActionCommand().equals("tryWithdraw")) {
if (checkInput(withdrawIn.getText())) {
if (withdrawIn.getText().isEmpty()) {
int input = JOptionPane.showOptionDialog(null, "Geben Sie bitte Ihre Daten ein!", "The title", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);
}
int amountChunk = Integer.parseInt(withdrawIn.getText());
currentBalance = accounts[currentAccount].gibKontoStand();
accounts[currentAccount].hebeGeldAb(amountChunk);
int input = JOptionPane.showOptionDialog(null, amountChunk + " abgehoben", "The title", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);
if (input == JOptionPane.OK_OPTION) {
withdrawPan.setVisible(false);
loggedInPan.setVisible(true);
withdrawIn.setText("");
validate();
}
}
} else if (evt.getActionCommand().equals("tryDeposit")) {
if (checkInput(depositIn.getText())) {
if (depositIn.getText().isEmpty()) {
int input = JOptionPane.showOptionDialog(null, "Geben Sie bitte Ihre Daten ein!", "The title", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);
}
int amountChunk = Integer.parseInt(depositIn.getText());
accounts[currentAccount].zahleGeldEin(amountChunk);
int input = JOptionPane.showOptionDialog(null, amountChunk + " eingezahlt", "The title", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);
if (input == JOptionPane.OK_OPTION) {
depositPan.setVisible(false);
loggedInPan.setVisible(true);
withdrawIn.setText("");
validate();
}
}
} else if (evt.getActionCommand().equals("tryTransfer")) {
if (checkInput(transAmount.getText()) && checkInput(transIbanIn.getText())) {
if (transAmount.getText().isEmpty() || transIbanIn.getText().isEmpty()) {
int input = JOptionPane.showOptionDialog(null, "Geben Sie bitte Ihre Daten ein!", "The title", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);
}
int result = findAccount(accounts, transIbanIn.getText());
if (result <= -2) {
int input = JOptionPane.showOptionDialog(null, "Etwas sehr schlimmes ist passiert", "The title", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);
if (input == JOptionPane.OK_OPTION) {
transferPan.setVisible(false);
loggedInPan.setVisible(true);
transIbanIn.setText("");
transAmount.setText("");
validate();
}
} else if (result == -1) {
int input = JOptionPane.showOptionDialog(null, "ÜberweiseungsKonto nicht gefunden", "The title", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);
} else if (result >= 0) {
accounts[currentAccount].hebeGeldAb(Integer.parseInt(transAmount.getText()));
accounts[result].zahleGeldEin(Integer.parseInt(transAmount.getText()));
int input = JOptionPane.showOptionDialog(null, "Überweisung erfolgreich", "The title", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);
if (input == JOptionPane.OK_OPTION) {
transferPan.setVisible(false);
loggedInPan.setVisible(true);
transAmount.setText("");
transIbanIn.setText("");
validate();
}
}
}
} else if (evt.getActionCommand().equals("leaveReg")) {
if (evt.getActionCommand().equals("leaveReg")) {
registerPan.setVisible(false);
}
homePan.setVisible(true);
validate();
} else if (evt.getActionCommand().equals("leaveLog")) {
loginPan.setVisible(false);
homePan.setVisible(true);
} else if (evt.getActionCommand().equals("leaveLogged")) {
loggedInPan.setVisible(false);
homePan.setVisible(true);
validate();
} else if (evt.getActionCommand().equals("leaveCheck")) {
checkPan.setVisible(false);
loggedInPan.setVisible(true);
validate();
} else if (evt.getActionCommand().equals("leaveWithdraw")) {
withdrawPan.setVisible(false);
loggedInPan.setVisible(true);
validate();
} else if (evt.getActionCommand().equals("leaveDeposit")) {
depositPan.setVisible(false);
loggedInPan.setVisible(true);
validate();
} else if (evt.getActionCommand().equals("leaveTransfer")) {
transferPan.setVisible(false);
loggedInPan.setVisible(true);
validate();
}
}
public int initializeRegIndex() throws FileNotFoundException {
Scanner scan = new Scanner(new File("regIndex.txt"));
// Scanner scan = new Scanner("finalguiaccounts.txt");
int regIndx = 0;
int currLine = 0;
while (scan.hasNextLine()) {
String input = scan.nextLine();
regIndx = Integer.parseInt(input);
currLine++;
}
return regIndx;
}
public void logRegIndex(int regIndx) throws IOException {
BufferedWriter bw = null;
FileWriter fw = null;
try {
// BankKonto Konto = new BankKonto(String ktoInhaber, String ktoNummer, int start, String ktoPIN)
fw = new FileWriter(new File("regIndex.txt"));
bw = new BufferedWriter(fw);
String content = Integer.toString(regIndx);
bw.write(content);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
if (fw != null) {
fw.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
public boolean checkInput(String inputs) {
boolean converted;
try {
Integer.parseInt(inputs);
converted = true;
} catch (NumberFormatException e) {
int input = JOptionPane.showOptionDialog(null, "Daten ungültig", "The title", JOptionPane.DEFAULT_OPTION, JOptionPane.INFORMATION_MESSAGE, null, null, null);
converted = false;
}
return converted;
}
public void logAccount(BankKonto[] log) throws IOException {
BufferedWriter bw = null;
FileWriter fw = null;
if (log != null) {
try {
// BankKonto Konto = new BankKonto(String ktoInhaber, String ktoNummer, int start, String ktoPIN)
fw = new FileWriter(new File("finalguiaccounts.txt"));
bw = new BufferedWriter(fw);
for (int i = 0; i <= accounts.length - 1; i++) {
if (accounts[i] != null) {
String content = log[i].gibName() + "," + log[i].gibKontoNummer() + "," + log[i].gibKontoStand() + "," + log[i].gibKontoPIN();
bw.write(content);
bw.newLine();
}
}
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (bw != null) {
bw.close();
}
if (fw != null) {
fw.close();
}
} catch (IOException ex) {
ex.printStackTrace();
}
}
}
}
public String[] splitString(String input) {
String[] strings = new String[4];
int for1,
for2,
for3;
String String1,
String2,
String3,
String4;
for1 = 0;
for2 = 0;
for3 = 0;
String1 = "";
String2 = "";
String3 = "";
String4 = "";
for (int i = 0; i <= input.length(); i++) {
if (input.charAt(i) == (',')) {
for1 = String1.length() + 1;
break;
} else {
String1 = String1 + input.charAt(i);
}
}
for (int i = for1; i <= input.length(); i++) {
if (input.charAt(i) == (',')) {
for2 = for1 + String2.length() + 1;
break;
} else {
String2 = String2 + input.charAt(i);
}
}
for (int i = for2; i <= input.length(); i++) {
if (input.charAt(i) == (',')) {
for3 = for2 + String3.length() + 1;
break;
} else {
String3 = String3 + input.charAt(i);
}
}
for (int i = for3; i <= input.length(); i++) {
if (i >= input.length() - 1) {
String4 = String4 + input.charAt(i);
break;
}
String4 = String4 + input.charAt(i);
}
strings[0] = String1;
strings[1] = String2;
strings[2] = String3;
strings[3] = String4;
return strings;
}
public BankKonto[] initializeAccounts(BankKonto[] accounts) throws FileNotFoundException {
Scanner scan = new Scanner(new File("finalguiaccounts.txt"));
// Scanner scan = new Scanner("finalguiaccounts.txt");
String[] inputs = new String[4];
int currLine = 0;
while (scan.hasNextLine()) {
String input = scan.nextLine();
inputs = splitString(input);
accounts[currLine] = new BankKonto(inputs[0], inputs[1], Integer.parseInt(inputs[2]), inputs[3]);
currLine++;
}
return accounts;
}
public int findAccount(BankKonto[] accountArr, String ibanStr) {
int result = -2;
for (int i = 0; i <= accounts.length - 1; i++) {
if (accounts[i] == null) {
result = -1;
} else if (((ibanStr.trim().equals(accounts[i].gibKontoNummer())))) {
result = i;
break;
}
}
return result;
}
public BankKonto createAccount() {
BankKonto create = new BankKonto(name, iban, 0, pin);
name = null;
iban = null;
pin = null;
return create;
}
public class BankKonto {
String kontoInhaber, kontoNummer, kontoPIN;
int kontoStand;
BankKonto(String ktoInhaber, String ktoNummer, int start, String ktoPIN) {
kontoInhaber = ktoInhaber;
kontoNummer = ktoNummer;
kontoPIN = ktoPIN;
kontoStand = start;
}
void hebeGeldAb(int betrag) {
kontoStand = kontoStand - betrag;
}
void zahleGeldEin(int betrag) {
kontoStand = kontoStand + betrag;
}
String gibKontoNummer() {
return kontoNummer;
}
String gibKontoPIN() {
return kontoPIN;
}
String gibName() {
return kontoInhaber;
}
int gibKontoStand() {
return kontoStand;
}
}
}
public class FinalGUI {
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws IOException {
// TODO code application logic here
MainFrame frame = new MainFrame();
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
}
Mit freundlichen Grüßen,
thelegend27