Menu

Sign in to track your progress and unlock all features.

Theme style

Log in

Full lesson preview

Create an Inventory System with Products Warehouses and Stock Entries

Design an inventory system using composition: Products stored in Warehouses tracked via stock entries. Implement methods to add entities, record stock, transfer items, and query quantities.

Python practice28 minComposition & Real-World ModelingAdvancedLast updated April 11, 2026

Problem statement

Build an Inventory system composed of Product and Warehouse objects. The Inventory should track stock quantities by creating/maintaining stock entries (product + warehouse -> quantity). Implement an API that allows: - Adding products and warehouses. - Recording incoming stock to a specific warehouse. - Transferring stock between warehouses while preserving total quantity. - Querying the quantity of a product in a specific warehouse and the total across all warehouses. Your classes should encourage composition: Inventory contains/manages Products, Warehouses, and the stock bookkeeping. Methods that mutate the inventory (add_product, add_warehouse, stock_product, transfer) should return the Inventory instance to allow method chaining. Query methods should return integers. Be careful to validate inputs: you may assume SKUs and warehouse codes are case-sensitive strings. Transfers should not allow moving more than available in the source warehouse. If a query is made for unknown product or warehouse, return 0 for stock queries.

Task

Model a multi-class inventory system using composition. Practice designing clear APIs for adding products/warehouses, recording stock entries, transferring stock, and querying per-warehouse and total quantities.

Examples

Basic add and stock

Input

Inventory().add_product('SKU1', 'Widget').add_warehouse('W1', 'Main').stock_product('SKU1', 'W1', 100).get_total_stock('SKU1')

Output

100

Create inventory, add product SKU1 and warehouse W1, record 100 units in W1. Total for SKU1 is 100.

Input format

A single expression creating/using the Inventory class. Examples show chaining: Inventory().add_product(...).add_warehouse(...).stock_product(...).get_total_stock(...).

Output format

Query methods return integers. The test harness compares str(returned_value) to the expected string.

Constraints

Do not use external libraries. Methods that change state should return the Inventory instance to support chaining. Query methods must return integers >= 0. Transfers must not allow negative quantities or moving more than available in the source warehouse.

Samples

Sample 1

Input

Inventory().add_product('P','Thing').add_warehouse('A','Alpha').stock_product('P','A',10).get_stock('P','A')

Output

10

After stocking 10 units of product 'P' in warehouse 'A', get_stock for that warehouse should return 10.