Skip to content

Instantly share code, notes, and snippets.

@pawelpluta
Last active January 29, 2021 19:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pawelpluta/7d710a8aa7d3b840e39deb941982d5b8 to your computer and use it in GitHub Desktop.
Save pawelpluta/7d710a8aa7d3b840e39deb941982d5b8 to your computer and use it in GitHub Desktop.
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()) {
deliveryService.send(packageToDeliver);
crateRepository.delete(packageToDeliver.deliveredCrates());
return SaleResult.sold();
}
return SaleResult.receiverAddressUnknown();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment