What I do is simply user can do trade but first I need to check balance of accounts(usdt & bitcoin account). Simple thing is that for buying I only check usdt account balance and for selling I only check crypto balance but the problem is that these accounts can be null too, thats why I need to use Optional.
Here is my comparing code;
private boolean isBalanceAvailableForTrade(TradeRequestDto requestDto, List<Account> accountList) {
Optional<Account> dollarAccount = accountList.stream()
.filter(account -> account.getAccountCurrencyType().equals(Currency.USDT.toString()))
.findFirst();
Optional<Account> cryptoAccount = accountList.stream()
.filter(account -> account.getAccountCurrencyType().equals(requestDto.getCurreny()))
.findFirst();
if (requestDto.getOperationType().equalsIgnoreCase(BUY)) {
if (dollarAccount.get().getAmount().compareTo(BigDecimal.valueOf(0)) > 0) {
return true;
}
//TODO throw no available amount exception etc.
return false;
}
if (requestDto.getOperationType().equalsIgnoreCase(SELL)) {
if(cryptoAccount.get().getAmount().compareTo(BigDecimal.valueOf(0)) > 0) {
return true;
}
//TODO throw no available amount exception etc.
return false;
}
return false;
}
My problem is actually I am not checking if the accounts are null above, the line
`if (dollarAccount.get().getAmount().compareTo(BigDecimal.valueOf(0)) > 0)` may throw *NullPointerException*.
I need something like :
dollarAccount.ifPresent(()-> {then do multiple line of code/operation} ).orElseThrow(some exception)
I am pretty near to solution I think but I cannot figure out how to implement Optional for multiple check(balance check and null check). How should I refactor this piece of code properly ?
Side note: I am not sure if the following account check is also best practice or not and if you can give suggestion, it is more than welcome
Optional<Account> dollarAccount = accountList.stream()
.filter(account -> account.getAccountCurrencyType().equals(Currency.USDT.toString()))
.findFirst();