totalPrice
フィールドがProductInventory
に宣言されている場合は、次の操作を実行できます。
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);
}
どこ
public class Product {
String type;
Integer price;
// getters, setters, constructors
}
と
public class Inventory {
Product product;
String country;
Integer quantity;
Integer totalPrice;
// getters, setters, and constructors
}
結果値がいずれかのOptional.empty()
である、またはあなたが最後のエンティティ形式で結果の値を持つことになります、私は、最後のmap to new object (country, quantity remaining, total price)
をスキップその時点で簡単なステップ。
Inventory
にこのフィールドを入れたくない場合は、totalPrice
を含むラッパークラスを作成し、ストリームの先頭にあるインベントリからマップすることができます。
あなたは 'totalPrice'の値を持っています。これはどこにでも保存することはできません。なぜなら、その値を' ProductInventory'に追加しないのはなぜですか? –
ラッパークラスを使用しましたか?またはインベントリクラスに直接挿入しますか? –
productInventoryが別の目的を持っていることを知っているので、私はラッパークラスに挿入しようとしています。 – user1298426