Skip to content
Katabench
Try free

Visit Every Invoice Line

Hard About 40 min Free, no account needed

Tax and printing each type-switch over invoice lines and fall back to a silent default, so a new line kind compiles and quietly goes untaxed. Rebuild both operations as visitors the lines dispatch to, so forgetting a kind becomes a compile error.

The brief

Billing models three kinds of invoice line: ProductLine, ServiceLine, and DiscountLine. The kinds rarely change. The operations over them keep multiplying: tax and printing today, with export and revenue recognition on the roadmap.

Each operation asks every line what it is. TaxCalculator.TaxCents runs an is chain, and InvoicePrinter.Describe runs a switch. Both end with a fallback: return 0 for tax, "unknown line" for printing. Next quarter a ShippingLine arrives. It will compile, and every invoice with shipping on it will quietly carry no tax on that line. Nothing points at the operations that needed to learn about the new kind, because each type switch is its own island with its own default.

The Visitor pattern turns that around. Each line knows its own kind, so it calls the matching method on whatever operation it is shown. Each operation implements one method per kind. When a kind is added, the visitor interface grows a method, and the compiler lists every operation that has not decided how to handle it. Visitor pays off exactly here: a stable set of kinds and a growing set of operations. An abstract TaxCents() and Describe() on each line would also close the gap, but then every new operation edits every line class, and the lines fill up with tax and export concerns.

Your job

Introduce IInvoiceLineVisitor with void VisitProduct(ProductLine line), void VisitService(ServiceLine line), and void VisitDiscount(DiscountLine line). Give the abstract InvoiceLine an abstract void Accept(IInvoiceLineVisitor visitor) (keep its parameterless constructor), and have each concrete line call the method for its own kind.

Rebuild both operations as visitors that accumulate their result: TaxCalculator exposes int TotalCents, and InvoicePrinter exposes string Text (entries joined with "; "). Keep every rule and rounding exactly as it is: products 20%, services 10%, a discount subtracting 20% of its amount, each computed per line in integer cents. Keep the constructors of the three line kinds. Solution shows every parsed line to both operations and prints <text> | tax:<total>.

The parser in Solution can keep its switch on the kind word. Text from outside has no type to dispatch on, so the edge that turns it into line objects is the one place a runtime check belongs, the same call the kata make-illegal-states-unrepresentable makes for its scanner feed. Once a line is typed, no operation should have to ask it what it is.

How it's graded

Your submission must (1) preserve every output, including per-line rounding and negative totals, and (2) satisfy every design rule shown in the results: the visitor contract exists and both operations implement it, neither operation references the InvoiceLine base type, the base type and all three kinds reference the visitor contract, and InvoiceLine stays abstract.

A trusted composition root then shows every line to your operations through Accept only, along with an unknown recording visitor and one line kind your code has never seen, which dispatches as a product. Each line must call the method for its own kind, and your operations must do their work inside the visitor methods. It also fails if TaxCalculator or InvoicePrinter has any method whose parameter could hold more than one line kind, such as object, the base type, or a shared interface, or if InvoiceLine.Accept has a body instead of being abstract.

A type switch asks "what are you?" and needs a default for answers it has never heard. A visitor lets the object answer for itself, and a new answer becomes a compile error instead of a quiet zero.

How it is graded

Executable structural checks grade the shape of your types as well as their behavior.

SOLID principles exercises →

What it practices

Visitor pattern
Add operations over a closed type hierarchy without type switches or silent defaults.

Related katas