Certainly! Here's a simple Java console-based Inventory Management System. It allows you to add items, remove items, update quantities, and display the inventory.
import java.util.HashMap;
import java.util.Map;
import java.util.Scanner;
class Item {
private String name;
private int quantity;
private double price;
public Item(String name, int quantity, double price) {
this.name = name;
this.quantity = quantity;
this.price = price;
}
public String getName() {
return name;
}
public int getQuantity() {
return quantity;
}
public double getPrice() {
return price;
}
public void setQuantity(int quantity) {
this.quantity = quantity;
}
public void setPrice(double price) {
this.price = price;
}
@Override
public String toString() {
return "Item{" +
"name='" + name + '\'' +
", quantity=" + quantity +
", price=" + price +
'}';
}
}
public class InventoryManagementSystem {
private Map<String, Item> inventory;
private Scanner scanner;
public InventoryManagementSystem() {
inventory = new HashMap<>();
scanner = new Scanner(System.in);
}
public void addItem() {
System.out.print("Enter item name: ");
String name = scanner.nextLine().trim();
if (inventory.containsKey(name)) {
System.out.println("Item already exists. Use update to change quantity or price.");
return;
}
System.out.print("Enter quantity: ");
int quantity = Integer.parseInt(scanner.nextLine());
System.out.print("Enter price: ");
double price = Double.parseDouble(scanner.nextLine());
inventory.put(name, new Item(name, quantity, price));
System.out.println("Item added successfully.");
}
public void removeItem() {
System.out.print("Enter item name to remove: ");
String name = scanner.nextLine().trim();
if (inventory.containsKey(name)) {
inventory.remove(name);
System.out.println("Item removed.");
} else {
System.out.println("Item not found.");
}
}
public void updateItem() {
System.out.print("Enter item name to update: ");
String name = scanner.nextLine().trim();
if (!inventory.containsKey(name)) {
System.out.println("Item not found.");
return;
}
Item item = inventory.get(name);
System.out.print("Enter new quantity (current: " + item.getQuantity() + "): ");
int quantity = Integer.parseInt(scanner.nextLine());
item.setQuantity(quantity);
System.out.print("Enter new price (current: " + item.getPrice() + "): ");
double price = Double.parseDouble(scanner.nextLine());
item.setPrice(price);
System.out.println("Item updated.");
}
public void displayInventory() {
if (inventory.isEmpty()) {
System.out.println("Inventory is empty.");
return;
}
System.out.println("Current Inventory:");
for (Item item : inventory.values()) {
System.out.println(item);
}
}
public void run() {
while (true) {
System.out.println("\nInventory Management System");
System.out.println("1. Add item");
System.out.println("2. Remove item");
System.out.println("3. Update item");
System.out.println("4. Display inventory");
System.out.println("5. Exit");
System.out.print("Choose an option: ");
String choice = scanner.nextLine();
switch (choice) {
case "1":
addItem();
break;
case "2":
removeItem();
break;
case "3":
updateItem();
break;
case "4":
displayInventory();
break;
case "5":
System.out.println("Exiting...");
scanner.close();
return;
default:
System.out.println("Invalid option. Try again.");
}
}
}
public static void main(String[] args) {
InventoryManagementSystem ims = new InventoryManagementSystem();
ims.run();
}
}
How to use:
- Run the program.
- Choose options from the menu to add, remove, update, or display items.
- Enter the requested details when prompted.
Let me know if you want me to add features like saving to a file, searching items, or using a graphical interface!