Skip to content

Commit c9b6e26

Browse files
committed
One more test
1 parent 21ba27e commit c9b6e26

2 files changed

Lines changed: 52 additions & 1 deletion

File tree

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
/**
@@ -91,7 +93,7 @@ public static Map<String, String> cheapestDealersByProduct(List<Invoice> invoice
9193
/**
9294
* From a given list of invoices, compute for every dealer the available products together with its price.
9395
*/
94-
public static Map<String, ProductWithPrice> computeDealerInventory(List<Invoice> invoices) {
96+
public static Map<String, List<ProductWithPrice>> computeDealerInventory(List<Invoice> invoices) {
9597
return Collections.emptyMap();
9698
}
9799

@@ -121,6 +123,34 @@ public String getProductName() {
121123
public BigDecimal getPrice() {
122124
return price;
123125
}
126+
127+
@Override
128+
public boolean equals(Object o) {
129+
if (this == o) return true;
130+
if (o == null || getClass() != o.getClass()) return false;
131+
132+
ProductWithPrice that = (ProductWithPrice) o;
133+
134+
if (price != null ? !price.equals(that.price) : that.price != null) return false;
135+
if (productName != null ? !productName.equals(that.productName) : that.productName != null) return false;
136+
137+
return true;
138+
}
139+
140+
@Override
141+
public int hashCode() {
142+
int result = productName != null ? productName.hashCode() : 0;
143+
result = 31 * result + (price != null ? price.hashCode() : 0);
144+
return result;
145+
}
146+
147+
@Override
148+
public String toString() {
149+
return "ProductWithPrice{" +
150+
"productName='" + productName + '\'' +
151+
", price=" + price +
152+
'}';
153+
}
124154
}
125155

126156
}

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

0 commit comments

Comments
 (0)