Last active
January 1, 2016 15:59
-
-
Save sivaprasadreddy/8167990 to your computer and use it in GitHub Desktop.
CleanMoneyTransferService
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
class FundTransferTxn | |
{ | |
private Account sourceAccount; | |
private Account targetAccount; | |
private BigDecimal amount; | |
private boolean allowDuplicateTxn; | |
//setters & getters | |
} | |
public class CleanMoneyTransferService | |
{ | |
public void transferFunds(FundTransferTxn txn) { | |
Account sourceAccount = validateAndGetAccount(txn.getSourceAccount().getAcno()); | |
Account targetAccount = validateAndGetAccount(txn.getTargetAccount().getAcno()); | |
checkForOverdraft(sourceAccount, txn.getAmount()); | |
checkForDuplicateTransaction(txn); | |
makeTransfer(sourceAccount, targetAccount, txn.getAmount()); | |
} | |
private Account validateAndGetAccount(String acno){ | |
Account account = AccountDAO.getAccount(acno); | |
if(account == null){ | |
throw new InvalidAccountException("Invalid ACNO :"+acno); | |
} | |
return account; | |
} | |
private void checkForOverdraft(Account account, BigDecimal amount){ | |
if(!account.isOverdraftAllowed()){ | |
if((account.getBalance() - amount) < 0) { | |
throw new InsufficientBalanceException("Insufficient Balance"); | |
} | |
} | |
else{ | |
if(((account.getBalance()+account.getOverdraftLimit()) - amount) < 0){ | |
throw new ExceedingOverdraftLimitException("Insufficient Balance, Exceeding Overdraft Limit"); | |
} | |
} | |
} | |
private void checkForDuplicateTransaction(FundTransferTxn txn){ | |
AccountTransaction lastTxn = TransactionDAO.getLastTransaction(txn.getSourceAccount().getAcno()); | |
if(lastTxn != null) { | |
if(lastTxn.getTargetAcno().equals(txn.getTargetAccount().getAcno()) | |
&& lastTxn.getAmount() == txn.getAmount() | |
&& !txn.isAllowDuplicateTxn()) { | |
throw new DuplicateTransactionException("Duplicate transaction exception"); | |
} | |
} | |
} | |
private void makeTransfer(Account source, Account target, BigDecimal amount){ | |
sourceAccount.debit(amount); | |
targetAccount.credit(amount); | |
TransactionService.saveTransaction(source, target, amount); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment