Product Management System using Array in C++

 Product Management System using C++

Step 1: Create a product class
#include<iostream>
#include<string>
using namespace std;
class Product
{
int id;
string name;
int price;
public:
Product()
{
id=0;
name="";
price=0;
}
Product(int i,string n,int p)
{
id=i;
name=n;
price=p;
}
void setID(int i)
{
id=i;
}
void setName(string n)
{
name=n;
}
void setPrice(int p)
{
price=p;
}
int getID()
{
return id;
}
string getName()
{
return name;
}
int getPrice()
{
return price;
}
void display()
{
cout<<"\n Product ID: "<<id;
cout<<"\n Product Name: "<<name;
cout<<"\n Product Price: "<<price;
}

bool addProduct(int i,string n,int p)
{
id=i;
name=n;
price=p;

return true;
}

};

Step 2: use this product class in main function

#include<iostream>
#include<string>
#include"Product.cpp"
using namespace std;
int main()
{
cout<<"******************************** Product Management System using C++ ****************************************";
int size=0;
int choice;
int id;
string name;
int price;
string pname="";
bool flag=false;
int pid=0;
bool flagEdit=false;
bool flagDelete=false;

Product p[10];
char c='n';
do
{
cout<<"\n Please enter your choice :";
cout<<"\n 1.Add Product :";
cout<<"\n 2.List of Product :";
cout<<"\n 3.Search Product  by name:";
cout<<"\n 4.Edit Product :";
cout<<"\n 5.Delete Product : \n";
cout<<" 6.Exit \n";
cin>>choice;
switch(choice)
{
    case 1:
    //add product
    cout<<"\n Please enter your product ID:";
    cin>>id;
    cout<<"\n Please enter your product Name:";
     cin>>name;
    cout<<"\n Please enter your product Price:";
     cin>>price;
    p[size].addProduct(id,name,price);
    size++;
break;
case 2:
//product list
for(int i=0;i<size;i++)
{
p[i].display();
}
  cout<<"\n Total Producnts: "<<size;//count products
break;
case 3: //search product by name
cout<<"\n please enter the product name ";
cin>>pname;
for(int k=0;k<size;k++)
{
if(p[k].getName()==pname)
           {
// cout<<"\n Product Found \n";
p[k].display();
flag=true;
           }
      }
if(flag)
{
break;
}
else
{
cout<<"\n Product not Found \n";
break;
}
break;
case 4:
//edit product  
cout<<"\n Edit product  \n";

cout<<"\n please enter the product id ";
cin>>pid;
for(int k1=0;k1<size;k1++)
{
if(p[k1].getID()==pid)
        {
// cout<<"\n Product Found \n";
//p[k1].display();
id= p[k1].getID();
cout<<"\n Please enter your product Name:";
     cin>>name;
    cout<<"\n Please enter your product Price:";
     cin>>price;
    p[k1].addProduct(id,name,price);
flagEdit=true;
        }
    }
   if(flagEdit=true)
   {
    cout<<"\n Product is edited successfully! \n";
   }
   else
   {
    cout<<"\n Product id is not exist \n";
   }
break;
case 5:
//Deelte  product  
cout<<"\n Delete product  Interface \n";

cout<<"\n please enter the product id ";
cin>>pid;

for(int k12=0;k12<size;k12++)
{
if(p[k12].getID()==pid)
        {
// cout<<"\n Product Found \n";
//p[k1].display();
id= p[k12].getID();
    p[id].setID(0);
    
      p[id].setName("");
        p[id].setPrice(0);
flagDelete=true;
  size--;
        }
    }
   if(flagDelete=true)
   {
    cout<<"\n Product has been removed successfully! \n";
   }
   else
   {
    cout<<"\n Product id is not exist \n";
   }
    break;
case 6: //exit
exit( 0 );//exit the program
   
}


cout<<"\n Do you want to enter more work Y/N : ";
cin>>c;
}while(c=='y'||c=='Y');
cout<<"\n ++++++++++++++++++++++++++ By Hafiz Muhammad Umar Hayat +++++++++++++++++++++++\n ";
return 0;
}
Result :








Comments