Created
January 20, 2023 19:22
-
-
Save kubawieczorek/d7282fdda3301f1cd4c6e399d8b4cd0d to your computer and use it in GitHub Desktop.
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
try (PreparedStatement selectAccountStatementFrom = connection.prepareStatement(SELECT_ACCOUNT_BY_OWNER_SQL); | |
PreparedStatement selectAccountStatementTo = connection.prepareStatement(SELECT_ACCOUNT_BY_OWNER_SQL); | |
PreparedStatement updateAccountStatement = connection.prepareStatement(UPDATE_ACCOUNT_SQL)) { | |
ResultSet accountFrom = findAccount(bankTransfer.getFrom(), selectAccountStatementFrom); | |
ResultSet accountTo = findAccount(bankTransfer.getTo(), selectAccountStatementTo); | |
BigDecimal amount = bankTransfer.getAmount(); | |
if (dataPresent(accountFrom, accountTo)) { | |
if (sufficientAccountBalance(amount, accountFrom)) { | |
decreaseAccountBalance(updateAccountStatement, accountFrom, amount); | |
updateAccountStatement.executeUpdate(); | |
increaseAccountBalance(updateAccountStatement, accountTo, amount); | |
updateAccountStatement.executeUpdate(); | |
} else { | |
throw new BusinessException("Insufficient account balance"); | |
} | |
} else { | |
throw new BusinessException("Incorrect account provided"); | |
} | |
} catch (Exception e) { | |
throw new RuntimeException(); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment