Skip to content
Java project #09Intermediate

Inventory Management System

Products with validated SKUs, stock in and out, reorder alerts, repricing, category valuation, search and a full audit trail of stock movements.

Estimated time
~5 hours to build
Files
6 files · 404 lines
Technologies
Java 21, TreeMap, Records, Streams / Collectors.groupingBy, BigDecimal for parsing prices

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

Build a command-driven inventory. Products have a SKU in the form ABC-123, a name, a category, a price, a quantity and a reorder level. Support adding products (rejecting duplicates and invalid data), receiving and dispatching stock (never below zero), repricing, listing all products with stock values, listing items at or below their reorder level with a suggested order quantity, valuation by category, search by name or category, and the history of movements for a SKU.

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.

TextArchitecture diagram
Main (commands, formatting)
|
v
Inventory ---- TreeMap<String, Product>
| Product: sku, name, category, price, qty, reorder
+---- List<Movement> (id, sku, type, units, balanceAfter)

Folder structure

TextProject tree
inventory-management/
├── README.md
├── src/Inventory.java
├── src/Main.java
├── src/Product.java
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

src/Inventory.java

66 lines
Javasrc/Inventory.java
import java.util.ArrayList;
import java.util.Collection;
import java.util.Comparator;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.TreeMap;
import java.util.stream.Collectors;
/** The product catalogue plus an audit trail of every stock movement. */
public class Inventory {
public record Movement(int id, String sku, String type, int units, int balanceAfter) { }
private final Map<String, Product> products = new TreeMap<>(); // sorted by SKU
private final List<Movement> movements = new ArrayList<>();
public void add(Product p) {
if (products.putIfAbsent(p.sku(), p) != null) throw new IllegalArgumentException("SKU " + p.sku() + " already exists");
log(p, "CREATE", p.quantity());
}
public Product get(String sku) {
return Optional.ofNullable(products.get(sku)).orElseThrow(() -> new IllegalArgumentException("unknown SKU " + sku));
}
public void receive(String sku, int units) {
Product p = get(sku);
p.receive(units);
log(p, "IN", units);
}
public void dispatch(String sku, int units) {
Product p = get(sku);
p.dispatch(units);
log(p, "OUT", units);
}
private void log(Product p, String type, int units) {
movements.add(new Movement(movements.size() + 1, p.sku(), type, units, p.quantity()));
}
public Collection<Product> all() { return products.values(); }
public List<Product> lowStock() {
return products.values().stream().filter(Product::needsReorder)
.sorted(Comparator.comparingInt(Product::quantity).thenComparing(Product::sku)).toList();
}
public Map<String, Long> valueByCategory() {
return products.values().stream()
.collect(Collectors.groupingBy(Product::category, TreeMap::new, Collectors.summingLong(Product::stockValue)));
}
public long totalValue() { return products.values().stream().mapToLong(Product::stockValue).sum(); }
public List<Movement> history(String sku) {
get(sku); // validates the SKU
return movements.stream().filter(m -> m.sku().equals(sku)).toList();
}
public List<Product> search(String text) {
String t = text.toLowerCase();
return products.values().stream().filter(p -> p.name().toLowerCase().contains(t) || p.category().toLowerCase().contains(t)).toList();
}
}

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

Terminal
javac -d out $(find src -name "*.java")
# Windows PowerShell: javac -d out (Get-ChildItem -Recurse src -Filter *.java).FullName

Run

Terminal
java -cp out Main

Test against the expected output

Terminal
java -cp out Main < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt
# Windows PowerShell:
# Get-Content tests\sample-input.txt | java -cp out Main > actual.txt

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
=== Inventory Management ===
>
Added LAP-101 Laptop 14in
>
Added MOU-201 Wireless Mouse
>
Added DES-301 Study Desk
>
Added CHA-302 Office Chair
>
Added PEN-401 Gel Pen Box
>
Error: SKU PEN-401 already exists
>
Error: SKU must look like ABC-123
>
LAP-101 now has 4 ** REORDER (level 5) **
>
MOU-201 now has 10 ** REORDER (level 15) **
>
Error: only 6 Study Desk in stock
>
CHA-302 now has 10
>
CHA-302 now has 3 ** REORDER (level 4) **
>
PEN-401 repriced to Rs 135.75
>
Error: prices have at most 2 decimals
>
Error: unknown SKU XYZ-999
>
SKU Name Category Price Qty Value
CHA-302 Office Chair Furniture Rs 5,299.00 3 Rs 15,897.00 !
DES-301 Study Desk Furniture Rs 7,499.00 6 Rs 44,994.00
LAP-101 Laptop 14in Electronics Rs 54,999.00 4 Rs 219,996.00 !
MOU-201 Wireless Mouse Electronics Rs 649.50 10 Rs 6,495.00 !
PEN-401 Gel Pen Box Stationery Rs 135.75 200 Rs 27,150.00
>
CHA-302 Office Chair qty 3 (reorder at 4, order 5)
LAP-101 Laptop 14in qty 4 (reorder at 5, order 6)
MOU-201 Wireless Mouse qty 10 (reorder at 15, order 20)
>
Electronics Rs 226,491.00
Furniture Rs 60,891.00
Stationery Rs 27,150.00
TOTAL Rs 314,532.00
>
SKU Name Category Price Qty Value
CHA-302 Office Chair Furniture Rs 5,299.00 3 Rs 15,897.00 !
DES-301 Study Desk Furniture Rs 7,499.00 6 Rs 44,994.00
>
#1 CREATE 12 -> balance 12
#6 OUT 8 -> balance 4
>
Input
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.txt and compare with tests/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

  1. 1Add an undo for the last movement.
  2. 2Report the fastest-moving products by units dispatched.
  3. 3Allow batch import of stock from a CSV file.

Built and tested it yourself? Mark it to update your dashboard.