Schedule Shipping and Return Tracking

Step 3: Schedule Shipping and Return Tracking

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:

  • Calls self.shipping.schedule(item) to get a tracking id.
  • Returns the dict with ok set to True and a tracking key holding that id.

The two failure paths from earlier steps stay exactly as they are, only the happy path grows a tracking field.

Hints
Starter code
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}
Write it on a laptop

The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.