Answer to Question #185847 in C++ for Abdul Basit

Question #185847

Write a program to manage the book store in which there is a class BOOK with five data members BookID, Title, Author, price and quantity. It also contains the following member functions:

• Input () function is used to input the values of data members.

• Display () function is used to display the values.

• Purchase () function is used to add to numbers of books in quantity.

• Sold () function is used to minus from the number of books from the quantity of book.

Create an object of BOOK. Input the values in data members of object and then display the data of object. In main () function, also use switch statement to prompt the user for choice of operations either to purchase or sell a book. Sold () function must also check the quantity available to sell.


1
Expert's answer
2021-04-26T10:32:18-0400
#include <iostream>
#include <string>
#include <iomanip>
using namespace std;


class BOOK{
private:
	//members BookID, Title, Author, price and quantity.
	int BookID;
	string Title;
	string Author;
	double Price;
	int Quantity;
public:


	//Input () function is used to input the values of data members.
	void Input(){
		cout<<"Enter the book ID: ";
		cin>>BookID;
		cin.ignore();
		cout<<"Enter the book Title: ";
		getline(cin,Title);
		cout<<"Enter the book Author: ";
		getline(cin,Author);
		cout<<"Enter the book Price: ";
		cin>>Price;
		cout<<"Enter the book Quantity: ";
		cin>>Quantity;
	}
	//Display () function is used to display the values.
	void Display(){
		cout<<"The book ID: "<<BookID<<"\n";
		cout<<"The book Title: "<<Title<<"\n";
		cout<<"The book Author: "<<Author<<"\n";
		cout<<"The book Price: "<<Price<<"\n";
		cout<<"The book Quantity: "<<Quantity<<"\n\n";
	}
	//Purchase () function is used to add to numbers of books in quantity.
	void Purchase(){
		this->Quantity++;
	}
	//Sold () function is used to minus from the number of books from the quantity of book.
	void Sold(){
		//Sold () function must also check the quantity available to sell.
		if(this->Quantity-1>=0){
			this->Quantity--;
		}else{
			cout<<"\n\nThere are not enough quantity of the books.\n\n";
		}
	}
	
};


int main(){  
	//Create an object of BOOK. Input the values in data members of object and then display the data of object. 
	BOOK myBOOK;
	myBOOK.Input();
	//In main () function, also use switch statement to prompt the user for choice of operations either to purchase or sell a book. 
	int ch=0;
	while(ch!=3){
		cout<<"1. Purchase a book, 2. Sell a book, 3. Exit: ";
		cin>>ch;
		switch(ch){
			case 1:
				myBOOK.Purchase();
				break;
			case 2:
				myBOOK.Sold();
				break;
			case 3:
				//exit
				break;
		}
	}
	system("pause");
	return 0;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS