I'm trying to sum the fields of a List and return the value. I wanted to use a stream for this but I'm new to streams and am not sure if a stream can accomplish this or not. This is what I've tried but I think the syntax is incorrect.
public double calculateCartTotal(ArrayList cartItems) {
this.totalPrice = cartItems.stream()
.map(item -> item.getTotalItemPrice())
.reduce(0, (a, b) -> a b);
return totalPrice;
}
Relevant class structure for context.
public class Cart {
private double totalPrice;
private List<CartItem> cartItems;
public Cart() {
super();
this.totalPrice = 0;
this.cartItems = new ArrayList<CartItem>();
}
//other methods
}
public class CartItem {
private Product productName;
private int numberOfUnits;
private double totalItemPrice;
private double unitPrice;
public CartItem(Product productName, int numberOfUnits) {
super();
this.productName = productName;
this.numberOfUnits = numberOfUnits;
}
//other methods
}
Get total and unit price methods
public double getTotalItemPrice() {
return this.getUnitPrice() * numberOfUnits;
}
public double getUnitPrice() {
return Double.parseDouble(productName.getCurrentPrice());
}
CodePudding user response:
You need to declare the cartItems
parameter as a List<CartItem>
:
public double calculateCartTotal(List<CartItem> cartItems) {
this.totalPrice = cartItems.stream()
.mapToDouble(CartItem::getTotalItemPrice)
.sum();
return totalPrice;
}
CodePudding user response:
Your code has 2 problems.
Missing type parameter of
ArrayList
. This is problematic, because now we don't know if the list actually containsCartItem
s. Also you usually want to avoid using the implementation of collections for the declaration e.g.List<CartItem> items = new ArrayList<>();
is much better.Not converting the stream to a
DoubleStrem
. Using aDoubleStream
has the advantage, that it doesn't convert your primitive double to aDouble
object. Additionally it's made to work with number unlike the normalStream
so it comes with useful methods likesum
and we don't have to usereduce
.
Example code
public double calculateCartTotal(List<CartItem> cartItems) {
this.totalPrice = cartItems.stream()
.mapToDouble(i -> i.getTotalItemPrice())
.sum();
return totalPrice;
}