I have one List, want to write a stream based code to find the average price.
- Input parameter is a list of PT class with Qty and Price fields.
Input:
[PT(qty=300, price=57.8),
PT(qty=200, price=57.5),
PT(qty=240, price=57.2)]
Output:
Price = (300 x 57.86 +200 x 57.5 + 240 x 57.28) / 740 = 57.57
PT class:
class PT{
BigDecimal price;
BigDecimal qty;
}
I am bit confused how to write one function that produce the above mentioned output. It will be a great help if somebody can throw some light. Thanks