help
This commit is contained in:
@@ -2,10 +2,10 @@ package streams;
|
|||||||
|
|
||||||
import java.util.ArrayList;
|
import java.util.ArrayList;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
import java.util.Comparator;
|
||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Optional;
|
import java.util.Optional;
|
||||||
import javax.swing.text.html.Option;
|
|
||||||
import tester.Tester;
|
import tester.Tester;
|
||||||
|
|
||||||
class Customer {
|
class Customer {
|
||||||
@@ -95,6 +95,78 @@ interface PurchaseDB {
|
|||||||
getPurchasesForSince(List<Integer> customerIds, int numberOfDaysAgo);
|
getPurchasesForSince(List<Integer> customerIds, int numberOfDaysAgo);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class Accounting {
|
||||||
|
CustomerDB customers;
|
||||||
|
InventoryDB inventory;
|
||||||
|
PurchaseDB purchases;
|
||||||
|
|
||||||
|
Accounting(
|
||||||
|
CustomerDB customers, InventoryDB inventory, PurchaseDB purchases
|
||||||
|
) {
|
||||||
|
this.customers = customers;
|
||||||
|
this.inventory = inventory;
|
||||||
|
this.purchases = purchases;
|
||||||
|
}
|
||||||
|
|
||||||
|
int revenueSince(int days) {
|
||||||
|
return this.purchases.getPurchasesSince(days)
|
||||||
|
.stream()
|
||||||
|
.flatMap(purchase -> purchase.getLineItems().stream())
|
||||||
|
.map(item -> {
|
||||||
|
Inventory inv =
|
||||||
|
this.inventory.getInventoryById(item.getInventoryId())
|
||||||
|
.orElseThrow(() -> new Error("Not there."));
|
||||||
|
return inv.getCostPerUnit() * item.getNumberOfItemsPurchased();
|
||||||
|
})
|
||||||
|
.reduce(0, (a, b) -> a + b);
|
||||||
|
}
|
||||||
|
|
||||||
|
int unitsSoldInPastMonth(int inventoryId) {
|
||||||
|
return purchases.getPurchasesSince(30)
|
||||||
|
.stream()
|
||||||
|
.flatMap(p -> p.getLineItems().stream())
|
||||||
|
.filter(item -> item.getInventoryId() == inventoryId)
|
||||||
|
.mapToInt(LineItem::getNumberOfItemsPurchased)
|
||||||
|
.sum();
|
||||||
|
}
|
||||||
|
|
||||||
|
String bestCustomer() {
|
||||||
|
return this.customers.getAllCustomers()
|
||||||
|
.stream()
|
||||||
|
.reduce((best, current) -> {
|
||||||
|
int bestSpent = howStupidIs(best.getId());
|
||||||
|
int currentSpent = howStupidIs(current.getId());
|
||||||
|
return currentSpent > bestSpent ? current : best;
|
||||||
|
})
|
||||||
|
.map(Customer::getName)
|
||||||
|
.orElseThrow(
|
||||||
|
()
|
||||||
|
-> new RuntimeException(
|
||||||
|
"You didn't have a best customer, fool."
|
||||||
|
)
|
||||||
|
);
|
||||||
|
}
|
||||||
|
|
||||||
|
private int howStupidIs(int customerId) {
|
||||||
|
return this.purchases.getPurchasesFor(customerId)
|
||||||
|
.stream()
|
||||||
|
.flatMap(p -> p.getLineItems().stream())
|
||||||
|
.map(
|
||||||
|
item
|
||||||
|
-> this.inventory.getInventoryById(item.getInventoryId())
|
||||||
|
.map(
|
||||||
|
inv
|
||||||
|
-> inv.getCostPerUnit() *
|
||||||
|
item.getNumberOfItemsPurchased()
|
||||||
|
)
|
||||||
|
.orElseThrow(
|
||||||
|
() -> new Error("That's not an inventory fool.")
|
||||||
|
)
|
||||||
|
)
|
||||||
|
.reduce(0, Integer::sum);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
class Examples {
|
class Examples {
|
||||||
Customer borace, germany, vietnam;
|
Customer borace, germany, vietnam;
|
||||||
Inventory cabbages, cinderBlocks, traps;
|
Inventory cabbages, cinderBlocks, traps;
|
||||||
@@ -105,6 +177,8 @@ class Examples {
|
|||||||
InventoryDB loot;
|
InventoryDB loot;
|
||||||
PurchaseDB crimes;
|
PurchaseDB crimes;
|
||||||
|
|
||||||
|
Accounting dave;
|
||||||
|
|
||||||
void init() {
|
void init() {
|
||||||
borace = new Customer(0, "Borace");
|
borace = new Customer(0, "Borace");
|
||||||
germany = new Customer(1, "Germany");
|
germany = new Customer(1, "Germany");
|
||||||
@@ -209,6 +283,8 @@ class Examples {
|
|||||||
.toList();
|
.toList();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
dave = new Accounting(victims, loot, crimes);
|
||||||
}
|
}
|
||||||
|
|
||||||
void test(Tester t) {
|
void test(Tester t) {
|
||||||
@@ -243,4 +319,16 @@ class Examples {
|
|||||||
List.of(profit, ireland, biden, cob)
|
List.of(profit, ireland, biden, cob)
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void testDave(Tester t) {
|
||||||
|
init();
|
||||||
|
|
||||||
|
t.checkExpect(dave.revenueSince(-1), 0);
|
||||||
|
t.checkExpect(dave.revenueSince(0), 54 * 28 + 1 * 1 + 3 * 10);
|
||||||
|
t.checkExpect(
|
||||||
|
dave.revenueSince(1), 2 * (54 * 28) + 2 * (1 * 1) + 3 * 10
|
||||||
|
);
|
||||||
|
t.checkExpect(dave.unitsSoldInPastMonth(0), 54 * 2);
|
||||||
|
t.checkExpect(dave.bestCustomer(), "Vietnam");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user