gui #1

Merged
Ghost merged 5 commits from gui into main 2023-07-01 10:52:33 +00:00
13 changed files with 179 additions and 113 deletions
Showing only changes of commit b301f685a5 - Show all commits

View File

@ -7,5 +7,15 @@
</content>
<orderEntry type="inheritedJdk" />
<orderEntry type="sourceFolder" forTests="false" />
<orderEntry type="module-library">
<library name="JUnit4">
<CLASSES>
<root url="jar://$MAVEN_REPOSITORY$/junit/junit/4.13.1/junit-4.13.1.jar!/" />
<root url="jar://$MAVEN_REPOSITORY$/org/hamcrest/hamcrest-core/1.3/hamcrest-core-1.3.jar!/" />
</CLASSES>
<JAVADOC />
<SOURCES />
</library>
</orderEntry>
</component>
</module>

View File

@ -6,53 +6,79 @@ import me.teridax.jcash.gui.login.LoginController;
import javax.swing.*;
public class Main {
public final class Main {
/**
* Main instance of this program. Contains the primary window.
*/
private static Main instance;
/**
* Primary window of this program
*/
private final JFrame window;
private Main() {
window = new JFrame();
window.setTitle("Bankautomat");
window.setLocationByPlatform(true);
window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
// create main window and set defaults
this.window = new JFrame();
this.window.setTitle("Bankautomat");
this.window.setLocationByPlatform(true);
this.window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String[] args) {
// create main instance and show the login screen
instance();
getInstance().popLoginScreen();
getInstance().showLoginScreen();
}
public static Main getInstance() {
return instance;
}
/**
* Attempts to create a new instance of the singleton class Main.
* This method throws an exception if the class is already instantiated.
* @throws IllegalStateException if the class is already instantiated
* @pre the static instance of this class should be null.
* @post the static instance of this class will be set
*/
public static void instance() {
if (instance == null) {
instance = new Main();
return;
if (null != instance)
throw new IllegalStateException(Main.class.getName() + " is already initialized");
Main.instance = new Main();
}
throw new IllegalStateException("me.teridax.jcash.Main is already initialized");
}
public void popLoginScreen() {
/**
* Shows the open dialog for selecting a database file. After selection, it then proceeds to prompt login.
* Afterward the selected account can be managed.
* This method is non-blocking and all work described is performed asynchronously on the AWT Event dispatcher.
*/
public void showLoginScreen() {
SwingUtilities.invokeLater(() -> {
// select db file
var path = Loader.load();
// read database and login
var login = new LoginController(path);
// when we have logged in set the account viewer as window content
login.addAccountSelectionListener(account -> {
var profileCont = new AccountController(account);
window.setContentPane(profileCont.getView());
window.pack();
this.window.setContentPane(profileCont.getView());
this.window.pack();
});
window.setContentPane(login.getView());
window.pack();
window.setVisible(true);
// we are not logged in yet, so show the login prompt on the main window
this.window.setContentPane(login.getView());
this.window.pack();
this.window.setVisible(true);
});
}
/**
* Logs the user out of the database, hiding the main window.
*/
public void logout() {
window.setContentPane(new JLabel("you're logged out"));
window.setVisible(false);

View File

@ -1,6 +1,6 @@
package me.teridax.jcash.banking;
import me.teridax.jcash.decode.Decoder;
import me.teridax.jcash.decode.StringUtils;
import java.util.Objects;
@ -20,17 +20,17 @@ public abstract class Account {
public static Account fromColumns(String[] columns) {
Objects.requireNonNull(columns);
var iban = Decoder.decodeUniqueIdentificationNumber(columns[0]);
var pin = Decoder.decodeUniqueIdentificationNumber(columns[1]);
var balance = Decoder.decodeCurrency(columns[2]);
var type = Decoder.decodeName(columns[3]);
var iban = StringUtils.decodeUniqueIdentificationNumber(columns[0]);
var pin = StringUtils.decodeUniqueIdentificationNumber(columns[1]);
var balance = StringUtils.decodeCurrency(columns[2]);
var type = StringUtils.decodeName(columns[3]);
try {
if (type.equals("Sparkonto")) {
var interest = Decoder.decodePercent(columns[4]);
var interest = StringUtils.decodePercent(columns[4]);
return new SavingsAccount(iban, pin, balance, interest);
} else if (type.equals("Girokonto")) {
var overdraft = Decoder.decodeCurrency(columns[5]);
var overdraft = StringUtils.decodeCurrency(columns[5]);
return new Girokonto(iban, pin, balance, overdraft);
} else {
throw new IllegalArgumentException("Invalid account type: " + type);

View File

@ -1,6 +1,6 @@
package me.teridax.jcash.banking;
import me.teridax.jcash.decode.Decoder;
import me.teridax.jcash.decode.StringUtils;
import java.io.IOException;
import java.nio.file.Files;
@ -34,7 +34,7 @@ public final class BankingManagementSystem {
var account = Account.fromColumns(tail(columns, 2));
var blz = Bank.validateBlz(columns[1]);
var name = Decoder.decodeName(columns[0]);
var name = StringUtils.decodeName(columns[0]);
var bankOfLine = new Bank(blz, name);
var bankOfSet = bms.banks.stream().filter(b -> b.equals(bankOfLine)).findFirst();

View File

@ -1,6 +1,6 @@
package me.teridax.jcash.banking;
import me.teridax.jcash.decode.Decoder;
import me.teridax.jcash.decode.StringUtils;
import java.util.Objects;
@ -28,12 +28,12 @@ public final class Owner {
if (columns.length != 6)
throw new IllegalArgumentException("Invalid number of columns: " + columns.length);
var uid = Decoder.decodeUniqueIdentificationNumber(columns[0]);
var familyName = Decoder.decodeName(columns[1]);
var name = Decoder.decodeName(columns[2]);
var street = Decoder.decodeStreet(columns[3]);
var zip = Decoder.decodeUniqueIdentificationNumber(columns[4]);
var city = Decoder.decodeName(columns[5]);
var uid = StringUtils.decodeUniqueIdentificationNumber(columns[0]);
var familyName = StringUtils.decodeName(columns[1]);
var name = StringUtils.decodeName(columns[2]);
var street = StringUtils.decodeStreet(columns[3]);
var zip = StringUtils.decodeUniqueIdentificationNumber(columns[4]);
var city = StringUtils.decodeName(columns[5]);
return new Owner(uid, familyName, name, zip, city, street);
}

View File

@ -4,7 +4,7 @@ import org.junit.Test;
import java.nio.file.Paths;
public class Testing {
public class Tests {
@Test
public void test() {

View File

@ -10,11 +10,29 @@ import java.util.regex.Pattern;
import static junit.framework.TestCase.assertEquals;
public class Decoder {
/**
* Utility class for converting various single line strings into a specific data type according
* to a format mostly dictated by a locale.
*/
public class StringUtils {
/**
* Locale to use when converting strings
*/
private static final Locale LOCALE = Locale.GERMANY;
/**
* NumberFormat to use when converting strings
*/
public static final NumberFormat LOCAL_NUMBER_FORMAT = NumberFormat.getInstance(LOCALE);
/**
* Attempts to convert the given string into a double value representing a percentage.
* The percentage is stored in the range [0,1] and can linearly be mapped to [0, 100] by multiplying with 100.
* @param number the string to convert
* @return the double value
* @throws IllegalArgumentException when the format is invalid
* @throws NullPointerException when the argument is null
*/
public static double decodePercent(String number) throws IllegalArgumentException, NullPointerException {
Objects.requireNonNull(number);
@ -28,6 +46,13 @@ public class Decoder {
}
}
/**
* Attempts to convert the given string into a currency value.
* @param currency the string to convert
* @return the double value
* @throws IllegalArgumentException when the format is invalid
* @throws NullPointerException when the argument is null
*/
public static double decodeCurrency(String currency) throws IllegalArgumentException, NullPointerException {
Objects.requireNonNull(currency);
@ -38,6 +63,14 @@ public class Decoder {
}
}
/**
* Attempts to convert the given string into universally unique number.
* This function does not check for duplicates. The number must be a positive integer.
* @param number the string to convert
* @return the integer serial number
* @throws IllegalArgumentException when the format is invalid
* @throws NullPointerException when the argument is null
*/
public static int decodeUniqueIdentificationNumber(String number) throws IllegalArgumentException, NullPointerException {
Objects.requireNonNull(number);
@ -56,6 +89,14 @@ public class Decoder {
}
}
/**
* Attempts to convert the given string into a name.
* This method performs validation and trimming.
* @param name the string to convert
* @return the qualified name
* @throws IllegalArgumentException when the format is invalid
* @throws NullPointerException when the argument is null
*/
public static String decodeName(String name) throws IllegalArgumentException, NullPointerException {
Objects.requireNonNull(name);
@ -70,6 +111,13 @@ public class Decoder {
}
}
/**
* Attempts to convert the given string into a street and an optional house address.
* @param street the string to convert
* @return the address name
* @throws IllegalArgumentException when the format is invalid
* @throws NullPointerException when the argument is null
*/
public static String decodeStreet(String street) throws IllegalArgumentException, NullPointerException {
Objects.requireNonNull(street);

View File

@ -6,10 +6,26 @@ import javax.swing.*;
import javax.swing.filechooser.FileNameExtensionFilter;
import java.io.File;
import static javax.swing.JFileChooser.APPROVE_OPTION;
/**
* Utility class for loading a BMS configuration from a csv file.
*/
public class Loader {
/**
* Filter that only allows for files with *.csv extension
*/
private static final FileNameExtensionFilter FILE_FILTER = new FileNameExtensionFilter("Comma separated value spreadsheet", "csv", "CSV");
private Loader() {}
/**
* Load a BMS from a csv file. Opens up a dialog which prompts the user to select a single file.
* Once the file is selected this function will try to parse the contents to a BMS and return the instance.
* @return a valid BMS instance loaded from a file
* @throws IllegalStateException When either no file is selected or the selected files content is invalid
*/
public static BankingManagementSystem load() throws IllegalStateException {
var fileChooser = new JFileChooser();
fileChooser.setMultiSelectionEnabled(false);
@ -19,7 +35,8 @@ public class Loader {
fileChooser.setAcceptAllFileFilterUsed(false);
fileChooser.setCurrentDirectory(new File("/home/teridax/IdeaProjects/JCash/res"));
if (fileChooser.showDialog(null, "Load database") == JFileChooser.APPROVE_OPTION) {
if (fileChooser.showDialog(null, "Load database") == APPROVE_OPTION) {
// parse file content
try {
return BankingManagementSystem.loadFromCsv(fileChooser.getSelectedFile().toPath());
} catch (Exception e) {

View File

@ -26,7 +26,7 @@ public class AccountController {
this.view.getLogout().addActionListener(e -> {
Main.getInstance().logout();
Main.getInstance().popLoginScreen();
Main.getInstance().showLoginScreen();
});
this.view.getDeposit().addActionListener(e -> {

View File

@ -1,7 +1,7 @@
package me.teridax.jcash.gui.account;
import me.teridax.jcash.banking.*;
import me.teridax.jcash.decode.Decoder;
import me.teridax.jcash.decode.StringUtils;
import javax.swing.*;
import java.awt.*;
@ -22,7 +22,6 @@ public class AccountView extends JPanel {
private JComboBox<String> accountSelection;
private JPanel content;
private JPanel buttonPanel;
private JButton logout;
private JButton transfer;
@ -48,12 +47,12 @@ public class AccountView extends JPanel {
this.name.setText(owner.getName() + " " + owner.getFamilyName());
this.address.setText(owner.getStreet() + " " + owner.getCity());
this.balance.setText(Decoder.LOCAL_NUMBER_FORMAT.format(account.getBalance()) + "");
this.balance.setText(StringUtils.LOCAL_NUMBER_FORMAT.format(account.getBalance()) + "");
this.type.setText(account.getClass().getSimpleName());
if (account instanceof Girokonto) {
this.typeSpecialLabel.setText("Überziehungsbetrag");
this.typeSpecialProperty.setText( Decoder.LOCAL_NUMBER_FORMAT.format(((Girokonto) account).getOverdraft()) + "");
this.typeSpecialProperty.setText( StringUtils.LOCAL_NUMBER_FORMAT.format(((Girokonto) account).getOverdraft()) + "");
} else if (account instanceof SavingsAccount) {
this.typeSpecialLabel.setText("Zinsbetrag");
this.typeSpecialProperty.setText( ((SavingsAccount) account).getInterest() + " %" );
@ -70,7 +69,6 @@ public class AccountView extends JPanel {
private void createLayout() {
setLayout(new BorderLayout(16, 16));
add(new JScrollPane(content), BorderLayout.CENTER);
add(buttonPanel, BorderLayout.SOUTH);
content.setLayout(new GridBagLayout());
var constraints = new GridBagConstraints();
@ -91,13 +89,17 @@ public class AccountView extends JPanel {
addInputRow(constraints, content, typeSpecialProperty, 7, typeSpecialLabel);
addInputRow(constraints, content, balance, 8, new JLabel("Kontostand", SwingConstants.RIGHT));
var buttonRightPanel = new JPanel();
buttonRightPanel.add(transfer);
buttonRightPanel.add(deposit);
buttonRightPanel.add(takeoff);
buttonPanel.add(logout, BorderLayout.LINE_START);
buttonPanel.add(buttonRightPanel, BorderLayout.LINE_END);
var buttonPanel = Box.createHorizontalBox();
buttonPanel.add(Box.createHorizontalStrut(4));
buttonPanel.add(logout);
buttonPanel.add(Box.createHorizontalStrut(4));
buttonPanel.add(Box.createGlue());
buttonPanel.add(transfer);
buttonPanel.add(Box.createHorizontalStrut(4));
buttonPanel.add(deposit);
buttonPanel.add(Box.createHorizontalStrut(4));
buttonPanel.add(takeoff);
add(buttonPanel, BorderLayout.SOUTH);
}
private void createComponents() {
@ -124,7 +126,6 @@ public class AccountView extends JPanel {
this.accountSelection = new JComboBox<>();
this.content = new JPanel();
this.buttonPanel = new JPanel(new BorderLayout());
this.logout = new JButton("Abmelden");
this.transfer = new JButton("Überweisen");

View File

@ -1,6 +1,7 @@
package me.teridax.jcash.gui.takeoff;
import me.teridax.jcash.banking.Account;
import me.teridax.jcash.decode.StringUtils;
import javax.swing.*;
import javax.swing.text.NumberFormatter;
@ -34,19 +35,6 @@ public class TakeoffView {
dialog.setVisible(true);
}
private NumberFormatter getNumberFormat(double balance) {
var format = NumberFormat.getNumberInstance(Locale.GERMANY);
format.setParseIntegerOnly(false);
format.setMaximumFractionDigits(2);
var formatter = new NumberFormatter(format);
formatter.setValueClass(Double.class);
formatter.setAllowsInvalid(true);
formatter.setMaximum(balance);
return formatter;
}
private void layoutComponents() {
dialog.getContentPane().setLayout(new GridBagLayout());
@ -98,7 +86,7 @@ public class TakeoffView {
this.cancel = new JButton("Abbrechen");
this.deposit = new JButton("Auszahlen");
this.value = new JFormattedTextField(getNumberFormat(account.getBalance()));
this.value = new JFormattedTextField(StringUtils.LOCAL_NUMBER_FORMAT);
this.dialog.setContentPane(new JPanel(new GridBagLayout()));
}

View File

@ -6,7 +6,7 @@ public class TransferDialog {
public TransferDialog(Account account, Runnable onDeposit) {
var view = new TransferView(account);
view.getDeposit().addActionListener(e -> {
view.getTransfer().addActionListener(e -> {
account.takeoff(view.getAmount());
onDeposit.run();
view.dispose();

View File

@ -1,13 +1,12 @@
package me.teridax.jcash.gui.transfer;
import me.teridax.jcash.banking.Account;
import me.teridax.jcash.decode.StringUtils;
import javax.swing.*;
import javax.swing.text.NumberFormatter;
import java.awt.*;
import java.text.NumberFormat;
import java.text.ParseException;
import java.util.Locale;
import static javax.swing.JOptionPane.ERROR_MESSAGE;
import static javax.swing.JOptionPane.showMessageDialog;
@ -16,7 +15,7 @@ public class TransferView {
private JDialog dialog;
private JButton cancel;
private JButton deposit;
private JButton transfer;
private JFormattedTextField iban;
private JFormattedTextField blz;
private JFormattedTextField value;
@ -28,45 +27,33 @@ public class TransferView {
public void showDialog() {
dialog.setModalityType(Dialog.ModalityType.APPLICATION_MODAL);
dialog.setTitle("Deposit money");
dialog.setTitle("Transfer money");
dialog.pack();
dialog.setSize(dialog.getWidth() * 2, dialog.getHeight());
dialog.setResizable(false);
dialog.setLocationRelativeTo(null);
dialog.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
dialog.setVisible(true);
}
private NumberFormatter getNumberFormat(double balance) {
var format = NumberFormat.getNumberInstance(Locale.GERMANY);
format.setParseIntegerOnly(false);
format.setMaximumFractionDigits(2);
var formatter = new NumberFormatter(format);
formatter.setValueClass(Double.class);
formatter.setAllowsInvalid(true);
formatter.setMaximum(balance);
return formatter;
}
private void layoutComponents() {
dialog.getContentPane().setLayout(new GridBagLayout());
var c = new GridBagConstraints();
c.gridx = 0;
c.gridy = 0;
c.weightx = 1;
c.weighty = 1;
c.fill = GridBagConstraints.BOTH;
c.gridwidth = 3;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.CENTER;
c.insets = new Insets(10, 10, 10, 10);
dialog.getContentPane().add(new JLabel("Takeoff money"), c);
c.insets = new Insets(4, 4, 4, 4);
dialog.getContentPane().add(new JLabel("Transfer money"), c);
c.gridx = 0;
c.gridy = 1;
c.gridwidth = 1;
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.LAST_LINE_END;
c.insets = new Insets(10, 10, 10, 10);
c.weightx = 0;
dialog.getContentPane().add(new JLabel("BLZ", SwingConstants.RIGHT), c);
@ -74,15 +61,13 @@ public class TransferView {
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.LAST_LINE_END;
c.insets = new Insets(10, 10, 10, 10);
c.weightx = 1;
c.weightx = 0.5;
dialog.getContentPane().add(blz, c);
c.gridx = 2;
c.gridy = 1;
c.fill = GridBagConstraints.NONE;
c.anchor = GridBagConstraints.LAST_LINE_END;
c.insets = new Insets(10, 10, 10, 10);
c.weightx = 0;
dialog.getContentPane().add(new JLabel("IBAN", SwingConstants.RIGHT), c);
@ -90,7 +75,6 @@ public class TransferView {
c.gridy = 1;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.LAST_LINE_END;
c.insets = new Insets(10, 10, 10, 10);
c.weightx = 1;
dialog.getContentPane().add(iban, c);
@ -98,7 +82,6 @@ public class TransferView {
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(new JLabel("Betrag", SwingConstants.RIGHT), c);
@ -106,35 +89,28 @@ public class TransferView {
c.gridy = 2;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.LAST_LINE_END;
c.insets = new Insets(10, 10, 10, 10);
c.weightx = 1;
c.weightx = 0.5;
dialog.getContentPane().add(value, c);
c.gridx = 2;
var buttonPanel = new JPanel(new FlowLayout(FlowLayout.TRAILING));
buttonPanel.add(cancel);
buttonPanel.add(transfer);
c.gridx = 0;
c.gridy = 3;
c.gridwidth = 4;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.LAST_LINE_END;
c.insets = new Insets(10, 10, 10, 10);
c.weightx = 1;
dialog.getContentPane().add(cancel, c);
c.gridx = 3;
c.gridy = 3;
c.fill = GridBagConstraints.HORIZONTAL;
c.anchor = GridBagConstraints.LAST_LINE_END;
c.insets = new Insets(10, 10, 10, 10);
c.weightx = 1;
dialog.getContentPane().add(deposit, c);
dialog.getContentPane().add(value, c);
dialog.getContentPane().add(buttonPanel, c);
}
private void createComponents(Account account) {
this.dialog = new JDialog();
this.cancel = new JButton("Abbrechen");
this.deposit = new JButton("Auszahlen");
this.value = new JFormattedTextField(getNumberFormat(account.getBalance()));
this.cancel = new JButton("Cancel");
this.transfer = new JButton("Transfer");
this.value = new JFormattedTextField(StringUtils.LOCAL_NUMBER_FORMAT);
this.iban = new JFormattedTextField();
this.blz = new JFormattedTextField();
@ -158,8 +134,8 @@ public class TransferView {
return cancel;
}
public JButton getDeposit() {
return deposit;
public JButton getTransfer() {
return transfer;
}
public void dispose() {