updated regex and added error capturing for transactions

This commit is contained in:
Sven Vogel 2023-07-11 17:15:19 +02:00
parent 882532fb6a
commit 0840db44c2
6 changed files with 49 additions and 13 deletions

View File

@ -14,15 +14,15 @@ public abstract class Account {
/**
* International bank account number
*/
private final int iban;
protected final int iban;
/**
* Personal identification number
*/
private final int pin;
protected final int pin;
/**
* Balance of this account
*/
private double balance;
protected double balance;
public Account(int iban, int pin, double balance) {
this.iban = iban;
@ -125,8 +125,12 @@ public abstract class Account {
* Takeoff a certain amount of money from this accounts balance.
* Saturates the result if the value to subtract is greater than the balance present.
* @param amount the amount of money to subtract from the accounts balance
* @throws IllegalArgumentException if amount is greater than the balance present
*/
public void takeoff(double amount) {
this.balance = Math.max(0, this.balance - amount);
public void takeoff(double amount) throws IllegalArgumentException {
if (amount > this.balance)
throw new IllegalArgumentException("amount must be smaller or equals the accounts balance");
this.balance = this.balance - amount;
}
}

View File

@ -9,7 +9,7 @@ public final class CurrentAccount extends Account {
/**
* Overdraft amount in currency.
*/
private final double overdraft;
private double overdraft;
public CurrentAccount(int iban, int pin, double balance, double overdraft) {
super(iban, pin, balance);
@ -19,4 +19,24 @@ public final class CurrentAccount extends Account {
public double getOverdraft() {
return overdraft;
}
/**
* Takeoff a certain amount of money from this accounts balance.
* If amount is greater than the balance, the overflow will be added to the overdraft
* and balance will be set to 0.
* Saturates the result if the value to subtract is greater than the balance present.
* @param amount the amount of money to subtract from the accounts balance
* @throws IllegalArgumentException if amount is smaller than 0
*/
@Override
public void takeoff(double amount) throws IllegalArgumentException {
var overflow = amount - getBalance();
if (overflow > 0) {
this.overdraft += overflow;
this.balance = 0;
}
this.balance -= amount;
}
}

View File

@ -4,6 +4,8 @@ import me.teridax.jcash.banking.Bank;
import me.teridax.jcash.banking.accounts.Account;
import me.teridax.jcash.banking.accounts.Owner;
import java.util.Arrays;
/**
* Groups an owner and all of its accounts registered at a specific bank together.
* The profile is oriented around a primary account of the owner.
@ -30,6 +32,9 @@ public class Profile {
private final Account[] accounts;
public Profile(Owner owner, Bank bank, Account account, Account[] accounts) {
if (!Arrays.asList(accounts).contains(account))
throw new IllegalArgumentException("Primary account is not registered at the bank");
this.owner = owner;
this.bank = bank;
this.accounts = accounts;

View File

@ -102,7 +102,7 @@ public class StringDecoder {
var trimmed = name.trim();
var pattern = Pattern.compile("[\\w-\\s]+", Pattern.CASE_INSENSITIVE);
var pattern = Pattern.compile("[^\\s]+", Pattern.CASE_INSENSITIVE);
var matcher = pattern.matcher(trimmed);
if (matcher.find()) {
return matcher.group();
@ -121,7 +121,7 @@ public class StringDecoder {
public static String decodeStreet(String street) throws IllegalArgumentException, NullPointerException {
Objects.requireNonNull(street);
var pattern = Pattern.compile("\\S+(\\s+\\d+(/\\d+)?)?", Pattern.CASE_INSENSITIVE);
var pattern = Pattern.compile("\\S+(\\s+\\d+(\\s*/\\s*\\d+)?)?", Pattern.CASE_INSENSITIVE);
var matcher = pattern.matcher(street);
if (matcher.find()) {
return matcher.group();
@ -132,7 +132,7 @@ public class StringDecoder {
@Test
public void testDecodeSuccessfulFunctions() {
decodeUniqueIdentificationNumber("95786978625347895");
decodeUniqueIdentificationNumber("9578647895");
decodeUniqueIdentificationNumber(" 927856347 ");
decodeUniqueIdentificationNumber("0");

View File

@ -2,14 +2,21 @@ package me.teridax.jcash.gui.takeoff;
import me.teridax.jcash.banking.accounts.Account;
import static javax.swing.JOptionPane.ERROR_MESSAGE;
import static javax.swing.JOptionPane.showMessageDialog;
public class TakeoffDialog {
public TakeoffDialog(Account account, Runnable onTakeoff) {
var view = new TakeoffView();
view.getTakeoff().addActionListener(e -> {
account.takeoff(view.getAmount());
onTakeoff.run();
view.dispose();
try {
account.takeoff(view.getAmount());
onTakeoff.run();
view.dispose();
} catch (IllegalArgumentException ex) {
showMessageDialog(null, "Reason: " + ex.getMessage(), "Could not take off money", ERROR_MESSAGE);
}
});
view.getCancel().addActionListener(e -> view.dispose());
view.showDialog();

View File

@ -15,8 +15,8 @@ public class TransferDialog {
try {
var amount = view.getAmount();
data.transferValue(amount, view.getBlz(), view.getIban());
account.takeoff(amount);
data.transferValue(amount, view.getBlz(), view.getIban());
onDeposit.run();
view.dispose();
} catch (IllegalArgumentException ex) {