Skip to main content

State Machine Diagram — Booking Status

Editorial

Purpose of the State Machine Diagram

A booking in our system is fundamentally a state machine. At any moment, a booking is in exactly one state, and specific events cause it to transition to another state. The State Machine Diagram captures this lifecycle visually.

This diagram answers:

  • What states can a Booking be in? — PENDING, CONFIRMED, CANCELLED, EXPIRED
  • What events trigger transitions? — paymentSuccess, paymentFailed, userCancel, holdTimeout
  • What guards prevent invalid transitions? — e.g., a CONFIRMED booking can only be cancelled before the show starts
  • What actions happen during transitions? — e.g., release seats, send notification, process refund

Unlike simpler state machines like the Vending Machine (DP6) with 4 states or the ATM (DP11) with 5 states, our booking system actually has two interrelated state machines: one for the Booking (PENDING → CONFIRMED/CANCELLED/EXPIRED) and one for the ShowSeat (AVAILABLE → HELD → BOOKED, or back to AVAILABLE). These two machines are coupled: when a Booking transitions to CONFIRMED, its ShowSeats transition from HELD to BOOKED. When a Booking transitions to CANCELLED, its ShowSeats transition back to AVAILABLE.

The State Pattern (Fundamentals S12) maps directly from this diagram — each rounded rectangle can become a concrete state class if we choose to implement it that way.

Identifying States from Requirements

From requirement R10, the booking follows a strict lifecycle. Let's derive each state:

Booking States:

State 1: PENDING

  • Derived from: R6, R7, R10 (booking created with held seats, awaiting payment)
  • Entry condition: User has selected seats and the system has created a booking
  • What the system does: Seats are HELD for this booking. A timer starts counting down the hold period.
  • Valid transitions out: paymentSuccess → CONFIRMED, paymentFailed → CANCELLED, userCancel → CANCELLED, holdTimeout → EXPIRED
  • Duration: Bounded by hold timeout (default 8 minutes). This is a transient state — the booking must leave PENDING before the timeout.

State 2: CONFIRMED

  • Derived from: R8, R10 (payment succeeded, booking is active)
  • Entry condition: Payment processed successfully within the hold period
  • What the system does: Seats transition from HELD to BOOKED. Confirmation notification sent. Ticket is valid for entry.
  • Valid transitions out: userCancel → CANCELLED (only if before show starts)
  • Duration: From confirmation until the show ends (or cancellation)

State 3: CANCELLED

  • Derived from: R9, R10 (booking cancelled by user or payment failure)
  • Entry condition: Either payment failed, user explicitly cancelled (from PENDING or CONFIRMED), or system cancelled
  • What the system does: All ShowSeats are released (status → AVAILABLE). If was CONFIRMED, refund may be initiated.
  • Valid transitions out: None — this is a terminal state
  • This is a sink state — once cancelled, a booking cannot be un-cancelled. The user must create a new booking.

State 4: EXPIRED

  • Derived from: R6, R10 (hold timeout elapsed without payment)
  • Entry condition: The hold period elapsed and makePayment was never called (or was called after expiration)
  • What the system does: All ShowSeats are released (status → AVAILABLE). No payment was ever charged.
  • Valid transitions out: None — this is a terminal state
  • EXPIRED is distinct from CANCELLED because no deliberate action triggered it — it's the result of inaction (timeout).

Why EXPIRED is separate from CANCELLED:

Both release seats, but the business semantics differ. CANCELLED means a deliberate action occurred (user cancelled or payment failed). EXPIRED means the user abandoned the flow. This distinction matters for analytics ("how many users drop off during payment?"), for communication ("Your booking expired" vs. "Your booking was cancelled"), and potentially for retry logic (expired bookings might auto-prompt "Try again?").

ShowSeat Sub-State Machine

The ShowSeat has its own state machine that is driven by the Booking state machine:

ShowSeat State 1: AVAILABLE

  • The seat is free for this show. Any user can select it.
  • Transitions: hold(booking) → HELD

ShowSeat State 2: HELD

  • The seat is temporarily reserved for a specific booking.
  • No other user can select this seat.
  • Transitions: book() → BOOKED (when booking is confirmed), release() → AVAILABLE (when booking is cancelled/expired)

ShowSeat State 3: BOOKED

  • The seat is permanently reserved. Payment has been completed.
  • Transitions: release() → AVAILABLE (when confirmed booking is cancelled)

Coupling between the two state machines:

Booking TransitionShowSeat Effect
Created (→ PENDING)ShowSeats: AVAILABLE → HELD
PENDING → CONFIRMEDShowSeats: HELD → BOOKED
PENDING → CANCELLEDShowSeats: HELD → AVAILABLE
PENDING → EXPIREDShowSeats: HELD → AVAILABLE
CONFIRMED → CANCELLEDShowSeats: BOOKED → AVAILABLE

This coupling is critical for correctness. If the Booking transitions to EXPIRED but we forget to release the ShowSeats, those seats are permanently locked — a serious bug that would appear as "phantom sold-out" shows.

Defining Transitions

Each transition has three components: event (what triggers it), guard (condition that must be true), and action (what happens during the transition).

Booking Transitions:

#From StateEventGuardActionTo State
T1[initial]createBooking(user, show, seats)all seats availablehold all ShowSeats, start timerPENDING
T2PENDINGpaymentSuccess(payment)hold not expiredset ShowSeats to BOOKED, store payment, send confirmationCONFIRMED
T3PENDINGpaymentFailed—release all ShowSeats, cancel timerCANCELLED
T4PENDINGuserCancel()—release all ShowSeats, cancel timerCANCELLED
T5PENDINGholdTimeouttimer elapsedrelease all ShowSeatsEXPIRED
T6CONFIRMEDuserCancel()show has not startedrelease all ShowSeats, initiate refundCANCELLED

ShowSeat Transitions:

#From StateEventGuardActionTo State
S1AVAILABLEhold(booking)—set heldAt = now, set heldByBooking = bookingHELD
S2HELDbook()—clear heldAt, clear heldByBookingBOOKED
S3HELDrelease()—clear heldAt, clear heldByBookingAVAILABLE
S4BOOKEDrelease()——AVAILABLE

Key observations:

  • T2 has a guard (hold not expired) — even if payment succeeds, if the hold has expired between payment initiation and payment completion, the booking cannot be confirmed. This prevents a race condition where seats were already released and re-held by another user.
  • T6 has a guard (show not started) — a CONFIRMED booking can only be cancelled before the show begins. Once the movie starts, cancellation is not allowed.
  • T5 is triggered by time, not user action — the System Timer fires this transition. This maps to UC11 from our use case diagram.
  • No transition from EXPIRED/CANCELLED back to PENDING — these are terminal states. The user must start a new booking.

Run the Two State Machines

Both machines are live below. Hold the seats, pay, cancel or let the clock run: every Booking transition moves its ShowSeats with it, and each guard decides in front of you. The missions walk you through every transition, the guards that can stop one, and the bug this lesson warns about.

Interactive: the Booking and ShowSeat state machines run live for seats 5C and 5D. Hold, pay with a valid or declined card, cancel, and move the clock; T1–T6 move the Booking between PENDING, CONFIRMED, CANCELLED and EXPIRED and drive each seat through S1–S4 between AVAILABLE, HELD and BOOKED, while the guards [all seats available], [hold not expired] and [show has not started] allow or block each move.

Complete State Machine Diagram

Two coupled UML state machines for a movie-ticket booking. Booking: from the initial state, T1 createBooking(user, show, seats) [all seats available] → PENDING; T2 paymentSuccess(payment) [hold not expired] → CONFIRMED; T3 paymentFailed → CANCELLED; T4 userCancel() → CANCELLED; T5 holdTimeout [timer elapsed] → EXPIRED; T6 userCancel() [show has not started] from CONFIRMED → CANCELLED. CANCELLED and EXPIRED are terminal: a new booking is needed. ShowSeat, driven by the Booking's transitions: from the initial state AVAILABLE; S1 hold(booking) → HELD; S2 book() → BOOKED; S3 release() HELD → AVAILABLE; S4 release() BOOKED → AVAILABLE. A coupling table gives each Booking transition's action and ShowSeat effect: T1 hold all ShowSeats, start timer (AVAILABLE → HELD); T2 set ShowSeats to BOOKED, store payment, send confirmation (HELD → BOOKED); T3 and T4 release all ShowSeats, cancel timer (HELD → AVAILABLE); T5 release all ShowSeats (HELD → AVAILABLE); T6 release all ShowSeats, initiate refund (BOOKED → AVAILABLE).

Key Design Decisions

1. Why 4 booking states and not 3 (merging EXPIRED into CANCELLED)?

Although both CANCELLED and EXPIRED release seats, they represent different business events. CANCELLED implies a deliberate action — the user chose to cancel or payment failed. EXPIRED implies abandonment — the user walked away. Keeping them separate enables:

  • Better analytics: "30% of bookings expire" tells you the hold period is too short or the payment flow is too complex
  • Different messaging: "Your booking was cancelled" vs. "Your booking expired — try again?"
  • Different retry logic: expired bookings might auto-suggest rebooking; cancelled bookings wouldn't

2. Why is there no PENDING --> CONFIRMED transition without the 'hold not expired' guard?

Imagine the race: User A holds seat 5C. The hold expires, seat 5C is released. User B holds seat 5C for a different booking. Now User A's payment completes (late). Without the guard, User A's booking would be confirmed — but seat 5C is now held by User B. The guard prevents this: if the hold has expired, payment is rejected even if the gateway approves it. The booking transitions to EXPIRED, not CONFIRMED.

3. Why does CONFIRMED --> CANCELLED have the 'show not started' guard?

Once the movie has started, the ticket has been "consumed" — the user has (or could have) occupied the seat. Allowing cancellation after the show starts creates refund disputes and complicates seat tracking. The guard enforces a clean cutoff.

4. How does this compare to other state machines in this course?

Like the ATM (DP11), the Booking state machine has a critical guard on the key transition (T2 guard = hold not expired; ATM T2 guard = PIN correct). Like the Vending Machine (DP6), it has a timeout-triggered transition (T5 = holdTimeout; VM = coin return timeout). But unlike both, the Booking state machine is coupled to a secondary state machine (ShowSeat) — no other problem in DP1-DP18 has this two-machine coupling. The Hotel Room Status state machine (DP14) is the closest analog from medium problems.

5. Why show ShowSeat as a sub-state machine?

In UML, a sub-state machine is a state machine triggered by another. The ShowSeat state machine is not independent — it's driven entirely by Booking transitions. Showing it alongside the Booking machine makes the coupling explicit and helps prevent bugs where a Booking transition "forgets" to update its ShowSeats.