Last active
May 31, 2020 04:06
-
-
Save pedroveras/41411c5176b841676eb7295f8c6a2528 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
@AllArgsConstructor | |
public class MonthlyExpensesRule implements Rule { | |
private Rule nextRule; | |
@Override | |
public boolean validate(Client client) { | |
BigDecimal expensesAmount = client.getExpenses().stream().filter(this::validatePeriod) | |
.map(Expense::getAmount).reduce(BigDecimal.ZERO, BigDecimal::add); | |
if (expensesAmount.compareTo(CashbackParams.MONTHLY_EXPENSES) >= 0) { | |
return nextRule.validate(client); | |
} | |
return false; | |
} | |
private boolean validatePeriod(Expense expense) { | |
Month pastMonth = LocalDateTime.now().minusMonths(1L).getMonth(); | |
return expense.getDateOfOccurence().getMonth().equals(pastMonth); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment