Project: A Café Order Calculator With Decimal Money
Build a program that prices a set of orders with decimal arithmetic, a tip and an example tax rate, prints a formatted receipt and verify every total with an independent decimal calculation.
In this lesson you will
- Combine records, lists and loops
- Use decimal for money
- Format a receipt
- Verify totals independently
About 28 minutes
From pieces to a receipt
This lesson joins the first seven. Omar wants a receipt for each customer: items with quantities, a subtotal, a service charge for groups and a total. You will model a line of the order as a record, keep lines in a list, loop through them, format a receipt, and prove the arithmetic with a second method. Start by writing the rules in plain words, because code that implements unclear rules cannot be right.
| Rule | Value |
|---|---|
| Prices | Flat white 3.20, tea 2.10, cake 3.50 |
| Group service charge | 10 percent if the order has 6 or more items |
| Tax | 19 percent in this lesson, as an example rate, not advice |
| Rounding | To the nearest cent, half away from zero, on the final amounts |
Money is kept in decimal, as lesson 2 explained, and rounding happens once, at the end of each calculation, with Math.Round and the MidpointRounding.AwayFromZero option so that the rule is explicit. The default for Math.Round is rounding halves to even, which is correct for statistics and surprising for receipts. Formatting uses the F2 specifier for two decimals and a width to align columns.
var prices = new Dictionary<string, decimal> { ["flat white"] = 3.20m, ["tea"] = 2.10m, ["cake"] = 3.50m };
var orders = new List<List<Line>>
{
new() { new("flat white", 2), new("cake", 1) },
new() { new("tea", 4), new("cake", 2), new("flat white", 1) },
new() { new("tea", 1) },
};
decimal grand = 0;
int number = 0;
foreach (var order in orders)
{
number++;
decimal subtotal = 0; int items = 0;
Console.WriteLine($"Order {number}");
foreach (var l in order)
{
decimal lineTotal = prices[l.Item] * l.Qty;
subtotal += lineTotal; items += l.Qty;
Console.WriteLine($" {l.Item,-10} x{l.Qty,2} {lineTotal,8:F2}");
}
decimal service = items >= 6 ? Math.Round(subtotal * 0.10m, 2, MidpointRounding.AwayFromZero) : 0m;
decimal tax = Math.Round((subtotal + service) * 0.19m, 2, MidpointRounding.AwayFromZero);
decimal total = subtotal + service + tax;
Console.WriteLine($" subtotal {subtotal,8:F2} | service {service,5:F2} | tax {tax,5:F2} | TOTAL {total,7:F2}");
grand += total;
}
Console.WriteLine($"grand total: {grand:F2}");
record Line(string Item, int Qty);
Order 1
flat white x 2 6.40
cake x 1 3.50
subtotal 9.90 | service 0.00 | tax 1.88 | TOTAL 11.78
Order 2
tea x 4 8.40
cake x 2 7.00
flat white x 1 3.20
subtotal 18.60 | service 1.86 | tax 3.89 | TOTAL 24.35
Order 3
tea x 1 2.10
subtotal 2.10 | service 0.00 | tax 0.40 | TOTAL 2.50
grand total: 38.63
The program printed three receipts. Order 1 has no service charge, because it has only three items, order 2 has seven items, so it gets a service charge of 1.86, and order 3 is a single tea. The totals are 11.78, 24.35, 2.50, and the grand total is 38.63. The columns line up because the format specifiers pad each field: the comma followed by a number sets a width, and a minus sign aligns the text to the left. Every amount has exactly two decimals, since the calculation uses decimal and rounds on purpose.
To verify the receipts, the course recomputed every order with Python’s decimal module, an independent implementation of exact decimal arithmetic. It multiplied prices by quantities, applied the service charge for orders with six or more items, rounded each charge to the cent with half-up rounding, and added the tax on the subtotal plus service. The three totals were 11.78, 24.35, 2.50, the same as the C# program, and the grand total matched too. The 19 percent used here is only an example rate chosen for the lesson, and real tax rules depend on your country, so check official sources for yours.
Check by hand: order 1 is 2 x 3.20 + 3.50 = 9.90, and tax is 19 percent of 9.90 = 1.881, which rounds to 1.88, so the total is 11.78. Order 2 is 4 x 2.10 + 2 x 3.50 + 3.20 = 8.40 + 7.00 + 3.20 = 18.60, service 1.86, tax on 20.46 is 3.8874, rounded to 3.89, total 24.35. Order 3 is 2.10 + 0.40 tax = 2.50. These are the numbers in the output.
Math.Round defaults to rounding halves to the nearest even digit. For receipts, choose MidpointRounding.AwayFromZero so that the rule is the one your customers expect.
A short table of rules is easy to review with the person who will use the program, and it becomes your checklist for tests.
Every program in this lesson was built and run with the .NET 8 SDK and C# 12 while the course was written. .NET and C# release every year, so check the official Microsoft documentation for the current version and for features added after .NET 8. The tax and service rates are examples for the lesson only.
Practice
Add a loyalty discount of 5 percent for orders over 20 euro, applied before tax. Read the order from command-line arguments. After each change, recompute one total by hand and compare with the program.
Check what you learned
Five quick questions. Pick an answer and you will see straight away whether it is right and why.
Why use decimal for the calculation?
- It stores base ten amounts exactly
- It is faster
- C# requires it
- It is shorter to type
Lesson 2 showed double’s rounding noise.
What does MidpointRounding.AwayFromZero do?
- Rounds to even
- Truncates
- Throws
- Rounds a half up for positive amounts
Matches the usual receipt rule.
What does {l.Item,-10} do?
- Removes text
- Rounds a number
- Repeats it
- Pads the text to 10 characters, left-aligned
It aligns columns.
How were the totals verified?
- By running twice
- By eye
- By Python decimal arithmetic
- They were not
All three matched.
Where should rounding happen?
- After every step in any mode
- Never
- Once, on the final amounts, with a stated rule
- Only for tips
Explicit and consistent.
Summary
- A project joins records, collections and loops.
- Use decimal for money and round explicitly.
- Format receipts with width and F2.
- Verify totals with an independent decimal calculation.
Questions
Why is the tax rate called an example?
Tax rules differ by country and change over time. Check official sources for the rate that applies to you.
Can I read prices from a file?
Yes. A later lesson reads JSON files. For now a dictionary keeps the example short.
Why a record for Line?
It carries data, has value equality and needs no extra code.
