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.
#include <iostream>
using namespace std;
class Book {
private:
int bookID;
string title;
string author;
int price;
int quantity;
public:
void Input();
void Display() const;
void Purchase(int numberItems);
void Sold(int numberItems);
};
void Book::Input() {
cout << "Please enter the data about the current book.\nEnter the book's ID: ";
cin >> this->bookID;
cout << "Enter the book's title: ";
cin >> this->title;
cout << "Enter the book's author: ";
cin >> this->author;
cout << "Enter the book's price: ";
cin >> this->price;
cout << "Enter the book's quantity: ";
cin >> this->quantity;
}
void Book::Display() const {
cout << "\nThe book info\nID: " << this->bookID << ", Title: " << this->title
<< ", Author: " << this->author << ", Price: " << this->price
<< ", Quantity: " << this->quantity << endl;
}
void Book::Purchase(int numberItems) {
this->quantity += numberItems;
}
void Book::Sold(int numberItems) {
if (numberItems > this->quantity) {
cout << "Error: not enough books to sold.";
} else {
this->quantity -= numberItems;
}
}
int main() {
int numberBooks;
int action;
Book one;
one.Input();
one.Display();
cout << "\nPlease chose the number of books your want to purchase or sold: ";
cin >> numberBooks;
cout << "\nPlease chose the action (by entering number 1 or 2):\n1. Purchase\n2. Sold\n";
cin >> action;
switch(action) {
case 1:
one.Purchase(numberBooks);
break;
case 2:
one.Sold(numberBooks);
break;
default:
cout << "Error: you have entered the wrong number\n";
return 0;
}
one.Display();
return 0;
}
Comments
Leave a comment