Inventory Management in Java: Explanation & Practice

Create an inventory system with products

Problem summary

Create Product class with name, price, quantity. Implement addStock(), sell(), and getValue() methods.

Starter code

// Create Product class with:
// - String name, double price, int quantity
// - addStock(int amount), sell(int amount), getValue()

public class Main {
    public static void main(String[] args) {
        // Create product, add stock, sell some
        // Print: Stock: <qty>, Value: lt;value>
    }
}

Expected output and test cases

  • 10+0-2 = 8 @ $10
    Stock: 8, Value: $80.0
  • Sold all
    Stock: 0, Value: $0.0
  • Added stock
    Stock: 15, Value: $75.0

Hints

  1. Track quantity as instance variable
  2. sell() decreases quantity
  3. getValue() = price * quantity

Related OOP Basics exercises

Practice all OOP Basics exercises · Run this idea in the Java compiler