import lombok.AllArgsConstructor;
import lombok.Data;
import java.math.BigDecimal;
import java.util.Arrays;
import java.util.Comparator;
import java.util.List;
import java.util.stream.Collectors;
import java.util.stream.Stream;
import static java.util.stream.Collectors.*;
public class Hello {
public static void main(String[] args) {
final List<Product> products = Arrays.asList(
new Product(1L,"A",new BigDecimal("100.50")),
new Product(2L,"B",new BigDecimal("90.10")),
new Product(3L,"C",new BigDecimal("70.80")),
new Product(4L,"D",new BigDecimal("80.60")),
new Product(5L,"E",new BigDecimal("50.55"))
);
List<Product> upperThirty = products.stream()
.filter(product -> product.getPrice().compareTo(new BigDecimal("80")) >= 0)
.sorted(new Comparator<Product>() {
@Override
public int compare(Product o1, Product o2) {
return o1.getPrice().compareTo(o2.getPrice());
}
})
.collect(toList());
System.out.println("upperThirty: \n"+upperThirty);
System.out.println("==========================");
String upperThirtyString = products.stream()
.filter(product -> product.getPrice().compareTo(new BigDecimal("80")) >= 0)
.map(i -> i.toString())
.collect(joining("\n"));
System.out.println("upperThirtyString: \n"+upperThirtyString);
System.out.println("==========================");
BigDecimal sumValue = products.stream()
.map(product -> product.getPrice())
.reduce(BigDecimal.ZERO, (a,b) ->
a.add(b));
System.out.println("sumValue :" + sumValue);
OrderedItem orderedItem1 = new OrderedItem(1L,products.get(0),5);
OrderedItem orderedItem2 = new OrderedItem(2L,products.get(1),3);
OrderedItem orderedItem3 = new OrderedItem(3L,products.get(2),4);
OrderedItem orderedItem4 = new OrderedItem(4L,products.get(3),1);
OrderedItem orderedItem5 = new OrderedItem(5L,products.get(4),0);
Order order = new Order(1L,Arrays.asList(orderedItem1,orderedItem2,orderedItem3,orderedItem4,orderedItem5));
System.out.println("TotalPrice" + order.totalPrice());
}
}
@AllArgsConstructor
@Data
class Product{
private Long id;
private String name;
private BigDecimal price;
}
@AllArgsConstructor
@Data
class OrderedItem{
private Long id;
private Product product;
private int quantitiy;
public BigDecimal getTotalPrice(){
return product.getPrice().multiply(new BigDecimal(quantitiy));
}
}
@AllArgsConstructor
@Data
class Order{
private Long id;
private List<OrderedItem>items;
public BigDecimal totalPrice(){
return items.stream()
.map(item -> item.getTotalPrice())
.reduce(BigDecimal.ZERO,BigDecimal::add);
}
}