Skip to content
Codewilds

Lesson 23 of 34 · Classes & Objects

Boss: Java Inventory Ledger with Classes

Boss battlehard100 XP2 min read

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) and int quantity
  • Item(String name, int price, int quantity) — throws IllegalArgumentException if name is null or blank, price is negative, or quantity is less than 1 (a price of 0 is fine)
  • getters getName(), getPrice() and getQuantity()
  • int totalValue() — price * quantity
  • void addQuantity(int amount) — adds amount; throws IllegalArgumentException if amount is less than 1
  • boolean removeQuantity(int amount) — if 1 <= amount <= quantity, subtracts it and returns true; otherwise changes nothing and returns false
  • toString() — "Rope x3 @ 12" (name, x, quantity, @, price)
  • equals and hashCode — 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 entry
  • Item find(String name) — the first item with that name, or null
  • boolean remove(String name, int quantity) — takes quantity units from the first item with that name; when its quantity reaches 0, the item disappears from the ledger. Returns false and changes nothing if there is no such item or it does not have enough units
  • int size() — the number of different items
  • int totalValue() — the sum of all items' total values
  • Item mostValuable() — the item with the highest total value (the earliest one on ties), or null for an empty ledger
  • String 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
Play the 8 free lessons

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 .

Write code ↓
Java+100 XP

Lesson 23 is part of the full course. The first 8 lessons are free.

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
Play the 8 free lessons

Want more than one course?

The All-Access Pass unlocks all 8 courses and every new one we release, for $120 once.