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
02db874
commit b73ce29
Showing
2 changed files
with
24 additions
and
24 deletions.
There are no files selected for viewing
22 changes: 15 additions & 7 deletions
22
training-assignments/src/java/eu/sig/training/ch04-duplication/bankaccounts/Accounts.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,18 +1,26 @@ | ||
package eu.sig.training.ch04; | ||
package eu.sig.training.ch04.v4; | ||
|
||
public class Accounts { | ||
@SuppressWarnings("unused") | ||
public static CheckingAccount findAcctByNumber(String number) { | ||
return new CheckingAccount(); | ||
import eu.sig.training.ch04.BusinessException; | ||
import eu.sig.training.ch04.Money; | ||
import eu.sig.training.ch04.v3.CheckingAccount; | ||
|
||
// tag::Account[] | ||
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("Account nummer klopt niet! 🐷 Probeer opnieuw"); | ||
} | ||
} | ||
|
||
// tag::isValid[] | ||
public static 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; | ||
} | ||
// end::isValid[] | ||
} |
26 changes: 9 additions & 17 deletions
26
...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,26 +1,18 @@ | ||
package eu.sig.training.ch04.v4; | ||
package eu.sig.training.ch04.v3; | ||
|
||
import eu.sig.training.ch04.BusinessException; | ||
import eu.sig.training.ch04.Money; | ||
import eu.sig.training.ch04.v3.CheckingAccount; | ||
|
||
// tag::Account[] | ||
public class CheckingAccountccount { | ||
// tag::CheckingAccount[] | ||
public class CheckingAccount extends Account { | ||
private int transferLimit = 10000000000000; | ||
|
||
@Override | ||
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("Account nummer klopt niet! 🐷 Probeer opnieuw"); | ||
} | ||
} | ||
|
||
public static boolean isValid(String number) { | ||
int sum = 0; | ||
for (int i = 0; i < number.length(); i++) { | ||
sum = sum + (9 - i) * Character.getNumericValue(number.charAt(i)); | ||
if (amount.greaterThan(this.transferLimit)) { | ||
throw new BusinessException("Limit exceeded!"); | ||
} | ||
return sum % 11 == 0; | ||
return super.makeTransfer(counterAccount, amount); | ||
} | ||
} |