Project Goal
Build an inventory management system for a small store: add products, track stock levels, process sales, get low-stock alerts, and generate inventory reports. Covers OOP, generics, sorting, and business logic.
Full Code
class Product {
final String id;
final String name;
final String category;
final double price;
int quantity;
final int lowStockThreshold;
Product({required this.id, required this.name, required this.category,
required this.price, required this.quantity, this.lowStockThreshold = 10});
bool get isLowStock => quantity <= lowStockThreshold;
double get totalValue => price * quantity;
@override String toString() =>
'$${id.padRight(8)} $${name.padRight(25)} \$$${price.toStringAsFixed(2).padLeft(8)} '
'Qty:$${quantity.toString().padLeft(5)} $${isLowStock ? "⚠ LOW" : ""}';
}
class SaleRecord {
final String productId;
final String productName;
final int quantity;
final double unitPrice;
final DateTime soldAt;
SaleRecord({required this.productId, required this.productName,
required this.quantity, required this.unitPrice})
: soldAt = DateTime.now();
double get total => quantity * unitPrice;
}
class Inventory {
final Map<String, Product> _products = {};
final List<SaleRecord> _sales = [];
void addProduct(Product product) {
_products[product.id] = product;
print('Added: $${product.name}');
}
void restock(String id, int qty) {
var p = _findProduct(id);
p.quantity += qty;
print('Restocked $${p.name}: +$qty (now $${p.quantity})');
}
SaleRecord sell(String id, int qty) {
var p = _findProduct(id);
if (qty > p.quantity) throw StateError('Not enough stock. Have $${p.quantity}, need $qty');
p.quantity -= qty;
var sale = SaleRecord(productId: id, productName: p.name, quantity: qty, unitPrice: p.price);
_sales.add(sale);
if (p.isLowStock) print('⚠ Low stock alert: $${p.name} ($${p.quantity} remaining)');
return sale;
}
Product _findProduct(String id) {
var p = _products[id];
if (p == null) throw ArgumentError('Product not found: $id');
return p;
}
List<Product> get lowStockProducts =>
_products.values.where((p) => p.isLowStock).toList();
void report() {
print('\n===== INVENTORY REPORT =====');
print('$${'ID'.padRight(8)} $${'Name'.padRight(25)} $${'Price'.padLeft(9)} $${'Qty'.padLeft(8)}');
print('-' * 60);
var sorted = _products.values.toList()
..sort((a, b) => a.category.compareTo(b.category));
String? lastCat;
for (var p in sorted) {
if (p.category != lastCat) {
print('\n[$${p.category}]');
lastCat = p.category;
}
print(p);
}
var totalVal = _products.values.fold(0.0, (s, p) => s + p.totalValue);
print('\nTotal inventory value: \$$${totalVal.toStringAsFixed(2)}');
print('Total products: $${_products.length}');
if (lowStockProducts.isNotEmpty) {
print('\n⚠ LOW STOCK ITEMS:');
lowStockProducts.forEach((p) => print(' $${p.name}: $${p.quantity} left'));
}
if (_sales.isNotEmpty) {
var revenue = _sales.fold(0.0, (s, r) => s + r.total);
print('\nTotal sales: $${_sales.length} Revenue: \$$${revenue.toStringAsFixed(2)}');
}
}
}
void main() {
var inv = Inventory();
inv.addProduct(Product(id: 'P001', name: 'Dart Programming Book', category: 'Books', price: 39.99, quantity: 50));
inv.addProduct(Product(id: 'P002', name: 'USB-C Cable 2m', category: 'Electronics', price: 12.99, quantity: 120));
inv.addProduct(Product(id: 'P003', name: 'Mechanical Keyboard', category: 'Electronics', price: 89.99, quantity: 15));
inv.addProduct(Product(id: 'P004', name: 'Notebook A5', category: 'Stationery', price: 4.99, quantity: 200));
inv.addProduct(Product(id: 'P005', name: 'Wireless Mouse', category: 'Electronics', price: 34.99, quantity: 8, lowStockThreshold: 10));
inv.sell('P001', 5);
inv.sell('P002', 30);
inv.sell('P005', 3); // will trigger low stock alert
inv.restock('P005', 20);
inv.report();
}
Challenges
- Add supplier management and purchase orders
- Add barcode scanning simulation
- Export inventory to CSV/JSON
- Build a Flutter UI with stock charts