Saturday, 23 March 2019

CPP-EX54

Create a C++ class for inventory of books containing author, title, price, and publisher and stock as data members. Book can be sold, if stock is available, otherwise purchase will be made. Write necessary member functions for the following:
    1. To accept details from user.
    2. To sale a book. (Sale contains book details & number of copies to be sold.)
    3. To Purchase a book. (Purchase contains book details & number of copies to be purchased)       
(Use new operator to allocate memory).


#include<iostream.h>
#include<conio.h>
#include<stdlib.h>
class Inventory
{
    char author[22];
    char title[22];
    int price;
    char publicer[22];
    int stock;
public:
    void accept(void);
    void sale(void);
    void purchase(void);
};
void Inventory :: accept(void)
{
    cout<<"\nAuthor name : ";cin>>author;
    cout<<"\nTitle name : ";cin>>title;
    cout<<"\nPrice : ";cin>>price;
    cout<<"\nPublicer : ";cin>>publicer;
    cout<<"\nStock : ";cin>>stock;
}
void Inventory :: sale(void)
{
    int no_of_sale;
    cout<<"\nAuthor name : "<<author;
    cout<<"\nTitle name : "<<title;
    cout<<"\nPrice : "<<price;
    cout<<"\nPublicer : "<<publicer;
    cout<<"\nStock : "<<stock;
    cout<<"\nEnter the no of sale : ";
    cin>>no_of_sale;
    stock = stock - no_of_sale;
    cout<<"\nStock : "<<stock;
}
void Inventory :: purchase(void)
{
    int no_of_purchase;
    cout<<"\nAuthor name : "<<author;
    cout<<"\nTitle name : "<<title;
    cout<<"\nPrice : "<<price;
    cout<<"\nPublicer : "<<publicer;
    cout<<"\nStock : "<<stock;
    cout<<"\nEnter the no of purchase : ";
    cin>>no_of_purchase;
    stock = stock + no_of_purchase;
    cout<<"\nStock : "<<stock;
}
int main()
{
    Inventory I[22];

    clrscr();
    int ch,n;
     cout<<"\n enter the rang";
        cin>>n;
    do
    {
        cout<<"\n 1.Accept";
        cout<<"\n 2.sale";
        cout<<"\n 3.purches";
        cout<<"\n 4.exit";
        cout<<"\n enter ur choice";
        cin>>ch;
        switch(ch)
        {
            case 1:
                for(int i=0;i<n;i++)
                {
                    I[i].accept();
                }
                break;
            case 2:
                for(i=0;i<n;i++)
                {
                    I[i].sale();
                }
                break;
            case 3:
                for(i=0;i<n;i++)
                {
                    I[i].purchase();
                }
                break;
            case 4:
                exit(0);
        }
    }
     while(ch!=4);
     getch();
     return(0);
}