Introduction
Electricity bills use progressive slabs: the first units are cheap and each slab beyond costs more. Implementing that correctly — plus duty, surcharges and rounding — is a realistic exercise in loops, enums and careful integer money arithmetic.
Problem statement
Objectives
- Represent tariffs as an enum with data
- Implement slab calculations with a loop
- Keep money exact using long paise
- Produce itemised, aligned output
Features
- Two tariffs with different slabs and fixed charges
- Itemised energy charges per slab
- Duty, late surcharges and rounding lines
- Validation and a billing summary
Concepts demonstrated
- Enums with constructors
- Progressive (slab) calculation
- Integer money arithmetic
- Validation
- Aggregation
System architecture
Tariff is an enum whose constants carry slab limits, paise-per-unit rates and a fixed charge. compute walks the slabs, charging min(remaining, slab size) × rate for each and recording a Line record for the bill, then adds fixed charge, duty, surcharge and rounding lines. parseTariff matches tariff names case-insensitively with a friendly error. main validates each line, prints each Bill, and summarises with streams.
Folder structure
Complete source code
src/Main.java
What each file does
- src/Main.java
- The
Tariffenum,compute(slabs, duty, surcharge, rounding), bill printing and the summary.
How to run it
Compile
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
# id name tariff previous current days_late C1001 Asha domestic 12450 12530 0 C1002 Ravi domestic 8800 9135 20 C1003 Bakery commercial 30210 30780 0 C1004 Meera domestic 5000 4990 0 C1005 Shop industrial 100 200 0 C1006 John domestic 7000 7250 40
Testing
- Run with
tests/sample-input.txtand compare withtests/expected-output.txt. - Ravi uses 335 units: 350 + 500 + 680 + 280 = ₹1,810 energy, plus fixed charge, duty and a 1.5% surcharge for 20 days late.
- John is 40 days late, which counts as three 15-day periods.
- A backwards reading and the unknown tariff 'industrial' are rejected.
Common errors
Bills are a few paise off
Store money as long paise and round explicitly once per charge; never accumulate doubles.
Every unit is charged at the highest rate
Charge each slab only for the units that fall inside it.
Possible improvements
- Add a time-of-day tariff with peak rates
- Read tariffs from a configuration file
- Generate a bill file per consumer
Additional challenges
- 1Add a subsidy for the first 50 units for domestic users.
- 2Show the cost per unit for each customer.
- 3Compare this month with last month's consumption.
Built and tested it yourself? Mark it to update your dashboard.