Shopping CPP
Shopping CPP
#include<stdio.h>
#include<fstream>
#include<string.h>
/* Class declaration */
class product
intprice,quantity;
char name[20],perishable[3];
public:
char* get_name();
intget_quantity();
voidadd_product();
voidupdate_product();
voidupdate_quantity(int);
voiddelete_product();
voidsale_product();
voidview_inventory();
}g;
char* product::get_name()
return name;
}
/* To return quantity of product */
int product::get_quantity()
return quantity;
void product::update_quantity(int n)
quantity=quantity-n;
void product::add_product()
system("cls");
char x[20];
cout<<"\n\t\t=======================================================================
========";
cout<<"\n\t\t=======================================================================
========";
fflush(stdin);
gets(x);
int flag=0;
ifstream file("product.dat");
while(file.read((char*)&g,sizeof(g)))
if(strcmpi(get_name(),x)==0)
flag=1;
break;
file.close();
if(flag==0)
strcpy(name,x);
cin>>price;
cin>>quantity;
fflush(stdin);
gets(perishable);
ofstream file("product.dat",ios::app);
file.write((char*)&g,sizeof(g));
file.close();
else
{
/* To update a product */
void product::update_product()
system("cls");
char x[20];
int flag=0,pos,choice;
cout<<"\n\t\t=======================================================================
========";
cout<<"\n\t\t=======================================================================
========";
fflush(stdin);
gets(x);
fstream file("product.dat");
while(!file.eof())
pos=file.tellg();
file.read((char*)&g,sizeof(g));
if(strcmpi(get_name(),x)==0)
{
flag=1;
break;
if(flag==0)
else
system("cls");
cout<<"\n\t\t=======================================================================
========";
cout<<"\n\t\t=======================================================================
========";
cout<<"\n\t\t|| ||";
cout<<"\n\t\t|| ||";
cout<<"\n\t\t|| ||";
cout<<"\n\t\t=======================================================================
========";
cin>>choice;
if(choice==1)
{
cout<<"\n\t\tEnter new price of the product :- ";
cin>>price;
else if(choice==2)
cin>>quantity;
file.seekg(pos);
file.write((char*)&g,sizeof(g));
file.close();
/* To delete a product */
void product::delete_product()
system("cls");
int flag=0;
char x[20];
cout<<"\n\t\t=======================================================================
========";
cout<<"\n\t\t=======================================================================
========";
cout<<"\n\n\t\tEnter the product name to delete :- ";
fflush(stdin);
gets(x);
ifstream file1("product.dat");
ofstream file2("temp.dat",ios::app);
while(file1.read((char*)&g,sizeof(g)))
if(strcmpi(get_name(),x)==0)
flag=1;
else
file2.write((char*)&g,sizeof(g));
file1.close();
file2.close();
remove("product.dat");
rename("temp.dat","product.dat");
if(flag==0)
else
/* To sale a product */
void product::sale_product()
{
system("cls");
char x[20];
int flag=0,pos,choice,n;
cout<<"\n\t\t=======================================================================
========";
cout<<"\n\t\t=======================================================================
========";
fflush(stdin);
gets(x);
fstream file("product.dat");
while(!file.eof())
pos=file.tellg();
file.read((char*)&g,sizeof(g));
if(strcmpi(get_name(),x)==0)
flag=1;
break;
if(flag==0)
else
cin>>n;
if(n>get_quantity())
else
update_quantity(n);
file.seekg(pos);
file.write((char*)&g,sizeof(g));
file.close();
/* To view inventory */
void product::view_inventory()
system("cls");
cout<<"\n\t\t=======================================================================
========";
cout<<"\n\n\t\t=====================================================================
==========";
voidmain_menu()
system("cls");
cout<<"\n\t\t=======================================================================
=========";
cout<<"\n\t\t=======================================================================
=========";
cout<<"\n\t\t|| ||";
cout<<"\n\t\t|| ||";
cout<<"\n\t\t|| ||";
cout<<"\n\t\t|| ||";
cout<<"\n\t\t|| !!!!!!!
cout<<"\n\t\t|| ||";
cout<<"\n\t\t|| ||";
cout<<"\n\t\t=======================================================================
=========";
/* Main function */
int main()
charch;
int choice;
do
main_menu();
cin>>choice;
if(choice==1)
p.add_product();
else if(choice==2)
p.update_product();
else if(choice==3)
p.delete_product();
else if(choice==4)
p.sale_product();
else if(choice==5)
int flag=0;
ifstream file("product.dat");
while(file.read((char*)&g,sizeof(g)))
flag=1;
p.view_inventory();
cout<<endl<<endl;
system("pause");
file.close();
if(flag==0)
system("cls");
else if(choice==6)
exit(0);
cin>>ch;
}while(ch=='y');
return 0;