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_quantity
totalPrice = product.price * input_quantity
country != input_country
then totalPrice = totalPrice + input_tax
totalPrice
from min to max I 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.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.