Java traps · Chapter 14 of 16
Static state shared between receipts
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.
Why you need this
With one till, a bill was built and printed before the next one began. If the shared list is emptied after each print, every receipt looks right, and the sharing never shows. It shows the moment two objects are alive at once and both use the field: here, two tills with a bill open each.
In a web server it is worse: every request runs on its own thread in one JVM, and every static field is shared by all of them, so one user's data can end up in another's response.
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 receipt that lists the other till's items
Write reproduce() in Repro.java. Open two receipts the way two tills at one counter do, one after the other on this one thread: receipt A (bill SBC-10431) gets a masala chai at ₹40, then receipt B (bill SBC-10432) gets a samosa at ₹20. Return B's lines().
The check runs your reproduce() twice: beside the original Receipt.java it must return ["masala chai ₹40", "samosa ₹20"] (A's chai on B's receipt), and beside a fixed Receipt.java it must return ["samosa ₹20"]. So it must really keep both receipts open and ask B for its lines. No threads are needed. No real waiting and no reading input: reproduce() must finish within 3 seconds.
- Task 2
Receipt.java: each receipt has its own lines
Fix Receipt in Receipt.java so that every receipt keeps its own lines, however many receipts are open at once: when receipt A gets a masala chai and receipt B a samosa, B's lines() is ["samosa ₹20"] and A's is ["masala chai ₹40"]. Printing one receipt must not change another.
Keep the constructor, add, lines, total and the text print gives (bill <id>, a line per item as <item> ₹<price>, then total ₹<total>, one per line) as they are. The check keeps several receipts open at the same time, adds to them in turn and looks at every one of them, not only the newest. A fix that makes a new receipt look right but empties the one still open is as wrong as the bug.
- Task 3
ReceiptTest.java: a test that would have caught it
Write a JUnit test class, ReceiptTest.java, that would have caught this bug before it reached the counters. The check runs your file three times: beside the original Receipt.java, where at least one test must fail; beside the fixed Receipt.java, where every test must pass; and beside a wrong fix that empties the shared lines whenever a new receipt is made, where at least one test must fail.
So keep two receipts open at once, add to both, and check both of them: the newer one has only its own items, and the older one still has all of its own. No real waiting: each test must finish within 3 seconds.
What you will learn
- Repro.java: a receipt that lists the other till's items
- Receipt.java: each receipt has its own lines
- ReceiptTest.java: a test that would have caught it
More in Money
Build "Static state shared between receipts" 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.