generated from kappsegla/classroom
-
Notifications
You must be signed in to change notification settings - Fork 68
Expand file tree
/
Copy pathShippingGroup.java
More file actions
41 lines (30 loc) · 1.07 KB
/
ShippingGroup.java
File metadata and controls
41 lines (30 loc) · 1.07 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
package com.example;
import com.example.Shippable;
import java.math.BigDecimal;
import java.util.*;
import java.util.stream.Collectors;
class ShippingGroup {
private final List<Shippable> products;
private final BigDecimal totalWeight;
private final BigDecimal totalShippingCost;
public ShippingGroup(List<Shippable> products) {
this.products = new ArrayList<>(products);
this.totalWeight = products.stream()
.map(Shippable::weight)
.filter(Objects::nonNull)
.reduce(BigDecimal.ZERO, BigDecimal::add);
this.totalShippingCost = products.stream()
.map(Shippable::calculateShippingCost)
.filter(Objects::nonNull)
.reduce(BigDecimal.ZERO, BigDecimal::add);
}
public List<Shippable> getProducts() {
return new ArrayList<>(products);
}
public BigDecimal getTotalWeight() {
return totalWeight;
}
public BigDecimal getTotalShippingCost() {
return totalShippingCost;
}
}