diff --git a/src/Main.java b/src/Main.java index 3e59c38..e456075 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,5 +1,19 @@ +import me.teridax.jcash.gui.login.LoginView; + +import javax.swing.*; + public class Main { + public static void main(String[] args) { - System.out.println("Hello world!"); + SwingUtilities.invokeLater(() -> { + // Loader.load(); + + var window = new JFrame(); + window.setTitle("Bankautomat"); + window.setLocationByPlatform(true); + window.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + window.add(new LoginView()); + window.setVisible(true); + }); } } \ No newline at end of file diff --git a/src/me/teridax/jcash/banking/Bank.java b/src/me/teridax/jcash/banking/Bank.java index 13bad12..41fd5d6 100644 --- a/src/me/teridax/jcash/banking/Bank.java +++ b/src/me/teridax/jcash/banking/Bank.java @@ -56,4 +56,15 @@ public final class Bank { return blz; } + + public Optional getAccount(int iban) { + for (var owner : this.accounts.entrySet()) { + for (var account : owner.getValue()) { + if (account.getIban() == iban) { + return Optional.of(account); + } + } + } + return Optional.empty(); + } } diff --git a/src/me/teridax/jcash/banking/BankingManagementSystem.java b/src/me/teridax/jcash/banking/BankingManagementSystem.java index 35d8839..c296c18 100644 --- a/src/me/teridax/jcash/banking/BankingManagementSystem.java +++ b/src/me/teridax/jcash/banking/BankingManagementSystem.java @@ -54,4 +54,8 @@ public final class BankingManagementSystem { throw new IllegalArgumentException("Could not parse file " + file, e); } } + + public Optional getBank(String blz) { + return this.banks.stream().filter(b -> b.getBlz().equals(blz)).findFirst(); + } } diff --git a/src/me/teridax/jcash/gui/Loader.java b/src/me/teridax/jcash/gui/Loader.java new file mode 100644 index 0000000..4219c0b --- /dev/null +++ b/src/me/teridax/jcash/gui/Loader.java @@ -0,0 +1,30 @@ +package me.teridax.jcash.gui; + +import me.teridax.jcash.banking.BankingManagementSystem; + +import javax.swing.*; +import javax.swing.filechooser.FileNameExtensionFilter; + +public class Loader { + + private static final FileNameExtensionFilter FILE_FILTER = new FileNameExtensionFilter("Comma separated value spreadsheet", "csv", "CSV"); + + public static BankingManagementSystem load() throws IllegalStateException { + var fileChooser = new JFileChooser(); + fileChooser.setMultiSelectionEnabled(false); + fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY); + fileChooser.setFileFilter(FILE_FILTER); + fileChooser.setDialogType(JFileChooser.OPEN_DIALOG); + fileChooser.setAcceptAllFileFilterUsed(false); + + if (fileChooser.showDialog(null, "Load database") == JFileChooser.APPROVE_OPTION) { + try { + return BankingManagementSystem.loadFromCsv(fileChooser.getSelectedFile().toPath()); + } catch (Exception e) { + throw new IllegalStateException("Unable to load database", e); + } + } + + throw new IllegalStateException("No file selected"); + } +} diff --git a/src/me/teridax/jcash/gui/login/LoginController.java b/src/me/teridax/jcash/gui/login/LoginController.java new file mode 100644 index 0000000..543f42e --- /dev/null +++ b/src/me/teridax/jcash/gui/login/LoginController.java @@ -0,0 +1,58 @@ +package me.teridax.jcash.gui.login; + +import me.teridax.jcash.banking.Account; +import me.teridax.jcash.banking.BankingManagementSystem; + +import java.awt.event.ActionEvent; +import java.util.Optional; + +public class LoginController { + + private LoginView view; + private LoginData data; + + private Optional account; + + public LoginController(BankingManagementSystem bms) { + this.view = new LoginView(); + this.data = new LoginData(bms); + this.account = Optional.empty(); + + addActionListeners(); + } + + private void addActionListeners() { + this.view.getLogin().addActionListener(this::login); + } + + private Optional getIban() { + try { + var iban = this.view.getIban().getText(); + return Optional.of(Integer.parseUnsignedInt(iban)); + } catch (NumberFormatException e) { + return Optional.empty(); + } + } + private Optional getPin() { + try { + var iban = this.view.getPin().getPassword(); + return Optional.of(Integer.parseUnsignedInt(new String(iban))); + } catch (NumberFormatException e) { + return Optional.empty(); + } + } + + private void login(ActionEvent ignored) { + var blz = this.view.getBlz().getText(); + var iban = this.getIban(); + if (iban.isEmpty()) { + return; + } + var pin = this.getPin(); + if (pin.isEmpty()) { + return; + } + + this.account = this.data.authenticateAccount(blz, iban.get(), pin.get()); + } +} diff --git a/src/me/teridax/jcash/gui/login/LoginData.java b/src/me/teridax/jcash/gui/login/LoginData.java new file mode 100644 index 0000000..730128e --- /dev/null +++ b/src/me/teridax/jcash/gui/login/LoginData.java @@ -0,0 +1,24 @@ +package me.teridax.jcash.gui.login; + +import me.teridax.jcash.banking.Account; +import me.teridax.jcash.banking.BankingManagementSystem; + +import java.util.Optional; + +public class LoginData { + + private final BankingManagementSystem bms; + + public LoginData(BankingManagementSystem bms) { + this.bms = bms; + } + + public Optional authenticateAccount(String blz, int iban, int pin) { + var optionalBank = bms.getBank(blz); + if (optionalBank.isEmpty()) + return Optional.empty(); + + var account = optionalBank.get().getAccount(iban); + return account.filter(value -> value.getPin() == pin); + } +} diff --git a/src/me/teridax/jcash/gui/login/LoginView.java b/src/me/teridax/jcash/gui/login/LoginView.java new file mode 100644 index 0000000..7bf3043 --- /dev/null +++ b/src/me/teridax/jcash/gui/login/LoginView.java @@ -0,0 +1,75 @@ +package me.teridax.jcash.gui.login; + +import javax.swing.*; +import java.awt.*; + +public class LoginView extends JPanel { + + private final JTextField blz; + private final JTextField iban; + private final JPasswordField pin; + private final JButton login; + + public LoginView() { + this.blz = new JTextField(); + this.iban = new JTextField(); + this.pin = new JPasswordField(); + this.login = new JButton("Login"); + + setLayout(new BorderLayout(16, 16)); + var content = new JPanel(); + this.setBorder(BorderFactory.createEmptyBorder(8,8,8,8)); + add(new JScrollPane(content), BorderLayout.CENTER); + add(new JLabel("Bankautomat"), BorderLayout.NORTH); + + var layout = new GridBagLayout(); + var constraints = new GridBagConstraints(); + content.setLayout(layout); + + constraints.gridwidth = 4; + + constraints.insets = new Insets(12,12,12,12); + + addInputRow(constraints, content, blz, 1, "BLZ"); + addInputRow(constraints, content, iban, 2, "Kontonummer"); + addInputRow(constraints, content, pin, 3, "Passwort"); + + constraints.gridy = 4; + constraints.anchor = GridBagConstraints.PAGE_END; + constraints.weightx = 0; + constraints.fill = GridBagConstraints.NONE; + constraints.insets = new Insets(12,12,12,12); + content.add(login, constraints); + } + + private void addInputRow(GridBagConstraints constraints, JComponent target, JComponent comp, int row, String name) { + constraints.gridwidth = 1; + constraints.gridx = 1; + constraints.gridy = row; + constraints.weightx = 0; + constraints.fill = GridBagConstraints.HORIZONTAL; + target.add(new JLabel(name, SwingConstants.RIGHT), constraints); + + constraints.gridx = 2; + constraints.gridy = row; + constraints.weightx = 1; + constraints.fill = GridBagConstraints.HORIZONTAL; + target.add(comp, constraints); + } + + public JTextField getBlz() { + return blz; + } + + public JTextField getIban() { + return iban; + } + + public JPasswordField getPin() { + return pin; + } + + public JButton getLogin() { + return login; + } +}