Introduction
Splitting a bill sounds trivial until three people share an item and the paise don't divide evenly. This project uses BigDecimal for exact decimal arithmetic and the largest-remainder method to distribute leftover paise fairly, so the shares always add up to the bill.
Problem statement
all), set tax and tip percentages and request a split at any time. Reject duplicate people, unknown names, invalid prices (non-positive or more than two decimals) and percentages outside 0–100. The split shows the subtotal, tax, tip and total, and each person's share, rounded to paise so that the shares sum exactly to the total.Objectives
- Use BigDecimal for money and understand scale and rounding modes
- Keep ordered per-person data in a LinkedHashMap
- Implement fair rounding with the largest-remainder method
- Build a small command interpreter
Features
- Items shared by any subset of people, or all
- Tax and tip, including fractional percentages
- Exact shares that always sum to the total
- Clear error messages for every invalid command
Concepts demonstrated
- Exact decimal arithmetic
- Rounding modes
- Largest-remainder apportionment
- Maps
- Command parsing
System architecture
Commands are parsed in a switch. Items are stored as records of a price and a list of names. split first divides each item's price exactly (10 decimal places) among its sharers and multiplies each person's total by (1 + tax + tip). It then rounds every share *down* to paise, computes how many paise are left over compared with the properly rounded total, and gives one paisa each to the people whose rounding lost the most. Finally it verifies that the shares add up to the total.
Folder structure
Complete source code
src/Main.java
What each file does
- src/Main.java
- The command loop,
moneyvalidation, andsplitwith exact sharing and fair rounding.
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.
person Asha person Ravi person Meera person Ravi item 450 all item 320.50 Asha Ravi item 180 Meera item 99.999 Asha item 60 John tax 5 tip 10 split item 100 all tip 12.5 split quit
Testing
- Run with
tests/sample-input.txtand compare withtests/expected-output.txt. - Asha and Ravi each owe (150 + 160.25) × 1.15 = 356.7875, so after rounding down one leftover paisa goes to each of them: 356.79.
- A price of 99.999 and an unknown person are rejected.
- Both splits end with 'Shares add up to the total: true'.
Common errors
ArithmeticException: Non-terminating decimal expansion
BigDecimal.divide needs a scale and rounding mode when the result doesn't terminate (e.g. dividing by 3).
Shares add up to 1 paisa less than the total
Rounding each share independently loses paise; distribute the difference as split does.
Possible improvements
- Support discounts and service charges
- Allow unequal shares (e.g. 2 parts for one person)
- Print a settle-up plan: who pays whom
Additional challenges
- 1Add a 'remove item' command.
- 2Split by percentage instead of by item.
- 3Show each person's itemised share.
Built and tested it yourself? Mark it to update your dashboard.