← Unit 3 · all exercises
Saved in this browser only

Stock on a shelf

Write an Item with a name and a quantity. sell reduces the quantity by one, but not below zero. restock adds the amount given. inStock returns whether any remain, and getQuantity returns the count.

class Item { Item(String name, int qty); void sell(); void restock(int amount); boolean inStock(); int getQuantity(); }

Item.java
Loading editor…
Results

Examples

  • Item i = new Item("pen", 3); i.sell(); i.getQuantity() → 2
  • new Item("pen", 0).inStock() → false

There are also 3 hidden tests you can't see. Solve the problem, not the examples.