Hello, world! 🌍
As a student diving into the world of programming with C, I recently challenged myself with an exciting project: creating an Inventory Management System! 🛠️ This program is a practical way to manage products, categories, quantities, and prices, all while learning key programming concepts.
Let me walk you through what I built, and maybe it will inspire you to try something similar! 🤓
Features of My Inventory Management System 🗂️
1. Add Products 🛒
You can add items to your inventory by specifying their category, name, quantity, and price. These details are stored in a file named Inventory.txt
. Here's how the data is saved:
//Category: INFORMATICS
Product: Mouse Quantity: 20 Price: 15.99€
2. View Inventory 📜
Want to see what you have in stock? The program can display the entire inventory in a neat format. No need to manually search through files—just run the program and select the View Inventory option!
3. Modify Quantities ✏️
If stock levels change, you can update the quantity of any product. This feature reads the file, modifies the necessary line, and saves the updates seamlessly.
4. Split by Category 🗃️
Organize your inventory further by splitting items into separate files based on their categories. For instance, all INFORMATICS
products go into Informatics.txt
, and ELECTRONICS
items go into Electronics.txt
.
The Code 🖥️
Here's a snippet of the core functionality:
// Function to add an item to the inventory
void addItem(char category[], char name[], int quantity, float price) {
FILE *pFile;
pFile = fopen("Inventory.txt", "a");
fprintf(pFile, "//Category: %s\nProduct: %s\tQuantity: %d\tPrice: %.2f€\n", category, name, quantity, price);
fclose(pFile);
}
I implemented several functions to handle specific tasks, like adding items, viewing the inventory, and even splitting it by category. These functions helped me learn how to work with file I/O, string manipulation, and conditional logic in C.
Lessons Learned 📚
- File Handling: Managing inventory required reading, writing, and modifying files. This was a fantastic introduction to file handling in C!
- String Operations: I practiced working with strings to format data and convert categories to uppercase.
- Problem-Solving: Debugging and refining my code taught me to think like a programmer.
Try It Out! 💻
Here’s the full code if you want to experiment:
#include <stdio.h>
#include <string.h>
#include <ctype.h>
// Function to add an item to the inventory
void addItem(char category[], char name[], int quantity, float price) {
FILE *pFile;
// Open file in append mode to add data
pFile = fopen("Inventory.txt", "a");
// Write the item details in the file
fprintf(pFile, "//Category: %s\nProduct: %s\tQuantity: %d\tPrice: %.2f€\n", category, name, quantity, price);
fclose(pFile); // Close the file
}
// Function to display the inventory
void viewInventory() {
char line[200];
FILE *pFile;
// Open the inventory file in read mode
pFile = fopen("Inventory.txt", "r");
// Read and print each line until EOF
while (fgets(line, sizeof(line), pFile)) {
printf("%s", line);
}
fclose(pFile); // Close the file
}
// Function to modify the quantity of an item
void modifyQuantity(char name[], int newQuantity) {
char line[200], updatedLine[200], itemName[30];
int itemQuantity;
float itemPrice;
FILE *pFile, *pTmp;
// Open the original file in read mode and a temporary file in write mode
pFile = fopen("Inventory.txt", "r");
pTmp = fopen("Temporary.txt", "w");
// Read each line and process
while (fgets(line, sizeof(line), pFile)) {
sscanf(line, "Product: %s\tQuantity: %d\tPrice:%f€", itemName, &itemQuantity, &itemPrice);
// If the product matches, update its quantity
if (strcmp(name, itemName) == 0) {
sprintf(updatedLine, "Product: %s\tQuantity: %d\tPrice: %.2f€\n", itemName, newQuantity, itemPrice);
fputs(updatedLine, pTmp);
} else {
// Otherwise, copy the original line
fputs(line, pTmp);
}
}
fclose(pFile);
fclose(pTmp);
// Replace the original file with the updated file
remove("Inventory.txt");
rename("Temporary.txt", "Inventory.txt");
}
// Function to split inventory items into separate files by category
void splitByCategory() {
char line[200], categoryName[30], name[30];
int quantity;
float price;
FILE *pFile, *pInf, *pElec;
// Open the inventory file and category files
pFile = fopen("Inventory.txt", "r");
pInf = fopen("Informatics.txt", "w");
pElec = fopen("Electronics.txt", "w");
// Process each line
while (fgets(line, sizeof(line), pFile)) {
sscanf(line, "//Category: %s\nProduct: %s\tQuantity: %d\tPrice:%f€", categoryName, name, &quantity, &price);
// Write items into the respective category file
if (strcmp(categoryName, "INFORMATICS") == 0) {
fputs(line, pInf);
} else if (strcmp(categoryName, "ELECTRONICS") == 0) {
fputs(line, pElec);
}
}
fclose(pFile);
fclose(pInf);
fclose(pElec);
}
int main() {
int choice;
char name[30], category[30];
int quantity, newQuantity;
float price;
printf("-- Welcome to the Inventory Management System --");
do {
// Display menu options
printf("\nWhat would you like to do?\n");
printf("1. Add Product\t 2. View Inventory\t 3. Modify Quantity\t 4. Split by Category\t 5. Exit\n");
scanf("%d", &choice);
switch (choice) {
case 1:
// Add a product
while (1) {
printf("Enter the product category (Informatics/Electronics):\n");
scanf("%s", category);
// Convert category to uppercase
for (int i = 0; i < strlen(category); i++) {
category[i] = toupper(category[i]);
}
// Validate category
if (strcmp(category, "INFORMATICS") == 0 || strcmp(category, "ELECTRONICS") == 0) {
break;
} else {
printf("Unsupported category. Please try again.\n");
}
}
printf("Enter the product name:\n");
scanf("%s", name);
printf("Enter the quantity of %s in stock:\n", name);
scanf("%d", &quantity);
printf("Enter the price per unit:\n");
scanf("%f", &price);
addItem(category, name, quantity, price);
break;
case 2:
// View inventory
printf("Here is your inventory:\n");
viewInventory();
break;
case 3:
// Modify product quantity
printf("Enter the product name:\n");
scanf("%s", name);
printf("Enter the new quantity:\n");
scanf("%d", &newQuantity);
modifyQuantity(name, newQuantity);
break;
case 4:
// Split inventory by category
splitByCategory();
break;
case 5:
// Exit
printf("Exiting the program.\n");
break;
default:
printf("Invalid choice. Please try again.\n");
}
} while (choice != 5);
return 0;
}
Feel free to share your feedback or suggest improvements! Let’s learn and grow together. 🚀
Have you built something similar or have ideas to enhance this project? Drop your thoughts in the comments! 💬
Happy coding! 👨💻👩💻