Facebook Pixel

Exercise: State Machine Diagrams

This exercise puts the state machine diagrams lesson into code.

Scenario

The diagram below shows a simplified document review workflow with three states. A document starts in Draft. The submit event moves it to Review. From Review, approve advances it to Published (a terminal state with no outgoing transitions), while reject sends it back to Draft.

Commands

CommandValid fromOutput on successOutput on failure
["submit"]Draft"Submitted: Review""Cannot submit from <state>"
["approve"]Review"Approved: Published""Cannot approve from <state>"
["reject"]Review"Rejected: Draft""Cannot reject from <state>"
["state"]any"State: <current state>"
Example
Input
7
state
submit
state
approve
state
reject
approve
Output
State: Draft
Submitted: Review
State: Review
Approved: Published
State: Published
Cannot reject from Published
Cannot approve from Published
Explanation
The document starts in Draft. After submitting, it moves to Review. After approving, it reaches Published, a terminal state. Both reject and approve fail from Published because the diagram has no outgoing transitions from that state.

Your task

Build the TRANSITIONS table from the diagram, then write the event dispatch: look up each event, reject illegal ones, and print the right line.

Invest in Yourself
Your new job is waiting. 83% of people that complete the program get a job offer. Unlock unlimited access to all content and features.
Go Pro