An order is never in a vague "in progress" blob. It is in exactly one state (cart, pending, paid, shipped, delivered, cancelled), and only certain events move it onward. Encoding that as a transition table makes illegal moves impossible: you cannot ship something that was never paid.
The table TRANSITIONS maps each state to the events it allows and where they lead.
fire(event): if the current state allows event, move to the target state and return True; otherwise stay put and return Falseclass Order:
TRANSITIONS = {
"cart": {"checkout": "pending"},
"pending": {"pay": "paid", "cancel": "cancelled"},
"paid": {"ship": "shipped", "refund": "cancelled"},
"shipped": {"deliver": "delivered"},
"delivered": {},
"cancelled": {},
}
def __init__(self):
self.state = "cart"
def fire(self, event):
"""Move along an allowed transition; return True on success, False if not allowed."""
# TODO: look up which events the current state allows in TRANSITIONS.
# If this event is one of them, move self.state to the target that
# entry names and report success with True. Otherwise leave self.state
# untouched and report False, an illegal move is rejected, not an error.
pass
The editor and the test runner need a wide screen. Open this page on a desktop browser and your progress will be waiting.
An order is never in a vague "in progress" blob. It is in exactly one state (cart, pending, paid, shipped, delivered, cancelled), and only certain events move it onward. Encoding that as a transition table makes illegal moves impossible: you cannot ship something that was never paid.
The table TRANSITIONS maps each state to the events it allows and where they lead.
fire(event): if the current state allows event, move to the target state and return True; otherwise stay put and return FalsePress Run Tests or ββ© to check your solution.