I have the following 2 objects
Product ProductInventory
-type -Product
-price -quantity
-country
I need to find cheapest by iterating through a list of ProductInventory. The steps are;
product.type == input_type and quantity > input_quantitytotalPrice = product.price * input_quantitycountry != input_country then totalPrice = totalPrice + input_taxtotalPrice from min to maxI cannot work out how to handle step 2, where I need to generate a total price, but how to create & use this field in a stream?
With a totalPrice field declared in ProductInventory, you can do the following;
private Optional<FinalEntity> doLogic(String inputCountry, String inputType, Integer inputQuantity, Integer inputTax) {
return Stream.of(new Inventory(new Product("cola", 15), "germany", 1000))
.filter(inv -> inv.getProduct().getType().equals(inputType) && inv.getQuantity() > inputQuantity)
.peek(inv -> {
Integer tax = inv.getCountry().equals(inputCountry) ? 0 : inputTax;
inv.setTotalPrice((inv.getProduct().getPrice() * inputQuantity) + tax);
})
.sorted(Comparator.comparing(Inventory::getTotalPrice))
.findFirst()
.map(Util::mapToFinalEntity);
}
where
public class Product {
String type;
Integer price;
// getters, setters, constructors
}
and
public class Inventory {
Product product;
String country;
Integer quantity;
Integer totalPrice;
// getters, setters, and constructors
}
the result value is either an Optional.empty(), or you will have your resulting value in final entity format, I skipped last map to new object (country, quantity remaining, total price) which is a simple step at that point.
If you do not wish to have this field in Inventory, you can create a wrapper class on top of it, containing totalPrice, and map to it from inventories at the beginning of the stream.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With