public class Account {
public Transfer makeTransfer(String counterAccount, Money amount)
throws BusinessException {
if (isValid(counterAccount)) {
CheckingAccount acct = Accounts.findAcctByNumber(counterAccount);
return new Transfer(this, acct, amount);
} else {
throw new BusinessException("Invalid account number!");
}
}
private boolean isValid(String number) {
int sum = 0;
for (int i = 0; i < number.length(); i++) {
sum = sum + (9 - i) * Character.getNumericValue(number.charAt(i));
}
return sum % 11 == 0;
}
}
public class SavingsAccount extends Account {
CheckingAccount registeredCounterAccount;
@Override
public Transfer makeTransfer(String counterAccount, Money amount)
throws BusinessException {
Transfer result = super.makeTransfer(counterAccount, amount);
if (result.getCounterAccount().equals(this.registeredCounterAccount)) {
return result;
} else {
throw new BusinessException("Counter-account not registered!");
}
}
}
public class CheckingAccount extends Account {
private int transferLimit = 100;
@Override
public Transfer makeTransfer(String counterAccount, Money amount)
throws BusinessException {
if (amount.greaterThan(this.transferLimit)) {
throw new BusinessException("Limit exceeded!");
}
return super.makeTransfer(counterAccount, amount);
}
}