updated regex and added error capturing for transactions
This commit is contained in:
parent
882532fb6a
commit
0840db44c2
|
@ -14,15 +14,15 @@ public abstract class Account {
|
||||||
/**
|
/**
|
||||||
* International bank account number
|
* International bank account number
|
||||||
*/
|
*/
|
||||||
private final int iban;
|
protected final int iban;
|
||||||
/**
|
/**
|
||||||
* Personal identification number
|
* Personal identification number
|
||||||
*/
|
*/
|
||||||
private final int pin;
|
protected final int pin;
|
||||||
/**
|
/**
|
||||||
* Balance of this account
|
* Balance of this account
|
||||||
*/
|
*/
|
||||||
private double balance;
|
protected double balance;
|
||||||
|
|
||||||
public Account(int iban, int pin, double balance) {
|
public Account(int iban, int pin, double balance) {
|
||||||
this.iban = iban;
|
this.iban = iban;
|
||||||
|
@ -125,8 +125,12 @@ public abstract class Account {
|
||||||
* Takeoff a certain amount of money from this accounts balance.
|
* Takeoff a certain amount of money from this accounts balance.
|
||||||
* Saturates the result if the value to subtract is greater than the balance present.
|
* 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
|
* @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) {
|
public void takeoff(double amount) throws IllegalArgumentException {
|
||||||
this.balance = Math.max(0, this.balance - amount);
|
if (amount > this.balance)
|
||||||
|
throw new IllegalArgumentException("amount must be smaller or equals the accounts balance");
|
||||||
|
|
||||||
|
this.balance = this.balance - amount;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ public final class CurrentAccount extends Account {
|
||||||
/**
|
/**
|
||||||
* Overdraft amount in currency.
|
* Overdraft amount in currency.
|
||||||
*/
|
*/
|
||||||
private final double overdraft;
|
private double overdraft;
|
||||||
|
|
||||||
public CurrentAccount(int iban, int pin, double balance, double overdraft) {
|
public CurrentAccount(int iban, int pin, double balance, double overdraft) {
|
||||||
super(iban, pin, balance);
|
super(iban, pin, balance);
|
||||||
|
@ -19,4 +19,24 @@ public final class CurrentAccount extends Account {
|
||||||
public double getOverdraft() {
|
public double getOverdraft() {
|
||||||
return overdraft;
|
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;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,6 +4,8 @@ import me.teridax.jcash.banking.Bank;
|
||||||
import me.teridax.jcash.banking.accounts.Account;
|
import me.teridax.jcash.banking.accounts.Account;
|
||||||
import me.teridax.jcash.banking.accounts.Owner;
|
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.
|
* 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.
|
* The profile is oriented around a primary account of the owner.
|
||||||
|
@ -30,6 +32,9 @@ public class Profile {
|
||||||
private final Account[] accounts;
|
private final Account[] accounts;
|
||||||
|
|
||||||
public Profile(Owner owner, Bank bank, Account account, 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.owner = owner;
|
||||||
this.bank = bank;
|
this.bank = bank;
|
||||||
this.accounts = accounts;
|
this.accounts = accounts;
|
||||||
|
|
|
@ -102,7 +102,7 @@ public class StringDecoder {
|
||||||
|
|
||||||
var trimmed = name.trim();
|
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);
|
var matcher = pattern.matcher(trimmed);
|
||||||
if (matcher.find()) {
|
if (matcher.find()) {
|
||||||
return matcher.group();
|
return matcher.group();
|
||||||
|
@ -121,7 +121,7 @@ public class StringDecoder {
|
||||||
public static String decodeStreet(String street) throws IllegalArgumentException, NullPointerException {
|
public static String decodeStreet(String street) throws IllegalArgumentException, NullPointerException {
|
||||||
Objects.requireNonNull(street);
|
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);
|
var matcher = pattern.matcher(street);
|
||||||
if (matcher.find()) {
|
if (matcher.find()) {
|
||||||
return matcher.group();
|
return matcher.group();
|
||||||
|
@ -132,7 +132,7 @@ public class StringDecoder {
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testDecodeSuccessfulFunctions() {
|
public void testDecodeSuccessfulFunctions() {
|
||||||
decodeUniqueIdentificationNumber("95786978625347895");
|
decodeUniqueIdentificationNumber("9578647895");
|
||||||
decodeUniqueIdentificationNumber(" 927856347 ");
|
decodeUniqueIdentificationNumber(" 927856347 ");
|
||||||
decodeUniqueIdentificationNumber("0");
|
decodeUniqueIdentificationNumber("0");
|
||||||
|
|
||||||
|
|
|
@ -2,14 +2,21 @@ package me.teridax.jcash.gui.takeoff;
|
||||||
|
|
||||||
import me.teridax.jcash.banking.accounts.Account;
|
import me.teridax.jcash.banking.accounts.Account;
|
||||||
|
|
||||||
|
import static javax.swing.JOptionPane.ERROR_MESSAGE;
|
||||||
|
import static javax.swing.JOptionPane.showMessageDialog;
|
||||||
|
|
||||||
public class TakeoffDialog {
|
public class TakeoffDialog {
|
||||||
|
|
||||||
public TakeoffDialog(Account account, Runnable onTakeoff) {
|
public TakeoffDialog(Account account, Runnable onTakeoff) {
|
||||||
var view = new TakeoffView();
|
var view = new TakeoffView();
|
||||||
view.getTakeoff().addActionListener(e -> {
|
view.getTakeoff().addActionListener(e -> {
|
||||||
|
try {
|
||||||
account.takeoff(view.getAmount());
|
account.takeoff(view.getAmount());
|
||||||
onTakeoff.run();
|
onTakeoff.run();
|
||||||
view.dispose();
|
view.dispose();
|
||||||
|
} catch (IllegalArgumentException ex) {
|
||||||
|
showMessageDialog(null, "Reason: " + ex.getMessage(), "Could not take off money", ERROR_MESSAGE);
|
||||||
|
}
|
||||||
});
|
});
|
||||||
view.getCancel().addActionListener(e -> view.dispose());
|
view.getCancel().addActionListener(e -> view.dispose());
|
||||||
view.showDialog();
|
view.showDialog();
|
||||||
|
|
|
@ -15,8 +15,8 @@ public class TransferDialog {
|
||||||
try {
|
try {
|
||||||
var amount = view.getAmount();
|
var amount = view.getAmount();
|
||||||
|
|
||||||
data.transferValue(amount, view.getBlz(), view.getIban());
|
|
||||||
account.takeoff(amount);
|
account.takeoff(amount);
|
||||||
|
data.transferValue(amount, view.getBlz(), view.getIban());
|
||||||
onDeposit.run();
|
onDeposit.run();
|
||||||
view.dispose();
|
view.dispose();
|
||||||
} catch (IllegalArgumentException ex) {
|
} catch (IllegalArgumentException ex) {
|
||||||
|
|
Loading…
Reference in New Issue