0% found this document useful (0 votes)
37 views

Shopping CPP

The document defines a C++ class for managing product inventory. The class contains methods for adding, updating, deleting and viewing products. It also contains methods for updating product quantities during sale and updating the data file. The main function displays a menu and calls the appropriate class methods based on the user's selection to manage the product inventory stored in a data file.

Uploaded by

Piyush Sidana
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views

Shopping CPP

The document defines a C++ class for managing product inventory. The class contains methods for adding, updating, deleting and viewing products. It also contains methods for updating product quantities during sale and updating the data file. The main function displays a menu and calls the appropriate class methods based on the user's selection to manage the product inventory stored in a data file.

Uploaded by

Piyush Sidana
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 13

#include<iostream>

#include<stdio.h>

#include<fstream>

#include<string.h>

using namespace std;

/* 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;

/* To return product name of product */

char* product::get_name()

return name;

}
/* To return quantity of product */

int product::get_quantity()

return quantity;

/* To update quantity of product */

void product::update_quantity(int n)

quantity=quantity-n;

/* To add a new product */

void product::add_product()

system("cls");

char x[20];

cout<<"\n\t\t=======================================================================
========";

cout<<"\n\t\t|| || ADD PRODUCT || ||";

cout<<"\n\t\t=======================================================================
========";

cout<<"\n\n\t\tEnter Product Name :- ";

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);

cout<<"\n\t\tEnter Product Price :- ";

cin>>price;

cout<<"\n\t\tEnter Product Quantity :- ";

cin>>quantity;

cout<<"\n\t\tIs it Perishable (Yes / No) :- ";

fflush(stdin);

gets(perishable);

cout<<"\n\n\t\t\t\t !!!!! Product added in inventory successfully !!!!!";

ofstream file("product.dat",ios::app);

file.write((char*)&g,sizeof(g));

file.close();

else
{

cout<<"\n\n\t\t\t\t !!!!! Product is already present in inventory !!!!!";

/* 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|| || UPDATE PRODUCT || ||";

cout<<"\n\t\t=======================================================================
========";

cout<<"\n\n\t\tEnter the product name to update :- ";

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)

cout<<"\n\n\t\t\t\t !!!!! Product is not present in inventory !!!!!";

else

system("cls");

cout<<"\n\t\t=======================================================================
========";

cout<<"\n\t\t|| || UPDATE PRODUCT || ||";

cout<<"\n\t\t=======================================================================
========";

cout<<"\n\t\t|| ||";

cout<<"\n\t\t|| !!!!!!!!!! 1. TO UPDATE PRICE !!!!!!!!!! ||";

cout<<"\n\t\t|| ||";

cout<<"\n\t\t|| !!!!!!!!!! 2. TO UPDATE QUANTITY !!!!!!!!!! ||";

cout<<"\n\t\t|| ||";

cout<<"\n\t\t=======================================================================
========";

cout<<"\n\n\t\tEnter your choice :- ";

cin>>choice;

if(choice==1)

{
cout<<"\n\t\tEnter new price of the product :- ";

cin>>price;

else if(choice==2)

cout<<"\n\t\tEnter new quantity of the product :- ";

cin>>quantity;

cout<<"\n\n\t\t\t\t !!!!! Product updated in inventory successfully !!!!!";

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|| || DELETE PRODUCT || ||";

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)

cout<<"\n\n\t\t\t\t !!!!! Product is not present in inventory !!!!!";

else

cout<<"\n\n\t\t\t\t !!!!! Product deleted from inventory successfully !!!!!";

/* 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|| || SALE PRODUCT || ||";

cout<<"\n\t\t=======================================================================
========";

cout<<"\n\n\t\tEnter the product name to sale :- ";

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)

cout<<"\n\n\t\t\t\t !!!!! Product is not present in inventory !!!!!";


}

else

cout<<"\n\t\tEnter the quantity of the product :- ";

cin>>n;

if(n>get_quantity())

cout<<"\n\n\t\t\t\t !!!!! Sorry, sufficient quantity is not present in inventory !!!!!";

else

cout<<"\n\n\t\t\t\t !!!!! Product sold from inventory successfully !!!!!";

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\t\t|| || INVENTORY || ||";


cout<<"\n\t\t=======================================================================
========";

cout<<"\n\n\t\t\t\t\t Product Name :- "<<name;

cout<<"\n\n\t\t\t\t\t Product Price :- "<<price;

cout<<"\n\n\t\t\t\t\t Product Quantity :- "<<quantity;

cout<<"\n\n\t\t\t\t\t Perishable :- "<<perishable;

cout<<"\n\n\t\t=====================================================================
==========";

/* To display main menu */

voidmain_menu()

system("cls");

cout<<"\n\t\t=======================================================================
=========";

cout<<"\n\t\t|| ########## --------------------------- ########## ||";

cout<<"\n\t\t|| ########## SHOPPING MANAGEMENT SYSTEM ########## ||";

cout<<"\n\t\t|| ########## --------------------------- ########## ||";

cout<<"\n\t\t=======================================================================
=========";

cout<<"\n\t\t|| ||";

cout<<"\n\t\t|| !!!!!!!!!! 1. TO ADD A NEW PRODUCT !!!!!!!!!! ||";

cout<<"\n\t\t|| ||";

cout<<"\n\t\t|| !!!!!!!!!! 2. TO UPDATE A PRODUCT !!!!!!!!!! ||";

cout<<"\n\t\t|| ||";

cout<<"\n\t\t|| !!!!!!!!!! 3. TO DELETE A PRODUCT !!!!!!!!!! ||";

cout<<"\n\t\t|| ||";

cout<<"\n\t\t|| !!!!!!!

!!! 4. TO SALE A PRODUCT !!!!!!!!!! ||";


cout<<"\n\t\t|| ||";

cout<<"\n\t\t|| !!!!!!!!!! 5. TO VIEW INVENTORY !!!!!!!!!! ||";

cout<<"\n\t\t|| ||";

cout<<"\n\t\t|| !!!!!!!!!! 6. TO EXIT !!!!!!!!!! ||";

cout<<"\n\t\t|| ||";

cout<<"\n\t\t=======================================================================
=========";

/* Main function */

int main()

charch;

int choice;

do

main_menu();

cout<<"\n\n\t\t\t\t\tEnter your choice :- ";

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");

cout<<"\n\n\t\t\t\t !!!!! No inventory Found !!!!!";

else if(choice==6)

exit(0);

cout<<"\n\nWant to go to main menu (y/n) :- ";

cin>>ch;
}while(ch=='y');

return 0;

You might also like