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

#include <iostream> #include <string> #include <fstream> #include \"bookType.h\"

ID: 3569547 • Letter: #

Question


#include <iostream>
#include <string>
#include <fstream>
#include "bookType.h"

using namespace std;

void getBookData(bookType books[], int& noOfBooks);
void printBookData(bookType books[], int noOfBooks);
void searchBookData(bookType books[], int bookCount);
void searchBookDataByISBN(bookType books[], int bookCount, string ISBN,
int& loc);
void searchBookDataByTitle(bookType books[], int bookCount, string title,
int& loc);
void updateCopiesInStock(bookType books[], int bookCount);
void showMenu();
void subMenu();

int main()
{
bookType books[100];

int numberOfBooks = 0;

int choice;

int i;

getBookData(books, numberOfBooks);

showMenu();
cin >> choice;

while(choice != 9)
{
switch (choice)
{
       case 1:
for (i = 0; i <numberOfBooks; i++)
books[i].printbookTitle();
cout << endl;
break;
       case 2:
for (i = 0; i <numberOfBooks; i++)
books[i].printbookTitleAndISBN();
cout << endl;
break;
       case 3:
searchBookData(books, numberOfBooks);
break;
       case 4:
updateCopiesInStock(books, numberOfBooks);
break;
       case 5:
printBookData(books, numberOfBooks);
break;
       default:
cout << "Invalid selection." << endl;
}

showMenu();
cin >> choice;
}

return 0;
}

void getBookData(bookType books[], int& noOfBooks)
{
string title;
string ISBN;
string Publisher;
int PublishYear;
string auth[4];
double cost;
int copies;
int authorCount;

int i, j;

ifstream infile;
char ch;

infile.open("bookData.txt");

if (!infile)
{
cout << "Cannot open Input file" << endl;
cout << "Exit the program" << endl;
return;
}

infile >> noOfBooks;
infile.get(ch);

for (i = 0; i < noOfBooks; i++)
{
getline(infile, title);
getline(infile,ISBN);
getline(infile,Publisher);
infile >> PublishYear >> cost >> copies >> authorCount;
infile.get(ch);

for (j = 0; j < authorCount; j++)
getline(infile, auth[j]);

books[i].setBookInfo(title, ISBN, Publisher,
PublishYear, auth, cost, copies,
authorCount);
}
}

void printBookData(bookType books[], int noOfBooks)
{
int i;

for (i = 0; i < noOfBooks; i++)
{
books[i].printInfo();
cout << endl << "---------------------------------" << endl;
}
}

void searchBookDataByISBN(bookType books[], int bookCount, string ISBN,
int& loc)
{
int i;
loc = -1;

for (i = 0; i < bookCount; i++)
if (books[i].isISBN(ISBN))
{
loc = i;
break;
}
}

void searchBookDataByTitle(bookType books[], int bookCount, string title,
int& loc)
{
int i;
loc = -1;

for (i = 0; i < bookCount; i++)
if (books[i].isTitle(title))
{
loc = i;
break;
}
}

void searchBookData(bookType books[], int bookCount)
{
int choice;
char ch;

int loc;

string str;

subMenu();

cin >> choice;
cin.get(ch);

switch(choice)
{
   case 1:
cout << "Enter the ISBN of the book." << endl;
getline(cin, str);
searchBookDataByISBN(books, bookCount, str, loc);
if (loc != -1)
cout << "The store sells this book." << endl;
else
cout << "The store does not sell this book" << endl;
break;
   case 2:
cout << "Enter the title of the book." << endl;
getline(cin, str);
searchBookDataByTitle(books, bookCount, str, loc);
if (loc != -1)
cout << "The store sells this book." << endl;
else
cout << "The store does not sell this book" << endl;
break;
   default:
cout << "Invalid choice" << endl;
}
}

void updateCopiesInStock(bookType books[], int bookCount)
{
int choice;

int loc;

int count;
char ch;

string str;

subMenu();
cin >> choice;
cin.get(ch);

switch (choice)
{
   case 1:
cout << "Enter the ISBN of the book." << endl;
getline(cin, str);
searchBookDataByISBN(books, bookCount, str, loc);
if (loc != -1)
{
cout << "Enter the number of books: " ;
cin >> count;
cout << endl;

books[loc].updateQuantity(count);
}
else
cout << "The store does not sell this book: " << endl;
break;
   case 2:
cout << "Enter the title of the book." << endl;
getline(cin, str);
searchBookDataByTitle(books, bookCount, str, loc);
if (loc != -1)
{
cout << "Enter the number of books" ;
cin >> count;
cout << endl;

books[loc].updateQuantity(count);
}
else
cout << "The store does not sell this book" << endl;
break;
   default:
cout << "Invalid choice" << endl;
   }
}

void showMenu()
{
cout << "Welcome to Rock's Book Store" << endl;
cout << "To make a selection enter the number and press enter"
<< endl;
cout << "1: Print a list of books" << endl;
cout << "2: Print a list of books and ISBN numbers" << endl;
cout << "3: To see if a book in store" << endl;
cout << "4: To update the number of copies of a book" << endl;
cout << "5: To print books data" << endl;
cout << "9: Exit the program." << endl;
}

void subMenu()
{
cout << "Enter" << endl;
cout << "1: To search the book by ISBN" << endl;
cout << "2: To search the book by title" << endl;
}

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

Each object of the class bookType can hold the following information 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 member variable.

Include the member functions to perform the various operations on objects of type bookType. For example, the usual 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 mumber 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 operation for the publisher, ISBN, book price, and authors. Add the appropriate constructors and a destructor (if one is needed).

Write the divinitions of the members function of the class bookType.

Explanation / Answer

Here is the correct answer with Output in the end:

Solution.

book.cpp

// book.cpp : Defines the entry point for the console application.

//

#include "stdafx.h"

#include <iostream>

#include <string>

#include <iomanip>

using namespace std;

//! Main class of BookType.

class bookType {

            private:

                        //! Data members.

                        string title;

                        string authors[4];

                        int        n_authors;

                        string ISBN;

                        string publisher;

                        int year;

                        double price;

                        int copies;

            public:

                        //! Constructor of objects.

                        bookType();

                        //! Destructor.

                        ~bookType();

                        //! Set and get 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;

}

bookType::~bookType() {

}

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_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::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::show_copies() {

            cout

                        << "Copies in stock:"

                        << copies

                        << endl;

}

void bookType::show_ISBN() {

            cout

                        << "ISBN:"

                        << ISBN

                        << endl;

}

void bookType::show_publisher() {

            cout

                        << "Publisher:"

                        << publisher

                        << endl;

}

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 bookType::compare_title(string str) {

            int x = (str == title);

            return x;

}

int main()

{         

            bookType a[100];

            int i;

            for(i = 0; i < 5; i++) {

                        a[i].add_author("Newton");

                        a[i].add_author("Kepler");

                        a[i].set_copies(100-i);

                        a[i].set_ISBN("123-123-123");

                        a[i].set_price(100.01+i*i);

                        a[i].set_publisher("Piter");

                        a[i].set_title("Star moving");

                        a[i].set_year(1660+i);

            }

            for(i = 0; i < 5; i++) {

                        a[i].show_authors();              

                        a[i].show_copies();

                        a[i].show_ISBN();

                        a[i].show_price();

                        a[i].show_publisher();

                        a[i].show_title();

                        a[i].show_year();

            }

            cout << "Compare(one) " << a[0].compare_title("No one") << endl;

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

            return 0;

}

Output.txt

Author(s):2

Newton Kepler

Copies in stock:100

ISBN:123-123-123

Price:100.01

Publisher:Piter

Title:Star moving

Year of publish:1660

Author(s):2

Newton Kepler

Copies in stock:99

ISBN:123-123-123

Price:101.01

Publisher:Piter

Title:Star moving

Year of publish:1661

Author(s):2

Newton Kepler

Copies in stock:98

ISBN:123-123-123

Price:104.01

Publisher:Piter

Title:Star moving

Year of publish:1662

Author(s):2

Newton Kepler

Copies in stock:97

ISBN:123-123-123

Price:109.01

Publisher:Piter

Title:Star moving

Year of publish:1663

Author(s):2

Newton Kepler

Copies in stock:96

ISBN:123-123-123

Price:116.01

Publisher:Piter

Title:Star moving

Year of publish:1664

Compare(one) 0

Compare(two) 1