Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

(a) Some of the characteristics of a book are the title, author(s), publisher, I

ID: 3803422 • Letter: #

Question

(a) Some of the characteristics of a book are the title, author(s), publisher, ISBN, price, and year of publication. Design the class bookType that defines the book as an ADT.

Each object of the class bookType can hold the following infomratin about a book: title, up to four authors, publisher, ISBN, price, and number of copies in stock. To keep track of the number of authors, add another data member.

Include the member functions to perform the various operations on the object of bookType. For example, the typical operations that can be performed on the title are to show the title, set the title, and check whether a title is the same as the actual title of the book. Similarly, the typical operations that can be performed on the number of copies in stock are to show the number of copies in stock, set the number of copies in stock, update the number of copies in stock, and return the number of copies in stock. Add similar operations for the publisher, ISBN, book price, and authors. Add the appropriate constructors and a destructor (if one is needed) don't use any pointers and you won't need a destructor

Write the definitions of the member functions of the class bookType.

Write a program that uses the class bookType and tests the various operations on the objects of class bookType. Declare a vector container of type bookType. Some of the operations that you should perform are to search for a book by its title, search by ISBN, and update the number of copies in stock.

sample output below:

Sample output: The title of the book is same The details of the book are: Title: Data Structures Publisher: XYZ Number of copies: 5 ISBN Number: 67837216 Year of Publication: 1995 Price: 90.50 Authors ABC DEF The title of the book is same The details of the book are: Title: Programming with C Publisher: WXY

Explanation / Answer

Please find the code below:

CODE:

#include <iostream>
#include <string>
#include <iomanip>
#include <vector>

using namespace std;

// BookType class
class BookType {
   private:

       //data members.
       string title;
       string authors[4];
       int   n_authors;
       string ISBN;
       string publisher;
       int year;
       double price;
       int copies;
      
   public:
       // Default Constructor.
       BookType();
      
       // getter and setter methods to access the above values.
       void set_title(string str);
       string get_title();
       void clear_authors();
       void add_author(string str);
       string get_author(int number);
       void set_ISBN(string str);
       string get_ISBN();
       void set_publisher(string str);
       string get_publisher();
       void set_year(int n);
       int get_year();
       void set_price(double n);
       double get_price();
       void set_copies(int n);
       int get_copies();
       void update_copies(int n);

       // Actions.
       void show_title();
       void show_copies();
       void show_ISBN();
       void show_publisher();
       void show_price();
       void show_authors();
       void show_year();
       // 1 - equal, 0 - non-equal.
       int compare_title(string str);
};

BookType::BookType() {
   title = "";
   authors[0] = "";
   authors[1] = "";
   authors[2] = "";
   authors[3] = "";
   n_authors = 0;
   ISBN = "";
   publisher = "";
   year = 0;
   price = 0;
   copies = 0;
}

void BookType::set_title(string str) {
   title = str;
}

string BookType::get_title() {
   return title;
}

void BookType::clear_authors() {
   n_authors = 0;
}

void BookType::add_author(string str) {
   if (n_authors < 4) {
       authors[n_authors] = str;
       n_authors++;
   }
}

string BookType::get_author(int number) {
   if (n_authors < number) {
       return authors[number - 1];
   }
   else {
       return "";
   }  
}

void BookType::set_year(int n) {
   year = n;
}

int BookType::get_year() {
   return year;
}

void BookType::set_copies(int n) {
   if (n >= 0) copies = n;
}

int BookType::get_copies() {
   return copies;
}

void BookType::set_price(double n) {
   if (n >= 0) price = n;
}

double BookType::get_price() {
   return price;
}

void BookType::update_copies(int n) {
   if (n >= 0) copies += n;
}

void BookType::show_title() {

   cout
       << "Title:"
       << title
       << endl;
}

void BookType::set_ISBN(string str) {
   ISBN = str;
}

string BookType::get_ISBN() {
   return ISBN;
}

void BookType::set_publisher(string str) {
   publisher = str;
}

string BookType::get_publisher() {
   return publisher;
}

void BookType::show_copies() {

   cout
       << "Copies in stock:"
       << copies
       << endl;
}

void BookType::show_ISBN() {

   cout
       << "ISBN:"
       << ISBN
       << endl;
}

void BookType::show_publisher() {

   cout
       << "Publisher:"
       << publisher
       << endl;
}

int BookType::compare_title(string str) {
   int x = (str == title);
   return x;
}

void BookType::show_price() {

   cout
       << "Price:"
       << setprecision(2)
       << fixed
       << price
       << endl;
}

void BookType::show_year() {

   cout
       << "Year of publish:"
       << year
       << endl;
}

void BookType::show_authors() {

   int i = 0;

   cout
       << "Author(s):"
       << n_authors
       << endl;

   for(i = 0;i < n_authors; i++) {
       cout << authors[i] << " ";
   }

   cout << endl;
}

int main()
{  
   // create a vector container
   vector<BookType> vec;
  
   int i;

   BookType bt;
   bt.add_author("ABC");
   bt.add_author("DEF");
   bt.set_copies(5);
   bt.set_ISBN("67837216");
   bt.set_price(90.50);
   bt.set_publisher("XYZ");
   bt.set_title("Data Structures");
   bt.set_year(1995);
   vec.push_back(bt);
  
   // add few more books like above.

// to display them, do as below.
for(int i=0;i<vec.size();i++){
   vec[i].show_authors();      
   vec[i].show_copies();
   vec[i].show_ISBN();
   vec[i].show_price();
   vec[i].show_publisher();
   vec[i].show_title();
   vec[i].show_year();
}

   cout << "Compare(one) " << vec[0].compare_title("No one") << endl;
   cout << "Compare(two) " << vec[0].compare_title("Star moving") << endl;

   return 0;
}

OUTPUT:
Author(s):2
ABC DEF
Copies in stock:5
ISBN:67837216
Price:90.50
Publisher:XYZ
Title:Data Structures
Year of publish:1995
Compare(one) 0
Compare(two) 0