Skip to content

Instantly share code, notes, and snippets.

Last active Jan 29, 2021
What would you like to do?
Tell don't ask - sales refactored with service invocation
public class Sales {
private final CrateRepository crateRepository;
private final DeliveryService deliveryService;
public Sales(CrateRepository crateRepository, DeliveryService deliveryService) {
this.crateRepository = crateRepository;
this.deliveryService = deliveryService;
public SaleResult sell(Integer soldCrates, ProductType type, Customer customer) {
Crates crates = Crates.of(crateRepository.findByTypeLimit(type, soldCrates));
if (!crates.hasAtLeastCountOf(soldCrates)) {
return SaleResult.productOutOfStock();
Package packageToDeliver = crates.packFor(customer);
if (packageToDeliver.isDeliverable()) {
return SaleResult.sold();
return SaleResult.receiverAddressUnknown();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment