0
Here's a simple C Program to develop an Inventory Management System to display the inventory of items in a store. The inventory maintains details such as name, price, quantity & cost. And displays the values in a structured way.
// Inventory Management System.
// V 1.0
// MCA Ist Sem, 2015, University of Kashmir.
#include<stdio.h>
#include<conio.h>
struct item
{
int itemno;
char name[30];
int quantity;
int price;
int tcost; //Total Cost
};
void add();
void update();
void sell();
void display();
void main()
{
int num;
while (1)
{
clrscr();
printf("\t\t\t*************************************\n");
printf(" \t\t\t| Inventory Management System |\n");
printf(" \t\t\t| V 1.0 |\n");
printf(" \t\t\t| PGDCA, University of Kashmir |\n");
printf("\t\t\t*************************************\n");
printf("\t\t\t\t1 : Add New Item\n");
printf("\t\t\t\t2 : Sell An Item \n");
printf("\t\t\t\t3 : Update Existing Item \n");
printf("\t\t\t\t4 : Show Inventory \n");
printf("\t\t\t\t5 : Exit \n");
printf("\t\t\t\t Enter your choice");
printf("\n\t\t\t*************************************\n");
scanf("%d",&num);
switch (num)
{
case 1:
add();
break;
case 2:
sell();
break;
case 3:
update();
break;
case 4:
display();
break;
case 5:
printf("Closing.....\n");
exit(1);
break;
default:
printf("Wrong Choice Entered, Press A Key To Continue... \n");
getch();
main();
}
}
}
void add()
{
struct item it;
FILE *ptr;
ptr=fopen("inventory.docx","a");
flushall();
printf("Enter Item Name\n");
scanf("%s",it.name);
printf("Enter Item Code\n");
scanf("%d",&it.itemno);
printf("Enter Quantity\n");
scanf("%d",&it.quantity);
printf("Enter Price\n");
scanf("%d",&it.price);
it.tcost=it.quantity*it.price; // Calculating Total Cost
fprintf(ptr,"\n%s\t%d\t%d\t%d\t%d",it.name,it.itemno,it.quantity,it.price,it.tcost); // Adding/Appending new record to the file.
fclose(ptr);
printf("ITEM ADDED SUCESSFULLY\n");
getch();
}
void display()
{
struct item it;
FILE * ptr;
ptr=fopen("inventory.docx","r");
if (ptr==NULL)
{
printf("Nothing to show, Inventory is empty!\n");
}
else
{
clrscr();
printf(" ***** INVENTORY *****\n");
printf("---------------------------------------------------------\n");
printf("|CODE| NAME | QUANTITY | PRICE | COST |\n");
printf("---------------------------------------------------------\n");
while(!feof(ptr))
{
fscanf(ptr,"%s%d%d%d%d",&it.name,&it.itemno,&it.quantity,&it.price,&it.tcost); //Reading the records in the file.
printf(" %-5d %-13s %-6d %-5d %d\n",it.itemno,it.name,it.quantity,it.price,it.tcost); //Printing the records
printf("---------------------------------------------------------\n");
}
fclose(ptr);
}
getch();

}
void sell()
{
struct item it[30];
FILE * ptr;
int i=0,j,k,qty,itno,flag=0;
ptr=fopen("inventory.docx","r");
if (ptr==NULL)
printf("No Items Available to sell, Inventory is empty!\n");
else
{
printf("Enter Item Code\n");
scanf("%d",&itno);
while(!feof(ptr))
{
fscanf(ptr,"%s%d%d%d%d",it[i].name,&it[i].itemno,&it[i].quantity,&it[i].price,&it[i].tcost);
i++;
}
j=i;
fclose(ptr);
ptr=fopen("inventory.docx","w");
for(k=0;k<j;k++)
{
if(itno==it[k].itemno)
{
printf("Enter Quantity\n");
scanf("%d",&qty);
if (qty>it[k].quantity)
printf("Insufficient Quantity\n");
else
{ it[k].quantity=it[k].quantity-qty; // Updating the quantity
it[k].tcost=it[k].quantity*it[k].price;
flag=1;
}
}
fprintf(ptr,"\n%s\t%d\t%d\t%d\t%d",it[k].name,it[k].itemno,it[k].quantity,it[k].price,it[k].tcost);
}
fclose(ptr);
if(flag!=1)
{
printf("ITEM IS NOT PRESENT\n");
}
else
{
printf("ITEMS SOLD\n");
}
}
getch();
}
void update()
{
struct item it[30];
FILE * ptr;
int i=0,j,k,qty,itno,prc,flag=0;
ptr=fopen("inventory.docx","r");
if (ptr==NULL)
printf("No Items Available to sell, Inventory is empty!\n");
else
{
printf("Enter Item Code\n");
scanf("%d",&itno);
while(!feof(ptr))
{
fscanf(ptr,"%s%d%d%d%d",&it[i].name,&it[i].itemno,&it[i].quantity,&it[i].price,&it[i].tcost);
i++;
}
j=i;
fclose(ptr);
ptr=fopen("inventory.docx","w");
for(k=0;k<j;k++)
{
if(itno==it[k].itemno)
{
printf("Enter New Quantity For %s\n", it[k].name);
scanf("%d",&qty);
printf("Enter New Price\n");
scanf("%d",&prc);
it[k].quantity=it[k].quantity+qty;
it[k].price=prc;
it[k].tcost=it[k].quantity*it[k].price;
flag=1;
}
fprintf(ptr,"\n%s\t%d\t%d\t%d\t%d",it[k].name,it[k].itemno,it[k].quantity,it[k].price,it[k].tcost);
}
fclose(ptr);
if(flag!=1)
{
printf("ITEM IS NOT PRESENT\n");
}
else
{
printf("DETAILS UPDATED\n");
}
}
getch();
}

 

Post a Comment

 
Top