TypeScript: An order state machine

An order moves through fixed states, and only some moves are allowed. Write the rules as a typed table.

What to build

A program that applies events to orders and rejects the moves the rules forbid.

Requirements

  • Declare the states as a literal union and the allowed moves as Record<State, readonly State[]>: created → paid or cancelled; paid → shipped or cancelled; shipped → delivered; delivered and cancelled → nothing.
  • new ID creates an order in state created (print order ID exists if it already does). ID STATE moves order ID to STATE if allowed and prints ID: OLD -> NEW; otherwise print ID: cannot go from OLD to STATE. An unknown order prints no order ID, and a STATE that is not a state prints unknown state STATE.
  • At the end print ID: STATE for every order, in the order they were created.

Input

Line 1: n. Then n events.

Output

One line per event that moves, fails or is rejected, then the final states.

Constraints

1 ≤ n ≤ 100.

Example

Input:

6
new A1
A1 paid
A1 delivered
A1 shipped
A1 delivered
B2 paid

Output:

A1: created -> paid
A1: cannot go from paid to delivered
A1: paid -> shipped
A1: shipped -> delivered
no order B2
A1: delivered

Why it works

With states as a literal union, the table of moves must list every state as a key, so a new state cannot be added without deciding where it can go, and text from the input is checked before it is trusted as a State.

Examples

Example 1 · Input
6
new A1
A1 paid
A1 delivered
A1 shipped
A1 delivered
B2 paid
Expected output
A1: created -> paid
A1: cannot go from paid to delivered
A1: paid -> shipped
A1: shipped -> delivered
no order B2
A1: delivered
Example 2 · Input
5
new X
new Y
X cancelled
X paid
Y lost
Expected output
X: created -> cancelled
X: cannot go from cancelled to paid
unknown state lost
X: cancelled
Y: created

Hints

Hint 1

type State = "created" | "paid" | "shipped" | "delivered" | "cancelled", and a Record<State, readonly State[]> of allowed next states.

Hint 2

Each event names the state it moves to; check that it is allowed from the current one.

Solution

Show one correct solution (try it yourself first)
const read = (): string => prompt() ?? "";

const STATES = ["created", "paid", "shipped", "delivered", "cancelled"] as const;
type State = (typeof STATES)[number];

const NEXT: Record<State, readonly State[]> = {
  created: ["paid", "cancelled"],
  paid: ["shipped", "cancelled"],
  shipped: ["delivered"],
  delivered: [],
  cancelled: [],
};

function isState(text: string): text is State {
  return (STATES as readonly string[]).includes(text);
}

const orders = new Map<string, State>();
const n = Number(read());
for (let i = 0; i < n; i++) {
  const [a, b] = read().split(" ");
  if (a === "new") {
    if (orders.has(b)) console.log("order " + b + " exists");
    else orders.set(b, "created");
    continue;
  }
  const current = orders.get(a);
  if (current === undefined) console.log("no order " + a);
  else if (!isState(b)) console.log("unknown state " + b);
  else if (!NEXT[current].includes(b)) console.log(a + ": cannot go from " + current + " to " + b);
  else {
    orders.set(a, b);
    console.log(a + ": " + current + " -> " + b);
  }
}
for (const [id, state] of orders) console.log(id + ": " + state);

Lesson: Enums, as const and literal types →