fixed unhandled exception

This commit is contained in:
Sven Vogel 2023-07-01 12:47:49 +02:00
parent b301f685a5
commit 0c19489b1f
5 changed files with 86 additions and 89 deletions

View File

@ -57,22 +57,28 @@ public final class Main {
*/ */
public void showLoginScreen() { public void showLoginScreen() {
SwingUtilities.invokeLater(() -> { SwingUtilities.invokeLater(() -> {
// select db file try {
var path = Loader.load(); // select db file
// read database and login var path = Loader.load();
var login = new LoginController(path); // read database and login
var login = new LoginController(path);
// when we have logged in set the account viewer as window content // when we have logged in set the account viewer as window content
login.addAccountSelectionListener(account -> { login.addAccountSelectionListener(account -> {
var profileCont = new AccountController(account); var profileCont = new AccountController(account);
this.window.setContentPane(profileCont.getView()); this.window.setContentPane(profileCont.getView());
this.window.pack(); this.window.revalidate();
}); this.window.repaint();
});
// we are not logged in yet, so show the login prompt on the main window // we are not logged in yet, so show the login prompt on the main window
this.window.setContentPane(login.getView()); this.window.setContentPane(login.getView());
this.window.pack(); this.window.setSize(800, 600);
this.window.setVisible(true); this.window.setVisible(true);
} catch (IllegalStateException e) {
System.out.println("no file selected. goodbye");
}
}); });
} }

View File

@ -51,10 +51,10 @@ public class AccountView extends JPanel {
this.type.setText(account.getClass().getSimpleName()); this.type.setText(account.getClass().getSimpleName());
if (account instanceof Girokonto) { if (account instanceof Girokonto) {
this.typeSpecialLabel.setText("Überziehungsbetrag"); this.typeSpecialLabel.setText("Overdraft");
this.typeSpecialProperty.setText( StringUtils.LOCAL_NUMBER_FORMAT.format(((Girokonto) account).getOverdraft()) + ""); this.typeSpecialProperty.setText( StringUtils.LOCAL_NUMBER_FORMAT.format(((Girokonto) account).getOverdraft()) + "");
} else if (account instanceof SavingsAccount) { } else if (account instanceof SavingsAccount) {
this.typeSpecialLabel.setText("Zinsbetrag"); this.typeSpecialLabel.setText("Interest rate");
this.typeSpecialProperty.setText( ((SavingsAccount) account).getInterest() + " %" ); this.typeSpecialProperty.setText( ((SavingsAccount) account).getInterest() + " %" );
} }
@ -80,14 +80,14 @@ public class AccountView extends JPanel {
accountSelectionPanel.add(iban, BorderLayout.CENTER); accountSelectionPanel.add(iban, BorderLayout.CENTER);
accountSelectionPanel.add(accountSelection, BorderLayout.EAST); accountSelectionPanel.add(accountSelection, BorderLayout.EAST);
addInputRow(constraints, content, accountSelectionPanel, 1, new JLabel("Kontonummer", SwingConstants.RIGHT)); addInputRow(constraints, content, accountSelectionPanel, 1, new JLabel("IBAN", SwingConstants.RIGHT));
addInputRow(constraints, content, name, 2, new JLabel("Vorname/Nachname", SwingConstants.RIGHT)); addInputRow(constraints, content, name, 2, new JLabel("Name/Family-name", SwingConstants.RIGHT));
addInputRow(constraints, content, address, 3, new JLabel("Adresse", SwingConstants.RIGHT)); addInputRow(constraints, content, address, 3, new JLabel("Address", SwingConstants.RIGHT));
addInputRow(constraints, content, bankName, 4, new JLabel("Bank", SwingConstants.RIGHT)); addInputRow(constraints, content, bankName, 4, new JLabel("Bank", SwingConstants.RIGHT));
addInputRow(constraints, content, blz, 5, new JLabel("BLZ", SwingConstants.RIGHT)); addInputRow(constraints, content, blz, 5, new JLabel("BLZ", SwingConstants.RIGHT));
addInputRow(constraints, content, type, 6, new JLabel("Konto", SwingConstants.RIGHT)); addInputRow(constraints, content, type, 6, new JLabel("Account", SwingConstants.RIGHT));
addInputRow(constraints, content, typeSpecialProperty, 7, typeSpecialLabel); addInputRow(constraints, content, typeSpecialProperty, 7, typeSpecialLabel);
addInputRow(constraints, content, balance, 8, new JLabel("Kontostand", SwingConstants.RIGHT)); addInputRow(constraints, content, balance, 8, new JLabel("Balance", SwingConstants.RIGHT));
var buttonPanel = Box.createHorizontalBox(); var buttonPanel = Box.createHorizontalBox();
buttonPanel.add(Box.createHorizontalStrut(4)); buttonPanel.add(Box.createHorizontalStrut(4));
@ -127,10 +127,10 @@ public class AccountView extends JPanel {
this.content = new JPanel(); this.content = new JPanel();
this.logout = new JButton("Abmelden"); this.logout = new JButton("Logout");
this.transfer = new JButton("Überweisen"); this.transfer = new JButton("Transfer");
this.deposit = new JButton("Einzahlen"); this.deposit = new JButton("Deposit");
this.takeoff = new JButton("Abheben"); this.takeoff = new JButton("Takeoff");
} }
private void addInputRow(GridBagConstraints constraints, JComponent target, JComponent comp, int row, JLabel label) { private void addInputRow(GridBagConstraints constraints, JComponent target, JComponent comp, int row, JLabel label) {

View File

@ -1,13 +1,12 @@
package me.teridax.jcash.gui.deposit; package me.teridax.jcash.gui.deposit;
import me.teridax.jcash.banking.Account; import me.teridax.jcash.banking.Account;
import me.teridax.jcash.decode.StringUtils;
import javax.swing.*; import javax.swing.*;
import javax.swing.text.NumberFormatter;
import java.awt.*; import java.awt.*;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.text.ParseException; import java.text.ParseException;
import java.util.Locale;
public class DepositView { public class DepositView {
@ -31,70 +30,62 @@ public class DepositView {
dialog.setVisible(true); dialog.setVisible(true);
} }
private NumberFormatter getNumberFormat() {
var format = NumberFormat.getNumberInstance(Locale.GERMANY);
format.setParseIntegerOnly(false);
format.setMaximumFractionDigits(2);
var formatter = new NumberFormatter(format);
formatter.setValueClass(Double.class);
formatter.setAllowsInvalid(false);
return formatter;
}
private void layoutComponents() { private void layoutComponents() {
dialog.getContentPane().setLayout(new GridBagLayout()); dialog.getContentPane().setLayout(new GridBagLayout());
var c = new GridBagConstraints(); var c = new GridBagConstraints();
c.gridx = 0; c.gridx = 0;
c.gridy = 0; c.gridy = 0;
c.weightx = 1; c.weightx = 1;
c.weighty = 1; c.weighty = 1;
c.fill = GridBagConstraints.BOTH; c.gridwidth = 3;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.CENTER; c.anchor = GridBagConstraints.CENTER;
c.insets = new Insets(10, 10, 10, 10); c.insets = new Insets(4, 4, 4, 4);
dialog.getContentPane().add(new JLabel("Deposit money"), c); dialog.getContentPane().add(new JLabel("Deposit money"), c);
c.gridx = 0; c.gridx = 0;
c.gridy = 1; c.gridy = 1;
c.gridwidth = 1;
c.fill = GridBagConstraints.NONE; c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.LAST_LINE_END; c.anchor = GridBagConstraints.LAST_LINE_END;
c.insets = new Insets(10, 10, 10, 10);
c.weightx = 0; c.weightx = 0;
dialog.getContentPane().add(new JLabel("Betrag", SwingConstants.RIGHT), c); dialog.getContentPane().add(new JLabel("Value", SwingConstants.RIGHT), c);
c.gridx = 1; c.gridx = 1;
c.gridy = 1; c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL; c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.LAST_LINE_END; c.anchor = GridBagConstraints.LAST_LINE_END;
c.insets = new Insets(10, 10, 10, 10); c.weightx = 0.5;
c.weightx = 1;
dialog.getContentPane().add(value, c); dialog.getContentPane().add(value, c);
c.gridx = 1;
c.gridy = 2;
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.LAST_LINE_END;
c.insets = new Insets(10, 10, 10, 10);
c.weightx = 0;
dialog.getContentPane().add(cancel, c);
c.gridx = 2; c.gridx = 2;
c.gridy = 2; c.gridy = 1;
c.fill = GridBagConstraints.NONE; c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.LINE_START;
c.weightx = 0;
dialog.getContentPane().add(new JLabel(""), c);
var buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
buttonPanel.add(cancel);
buttonPanel.add(deposit);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 3;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.LAST_LINE_END; c.anchor = GridBagConstraints.LAST_LINE_END;
c.insets = new Insets(10, 10, 10, 10); c.insets = new Insets(10, 10, 10, 10);
c.weightx = 0; dialog.getContentPane().add(buttonPanel, c);
dialog.getContentPane().add(deposit, c);
} }
private void createComponents(Account account) { private void createComponents(Account account) {
this.dialog = new JDialog(); this.dialog = new JDialog();
this.cancel = new JButton("Abbrechen"); this.cancel = new JButton("Cancel");
this.deposit = new JButton("Einzahlen"); this.deposit = new JButton("Deposit");
this.value = new JFormattedTextField(getNumberFormat()); this.value = new JFormattedTextField(StringUtils.LOCAL_NUMBER_FORMAT);
this.dialog.setContentPane(new JPanel(new GridBagLayout())); this.dialog.setContentPane(new JPanel(new GridBagLayout()));
} }

View File

@ -4,11 +4,11 @@ import me.teridax.jcash.banking.Account;
public class TakeoffDialog { public class TakeoffDialog {
public TakeoffDialog(Account account, Runnable onDeposit) { public TakeoffDialog(Account account, Runnable onTakeoff) {
var view = new TakeoffView(account); var view = new TakeoffView(account);
view.getDeposit().addActionListener(e -> { view.getTakeoff().addActionListener(e -> {
account.takeoff(view.getAmount()); account.takeoff(view.getAmount());
onDeposit.run(); onTakeoff.run();
view.dispose(); view.dispose();
}); });
view.getCancel().addActionListener(e -> view.dispose()); view.getCancel().addActionListener(e -> view.dispose());

View File

@ -4,11 +4,9 @@ import me.teridax.jcash.banking.Account;
import me.teridax.jcash.decode.StringUtils; import me.teridax.jcash.decode.StringUtils;
import javax.swing.*; import javax.swing.*;
import javax.swing.text.NumberFormatter;
import java.awt.*; import java.awt.*;
import java.text.NumberFormat; import java.text.NumberFormat;
import java.text.ParseException; import java.text.ParseException;
import java.util.Locale;
import static javax.swing.JOptionPane.ERROR_MESSAGE; import static javax.swing.JOptionPane.ERROR_MESSAGE;
import static javax.swing.JOptionPane.showMessageDialog; import static javax.swing.JOptionPane.showMessageDialog;
@ -17,7 +15,7 @@ public class TakeoffView {
private JDialog dialog; private JDialog dialog;
private JButton cancel; private JButton cancel;
private JButton deposit; private JButton takeoff;
private JFormattedTextField value; private JFormattedTextField value;
public TakeoffView(Account account) { public TakeoffView(Account account) {
@ -27,7 +25,7 @@ public class TakeoffView {
public void showDialog() { public void showDialog() {
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL); dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
dialog.setTitle("Deposit money"); dialog.setTitle("Takeoff money");
dialog.pack(); dialog.pack();
dialog.setResizable(false); dialog.setResizable(false);
dialog.setLocationRelativeTo(null); dialog.setLocationRelativeTo(null);
@ -36,56 +34,58 @@ public class TakeoffView {
} }
private void layoutComponents() { private void layoutComponents() {
dialog.getContentPane().setLayout(new GridBagLayout());
var c = new GridBagConstraints(); var c = new GridBagConstraints();
c.gridx = 0; c.gridx = 0;
c.gridy = 0; c.gridy = 0;
c.weightx = 1; c.weightx = 1;
c.weighty = 1; c.weighty = 1;
c.fill = GridBagConstraints.BOTH; c.gridwidth = 3;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.CENTER; c.anchor = GridBagConstraints.CENTER;
c.insets = new Insets(10, 10, 10, 10); c.insets = new Insets(4, 4, 4, 4);
dialog.getContentPane().add(new JLabel("Takeoff money"), c); dialog.getContentPane().add(new JLabel("Takeoff money"), c);
c.gridx = 0; c.gridx = 0;
c.gridy = 1; c.gridy = 1;
c.gridwidth = 1;
c.fill = GridBagConstraints.NONE; c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.LAST_LINE_END; c.anchor = GridBagConstraints.LAST_LINE_END;
c.insets = new Insets(10, 10, 10, 10);
c.weightx = 0; c.weightx = 0;
dialog.getContentPane().add(new JLabel("Betrag", SwingConstants.RIGHT), c); dialog.getContentPane().add(new JLabel("Value", SwingConstants.RIGHT), c);
c.gridx = 1; c.gridx = 1;
c.gridy = 1; c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL; c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.LAST_LINE_END; c.anchor = GridBagConstraints.LAST_LINE_END;
c.insets = new Insets(10, 10, 10, 10); c.weightx = 0.5;
c.weightx = 1;
dialog.getContentPane().add(value, c); dialog.getContentPane().add(value, c);
c.gridx = 1;
c.gridy = 2;
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.LAST_LINE_END;
c.insets = new Insets(10, 10, 10, 10);
c.weightx = 0;
dialog.getContentPane().add(cancel, c);
c.gridx = 2; c.gridx = 2;
c.gridy = 2; c.gridy = 1;
c.fill = GridBagConstraints.NONE; c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.LINE_START;
c.weightx = 0;
dialog.getContentPane().add(new JLabel(""), c);
var buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
buttonPanel.add(cancel);
buttonPanel.add(takeoff);
c.gridx = 0;
c.gridy = 2;
c.gridwidth = 3;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.LAST_LINE_END; c.anchor = GridBagConstraints.LAST_LINE_END;
c.insets = new Insets(10, 10, 10, 10); c.insets = new Insets(10, 10, 10, 10);
c.weightx = 0; dialog.getContentPane().add(buttonPanel, c);
dialog.getContentPane().add(deposit, c);
} }
private void createComponents(Account account) { private void createComponents(Account account) {
this.dialog = new JDialog(); this.dialog = new JDialog();
this.cancel = new JButton("Abbrechen"); this.cancel = new JButton("Cancel");
this.deposit = new JButton("Auszahlen"); this.takeoff = new JButton("Takeoff");
this.value = new JFormattedTextField(StringUtils.LOCAL_NUMBER_FORMAT); this.value = new JFormattedTextField(StringUtils.LOCAL_NUMBER_FORMAT);
this.dialog.setContentPane(new JPanel(new GridBagLayout())); this.dialog.setContentPane(new JPanel(new GridBagLayout()));
@ -108,8 +108,8 @@ public class TakeoffView {
return cancel; return cancel;
} }
public JButton getDeposit() { public JButton getTakeoff() {
return deposit; return takeoff;
} }
public void dispose() { public void dispose() {