w3resource

Java: Inventory Management

Java OOP: Exercise-13 with Solution

Write a Java program to create a class called "Inventory" with a collection of products and methods to add and remove products, and to check for low inventory.

Sample Solution:

Java Code:

//Product.java
public class Product {
  private String name;
  private int quantity;

  public Product(String name, int quantity) {
    this.name = name;
    this.quantity = quantity;
  }

  public String getName() {
    return name;
  }

  public void setName(String name) {
    this.name = name;
  }

  public int getQuantity() {
    return quantity;
  }

  public void setQuantity(int quantity) {
    this.quantity = quantity;
  }
}

In the above code, we create a class called "Product" with two private attributes, "name" and "quantity". We also create a constructor to initialize these attributes and getter and setter methods to access and modify them.

//Inventory.java
import java.util.ArrayList;

public class Inventory {
  private ArrayList < Product > products;

  public Inventory() {
    products = new ArrayList < Product > ();
  }

  public void addProduct(Product product) {
    products.add(product);
  }

  public void removeProduct(Product product) {
    products.remove(product);
  }

  public void checkLowInventory() {
    for (Product product: products) {
      if (product.getQuantity() <= 100) {
        System.out.println(product.getName() + " is running low on inventory. Current quantity: " + product.getQuantity());
      }
    }
  }
}

Here we create a class called "Inventory" with a private attribute "products", which is an ArrayList of Product objects. We also create a constructor to initialize this attribute as an empty list and methods to add and remove products from the list. Additionally, we create a method called "checkLowInventory()" to check for low inventory levels in the products list.

//Main.java
public class Main {
public static void main(String[] args) {
Inventory inventory = new Inventory();
    Product product1 = new Product("LED TV", 200);
    Product product2 = new Product("Mobile", 80);
    Product product3 = new Product("Tablet", 50);
    System.out.println("Add three products in inventory:");
    inventory.addProduct(product1);
    inventory.addProduct(product2);
    inventory.addProduct(product3);
    System.out.println("\nCheck low inventory:");
    inventory.checkLowInventory();
    System.out.println("\nRemove Tablet from the inventory!");
    inventory.removeProduct(product3);
    System.out.println("\nAgain check low inventory:"); 
    inventory.checkLowInventory();
  }
}

In the "Main" class, we create an instance of the Inventory class and add three Product objects to the list. We then call the "checkLowInventory()" method to check for low inventory levels. Next, we remove one of the products from the list and call the "checkLowInventory()" method again to see if there are any other low inventory levels.

Sample Output:

Add three products in inventory:

Check low inventory:
Mobile is running low on inventory. Current quantity: 80
Tablet is running low on inventory. Current quantity: 50

Remove product3 from the inventory!

Again check low inventory:
Mobile is running low on inventory. Current quantity: 80

Flowchart:

Flowchart: Java  OOP Exercises: Inventory Management.
Flowchart: Java  OOP Exercises: Inventory Management.

Java Code Editor:

Improve this sample solution and post your code through Disqus.

Java OOP Previous: Airplane class to check flight status and delay.
Java OOP Next: School Management System.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Follow us on Facebook and Twitter for latest update.