#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MAX_ITEMS 100
// Structure to represent an item in the warehouse
typedef struct {
char name[50];
int quantity;
float price;
} Item;
// Function to add a new item to the warehouse
void addItem(Item items[], int *count) {
if (*count < MAX_ITEMS) {
printf("Enter item name: ");
scanf("%s", items[*count].name);
printf("Enter item quantity: ");
scanf("%d", &items[*count].quantity);
printf("Enter item price: ");
scanf("%f", &items[*count].price);
(*count)++;
printf("Item added successfully!\n");
} else {
printf("Warehouse is full!\n");
}
}
// Function to display all items in the warehouse
void displayItems(Item items[], int count) {
if (count > 0) {
printf("--------------------------------------------------\n");
printf("Name\t\tQuantity\tPrice\n");
printf("--------------------------------------------------\n");
for (int i = 0; i < count; i++) {
printf("%s\t\t%d\t\t%.2f\n", items[i].name, items[i].quantity, items[i].price);
}
printf("--------------------------------------------------\n");
} else {
printf("Warehouse is empty!\n");
}
}
// Function to search for an item by name
void searchItem(Item items[], int count) {
char searchName[50];
printf("Enter item name to search: ");
scanf("%s", searchName);
int found = 0;
for (int i = 0; i < count; i++) {
if (strcmp(items[i].name, searchName) == 0) {
printf("Item found!\n");
printf("Name: %s\n", items[i].name);
printf("Quantity: %d\n", items[i].quantity);
printf("Price: %.2f\n", items[i].price);
found = 1;
break;
}
}
if (!found) {
printf("Item not found!\n");
}
}
// Function to update the quantity of an item
void updateQuantity(Item items[], int count) {
char updateName[50];
printf("Enter item name to update quantity: ");
scanf("%s", updateName);
int found = 0;
for (int i = 0; i < count; i++) {
if (strcmp(items[i].name, updateName) == 0) {
printf("Enter new quantity: ");
scanf("%d", &items[i].quantity);
printf("Quantity updated successfully!\n");
found = 1;
break;
}
}
if (!found) {
printf("Item not found!\n");
}
}
int main() {
Item items[MAX_ITEMS];
int count = 0;
int choice;
do {
printf("\nWarehouse Management System\n");
printf("1. Add Item\n");
printf("2. Display Items\n");
printf("3. Search Item\n");
printf("4. Update Quantity\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
addItem(items, &count);
break;
case 2:
displayItems(items, count);
break;
case 3:
searchItem(items, count);
break;
case 4:
updateQuantity(items, count);
break;
case 5:
printf("Exiting...\n");
break;
default:
printf("Invalid choice!\n");
}
} while (choice != 5);
return 0;
}
Explanation:
#include <stdio.h> and #include <stdlib.h>: These headers provide standard input/output and general utility functions.
#include <string.h>: This header provides string manipulation functions.
#define MAX_ITEMS 100: Defines a constant for the maximum number of items the warehouse can hold.
typedef struct { ... } Item;: Defines a structure Item to represent an item with properties: name, quantity, and price.
addItem(Item items[], int *count): Adds a new item to the items array and increments the count.
displayItems(Item items[], int count): Displays all items in the items array.
searchItem(Item items[], int count): Searches for an item by name and displays its details if found.
updateQuantity(Item items[], int count): Updates the quantity of an item by name.
main():
Declares an array items of type Item to store the warehouse items.
Declares count to keep track of the number of items.
Presents a menu-driven interface to the user.
Calls the appropriate functions based on the user's choice.
How to compile and run:
Save the code as a .c file (e.g., warehouse.c).
Compile the code using a C compiler (like GCC):
bash
gcc warehouse.c -o warehouse
Run the executable:
bash
./warehouse
Key improvements and features:
Structure for items: Using a structure makes the code more organized and readable.
Function modularity: Each task is handled by a separate function, making the code easier to maintain and modify.
Input validation: Basic input validation is included (e.g., checking if the warehouse is full).
User-friendly interface: The menu-driven interface makes the program easy to use.
This program provides a basic framework for a warehouse management system. You can extend it further by adding more features like:
Deleting items: Implement a function to remove items from the warehouse.
Saving and loading data: Implement functions to save the warehouse data to a file and load it back.
Reporting: Generate reports on inventory levels, low stock items, etc.
More sophisticated search: Allow searching by other criteria like price range or quantity.
Error handling: Implement more robust error handling to handle invalid input and other potential issues.