Sunday, 27 January 2013

How to create a program for a Sopping List using C++

Implement a class with carry's to perform various operation in a shopping mall like adding an item to a list of an order, deleting an item and printing total value of order.
The list includes details like code number and price of each item.

#include<iostream.h>
#include<conio.h>


const m=50;


class ITEMS

{
  int itemcode[m];
  float itemprice[m];
  int count;
  public:
  void CNT(void){ count=0; } //initializes count to 0
  void getitem(void);
  void displaysum(void);
  void remove(void);
  void displayitem(void);
};
  

void ITEMS::getitem(void)

{
  cout<<"Enter item code:";
  cin>>itemcode[count];
  cout<<"Enter Item Cost:";
  cin>>itemprice[count];
  count++;
  cout<<"Item enlisted!";
}

void ITEMS::displaysum(void)

{
  float sum=0;
  for(int i=0; i<count; i++)
  {
  sum=sum+itemprice[i];
  }
  cout<<"\nTotal value:"<<sum<<"\n";
}
void ITEMS::remove(void)
{
  int a,f=0;
  cout<<"Enter Item code:";
  cin>>a;

  for(int i=0; i<count; i++)

  {
   if(itemcode[i]==a)
   {
   itemprice[i]=0;
   cout<<"Item Removed!";
   f=1;
   }
  }
  if(f==0){
  cout<<"Item not found!";
  }
}
void ITEMS::displayitem(void)
{
  cout<<"\nCode Price\n";
  for(int i=0; i<count; i++)
  {
   if(itemprice[i]!=0){
    cout<<"\n"<<itemcode[i];
    cout<<"  "<<itemprice[i];
   }
  }
  cout<<"\n";
}


int main()

{
  ITEMS order;
  order.CNT();
  int x;

  do

   {
   clrscr();
   cout<<"\nYou can do the following;"
       <<"Enter appropriate number \n";
   cout<<"\n1 : Add an Item";
   cout<<"\n2 : Display total value";
   cout<<"\n3 : Delete an item";
   cout<<"\n4 : Display all items";
   cout<<"\n5 : Quit";
   cout<<"\n\nWhat is your option?";
   gotoxy(25,25);
   cout<<"Command:";

   cin>>x;

   gotoxy(1,10);
   switch(x)
   {
    case 1:
    order.getitem();
    getch();
    break;
    case 2:
    order.displaysum();
    getch();
    break;
    case 3:
    order.remove();
    getch();
    break;
    case 4:
    order.displayitem();
    getch();
    break;
    case 5:
    break;
    default:
    cout<<"Error in input; try again\n";
    }
  }
  while(x!=5);

return (0);


}


No comments:

Post a Comment