Thursday, 19 July 2012

Arrays within a Class in C++

#include<iostream>
#include<string>


/*Let us consider a shopping list of different items for which we place an order with a dealer.This list includes
details such as the code and the price of each item. We would like to perform operations such as adding an item to the list, deleting an item from a list and printing the total value of the order.*/


using namespace std;

const int  val=50;

class ITEM
{
private:
    int item_code[val];
    int item_price[val];
    int count;
public:
    void initiliaze();
    void get_item();
    void display_item();
    void display_sum();
    void remove();
};
void ITEM::initiliaze()
{
    count=0;
}

void ITEM::get_item()
{
    cout<<"Enter the Item code == "<<endl;
    cin>>item_code[count];

    cout<<"Enter the Item cost == "<<endl;
    cin>>item_price[count];
    count++;
}

void ITEM::display_sum()
{
    int sum=0;
    for(int i=0; i<count;i++)
    {
        sum=sum + item_price[i];
    }
    cout<<"The Total Value Of The Cost Is == "<<sum<<endl;
}

void ITEM::display_item()
{
    cout<<"\nCode  Price\n";
    for(int k=0;k<count;k++)
    {
        cout<<"\n"<<item_code[k];
        cout<<"   "<<item_price[k];
    }
}
void ITEM::remove()
{
    int del;
    cout<<"Enter the code you want to remove == ";
    cin>>del;
    for(int search=0; search<count; search++)
    {
        if(del == item_code[search])
        {
            item_price[search]=0;
            item_code[search]=0;
        }
    }
}


int main()
{
    ITEM order;

    order.initiliaze();
    int x;
    do
    {
        cout<<"\n\nYou have the following opton";
        cout<<"\nEnter the Appropriate number";

        cout<<"\n\nPress 1 for ADD AN ITEMS";
        cout<<"\n\nPress 2 for DISPLAY TOTAL VALUE";
        cout<<"\n\nPress 3 for DELETE AN ITEM";
        cout<<"\n\nPress 4 for DISPLAY ALL ITEMS";
        cout<<"\n\nPress 5 for QUIT";

        cout<<"\nEnter The Desired Number == \n";
        cin>>x;
        switch(x)
        {
        case 1:
            {
                order.get_item();
                break;
            }
        case 2:
            {
                order.display_sum();
                break;
            }
        case 3:
            {
                order.remove();
                break;
            }
        case 4:
            {
                order.display_item();
                break;
            }
        case 5:
            break;
        default: cout<<"Incorrect option Please Press the right number == ";
        }

    }while(x!=5);

    getchar();
    return 0;
}

No comments:

Post a Comment

Post a Comment