
What You’ll Learn
- Defining and using Java classes and objects
- Implementing collections to manage data
- Handling user input and output via the console
- Applying control structures for program flow
Step 1: Define the Product Class
Begin by creating a Product
class to encapsulate product details.
javaCopyEditpublic class Product {
private String name;
private int quantity;
public Product(String name, int quantity) {
this.name = name;
this.quantity = quantity;
}
// Getters and setters
public String getName() { return name; }
public int getQuantity() { return quantity; }
public void setQuantity(int quantity) { this.quantity = quantity; }
}
Step 2: Create the Inventory Class
Next, develop an Inventory
class to manage a collection of products.w3resource
javaCopyEditimport java.util.ArrayList;
public class Inventory {
private ArrayList<Product> products = new ArrayList<>();
public void addProduct(Product product) {
products.add(product);
}
public void removeProduct(String name) {
products.removeIf(p -> p.getName().equalsIgnoreCase(name));
}
public void checkLowInventory(int threshold) {
for (Product p : products) {
if (p.getQuantity() <= threshold) {
System.out.println(p.getName() + " is low on stock: " + p.getQuantity());
}
}
}
public void displayInventory() {
for (Product p : products) {
System.out.println(p.getName() + " - Quantity: " + p.getQuantity());
}
}
}
Step 3: Develop the Main Application
Finally, implement the main class to interact with the user.w3resource
javaCopyEditimport java.util.Scanner;
public class InventoryApp {
public static void main(String[] args) {
Inventory inventory = new Inventory();
Scanner scanner = new Scanner(System.in);
boolean running = true;
while (running) {
System.out.println("\nInventory Management System");
System.out.println("1. Add Product");
System.out.println("2. Remove Product");
System.out.println("3. Check Low Inventory");
System.out.println("4. Display Inventory");
System.out.println("5. Exit");
System.out.print("Choose an option: ");
int choice = scanner.nextInt();
scanner.nextLine(); // Consume newline
switch (choice) {
case 1:
System.out.print("Enter product name: ");
String name = scanner.nextLine();
System.out.print("Enter quantity: ");
int qty = scanner.nextInt();
inventory.addProduct(new Product(name, qty));
break;
case 2:
System.out.print("Enter product name to remove: ");
String removeName = scanner.nextLine();
inventory.removeProduct(removeName);
break;
case 3:
System.out.print("Enter low stock threshold: ");
int threshold = scanner.nextInt();
inventory.checkLowInventory(threshold);
break;
case 4:
inventory.displayInventory();
break;
case 5:
running = false;
break;
default:
System.out.println("Invalid option.");
}
}
scanner.close();
}
}
Sample Output
mathematicaCopyEditInventory Management System
1. Add Product
2. Remove Product
3. Check Low Inventory
4. Display Inventory
5. Exit
Choose an option: 1
Enter product name: Widget
Enter quantity: 50
Inventory Management System
1. Add Product
2. Remove Product
3. Check Low Inventory
4. Display Inventory
5. Exit
Choose an option: 4
Widget - Quantity: 50
Additional Resources
For a more in-depth exploration and GUI-based implementation, consider the following resources:
- Project Gurukul’s Java Inventory Management System: This project utilizes Java Swing for the user interface and integrates with a MySQL database for data management. It provides features like adding products, managing categories, and tracking inventory and sales. Project Gurukul
- Tutusfunny’s Inventory System Using Java: This tutorial guides you through building a MilkBar Inventory Management System using Java, focusing on practical implementation and user interaction. tutussfunny.com