Challenge: invoice totals
Time to put the whole module together on a job every business system does: pricing an invoice.
A wholesaler's billing program receives three order lines and a customer discount. It has to produce the figures that get printed on the invoice and posted to the accounts. Accountants will reconcile these numbers against the ledger, so every cent matters — which means you need to be precise about where rounding happens:
- Line totals (quantity × unit price) are exact; no rounding needed.
- The discount is a percentage of the subtotal, rounded to the cent.
- VAT at 23% is charged on the discounted amount, rounded to the cent.
- The total is net + VAT.
Round each of those two figures once, from full-precision inputs. Forget to round, or round the wrong value, and you'll be a cent out — the tests will notice.
The invoice total goes into a field that can hold at most 999,999.99. Very
large orders must be caught with ON SIZE ERROR rather than printed with
their leading digits silently chopped off.
Before you submit
- Did you pick the verbs or
COMPUTEdeliberately? Either is fine here; what matters is that each figure matches the rule in the table. - Are the discount and VAT the only values with
ROUNDED? - Does the
TOTAL:line appear only when the arithmetic succeeded, with the error line in its place otherwise? - Try a quick sanity check by hand: net + VAT should be roughly 1.23 × net.
On the job
Real billing programs keep money in COMP-3 fields, exactly as the
starter does. When a customer queries an invoice, the first thing a
support analyst checks is whether rounding was applied per line or per
invoice, so specs spell it out, and so should your code.
Your task
Read seven values, one per line:
- quantity, unit price — line 1
- quantity, unit price — line 2
- quantity, unit price — line 3
- customer discount in percent (e.g.
10or2.5)
Calculate:
| Figure | Rule |
|---|---|
| LINE n | quantity × unit price |
| SUBTOTAL | sum of the three lines |
| DISCOUNT | subtotal × discount% ÷ 100, rounded to the cent |
| NET | subtotal − discount |
| VAT | net × 23 ÷ 100, rounded to the cent |
| TOTAL | net + VAT, into WS-TOTAL (max 999,999.99) |
Display every figure through WS-OUT (PIC Z,ZZZ,ZZ9.99), each label
padded to 10 characters. For 10, 25.00, 3, 399.99, 7, 6.50,
10:
LINE 1: 250.00
LINE 2: 1,199.97
LINE 3: 45.50
SUBTOTAL: 1,495.47
DISCOUNT: 149.55
NET: 1,345.92
VAT: 309.56
TOTAL: 1,655.48
If the total doesn't fit in WS-TOTAL, display this instead of the
TOTAL: line:
ERROR: TOTAL EXCEEDS 999,999.99