Question 1:
Develop an Inventory Management System for a shop dealing in electrical
goods.
Source Code:
import java.util.ArrayList;
import java.util.Scanner;
class Product {
int id;
String name;
int quantity;
double price;
public Product(int id, String name, int quantity, double price) {
this.id = id;
this.name = name;
this.quantity = quantity;
this.price = price;
@Override
public String toString() {
return "ID: " + id + ", Name: " + name + ", Quantity: " + quantity + ", Price: $" + price;
public class InventoryManagementSystem {
private static ArrayList<Product> inventory = new ArrayList<>();
private static Scanner scanner = new Scanner(System.in);
public static void main(String[] args) {
while (true) {
System.out.println("\n--- Inventory Management System ---");
System.out.println("1. Add Product");
System.out.println("2. View Products");
System.out.println("3. Update Product");
System.out.println("4. Delete Product");
System.out.println("5. Exit");
System.out.print("Enter your choice: ");
int choice = scanner.nextInt();
switch (choice) {
case 1:
addProduct();
break;
case 2:
viewProducts();
break;
case 3:
updateProduct();
break;
case 4:
deleteProduct();
break;
case 5:
System.out.println("Exiting the program.");
return;
default:
System.out.println("Invalid choice! Please try again.");
private static void addProduct() {
System.out.print("Enter Product ID: ");
int id = scanner.nextInt();
scanner.nextLine(); // Consume newline
System.out.print("Enter Product Name: ");
String name = scanner.nextLine();
System.out.print("Enter Quantity: ");
int quantity = scanner.nextInt();
System.out.print("Enter Price: ");
double price = scanner.nextDouble();
inventory.add(new Product(id, name, quantity, price));
System.out.println("Product added successfully!");
private static void viewProducts() {
if (inventory.isEmpty()) {
System.out.println("No products in inventory.");
} else {
System.out.println("\n--- Product List ---");
for (Product product : inventory) {
System.out.println(product);
private static void updateProduct() {
System.out.print("Enter Product ID to update: ");
int id = scanner.nextInt();
boolean found = false;
for (Product product : inventory) {
if (product.id == id) {
found = true;
System.out.print("Enter new Quantity: ");
product.quantity = scanner.nextInt();
System.out.print("Enter new Price: ");
product.price = scanner.nextDouble();
System.out.println("Product updated successfully!");
break;
if (!found) {
System.out.println("Product not found!");
private static void deleteProduct() {
System.out.print("Enter Product ID to delete: ");
int id = scanner.nextInt();
boolean removed = inventory.removeIf(product -> product.id == id);
if (removed) {
System.out.println("Product deleted successfully!");
} else {
System.out.println("Product not found!");
}
Output:
Adding a Product:
--- Inventory Management System ---
1. Add Product
2. View Products
3. Update Product
4. Delete Product
5. Exit
Enter your choice: 1
Enter Product ID: 101
Enter Product Name: LED Bulb
Enter Quantity: 50
Enter Price: 2.5
Product added successfully!
Adding Another Product:
--- Inventory Management System ---
1. Add Product
2. View Products
3. Update Product
4. Delete Product
5. Exit
Enter your choice: 1
Enter Product ID: 102
Enter Product Name: Wire Roll
Enter Quantity: 20
Enter Price: 15.0
Product added successfully!
Viewing Products:
--- Inventory Management System ---
1. Add Product
2. View Products
3. Update Product
4. Delete Product
5. Exit
Enter your choice: 2
--- Product List ---
ID: 101, Name: LED Bulb, Quantity: 50, Price: $2.5
ID: 102, Name: Wire Roll, Quantity: 20, Price: $15.0
Updating a Product:
--- Inventory Management System ---
1. Add Product
2. View Products
3. Update Product
4. Delete Product
5. Exit
Enter your choice: 3
Enter Product ID to update: 101
Enter new Quantity: 60
Enter new Price: 3.0
Product updated successfully!
Viewing Updated Products:
--- Inventory Management System ---
1. Add Product
2. View Products
3. Update Product
4. Delete Product
5. Exit
Enter your choice: 2
--- Product List ---
ID: 101, Name: LED Bulb, Quantity: 60, Price: $3.0
ID: 102, Name: Wire Roll, Quantity: 20, Price: $15.0
Deleting a Product:
--- Inventory Management System ---
1. Add Product
2. View Products
3. Update Product
4. Delete Product
5. Exit
Enter your choice: 4
Enter Product ID to delete: 102
Product deleted successfully!
Viewing Products After Deletion:
--- Inventory Management System ---
1. Add Product
2. View Products
3. Update Product
4. Delete Product
5. Exit
Enter your choice: 2
--- Product List ---
ID: 101, Name: LED Bulb, Quantity: 60, Price: $3.0
Exiting the Program:
--- Inventory Management System ---
1. Add Product
2. View Products
3. Update Product
4. Delete Product
5. Exit
Enter your choice: 5
Exiting the program.