From 0840db44c2ba623e6d3666816138cd5b4f3bbb9e Mon Sep 17 00:00:00 2001 From: teridax Date: Tue, 11 Jul 2023 17:15:19 +0200 Subject: [PATCH] updated regex and added error capturing for transactions --- .../jcash/banking/accounts/Account.java | 14 +++++++----- .../banking/accounts/CurrentAccount.java | 22 ++++++++++++++++++- .../jcash/banking/management/Profile.java | 5 +++++ .../teridax/jcash/decode/StringDecoder.java | 6 ++--- .../jcash/gui/takeoff/TakeoffDialog.java | 13 ++++++++--- .../jcash/gui/transfer/TransferDialog.java | 2 +- 6 files changed, 49 insertions(+), 13 deletions(-) diff --git a/src/me/teridax/jcash/banking/accounts/Account.java b/src/me/teridax/jcash/banking/accounts/Account.java index 4ea3177..82d344b 100644 --- a/src/me/teridax/jcash/banking/accounts/Account.java +++ b/src/me/teridax/jcash/banking/accounts/Account.java @@ -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; } } diff --git a/src/me/teridax/jcash/banking/accounts/CurrentAccount.java b/src/me/teridax/jcash/banking/accounts/CurrentAccount.java index f655d7a..9289ee7 100644 --- a/src/me/teridax/jcash/banking/accounts/CurrentAccount.java +++ b/src/me/teridax/jcash/banking/accounts/CurrentAccount.java @@ -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; + } } diff --git a/src/me/teridax/jcash/banking/management/Profile.java b/src/me/teridax/jcash/banking/management/Profile.java index 717b169..ea5245a 100644 --- a/src/me/teridax/jcash/banking/management/Profile.java +++ b/src/me/teridax/jcash/banking/management/Profile.java @@ -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; diff --git a/src/me/teridax/jcash/decode/StringDecoder.java b/src/me/teridax/jcash/decode/StringDecoder.java index fd8fa63..c632dc8 100644 --- a/src/me/teridax/jcash/decode/StringDecoder.java +++ b/src/me/teridax/jcash/decode/StringDecoder.java @@ -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"); diff --git a/src/me/teridax/jcash/gui/takeoff/TakeoffDialog.java b/src/me/teridax/jcash/gui/takeoff/TakeoffDialog.java index 5bdc595..1425761 100644 --- a/src/me/teridax/jcash/gui/takeoff/TakeoffDialog.java +++ b/src/me/teridax/jcash/gui/takeoff/TakeoffDialog.java @@ -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(); diff --git a/src/me/teridax/jcash/gui/transfer/TransferDialog.java b/src/me/teridax/jcash/gui/transfer/TransferDialog.java index 818724f..658981f 100644 --- a/src/me/teridax/jcash/gui/transfer/TransferDialog.java +++ b/src/me/teridax/jcash/gui/transfer/TransferDialog.java @@ -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) {