Core Java · Chapter 44 of 48

Project: station billing

By the end of this chapter you can build a small program from several files that each do one job: records that refuse bad values, a class that guards its own list, and exact money rules with a printed receipt. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.

  • Part: Projects
  • 6 tasks
  • Java
  • In the all labs plan

The first 2 chapters of Core Java are free. This one comes with the all labs plan.

Why you need this

The pilot is over. From Monday, every counter at SBC, NDLS and CSMT bills real passengers, and Meera wants one billing module that all three run: "Offers, GST, a receipt that adds up. And when someone types something silly, it says no instead of printing a wrong bill."

Until now each chapter had one idea and one or two files. A real module mixes them: records, a list that changes, BigDecimal, formatting and exceptions. Put all of it in one class and every change to an offer risks breaking the receipt, and a bad quantity typed at 6 am travels all the way to a printed bill before anyone notices.

What you build: 6 tasks

Each task has tests. You write the code in the lab's editor and press Check.

  1. Task 1

    MenuItem.java and OrderLine.java: refuse bad values, and total a line

    In MenuItem.java, make the compact constructor throw an IllegalArgumentException when the code or the name is null or blank (empty or only spaces), or the price is null, zero or negative. In OrderLine.java, make the compact constructor throw an IllegalArgumentException when the item is null or the quantity is below 1, and make lineTotal() return the item's price times the quantity, as a BigDecimal.

    For example, new MenuItem("masala", " ", 40.00) and new MenuItem("masala", "Masala chai", 0.00) throw, new MenuItem("elaichi", "Elaichi chai", 45.00) works; new OrderLine(MASALA_CHAI, 0) throws; a line of 2 masala chai totals 80.00 and 3 cutting chai 75.00.

  2. Task 2

    Order.java: add items, merging repeats, and total the lines

    In Order.java, make add(item, qty) add qty of the item to the order. An item whose code is already on the order joins that line: the line gets the two quantities added together and keeps its place. A new item goes at the end. A quantity below 1 throws an IllegalArgumentException. Make lines() return the lines as a list the caller cannot change (adding to it throws UnsupportedOperationException), and subtotal() return the sum of every line total, 0.00 for an empty order.

    For example, adding 1 masala chai, 1 bun maska, then 2 masala chai gives the lines [3 x masala chai, 1 x bun maska] and a subtotal of 170.00.

  3. Task 3

    Order.java: take items off an order safely

    In Order.java, make remove(code, qty) take qty of the item with that code off the order. When nothing is left of a line, the line goes away; otherwise it keeps its place with the smaller quantity. An item not on the order, or a quantity below 1 or more than the line has, throws an IllegalArgumentException and leaves the order exactly as it was.

    For example, with 3 masala chai and 1 bun maska, remove("masala", 1) leaves [2 masala chai, 1 bun maska]; remove("bun", 1) then leaves [2 masala chai]. remove("cutting", 1) and remove("masala", 5) both throw, and the order is still [2 masala chai].

  4. Task 4

    Billing.java: apply the better of the two offers

    In Billing.java, make discount(order) return the bigger of two offers, with two decimals. Combo: COMBO_OFF (10.00) for every pair of a masala chai and a bun maska, that is, 10.00 times the smaller of the two quantities. Bulk: BULK_RATE (10%) of the subtotal when the subtotal is BULK_FROM (1000.00) or more, otherwise nothing. No offer gives 0.00.

    For example, 2 masala chai and 1 bun maska give 10.00; 3 and 3 give 30.00; 5 cutting chai give 0.00; 25 masala chai (exactly 1000.00) give 100.00; 25 masala chai and 2 bun maska give 110.00 (bulk beats combo 20.00); 20 and 20 give 200.00 (combo beats bulk 180.00); 24 masala chai and 3 Parle-G (990.00) give 0.00.

  5. Task 5

    Billing.java: add 5% GST and work out the total to the paisa

    In Billing.java, make gst(order) return GST_RATE (5%) of the subtotal minus the discount, rounded half up to exactly two decimals, and total(order) return the subtotal minus the discount plus the GST, also with exactly two decimals. The check compares with equals, so the scale matters: 6.0000 or 6 where 6.00 is expected fails.

    For example, 2 masala chai and 1 bun maska: GST 6.00, total 126.00. 13 masala chai, 19 cutting chai and 1 Parle-G (subtotal 1005.00, discount 100.50): GST 45.23 (45.225 rounds up), total 949.73. 1 cutting chai: GST 1.25, total 26.25.

  6. Task 6

    Billing.java: print the receipt

    In Billing.java, make receipt(order) return the printed receipt, every line ending with \n. First Tapri, a space and the station. Then one row per order line, labelled with the quantity, " x " and the item's name, then rows labelled Subtotal, Discount (only when the discount is more than zero, shown as a negative amount), GST 5% and Total. Each row is its label padded on the right to 22 characters, then the amount with two decimals padded on the left to 8. An empty order throws an IllegalStateException.

    For example, 2 masala chai and 1 bun maska at SBC give the seven lines: Tapri SBC / 2 x Masala chai 80.00 / 1 x Bun maska 50.00 / Subtotal 130.00 / Discount -10.00 / GST 5% 6.00 / Total 126.00. 1 cutting chai at NDLS has no Discount row.

What you will learn

  • Splitting a program by job: data, the thing that changes, and the rules, each in its own file.
  • Validating at the door: a compact constructor that makes a bad record impossible.
  • Guarding a list of immutable records: replacing a record in place, and handing out a copy nobody can change.
  • Money and a lined-up receipt: BigDecimal rounding, max, and padded columns with String.format.

More in Projects

Build "Project: station billing" yourself

Tapri runs chai counters at railway stations, and every counter runs a Java program you write. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.

The first 2 chapters of Core Java are free. This one comes with the all labs plan.