Introduction
Every shop and warehouse has to answer the same questions: what do we have, what is it worth, and what must we reorder? This project splits the problem into a Product that protects its own stock level, an Inventory service that coordinates products and records every movement, and a thin command-line front end.
Problem statement
Objectives
- Separate the domain model, the service layer and the user interface
- Protect invariants inside a class with validating methods
- Use TreeMap and stream collectors for reports
- Keep an immutable audit log with records
Features
- Validated SKUs, prices and quantities
- Stock in/out with reorder warnings
- Category valuation and grand total
- Search, low-stock report and per-SKU history
Concepts demonstrated
- Layered design
- Encapsulation
- Audit logging
- Grouping and aggregation
- Exception-based validation
System architecture
Product validates its data in the constructor and changes its quantity only through receive and dispatch, which throw on invalid requests. Inventory stores products in a TreeMap keyed by SKU (so listings are sorted) and appends a Movement record for every create, in and out. Reports use streams: lowStock filters and sorts, valueByCategory groups with summingLong. Main only parses commands, converts prices with BigDecimal into paise and formats output.
Folder structure
Complete source code
src/Inventory.java
What each file does
- src/Product.java
- The product entity with validation and the stock-changing operations.
- src/Inventory.java
- The service: catalogue, movements, low-stock and valuation reports, search.
- src/Main.java
- Command parsing, price conversion and tabular output.
How to run it
Compile
Run
Test against the expected output
Expected output
Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.
add LAP-101, Laptop 14in, Electronics, 54999, 12, 5 add MOU-201, Wireless Mouse, Electronics, 649.50, 40, 15 add DES-301, Study Desk, Furniture, 7499, 6, 3 add CHA-302, Office Chair, Furniture, 5299, 4, 4 add PEN-401, Gel Pen Box, Stationery, 120, 200, 50 add PEN-401, Duplicate, Stationery, 1, 1, 1 add bad-1, Broken SKU, Misc, 10, 1, 1 out LAP-101 8 out MOU-201 30 out DES-301 10 in CHA-302 6 out CHA-302 7 price PEN-401 135.75 price PEN-401 1.234 in XYZ-999 5 list low value find furn history LAP-101 quit
Testing
- Run with
tests/sample-input.txtand compare withtests/expected-output.txt. - Dispatching 10 Study Desks fails because only 6 are in stock; nothing changes.
- The low-stock report lists three products with suggested orders of (2 × reorder level − quantity).
- Electronics are valued at 4 × 54,999 + 10 × 649.50 = ₹226,491.
Common errors
Quantities go negative
Check availability inside the method that changes the quantity, not in the caller.
Prices like 649.5 become 649
Parse prices with BigDecimal and convert to paise; don't use Integer.parseInt.
Possible improvements
- Persist products and movements to CSV files
- Add suppliers and purchase orders
- Support stock in several warehouses
Additional challenges
- 1Add an
undofor the last movement. - 2Report the fastest-moving products by units dispatched.
- 3Allow batch import of stock from a CSV file.
Built and tested it yourself? Mark it to update your dashboard.