With stock reserved and payment taken, the last subsystem is Shipping. Its schedule(item) method books a shipment and hands back a tracking id (the string trk- followed by the item name). The facade should surface that id to the caller so a single place_order call returns everything the customer needs.
Extend place_order so that after a successful charge it:
The two failure paths from earlier steps stay exactly as they are, only the happy path grows a tracking field.
class Inventory:
def __init__(self, stock):
self.stock = dict(stock)
def reserve(self, item, qty):
if self.stock.get(item, 0) >= qty:
self.stock[item] -= qty
return True
return False
def release(self, item, qty):
self.stock[item] = self.stock.get(item, 0) + qty
class Payment:
def __init__(self, balance):
self.balance = balance
def charge(self, amount):
if amount <= self.balance:
self.balance -= amount
return True
return False
class Shipping:
def schedule(self, item):
return "trk-" + item
class CheckoutFacade:
def __init__(self, inventory, payment, shipping):
self.inventory = inventory
self.payment = payment
self.shipping = shipping
self.orders = []
def place_order(self, item, qty, amount):
if not self.inventory.reserve(item, qty):
return {"ok": False, "reason": "out_of_stock"}
if not self.payment.charge(amount):
self.inventory.release(item, qty)
return {"ok": False, "reason": "payment_declined"}
# TODO: schedule shipping via self.shipping.schedule(item) and return
# {"ok": True, "tracking": <the returned tracking id>}.
return {"ok": True}
The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
With stock reserved and payment taken, the last subsystem is Shipping. Its schedule(item) method books a shipment and hands back a tracking id (the string trk- followed by the item name). The facade should surface that id to the caller so a single place_order call returns everything the customer needs.
Extend place_order so that after a successful charge it:
The two failure paths from earlier steps stay exactly as they are, only the happy path grows a tracking field.
Press Run Tests or ββ© to check your solution.