question 289 Advanced OOP & Design · medium
An order service with injected collaborators
Write OrderService(payments, orders, notifier). place_order(order_id, customer_id, lines) adds up the lines (quantity × unit_price, rounded to 2 places), charges the customer through payments.charge(customer_id, total) (which returns a payment reference or raises ValueError when the card is declined), saves the order with orders.save(order_id, total, reference), and tells the customer through notifier.send(customer_id, text). A declined payment saves nothing, sends "Order <id>: payment declined", and returns None. If saving fails, the error reaches the caller and no message is sent. Checks use fakes for all three collaborators.
Class
OrderService(payments, orders, notifier)Successreturns the reference; sends
"Order <id> confirmed: <total>"Declinednothing saved; sends
"Order <id>: payment declined"; returns NoneSave failsthe exception propagates; nothing is sent