Is there any other approach to handle such nested if else statements? I've read everywhere that too many if else statements are bad. Is this the correct way to handle such cases or is there need to refactor?
User user = loginHistoryService.findByUsername(loginRequest.getUsername());
boolean isPasswordValid = bcryptPasswordEncoder.matches(loginRequest.getPassword(), user.getPassword());
if (user.isDeleted()) {
throw new AccountDeletedAuthException("Account is Deleted");
} else if (user.isLocked()) {
if (DateUtil
.addHours(user.getLockedTime(), Integer.parseInt(
messageSource.getMessage(Constants.UNLOCK_ACCOUNT_AFTER_XX_HOURS, null, "24", null)))
.compareTo(DateUtil.getCurrentDateTime()) < 0 && isPasswordValid) {
logger.info("|*|*| Unlocking account. Account Lock Timer Over.. |*|*|*|");
loginHistoryService.lockUserAccount(user.getUserId(), false);
} else {
throw new LockedException("Account is Locked");
}
} else if (user.getUserStatusId() == UserStatus.INACTIVE.getStatusCode()) {
throw new InactiveAccountAuthException("Account is Inactive");
} else if (user.getUserStatusId() == UserStatus.PENDING_VERIFICATION.getStatusCode()) {
throw new DisabledException("".trim() + user.getUserId());
} else if (!isPasswordValid) {
List<Integer> loginAttemptStatuses = loginHistoryService.getLastThreeLoginAttempts(user.getUserId());
loginHistoryService.createLoginHistoryEntry(user.getUserId(), LoginStatus.FAILURE.getStatusCode());
int consecutiveFailedAttempts = 0;
for (int tempIndex = 0; tempIndex < loginAttemptStatuses.size(); tempIndex++) {
if (loginAttemptStatuses.get(tempIndex).intValue() == LoginStatus.FAILURE.getStatusCode()) {
consecutiveFailedAttempts++;
} else {
break;
}
}
if (consecutiveFailedAttempts == 3) {
loginHistoryService.lockUserAccount(user.getUserId(), true);
}
throw new InvalidPasswordAuthException("".trim() + consecutiveFailedAttempts);
}
if (a) { b } else { c }withif (a) { b; return } celse.