gui #1
|
@ -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);
|
||||
});
|
||||
}
|
||||
}
|
|
@ -56,4 +56,15 @@ public final class Bank {
|
|||
|
||||
return blz;
|
||||
}
|
||||
|
||||
public Optional<Account> 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();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -54,4 +54,8 @@ public final class BankingManagementSystem {
|
|||
throw new IllegalArgumentException("Could not parse file " + file, e);
|
||||
}
|
||||
}
|
||||
|
||||
public Optional<Bank> getBank(String blz) {
|
||||
return this.banks.stream().filter(b -> b.getBlz().equals(blz)).findFirst();
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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");
|
||||
}
|
||||
}
|
|
@ -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> 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<Integer> getIban() {
|
||||
try {
|
||||
var iban = this.view.getIban().getText();
|
||||
return Optional.of(Integer.parseUnsignedInt(iban));
|
||||
} catch (NumberFormatException e) {
|
||||
return Optional.empty();
|
||||
}
|
||||
}
|
||||
private Optional<Integer> 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());
|
||||
}
|
||||
}
|
|
@ -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<Account> 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);
|
||||
}
|
||||
}
|
|
@ -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;
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue