In this tutorial, we will build a simple Inventory Management System using core Java. This system allows you to manage inventory by adding products, displaying product details, and updating product quantities. Please note that this is a simplified version, and a real-world inventory management system would involve more complex features, database integration, and inventory tracking.
Inventory Management System using Core Java
import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
class Product {
private int id;
private String name;
private int quantity;
public Product(int id, String name, int quantity) {
this.id = id;
this.name = name;
this.quantity = quantity;
}
public int getId() {
return id;
}
public String getName() {
return name;
}
public int getQuantity() {
return quantity;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
}
class InventoryManagementSystem {
private List<Product> products;
public InventoryManagementSystem() {
this.products = new ArrayList<>();
}
public void addProduct(Product product) {
products.add(product);
System.out.println("Product added: " + product.getName());
}
public void displayProducts() {
System.out.println("Products in inventory:");
for (Product product : products) {
System.out.println("ID: " + product.getId() + ", Name: " + product.getName() + ", Quantity: " + product.getQuantity());
}
}
public void updateProductQuantity(int productId, int newQuantity) {
for (Product product : products) {
if (product.getId() == productId) {
product.setQuantity(newQuantity);
System.out.println("Quantity updated for product: " + product.getName());
return;
}
}
System.out.println("Product not found.");
}
}
public class InventoryManagementSystemExample {
public static void main(String[] args) {
InventoryManagementSystem inventorySystem = new InventoryManagementSystem();
Product product1 = new Product(1, "Chair", 10);
Product product2 = new Product(2, "Table", 5);
inventorySystem.addProduct(product1);
inventorySystem.addProduct(product2);
inventorySystem.displayProducts();
System.out.println("Enter the product ID to update quantity: ");
Scanner scanner = new Scanner(System.in);
int productId = scanner.nextInt();
System.out.println("Enter the new quantity: ");
int newQuantity = scanner.nextInt();
inventorySystem.updateProductQuantity(productId, newQuantity);
inventorySystem.displayProducts();
}
}
Explanation:
The Product class represents a product with properties such as id, name, and quantity.
The InventoryManagementSystem class manages the inventory of products. It has methods to add products, display product details, and update product quantities.
In the InventoryManagementSystemExample class, we create an instance of the InventoryManagementSystem class and some sample Product objects.
We add the products to the inventory using the addProduct() method and display all the products using the displayProducts() method.
Then, we prompt the user to enter the product ID for which they want to update the quantity and the new quantity. We call the updateProductQuantity() method to update the product's quantity.
Finally, we display the updated product details by calling the displayProducts() method.
Output:
Product added: Chair Product added: Table Products in inventory: ID: 1, Name: Chair, Quantity: 10 ID: 2, Name: Table, Quantity: 5 Enter the product ID to update quantity: 1 Enter the new quantity: 15 Quantity updated for product: Chair Products in inventory: ID: 1, Name: Chair, Quantity: 15 ID: 2, Name: Table, Quantity: 5
This is a basic implementation to give you an idea of how an Inventory Management System can be built using core Java. You can enhance this code by adding more features such as adding prices, implementing product search by ID or name, handling product stock levels, implementing data persistence using a database, and incorporating additional inventory management operations based on your requirements.
Great post!
ReplyDelete