Skip to Content
Course content

216: Building a Command-Line Inventory System

Click on the "Edit" button in the top corner of the screen to edit your slide content.

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.
Rating
0 0

There are no comments for now.

to be the first to leave a comment.