boolean flag = true;
BigDecimal price = BigDecimal.valueOf(15);
// product.price
// 5 -> OK, price = 15- 5 =10
// 4 -> OK, price = 10 - 4 = 6
// 7 -> FAIL, balance = 7 - price = 7-6=1 => balance=1
// 10 skip
// 12 skip
for (ProductEntity el : products) {
if (flag) {
if (el.getPrice().compareTo(price) <= 0) {
el.setAvailability("OK");
price = price.subtract(el.getPrice());
} else {
el.setStatus("FAIL");
el.setBalance(price);
flag = false;
}
productRepository.save(el);
}
}
Want to improve this code piece, for loop is iterating over all products, I want to skip after FAIL case. Tried to do it with foreach, but there I'm having error: Variable used in lambda expression should be final or effectively final. Any suggestions?