Polski

Expense approval chain

An expense travels through links – receipt check, team lead, manager, director – until someone approves or rejects it. Chain of Responsibility with pluggable links.

easy · typescript · javascript · python · csharp · java · go · 10 XP

In the company's expense app the amount decides who approves: a team lead up to 1,000, a manager up to 5,000, a director up to 20,000. Before that, every expense above 50 must have a receipt. The rules keep changing (finance wants a fraud check next), so instead of one huge if we build a Chain of Responsibility: every link either handles the expense or passes it on.

Task

  1. ApprovalHandler (in Go – the embedded Link struct):

    • setNext(handler) links the next handler and returns it, so the chain can be built fluently: a.setNext(b).setNext(c);
    • the default handle(expense) passes the expense on and, at the end of the chain, returns rejected: no approver.
  2. ReceiptCheck – an expense above 50 without a receipt ends with rejected by receipt-check; anything else goes on.
  3. Approver(name, limit) – approves an amount not greater than limit (approved by <name>) and passes bigger ones on.
  4. defaultChain() – the chain receipt-check → team-lead (1,000) → manager (5,000) → director (20,000).
ExpenseResult
300, with a receiptapproved by team-lead
1,001, with a receiptapproved by manager
120, no receiptrejected by receipt-check
25,000rejected: no approver

The tests add their own links (a fraud check and a recorder) – they must work without changing the existing classes. Names per language: set_next/default_chain (Python), SetNext/ExpenseApproval.DefaultChain() (C#), ExpenseApproval.defaultChain() (Java), SetNext/PassOn/DefaultChain() (Go).

Topics: Chain of Responsibility

Related articles