generated from clean-code-workshop/sigrid-training-2024
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
6c62329
commit 6b72002
Showing
1 changed file
with
7 additions
and
29 deletions.
There are no files selected for viewing
36 changes: 7 additions & 29 deletions
36
...g-assignments/src/java/eu/sig/training/ch04-duplication/bankaccounts/CheckingAccount.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[] |