Boss

Boss: Guild Inventory

The boss
Boss Fight!
Every passing test breaks a shield.

The Lantern Guild's vault is chaos: pearls in the boot cupboard, three ledgers that disagree. The quartermaster hands you the keys: "Build me an inventory system I can trust."

Rarity is ready. You'll build the rest in four steps. Run your code after each step: the tests show which parts already work.

Assignment

Step 1: the items

  1. Make Item a frozen dataclass with the fields name: str, rarity: Rarity and value: int.

Step 2: the stock (like the Shelf from the last lesson)

  1. Inventory(owner) stores owner. It keeps a dict that maps each Item to how many you have.
  2. count(item) returns how many you have, or 0.
  3. add(item, qty=1) raises ValueError if qty is not positive.
  4. remove(item, qty=1) raises ValueError if you don't have that many. Items that reach 0 disappear completely.

Step 3: reports

  1. A read-only property total_value: for every item, multiply its value by its count, and add them all up.
  2. names(rarity) returns the names of the items you hold with that rarity, sorted A to Z.
  3. __str__ returns text like Ada's vault: 3 items worth 90 gold. "Items" means the total count of all units.

Step 4: loading from text

  1. A class method from_lines(owner, lines) builds a full inventory. Each line looks like "Moon Pearl,RARE,40,2": the name, the rarity's name, the value and the quantity.