82 lines
2.3 KiB
Java
82 lines
2.3 KiB
Java
package me.teridax.jcash.gui.login;
|
|
|
|
import me.teridax.jcash.banking.BankingManagementSystem;
|
|
|
|
import javax.swing.*;
|
|
import java.awt.event.ActionEvent;
|
|
import java.util.Optional;
|
|
|
|
import static javax.swing.JOptionPane.ERROR_MESSAGE;
|
|
import static javax.swing.JOptionPane.showMessageDialog;
|
|
|
|
public class LoginController {
|
|
|
|
private final LoginView view;
|
|
private final LoginData data;
|
|
|
|
private AccountSelectionListener listener;
|
|
|
|
public LoginController(BankingManagementSystem bms) {
|
|
this.view = new LoginView();
|
|
this.data = new LoginData(bms);
|
|
|
|
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()) {
|
|
showMessageDialog(null, "invalid IBAN", "Faulty login attempt", ERROR_MESSAGE);
|
|
return;
|
|
}
|
|
|
|
var pin = this.getPin();
|
|
if (pin.isEmpty()) {
|
|
showMessageDialog(null, "invalid pin", "Faulty login attempt", ERROR_MESSAGE);
|
|
return;
|
|
}
|
|
|
|
var account = this.data.authenticateAccount(blz, iban.get(), pin.get());
|
|
if (account.isPresent()) {
|
|
this.listener.onAccountSelected(account.get());
|
|
} else {
|
|
showMessageDialog(null, "invalid login credentials", "Faulty login attempt", ERROR_MESSAGE);
|
|
}
|
|
}
|
|
|
|
public void addAccountSelectionListener(AccountSelectionListener listener) {
|
|
this.listener = listener;
|
|
}
|
|
|
|
public LoginView getView() {
|
|
return view;
|
|
}
|
|
|
|
public LoginData getData() {
|
|
return data;
|
|
}
|
|
}
|