Core Java · Chapter 22 of 48
Records
By the end of this chapter you can write a record in one line, check its values when it is built, change it by making a new one, and keep a list inside it safe from the outside. Build it in your browser: a real editor and terminal, AI that explains, and tests that tell you when it works.
- Part: Objects
- 5 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
Anjali printed a test receipt at Mumbai CSMT with a line reading "Masala chai x -2", and the bill came out ₹80 short. Kabir is reading PriceTag, a class with three fields, a constructor and three getters but no equals: two tags for the same chai at the same station are not equal, so a HashSet of prices holds duplicates.
With what you know so far, a class that just carries values costs thirty lines: fields, a constructor, accessors, equals, hashCode and toString, all by hand (chapter 20). Forget one field in equals and two different tags count as the same. And nothing stops a quantity of -2.
What you build: 5 tasks
Each task has tests. You write the code in the lab's editor and press Check.
- Task 1
OrderLine.java: refuse a bad order line
In OrderLine.java, fill in the compact constructor of the OrderLine record so every new line is checked. Throw an IllegalArgumentException when the item is null or blank (only spaces), when the price is negative, or when qty is 0 or less. A price of 0 is fine: a free item. Otherwise strip the spaces around the item name, so item() returns it without them.
For example, new OrderLine(" Masala chai ", 40, 2) has the item Masala chai; quantities 0 and -2, a price of -40, an item of three spaces and a null item all throw; a Free refill at price 0 is accepted.
- Task 2
OrderLine.java: what one line costs
In OrderLine.java, make total() return the price times qty, in rupees.
For example, 3 masala chai at 40 returns 120, and 1 bun maska at 50 returns 50.
- Task 3
OrderLine.java: change the quantity on a new copy
In OrderLine.java, make withQty(newQty) return a new OrderLine, never this same one, with the same item and price and newQty as its quantity. This line keeps its own quantity. The copy is checked like any new line, so withQty(0) throws an IllegalArgumentException (this needs task 1).
For example, 1 bun maska at 50 with withQty(2) gives a line of Bun maska at 50 with qty 2, and the original still has qty 1.
- Task 4
Order.java: an order whose lines nobody can change
In Order.java, fill in the compact constructor of the Order record so the order keeps its own unmodifiable copy of the lines it is given. Adding to the list that was passed in must not change the order, and adding to the list that lines() returns must throw an UnsupportedOperationException. Then make total() return the sum of every line's total (task 2), and 0 for an order with no lines.
For example, an SBC order made from a list of 2 masala chai at 40 and 1 bun maska at 50 still has 2 lines after a Parle-G is added to that list, and its total is 130. An NDLS order with no lines totals 0.
- Task 5 · interview question
Interview: PriceTag.java, turn the class into a record
In PriceTag.java, replace the whole class with a record named PriceTag whose components are station, item and price, in that order (a String, a String and an int). The record writes equals, hashCode and toString for you: two tags with the same station, item and price are equal, have the same hash code and count once in a HashSet, and a tag with another station is not equal.
For example, new PriceTag("SBC", "Masala chai", 40) equals another tag with the same three values, not the NDLS tag for the same chai at 40, and its toString is PriceTag[station=SBC, item=Masala chai, price=40].
What you will learn
- record: one line that declares the fields, the constructor, the accessors, equals, hashCode and toString.
- Compact constructor: where a record checks and tidies its values.
- with-style copies: how to "change" a value that cannot change.
- Shallow immutability: why a list inside a record needs a copy.
More in Objects
- 10. Classes and objects
- 11. Constructors and this
- 12. Encapsulation
- 13. The static keyword
- 14. Inheritance and overriding
- 15. super, this and final
- 16. Packages and access modifiers
- 17. Abstract classes
- 18. Interfaces
- 19. Polymorphism
- 20. The Object class and memory
- 21. Enums
- 23. Sealed types and pattern matching
Build "Records" 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.