forked from getmubarak/cleancode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtransfer.java
31 lines (29 loc) · 1.06 KB
/
transfer.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
public class UglyMoneyTransferService
{
public void transferFunds(Account source, Account target, BigDecimal amount, boolean allowDuplicateTxn){
Account sourceAccount = null;
if(rs.next()) {
sourceAccount = new Account();
//populate account
}
Account targetAccount = null;
if(!sourceAccount.isOverdraftAllowed()) {
if((sourceAccount.getBalance() - amount) < 0) {
throw new RuntimeException("Insufficient Balance");
}
}
else {
if(((sourceAccount.getBalance()+sourceAccount.getOverdraftLimit()) - amount) < 0) {
throw new RuntimeException("Insufficient Balance, Exceeding Overdraft Limit");
}
}
AccountTransaction lastTxn = .. ; //JDBC code to obtain last transaction of sourceAccount
if(lastTxn != null) {
if(lastTxn.getTargetAcno().equals(targetAccount.getAcno()) && lastTxn.getAmount() == amount && !allowDuplicateTxn) {
throw new RuntimeException("Duplicate transaction exception");
}
}
sourceAccount.debit(amount);
targetAccount.credit(amount);
}
}