fuck
This commit is contained in:
@@ -1,9 +1,11 @@
|
||||
package streams;
|
||||
|
||||
import java.util.ArrayList;
|
||||
import java.util.Arrays;
|
||||
import java.util.List;
|
||||
import java.util.Map;
|
||||
import java.util.Optional;
|
||||
import javax.swing.text.html.Option;
|
||||
import tester.Tester;
|
||||
|
||||
class Customer {
|
||||
@@ -93,34 +95,152 @@ interface PurchaseDB {
|
||||
getPurchasesForSince(List<Integer> customerIds, int numberOfDaysAgo);
|
||||
}
|
||||
|
||||
class Victims implements CustomerDB {
|
||||
Map<Integer, Customer> victims;
|
||||
|
||||
Victims() {}
|
||||
|
||||
public Optional<Customer> getCustomerById(int id) {
|
||||
return Optional.ofNullable(this.victims.get(id));
|
||||
}
|
||||
|
||||
public List<Customer> getAllCustomers() {
|
||||
return this.victims.values().stream().toList();
|
||||
}
|
||||
}
|
||||
|
||||
class Examples {
|
||||
Customer borace, germany, vietnam;
|
||||
Inventory cabbages, cinderBlocks;
|
||||
LineItem lottaCabbages, oneCinderBlock;
|
||||
Purchase profit;
|
||||
Inventory cabbages, cinderBlocks, traps;
|
||||
LineItem lottaCabbages, oneCinderBlock, tripleTrap;
|
||||
Purchase profit, ireland, cob, biden;
|
||||
|
||||
CustomerDB victims;
|
||||
InventoryDB loot;
|
||||
PurchaseDB crimes;
|
||||
|
||||
void init() {
|
||||
borace = new Customer(0, "Borace");
|
||||
borace = new Customer(0, "Germany");
|
||||
borace = new Customer(0, "Vietnam");
|
||||
germany = new Customer(1, "Germany");
|
||||
vietnam = new Customer(2, "Vietnam");
|
||||
|
||||
cabbages = new Inventory(0, "Expensive cabbages.", 28);
|
||||
cinderBlocks = new Inventory(1, "Cinder blocks.", 1);
|
||||
traps = new Inventory(2, "Traps.", 10);
|
||||
|
||||
lottaCabbages = new LineItem(0, 54);
|
||||
oneCinderBlock = new LineItem(1, 1);
|
||||
tripleTrap = new LineItem(2, 3);
|
||||
|
||||
// Borace a day ago.
|
||||
profit = new Purchase(0, List.of(lottaCabbages, oneCinderBlock), 0, 1);
|
||||
// Germany four days ago.
|
||||
ireland = new Purchase(1, List.of(oneCinderBlock), 1, 4);
|
||||
// Vietnam five days ago.
|
||||
cob = new Purchase(2, List.of(tripleTrap), 2, 5);
|
||||
// Vietnam today.
|
||||
biden = new Purchase(
|
||||
3, List.of(lottaCabbages, tripleTrap, oneCinderBlock), 2, 0
|
||||
);
|
||||
|
||||
victims = new CustomerDB() {
|
||||
List<Customer> customers = Arrays.asList(borace, germany, vietnam);
|
||||
|
||||
public Optional<Customer> getCustomerById(int id) {
|
||||
// yess use streams for everything...
|
||||
return customers.stream()
|
||||
.filter(customer -> customer.getId() == id)
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public List<Customer> getAllCustomers() { return customers; }
|
||||
};
|
||||
|
||||
loot = new InventoryDB() {
|
||||
List<Inventory> inventory =
|
||||
Arrays.asList(cabbages, cinderBlocks, traps);
|
||||
|
||||
public Optional<Inventory> getInventoryById(int id) {
|
||||
return inventory.stream()
|
||||
.filter(i -> i.getId() == id)
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public List<Inventory> getAllInventory() { return inventory; }
|
||||
};
|
||||
|
||||
crimes = new PurchaseDB() {
|
||||
List<Purchase> purchases =
|
||||
Arrays.asList(profit, ireland, biden, cob);
|
||||
|
||||
public Optional<Purchase> getPurchaseById(int id) {
|
||||
return purchases.stream()
|
||||
.filter(p -> p.getId() == id)
|
||||
.findFirst();
|
||||
}
|
||||
|
||||
public List<Purchase> getPurchasesSince(int numberOfDaysAgo) {
|
||||
return purchases.stream()
|
||||
.filter(p -> p.getNumberOfDaysAgo() <= numberOfDaysAgo)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public List<Purchase> getPurchasesFor(int customerId) {
|
||||
return purchases.stream()
|
||||
.filter(p -> p.getCustomerIdOfPurchaser() == customerId)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public List<Purchase> getPurchasesFor(List<Integer> customerIds) {
|
||||
return purchases.stream()
|
||||
.filter(
|
||||
p -> customerIds.contains(p.getCustomerIdOfPurchaser())
|
||||
)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public List<Purchase> getPurchasesForSince(
|
||||
int customerId, int numberOfDaysAgo
|
||||
) {
|
||||
return purchases.stream()
|
||||
.filter(
|
||||
p
|
||||
-> p.getCustomerIdOfPurchaser() == customerId &&
|
||||
p.getNumberOfDaysAgo() <= numberOfDaysAgo
|
||||
)
|
||||
.toList();
|
||||
}
|
||||
|
||||
public List<Purchase> getPurchasesForSince(
|
||||
List<Integer> customerIds, int numberOfDaysAgo
|
||||
) {
|
||||
return purchases.stream()
|
||||
.filter(
|
||||
p
|
||||
-> customerIds.contains(p.getCustomerIdOfPurchaser()) &&
|
||||
p.getNumberOfDaysAgo() <= numberOfDaysAgo
|
||||
)
|
||||
.toList();
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
void test(Tester t) {
|
||||
init();
|
||||
|
||||
t.checkExpect(victims.getCustomerById(0), Optional.of(borace));
|
||||
t.checkExpect(victims.getCustomerById(9), Optional.empty());
|
||||
t.checkExpect(
|
||||
victims.getAllCustomers(), List.of(borace, germany, vietnam)
|
||||
);
|
||||
|
||||
t.checkExpect(loot.getInventoryById(0), Optional.of(cabbages));
|
||||
t.checkExpect(loot.getInventoryById(3), Optional.empty());
|
||||
t.checkExpect(
|
||||
loot.getAllInventory(), List.of(cabbages, cinderBlocks, traps)
|
||||
);
|
||||
|
||||
t.checkExpect(crimes.getPurchaseById(0), Optional.of(profit));
|
||||
t.checkExpect(crimes.getPurchaseById(100000002), Optional.empty());
|
||||
t.checkExpect(crimes.getPurchasesSince(-1), List.of());
|
||||
t.checkExpect(crimes.getPurchasesSince(0), List.of(biden));
|
||||
t.checkExpect(crimes.getPurchasesSince(2), List.of(profit, biden));
|
||||
t.checkExpect(crimes.getPurchasesFor(0), List.of(profit));
|
||||
t.checkExpect(crimes.getPurchasesFor(2), List.of(biden, cob));
|
||||
t.checkExpect(
|
||||
crimes.getPurchasesFor(List.of(0, 2)), List.of(profit, biden, cob)
|
||||
);
|
||||
t.checkExpect(crimes.getPurchasesForSince(0, 0), List.of());
|
||||
t.checkExpect(crimes.getPurchasesForSince(2, 2), List.of(biden));
|
||||
t.checkExpect(
|
||||
crimes.getPurchasesForSince(List.of(0, 1, 2), 9),
|
||||
List.of(profit, ireland, biden, cob)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user