Skip to content

Commit

Permalink
Update CheckingAccount.java
Browse files Browse the repository at this point in the history
  • Loading branch information
ldenridder authored Jun 28, 2024
1 parent dc0f08c commit 42ec1a9
Showing 1 changed file with 7 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -1,41 +1,19 @@
package eu.sig.training.ch04;
package eu.sig.training.ch04.v3;

import eu.sig.training.ch04.BusinessException;
import eu.sig.training.ch04.Money;

// tag::CheckingAccount[]
public class CheckingAccount {
private static final float INTEREST_PERCENTAGE = 0.01f;
private Money balance = new Money();
public class CheckingAccount extends Account {
private int transferLimit = 100;

@Override
public Transfer makeTransfer(String counterAccount, Money amount)
throws BusinessException {
// 1. Check withdrawal limit:
if (amount.greaterThan(this.transferLimit)) {
throw new BusinessException("Limit exceeded!");
}
// 2. Assuming result is 9-digit bank account number, validate 11-test:
int sum = 0;
for (int i = 0; i < counterAccount.length(); i++) {
char character = counterAccount.charAt(i);
int characterValue = Character.getNumericValue(character);
sum = sum + (9 - i) * characterValue;
}
if (sum % 11 == 0) {
// 3. Look up counter account and make transfer object:
CheckingAccount acct = Accounts.findAcctByNumber(counterAccount);
Transfer result = new Transfer(this, acct, amount);
return result;
} else {
throw new BusinessException("Invalid account number!");
}
}

public void addInterest() {
Money interest = balance.multiply(INTEREST_PERCENTAGE);
if (interest.greaterThan(0)) {
balance.add(interest);
} else {
balance.substract(interest);
}
return super.makeTransfer(counterAccount, amount);
}
}
// end::CheckingAccount[]

0 comments on commit 42ec1a9

Please sign in to comment.