#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// Structure to represent an item in the inventory
struct Item {
int id;
char name[50];
float price;
int quantity;
};
// Function prototypes
void displayMenu();
void addItem(struct Item inventory[], int *itemCount);
void displayInventory(const struct Item inventory[], int itemCount);
void searchItem(const struct Item inventory[], int itemCount);
int main() {
struct Item inventory[100]; // Maximum of 100 items in the inventory
int itemCount = 0;
int choice;
do {
displayMenu();
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addItem(inventory, &itemCount);
break;
case 2:
displayInventory(inventory, itemCount);
break;
case 3:
searchItem(inventory, itemCount);
break;
case 4:
printf("Exiting the program. Goodbye!\n");
break;
default:
printf("Invalid choice. Please enter a valid option.\n");
}
} while (choice != 4);
return 0;
}
void displayMenu() {
printf("\n--- Online Store Inventory Management ---\n");
printf("1. Add Item\n");
printf("2. Display Inventory\n");
printf("3. Search Item\n");
printf("4. Exit\n");
}
void addItem(struct Item inventory[], int *itemCount) {
if (*itemCount == 100) {
printf("Cannot add more items. Inventory is full.\n");
return;
}
struct Item newItem;
printf("Enter item details:\n");
printf("ID: ");
scanf("%d", &newItem.id);
printf("Name: ");
scanf("%s", newItem.name);
printf("Price: ");
scanf("%f", &newItem.price);
printf("Quantity: ");
scanf("%d", &newItem.quantity);
inventory[*itemCount] = newItem;
(*itemCount)++;
printf("Item added to the inventory successfully.\n");
}
void displayInventory(const struct Item inventory[], int itemCount) {
if (itemCount == 0) {
printf("Inventory is empty.\n");
return;
}
printf("\n--- Inventory ---\n");
printf("%-5s%-20s%-10s%-10s\n", "ID", "Name", "Price", "Quantity");
for (int i = 0; i < itemCount; i++) {
printf("%-5d%-20s%-10.2f%-10d\n", inventory[i].id, inventory[i].name, inventory[i].price, inventory[i].quantity);
}
}
void searchItem(const struct Item inventory[], int itemCount) {
if (itemCount == 0) {
printf("Inventory is empty. Cannot search for items.\n");
return;
}
int searchId;
printf("Enter the ID of the item to search: ");
scanf("%d", &searchId);
for (int i = 0; i < itemCount; i++) {
if (inventory[i].id == searchId) {
printf("Item found:\n");
printf("%-5s%-20s%-10s%-10s\n", "ID", "Name", "Price", "Quantity");
printf("%-5d%-20s%-10.2f%-10d\n", inventory[i].id, inventory[i].name, inventory[i].price, inventory[i].quantity);
return;
}
}
printf("Item with ID %d not found in the inventory.\n", searchId);
}