Answer to Question #185690 in C++ for Laraib

Question #185690

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-27T05:23:30-0400
#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;
}

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