Projects · 7 / 19 Exercise Hard

Inventory control

Objective

Create a Python program that controls a shop inventory with its own class and a custom exception for insufficient stock.

Instructions

  • Read an integer n and then n commands, one per line. Names have no spaces and quantities are positive integers.
  • add NAME QUANTITY: adds units and shows Added: NAME (+QUANTITY).
  • remove NAME QUANTITY: subtracts units and shows Removed: NAME (-QUANTITY). If the product does not exist, show Error: unknown product NAME. If there are fewer units than requested, show Error: insufficient stock of NAME (there are H, requested Q) and change nothing.
  • check NAME: shows NAME: units, or the unknown product error.
  • report: one NAME: units line per product, sorted from most to fewest units (tie: by name), or Empty inventory if there are no products; then Total units: T.
Input
6
add tornillo 50
add clavo 20
remove tornillo 15
check tornillo
remove clavo 30
report
Output
Added: tornillo (+50)
Added: clavo (+20)
Removed: tornillo (-15)
tornillo: 35
Error: insufficient stock of clavo (there are 20, requested 30)
tornillo: 35
clavo: 20
Total units: 55

Open the editor in full screen: the statement on the left and your code, the output and the tests on the right.

Start exercise