Problem Statement:
A book shop maintains the inventory of books that are being sold at the shop. The list
includes details such as author, title, price, publisher and stock position. Whenever a
customer wants a book, the sales person inputs the title and author and the system
searches the list and displays whether it is available or not. If it is not, an appropriate
message is displayed. If it is, then the system displays the book details and requests
for the number of copies required. If the requested copies book details and requests
for the number of copies required. If the requested copies are available, the total cost
of the requested copies is displayed; otherwise the message “Required copies not in
stock” is displayed.
Design a system using a class called books with suitable member functions and
Constructors. Use new operator in constructors to allocate memory space required.
Implement C++ program for the system.
//Omkar Nath Singh #include<iostream> #include<string.h> #include<stdlib.h> using namespace std; class book { private: char *author,*title,*publisher; float *price; int *stock; public: book() { author= new char[20]; title=new char[20]; publisher=new char[20]; price= new float; stock=new int; } void feeddata(); void editdata(); void showdata(); int search(char[],char[]); void buybook(); }; void book::feeddata() { cin.ignore(); cout<<"\nEnter Author Name: "; cin.getline(author,20); cout<<"Enter Title Name: "; cin.getline(title,20); cout<<"Enter Publisher Name: "; cin.getline(publisher,20); cout<<"Enter Price: "; cin>>*price; cout<<"Enter Stock Position: "; cin>>*stock; } void book::editdata() { cout<<"\nEnter Author Name: "; cin.getline(author,20); cout<<"Enter Title Name: "; cin.getline(title,20); cout<<"Enter Publisher Name: "; cin.getline(publisher,20); cout<<"Enter Price: "; cin>>*price; cout<<"Enter Stock Position: "; cin>>*stock; } void book::showdata() { cout<<"\nAuthor Name: "<<author; cout<<"\nTitle Name: "<<title; cout<<"\nPublisher Name: "<<publisher; cout<<"\nPrice: "<<*price; cout<<"\nStock Position: "<<*stock; } int book::search(char tbuy[20],char abuy[20] ) { if(strcmp(tbuy,title)==0 && strcmp(abuy,author)==0) return 1; else return 0; } void book::buybook() { int count; cout<<"\nEnter Number Of Books to buy: "; cin>>count; if(count<=*stock) { *stock=*stock-count; cout<<"\nBooks Bought Sucessfully"; cout<<"\nAmount: Rs. "<<(*price)*count; } else cout<<"\nRequired Copies not in Stock"; } int main() { book *B[20]; int i=0,r,t,choice; char titlebuy[20],authorbuy[20]; while(1) { cout<<"\n\n\t\tMENU" <<"\n1. Entry of New Book" <<"\n2. Buy Book" <<"\n3. Search For Book" <<"\n4. Edit Details Of Book" <<"\n5. Exit" <<"\n\nEnter your Choice: "; cin>>choice; switch(choice) { case 1: B[i] = new book; B[i]->feeddata(); i++; break; case 2: cin.ignore(); cout<<"\nEnter Title Of Book: "; cin.getline(titlebuy,20); cout<<"Enter Author Of Book: "; cin.getline(authorbuy,20); for(t=0;t<i;t++) { if(B[t]->search(titlebuy,authorbuy)) { B[t]->buybook(); break; } } if(t==1) cout<<"\nThis Book is Not in Stock"; break; case 3: cin.ignore(); cout<<"\nEnter Title Of Book: "; cin.getline(titlebuy,20); cout<<"Enter Author Of Book: "; cin.getline(authorbuy,20); for(t=0;t<i;t++) { if(B[t]->search(titlebuy,authorbuy)) { cout<<"\nBook Found Successfully"; B[t]->showdata(); break; } } if(t==i) cout<<"\nThis Book is Not in Stock"; break; case 4: cin.ignore(); cout<<"\nEnter Title Of Book: "; cin.getline(titlebuy,20); cout<<"Enter Author Of Book: "; cin.getline(authorbuy,20); for(t=0;t<i;t++) { if(B[t]->search(titlebuy,authorbuy)) { cout<<"\nBook Found Successfully"; B[t]->editdata(); break; } } if(t==i) cout<<"\nThis Book is Not in Stock"; break; case 5: exit(0); default: cout<<"\nInvalid Choice Entered"; } } return 0; }
Output:
omkar@wordpress:~$ g++ book.cpp omkar@wordpress:~$ ./a.out MENU 1. Entry of New Book 2. Buy Book 3. Search For Book 4. Edit Details Of Book 5. Exit Enter your Choice: 1 Enter Author Name: Test Author Enter Title Name: Test Title Enter Publisher Name: Test Pub 1 Enter Price: 100 Enter Stock Position: 2 MENU 1. Entry of New Book 2. Buy Book 3. Search For Book 4. Edit Details Of Book 5. Exit Enter your Choice: 2 Enter Title Of Book: Test Title Enter Author Of Book: Test Author Enter Number Of Books to buy: 1 Books Bought Sucessfully Amount: Rs. 100 MENU 1. Entry of New Book 2. Buy Book 3. Search For Book 4. Edit Details Of Book 5. Exit Enter your Choice: 5 omkar@wordpress:~$
how to do this using static variable for transaction of books.. please help sir..
LikeLike
Abhishek Sengar,
You can make changes to some the variables which you require to have the same values for all the different objects as “static”. Such possible variables are :
1. Total amount of books bought.
2. Any new other added functionality.
Add a new variable “static final_amount” in the class or globally, and update it every time a new book is bought.
Hope I helped!
Do revert back, if any further clarification is needed.
LikeLike
Without using case …plz suggest me
LikeLike
In that case, call the respective functions in order directly. It will run for all functions once for a quick representation.
LikeLike
whether some design patterns are used here (for example, abstract factory)
LikeLike
I doubt of using any design patterns here deliberately, but one can see use of Singleton design pattern atleast.
LikeLike
ok, would you advise me if you could use any additions here
for example, is there any design pattern to write to the price file for the book?
or any other that could be easily added to your program
LikeLike
Output for the program
LikeLike
g++ program.cpp
./a.out
Run these command and see the output.
LikeLike
It doesn’t run please send the out put for the program
LikeLike
updated,
LikeLike
Hi there I am so delighted I found your blog page, I really found you by accident, while I was researching on Bing for something else, Anyhow I am here now and would just like to say thank you for a fantastic post and a all round thrilling blog (I also love the theme/design), I don’t have time to look over it all at the moment but I have book-marked it and also included your RSS feeds, so when I have time I will be back to read a lot more, Please do keep up the excellent job.
LikeLike
Hello my friend! I wish to say that this article is awesome, nice written and include approximately all important infos. I would like to see more posts like this.
LikeLike
I got what you intend,saved to bookmarks, very decent website .
LikeLike
Can you plz send me the Algorithm, Flowchart, and pseudo code of this book-shop program.
LikeLike
I want to learn more about c++ i guss this web is develpo for that purpose, this code is more excited but i don’t know for class and obeject in this program please help me
LikeLike
Hello, could you explain what does it mean “void book::showdata()” ?
LikeLike
Its a method definition. Declaration of the methods are given in the class definition.
LikeLike
if you add comments it will be more helpful
LikeLike
Where is the output
LikeLike
updated.
LikeLike
While running this program there are lot of errors showing
LikeLike
Use g++ compiler for this.
LikeLike