Java
Completed
-
Section 1: Getting Started
-
Section 2: Core Syntax
-
Section 3: Object-Oriented Java
-
Section 4: Collections Framework
-
Section 5: Exception Handling
-
Section 6: Generics
-
Section 7: Functional Java
-
Section 8: Concurrency
-
Section 9: I/O and NIO
-
Section 10: JVM Internals
-
Section 11: Modern Java Features
-
Section 12: Build Tools and Project Structure
-
Section 13: Testing
-
Section 14: Databases and Persistence
-
Section 15: Networking
-
Section 16: Design and Best Practices
-
Section 17: Reflection and Annotations
-
Section 18: Logging and Diagnostics
-
Section 19: Date, Time, and Internationalization
-
Section 20: Java Platform Module System
-
Section 21: Security in Java
-
Section 22: Advanced Collections and Data Structures
-
Section 23: More Concurrency Patterns
-
Section 24: Compression, Files, and System Integration
-
Section 25: GUI Programming
-
Section 26: Practical Projects
-
Section 27: Data Structures and Algorithms
-
Section 28: Interview and Algorithm Practice
-
Section 29: JSON and Data Interchange
-
Section 30: More Concurrency Utilities
-
Section 31: More Collections and Streams Practice
-
Section 32: More File and System Programming
-
Section 33: Standard Library Deep Dive
-
Section 34: More Practice and Drills
-
Section 35: More Testing and Quality
-
Section 36: More Design Patterns and Architecture
-
Section 37: Career and Ecosystem
-
Section 38: More OOP and Architecture Practice
-
Section 39: More Enterprise Concepts
-
Section 40: Advanced JavaFX
-
Section 41: More Interview Practice
216: Building a Command-Line Inventory System
I remember working with a junior dev named Mark a few years back. He was tasked with tracking the hardware assets for our small satellite office—keyboards, monitors, the works. Mark decided to use a shared spreadsheet, which seemed fine for the first week. But by week three, he had three different versions of the "Master" sheet, and he spent an entire Friday afternoon manually counting monitors because someone had deleted a row in the shared doc. He came to me completely frazzled, realizing that a spreadsheet isn't a system; it's just a list that's easy to break. That's when we sat down and spent an hour sketching out a basic command-line tool to handle the inventory properly.
Building a CLI inventory system is the perfect way to synthesize everything you've learned about objects, collections, and user input. Instead of just writing isolated methods, you're now managing the "state" of an application—meaning the data persists in memory as long as the program is running, and the user can interact with that data in real-time.
Modeling the Inventory Item
Before you worry about the menu or the loops, you need a solid blueprint for what you're actually tracking. If you just use a List<String>, you're back to Mark's spreadsheet problem—you can't easily track quantity or price. You need a dedicated class. I usually recommend keeping this class lean; it should be a simple POJO (Plain Old Java Object) that holds the data.
public class Product {
private String id;
private String name;
private int quantity;
private double price;
public Product(String id, String name, int quantity, double price) {
this.id = id;
this.name = name;
this.quantity = quantity;
this.price = price;
}
// Getters and a method to update quantity
public String getId() { return id; }
public String getName() { return name; }
public int getQuantity() { return quantity; }
public void setQuantity(int quantity) { this.quantity = quantity; }
@Override
public String toString() {
return String.format("ID: %s | Name: %s | Qty: %d | Price: $%.2f", id, name, quantity, price);
}
}
The Execution Loop and User Interface
The core of any CLI tool is the "Read-Eval-Print Loop" (REPL). You don't want the program to end after one action; you want it to keep running until the user explicitly tells it to stop. I've seen too many beginners write a separate main method for every feature. Instead, use a while loop and a switch statement to route the user's choice.
You'll want to use an ArrayList to store your products. Why? Because you don't know how many items the user will add. A fixed-size array would be a nightmare here. Here is how I typically structure the main control flow:
Scanner scanner = new Scanner(System.in);
List<Product> inventory = new ArrayList<>();
boolean running = true;
while (running) {
System.out.println("\n--- Inventory Management ---");
System.out.println("1. Add Product\n2. View All\n3. Update Stock\n4. Exit");
System.out.print("Choose an option: ");
String choice = scanner.nextLine();
switch (choice) {
case "1" -> addProduct(scanner, inventory);
case "2" -> viewInventory(inventory);
case "3" -> updateStock(scanner, inventory);
case "4" -> running = false;
default -> System.out.println("Invalid option, try again.");
}
}
Managing State and Searching
The trickiest part of this system is the "Update" functionality. Since your products are stored in a list, you can't just jump to a specific item. You have to iterate through the list and find the product that matches the ID provided by the user. I always suggest using a boolean flag to track if the item was actually found; otherwise, the user might think the update worked when they actually typed the ID wrong.
When you're searching, avoid using == for the IDs. Since IDs are Strings, always use .equals(). It's a classic mistake that has cost me more hours of debugging than I'd care to admit. Once you find the matching object, you can call your setter method to change the quantity, and the change will be reflected in the list because you're working with a reference to the object, not a copy of it.
📋 Practical Task
Exercise: Implementing a Low-Stock Alert Trigger
Your inventory system is working, but the user still has to manually scroll through the whole list to see what's running low. Your task is to extend the current system by adding a "Low Stock Report" feature.
- Modify the main menu to include a fifth option: "5. Low Stock Report".
- Implement a method called
printLowStockReport(List<Product> inventory). - This method should iterate through the inventory and print only the products where the quantity is less than 5.
- If no products are low on stock, the program should print: "All stock levels are healthy."
- Ensure your output clearly distinguishes between the full inventory view and the filtered low-stock report.
There are no comments for now.