Book Shop – using dynamic memory allocation (C++)
April 23, 2016
Categorised in: C++ Codes, OOMP Codes
/* A book shop maintains the inventory of books that are being sold at shop. The list includes details such as author,title,price,publisher and stock position.Whwnever customer wants a book the sale person inputs the title and author and the system searches the list and display wheather it is available or not.If it is not appropretae message is displayed.If it is,then system displays book details and request for no. of copies required.If requested copies are available,the total cost of requested copies is displayed, otherwise "requested copies not in stock ." is displayed.Design a system using class called book with suitable member function and contructor. Use new operator in construtor to allocate memory. Implement c++ program for system*/ #include<iostream> #include<string.h> #include<iomanip> using namespace std; class book { public: float price; string title,author,publisher; int stock; void accept(); void display(); book(); }; book :: book() { string *title=new string; title='\0'; string *author=new string; author='\0'; string *publisher=new string; publisher='\0'; price=0; stock=0; } void book :: accept() { cout<<"\n"; cout<<"Enter title : "; getline(cin,title); getline(cin,title); cout<<"Enter author : "; getline(cin,author); cout<<"Enter publisher : "; getline(cin,publisher); cout<<"Price : "; cin>>price; cout<<"Stock : "; cin>>stock; cout<<"\n"; } void book :: display() { cout<<"Book Name : "<<title<<endl; cout<<"Author Name : "<<author<<endl; cout<<"Publisher Name : "<<publisher<<endl; cout<<"Price : "<<price<<endl; } int main() { book b[20]; int ch,n=0,copy,i,no; char ch1; string tit,auth; do { cout<<"\n\t1.Enter Book Information.\n\t2.Shop for a book and display total cost.\nSelect choice : "; cin>>ch; switch(ch) { case 1: cout<<"\nHow many books you want to enter : "; cin>>n; for(i=0;i<n;i++) { b[i].accept(); } break; case 2: cout<<"\nEnter name of book u want to search : "; getline(cin,tit); getline(cin,tit); cout<<"\nEnter name of author : "; getline(cin,auth); i=0; while(i<n && b[i].title!=tit && b[i].author!=auth) { i++; } if(i>n) { cout<<"\nBook is not available."; break; } else { b[i].display(); cout<<"\nHow many copies u want?"; cin>>copy; if(b[i].stock<copy) { cout<<"\nThis much copies are not available."; break; } else { b[i].stock=b[i].stock-copy; cout<<"\n\tTotal amount of "<<tit<<" is "<<(copy*b[i].price)<<" Rs."; } } break; }cout<<"\nDo u want to continue?(y/n)"; cin>>ch1; }while(ch1=='y' or ch1=='Y'); return 0; }
Pratik Kataria is currently learning Springboot and Hibernate.
Technologies known and worked on: C/C++, Java, Python, JavaScript, HTML, CSS, WordPress, Angular, Ionic, MongoDB, SQL and Android.
Softwares known and worked on: Adobe Photoshop, Adobe Illustrator and Adobe After Effects.