# Python Inventory Manager with File Persistence

TechBuilt Open School — Free, self-guided technical project kit

- **Track:** Python Programming
- **Level:** Beginner
- **Access:** No account, signup, payment or enrollment required
- **Format:** Guided plan and runnable starter example, not a complete solution or a live class
- **Online guide:** https://techbuiltos.online/projects/python-cli-inventory

## Project goal

Practice Python functions, dictionaries, defensive input handling and simple persistence.

Create a command-line inventory program that validates quantities, calculates stock values and later saves records to JSON.

## Before you begin

- Python 3.11 or newer
- Variables, collections, loops and functions

## Local setup

1. Save the starter as inventory.py.
2. Run python inventory.py in your terminal.
3. After you understand the in-memory version, add load/save commands using pathlib and json.

## Starter example — inventory.py

The sample below is a starting point. It may require the prerequisites above. Complete the subsequent milestones to finish your own project.

```python
from decimal import Decimal

products = {
    "P001": {"name": "Notebook", "price": Decimal("150.00"), "quantity": 8}
}

def sell(sku: str, count: int) -> None:
    if count <= 0:
        raise ValueError("Quantity must be positive")
    product = products.get(sku)
    if product is None:
        raise KeyError(f"Unknown SKU: {sku}")
    if product["quantity"] < count:
        raise ValueError("Insufficient stock")
    product["quantity"] -= count

def stock_value() -> Decimal:
    return sum(
        (item["price"] * item["quantity"] for item in products.values()),
        start=Decimal("0.00"),
    )

sell("P001", 2)
print("Remaining stock value:", stock_value())
```

## Build milestones

1. Model products with SKU, name, price and quantity.
2. Implement safe add, sell and restock functions with clear error messages.
3. Calculate total inventory value from price times quantity.
4. Load and save JSON to a local file and handle missing/corrupt files.
5. Add unit tests for duplicate SKU and insufficient stock.

## Completion checklist

- [ ] The code rejects negative stock changes.
- [ ] Selling more stock than available raises a helpful error.
- [ ] Inventory totals are computed correctly.
- [ ] After persistence is implemented, restarting restores saved records.

## Optional extensions

- Refactor inventory operations into a class with tests.
- Add a CSV import with defensive column/type validation.

## Related learning

- Individual course roadmap: https://techbuiltos.online/courses/oop
- Individual course roadmap: https://techbuiltos.online/courses/dsa

- Specialization: https://techbuiltos.online/specializations/python-developer
- Specialization: https://techbuiltos.online/specializations/python-desktop-application-developer

---

This original self-study project is independent of paid cohorts. Review any external library documentation before use. Do not commit real credentials, private client files or personal data to public repositories.
