CPP PROGRAM TO MAINTAIN A BOOK RECORD.
1. A Book shop maintains the inventory of books that are being sold at the shop. The list includes details such as title, author, publisher, price and available stock. Write a program in C++ which will have a class called books with suitable member functions for
i. Add
ii. Update
iii. Search a book
iv. Purchase a book (update the stock and display the total cost)
v. Record number of successful/unsuccessful transactions (use static data members to keep count of transactions)
Use new operator in constructors to allocate memory space required.
#include<iostream>
#include<conio.h>
#include<string>
using namespace std;
class book
{
char *title,*publisher,*author;
float *price;
static int avastock;
public:
book()
{
title=new char[10];
publisher=new char[10];
author=new char[10];
price=new float;
}
void add();
void update();
int search(char t[10],char a[10]);
void display();
void purchase();
};
int book::avastock=0;
void book::add()
{
cout<<"Enter title\n";
cin>>title;
cout<<"publisher\n";
cin>>publisher;
cout<<"author\n";
cin>>author;
cout<<"price\n";
cin>>*price;
cout<<"available stock\n";
cin>>avastock;
}
void book::update()
{
cout<<"Enter title\n";
cin>>title;
cout<<"publisher\n";
cin>>publisher;
cout<<"author\n";
cin>>author;
cout<<"price\n";
cin>>*price;
cout<<"available stock\n";
cin>>avastock;
}
int book::search(char t[10],char a[10])
{
if(strcmp(t,title)==0&&strcmp(a,author)==0)
return 1;
else
return 0;
}
void book::display()
{
cout<<"Enter title\n"<<title;
cout<<"publisher\n"<<publisher;
cout<<"author\n"<<author;
cout<<"price\n"<<price;
cout<<"available stock\n"<<avastock;
}
void book::purchase()
{
int total,n;
cout<<"Enter no ofcopies\n";
cin>>n;
if(n<=avastock)
{
total=n**price;
}
avastock=avastock-n;
cout<<total<<"\n";
}
int main()
{
book *b[20];
int n=0,op=0,p=0;
char t[10],a[10];int i;
// clrscr();
while(op!=6)
{
cout<<"Enter 1 for add books\n";
cout<<"Enter 2 for update books\n";
cout<<"Enter 3 for search books\n";
cout<<"Enter 4 for purchase books\n";
cin>>op;
switch(op)
{
case 1:b[n]->add();
n++;
break;
case 2:cout<<"Enter the title of book \n";
cout<<"Enter the author of book\n";
cin>>t>>a;
for(i=0;i<n;i++)
{
if(b[i]->search(t,a))
{cout<<"Book found";
b[i]->update();
break;
}
}
case 3:cout<<"Enter the title of book \n";
cout<<"Enter the author of book\n";
cin>>t>>a;
for(i=0;i<n;i++)
{
if(b[i]->search(t,a))
cout<<"Book found";
b[i]->display();
break;
}
case 4:cout<<"Enter the title of book \n";
cout<<"Enter the author of book\n";
cin>>t>>a;
for(i=0;i<n;i++)
{
if(b[i]->search(t,a))
{
b[i]->purchase();
p=1;
break;
}
}
if(p==0)
cout<<"Not in stock";
break;
}
}
getch();
return 0;
}
#include<string>
using namespace std;
class book
{
char *title,*publisher,*author;
float *price;
static int avastock;
public:
book()
{
title=new char[10];
publisher=new char[10];
author=new char[10];
price=new float;
}
void add();
void update();
int search(char t[10],char a[10]);
void display();
void purchase();
};
int book::avastock=0;
void book::add()
{
cout<<"Enter title\n";
cin>>title;
cout<<"publisher\n";
cin>>publisher;
cout<<"author\n";
cin>>author;
cout<<"price\n";
cin>>*price;
cout<<"available stock\n";
cin>>avastock;
}
void book::update()
{
cout<<"Enter title\n";
cin>>title;
cout<<"publisher\n";
cin>>publisher;
cout<<"author\n";
cin>>author;
cout<<"price\n";
cin>>*price;
cout<<"available stock\n";
cin>>avastock;
}
int book::search(char t[10],char a[10])
{
if(strcmp(t,title)==0&&strcmp(a,author)==0)
return 1;
else
return 0;
}
void book::display()
{
cout<<"Enter title\n"<<title;
cout<<"publisher\n"<<publisher;
cout<<"author\n"<<author;
cout<<"price\n"<<price;
cout<<"available stock\n"<<avastock;
}
void book::purchase()
{
int total,n;
cout<<"Enter no ofcopies\n";
cin>>n;
if(n<=avastock)
{
total=n**price;
}
avastock=avastock-n;
cout<<total<<"\n";
}
int main()
{
book *b[20];
int n=0,op=0,p=0;
char t[10],a[10];int i;
// clrscr();
while(op!=6)
{
cout<<"Enter 1 for add books\n";
cout<<"Enter 2 for update books\n";
cout<<"Enter 3 for search books\n";
cout<<"Enter 4 for purchase books\n";
cin>>op;
switch(op)
{
case 1:b[n]->add();
n++;
break;
case 2:cout<<"Enter the title of book \n";
cout<<"Enter the author of book\n";
cin>>t>>a;
for(i=0;i<n;i++)
{
if(b[i]->search(t,a))
{cout<<"Book found";
b[i]->update();
break;
}
}
case 3:cout<<"Enter the title of book \n";
cout<<"Enter the author of book\n";
cin>>t>>a;
for(i=0;i<n;i++)
{
if(b[i]->search(t,a))
cout<<"Book found";
b[i]->display();
break;
}
case 4:cout<<"Enter the title of book \n";
cout<<"Enter the author of book\n";
cin>>t>>a;
for(i=0;i<n;i++)
{
if(b[i]->search(t,a))
{
b[i]->purchase();
p=1;
break;
}
}
if(p==0)
cout<<"Not in stock";
break;
}
}
getch();
return 0;
}
No comments:
Post a Comment
If you have any problems related to solutions or any concept please let me know.