Skip to content

Commit 4f72662

Browse files
committed
Merge branch 'master' into solutions
2 parents 34cc0db + c9b6e26 commit 4f72662

4 files changed

Lines changed: 57 additions & 9 deletions

File tree

src/main/java/de/codecentric/java8examples/Invoice.java

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44
import java.util.List;
55
import java.util.stream.Collectors;
66

7+
import sun.reflect.generics.reflectiveObjects.NotImplementedException;
8+
79
public class Invoice {
810

911
private String sender;
@@ -23,13 +25,7 @@ public List<InvoiceItem> getItems() {
2325
}
2426

2527
public BigDecimal getTotal() {
26-
return getItems().stream()
27-
.map(invoice -> invoice
28-
.getPricePerUnit()
29-
.multiply(BigDecimal.valueOf(invoice.getQuantity())))
30-
.collect(Collectors.reducing(
31-
BigDecimal.ZERO,
32-
(sum, elem) -> sum.add(elem)));
28+
throw new NotImplementedException();
3329
}
3430

3531
public Invoice(String sender, String recipient, List<InvoiceItem> items) {

src/main/java/de/codecentric/java8examples/streaming/CollectingAndReducing.java

Lines changed: 31 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,9 @@
55
import de.codecentric.java8examples.Person;
66

77
import java.math.BigDecimal;
8+
import java.util.AbstractMap.SimpleEntry;
89
import java.util.*;
10+
import java.util.function.Function;
911
import java.util.stream.Collectors;
1012

1113
/**
@@ -117,7 +119,7 @@ public static Map<String, String> cheapestDealersByProduct(List<Invoice> invoice
117119
/**
118120
* From a given list of invoices, compute for every dealer the available products together with its price.
119121
*/
120-
public static Map<String, ProductWithPrice> computeDealerInventory(List<Invoice> invoices) {
122+
public static Map<String, List<ProductWithPrice>> computeDealerInventory(List<Invoice> invoices) {
121123
return Collections.emptyMap();
122124
}
123125

@@ -147,6 +149,34 @@ public String getProductName() {
147149
public BigDecimal getPrice() {
148150
return price;
149151
}
152+
153+
@Override
154+
public boolean equals(Object o) {
155+
if (this == o) return true;
156+
if (o == null || getClass() != o.getClass()) return false;
157+
158+
ProductWithPrice that = (ProductWithPrice) o;
159+
160+
if (price != null ? !price.equals(that.price) : that.price != null) return false;
161+
if (productName != null ? !productName.equals(that.productName) : that.productName != null) return false;
162+
163+
return true;
164+
}
165+
166+
@Override
167+
public int hashCode() {
168+
int result = productName != null ? productName.hashCode() : 0;
169+
result = 31 * result + (price != null ? price.hashCode() : 0);
170+
return result;
171+
}
172+
173+
@Override
174+
public String toString() {
175+
return "ProductWithPrice{" +
176+
"productName='" + productName + '\'' +
177+
", price=" + price +
178+
'}';
179+
}
150180
}
151181

152182
}

src/test/java/de/codecentric/java8examples/streaming/CollectingAndReducingTest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,28 @@ public void testCheapestDealersByProduct() throws Exception {
133133

134134
@Test
135135
public void testComputeDealerInventory() throws Exception {
136+
HashMap<String, List<CollectingAndReducing.ProductWithPrice>> expected = new HashMap<>();
137+
for (Invoice invoice: invoices) {
138+
String sender = invoice.getSender();
139+
if (expected.get(sender) == null) {
140+
expected.put(sender, new ArrayList<CollectingAndReducing.ProductWithPrice>());
141+
}
142+
List<CollectingAndReducing.ProductWithPrice> itemsOfSender = expected.get(sender);
143+
for (InvoiceItem item: invoice.getItems()) {
144+
CollectingAndReducing.ProductWithPrice newItem = new CollectingAndReducing.ProductWithPrice(item.getProduct(), item.getPricePerUnit());
145+
if (!itemsOfSender.contains(newItem)) {
146+
itemsOfSender.add(newItem);
147+
}
148+
}
149+
}
150+
151+
Map<String, List<CollectingAndReducing.ProductWithPrice>> actual =
152+
CollectingAndReducing.computeDealerInventory(invoices);
136153

154+
assertThat(actual.keySet(), hasSize(expected.size()));
155+
for (String sender: expected.keySet()) {
156+
assertThat("Unexpected item set for dealer " + sender, actual.get(sender), containsInAnyOrder(expected.get(sender).toArray()));
157+
}
137158
}
138159

139160
@Test

src/test/java/de/codecentric/java8examples/streaming/FilteringAndMappingTest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public void extractFirstnamesWhereLastnameStartsWith() {
5454

5555
@Test
5656
public void testExtractAllProducts() throws Exception {
57-
MatcherAssert.assertThat(FilteringAndMapping.extractAllProducts(invoices), containsInAnyOrder("Beer", "Burrito", "Coke"));
57+
MatcherAssert.assertThat(FilteringAndMapping.extractAllProducts(invoices),
58+
containsInAnyOrder("Beer", "Burger", "Corn Flakes", "Chips", "Coke", "Cake", "Left-Handed Scissors"));
5859
}
5960
}

0 commit comments

Comments
 (0)