Java traps · Chapter 10 of 16
BigDecimal equals versus compareTo
Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Money
- 3 tasks
- Java
- In the all labs plan
The first 2 chapters of Java traps are free. This one comes with the all labs plan.
What you build: 3 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
Repro.java: a paid bill that comes back unmatched
Write reproduce() in Repro.java. Make one bill, SBC-10421, that the till stored as new BigDecimal("40"), and the bank's payment for it, new BigDecimal("40.00"), each in a map of bill id to amount, and return what Settlement.unmatched gives for them.
The check runs your reproduce() twice: beside the original Settlement.java it must return ["SBC-10421"] (a bill paid in full, unmatched), and beside a fixed Settlement.java it must return [] (an empty list). So it must really ask unmatched. No real waiting and no reading input: reproduce() must finish within 3 seconds.
- Task 2
Settlement.java: paid bills match the bank's amounts
Fix unmatched(Map<String, BigDecimal> bills, Map<String, BigDecimal> bank) in Settlement.java so that a bill is unmatched only when the bank paid a different amount (by even a paisa) or never paid it: till 40 and bank 40.00 match, 0 and 0.00 match, 40.50 against 40.00 is unmatched, 40.01 against 40.00 is unmatched, and a bill missing from the bank's map is unmatched. A bank payment for a bill the till never had is ignored.
Keep the rest as it is: the ids come back sorted, and each unmatched bill prints its log line (unmatched <id>: till <amount>, bank <amount or nothing>). The check uses its own bills, with amounts written with no decimals, one or two, and differences of a single paisa.
- Task 3
SettlementTest.java: a test that would have caught it
Write a JUnit test class, SettlementTest.java, that would have caught this bug before the first night's settlement. The check runs your file three times: beside the original Settlement.java, where at least one test must fail; beside the fixed Settlement.java, where every test must pass; and beside a wrong fix that compares whole rupees with intValue(), where at least one test must fail.
So test both sides: the same amount written with a different number of decimals (40 and 40.00) is matched, and a bill paid a few paise short (40.50 against 40.00) is unmatched. No real waiting: each test must finish within 3 seconds.
What you will learn
- Repro.java: a paid bill that comes back unmatched
- Settlement.java: paid bills match the bank's amounts
- SettlementTest.java: a test that would have caught it
More in Money
Build "BigDecimal equals versus compareTo" yourself
Tapri Counter runs on Java, and Kabir, its senior developer, keeps assigning you tickets. Each is a real trap interviews love to ask about. A real editor and terminal in your browser, AI that explains, and a Check that says when it works.
The first 2 chapters of Java traps are free. This one comes with the all labs plan.