Lesson 23 of 34 · Classes & Objects
Boss: Java Inventory Ledger with Classes
Auric the Gilded Golem blocks the way. Every test you pass deals damage — pass them all to defeat it and claim a loot chest.
Deep in the Bytecode Highlands, the Merchants' Guild hides its treasure behind Auric the Gilded Golem. The golem counts every coin twice and crushes anyone whose books don't balance — build him a ledger he cannot fault.
Battle plan
This fight uses the whole chapter: constructors that validate their input, private fields with getters, instance methods that protect an object's rules, toString, equals and hashCode, and a class that manages a list of other objects.
Ledger ledger = new Ledger();
ledger.add(new Item("Rope", 12, 3));
ledger.add(new Item("Lantern", 40, 1));
ledger.add(new Item("Rope", 12, 2)); // an equal item: Rope now has quantity 5
System.out.println(ledger.report());
Rope x5 @ 12 = 60
Lantern x1 @ 40 = 40
Total: 100
Treat equality carefully: two Item objects describe the same kind of item when their name and price match, even if their quantities differ. That is exactly what lets the ledger merge them.
Assignment
class Item
- private fields
final String name,final int price(coins per unit) andint quantity Item(String name, int price, int quantity)— throwsIllegalArgumentExceptionifnameisnullor blank,priceis negative, orquantityis less than 1 (a price of 0 is fine)- getters
getName(),getPrice()andgetQuantity() int totalValue()—price * quantityvoid addQuantity(int amount)— addsamount; throwsIllegalArgumentExceptionifamountis less than 1boolean removeQuantity(int amount)— if1 <= amount <= quantity, subtracts it and returnstrue; otherwise changes nothing and returnsfalsetoString()—"Rope x3 @ 12"(name,x, quantity,@, price)equalsandhashCode— items are equal when they have the same name and the same price; the quantity does not matter
class Ledger
- a private
List<Item>that keeps items in the order they were first added void add(Item item)— if an equal item is already in the ledger, increase its quantity instead of adding a second entryItem find(String name)— the first item with that name, ornullboolean remove(String name, int quantity)— takesquantityunits from the first item with that name; when its quantity reaches 0, the item disappears from the ledger. Returnsfalseand changes nothing if there is no such item or it does not have enough unitsint size()— the number of different itemsint totalValue()— the sum of all items' total valuesItem mostValuable()— the item with the highest total value (the earliest one on ties), ornullfor an empty ledgerString report()— one line per item,"<item> = <total value>", followed by"Total: <sum>", separated by"\n"with no newline at the end. An empty ledger reports just"Total: 0"
Every passing test chips gold off Auric's armour. Make all ten pass to claim the treasury.
Premium lesson
Unlock the full Learn Java course
- All 34 lessons, bosses and quizzes — lifetime access
- One payment of $35, no subscription
- Finish within 30 days and claim $10.50 (30%) back
Want more than one course?
The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.
Keep learning
Need a quick reminder? The Java cheat sheet has every topic of Learn Java on one page.
Lesson last updated on .