Question: C++ Assignment My BookClass.h and LibraryMain.cpp appear below the requirements and are marked as each one. Requirements: 1) Create LibraryMain.cpp that includes BookClass.h. 2) Declare

C++ Assignment My BookClass.h and LibraryMain.cpp appear below the requirements and are marked as each one.

Requirements: 1) Create LibraryMain.cpp that includes BookClass.h.

2) Declare an array of BookClass items: BookClass books[MAX_BOOKS];.

3) Use the following function void getBookInfo (BookClass &);

4) Write the following functions for BookMain.cpp

a) void getBooks (BookClass[], int &); Uses input data file books.txt to load the data structure. Uses a loop to read the data from the file. Calls storeBook to store the book information in an array location. Hint: bookList{i}.storeBook (....) where bookList is the name of the array of class items and .... is the data for the book you read in from the file.

b) voidShowBooks (BookClass[],int); Loops through all the books in the array, calling member function displayBookInfo each pass to display the book in the current array location.

c) void showTitles (BookClass[], int); Loops through all the books in the array, calling member function getTitle each pass to display the title of the book in the current array location.

d) int findBook (string, BookClass[], int);A search that loops through the book array calling member function getTitle then compares the title to the search item passed in as an input argument.

e) int getMenuChoice(); Displays the following menu and returns the users choice: 1: Displayall books 2. Display book titles 3. Find book 4. Check out 5. Check in 6. Exit program

f) Menu commands "Check In" and "Check Out" ask the user to enter the name of a book, invoke findBook to determine if the book is in the library, then if the book is in the Library (array of books), sends the location of the book in the array to either checkOutBook or returnBook to increment or decrement numInStock for the particular book.

5. BookMain.cpp program

a) Calls getBooks to load the input file into the array class structure.

b) Uses a switch statement to call getMenuChoice and process the user's choice.

c) Since you are mixing cin and getline, you will need to use cin.ignore to avoid an infinite loop situation.

6. Output must be labeled, organized and easy to read.

BookClass.h Code

#include

#include

using namespace std;

class BookClass

{

string title;

string author;

string publisher;

string isbn;

double price;

int year;

int numInStock;

public:

void storeBook(string bookTitle, string authorName, string bookPublisher, string bookISBN,

double bookPrice, int bookYear, int booksInStock)

{

title = bookTitle;

author = authorName;

publisher = bookPublisher;

isbn = bookISBN;

price = bookPrice;

year = bookYear;

numInStock = booksInStock;

}

void displayBookInfo()

{

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

cout << "Author: " << author << endl;

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

cout << "ISBN: " << isbn << endl;

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

cout << "Year: " << year << endl;

cout << "Books in Stock: " << numInStock << endl;

}

void checkOutBook()

{

if (numInStock > 0)

{

cout << "Checking out a book." << endl;

numInStock--;

}

else

cout << "No books in stock." << endl;

}

void returnBook()

{

cout << "Book returned successfully." << endl;

numInStock++;

}

string getTitle()

{

return title;

}

int getNumInStock()

{

return numInStock;

}

};

LibraryMain.cpp Code

#include BookClass.h

void BookClass::IncreaseDecreaseCopies(int flag)

{ // 0 for increment and 1 for decrement

int newNumber;

if (flag == 0)

{

newNumber = stoi(this->info[6]);

newNumber++;

this->info[6] = to_string(newNumber);

}

if (flag == 1)

{

newNumber = stoi(this->info[6]);

newNumber--;

this->info[6] = to_string(newNumber);

}

}

string BookClass::getCopiesLeft()

{

return this->info[6];

}

string BookClass::getTitle()

{

return this->info[0];

}

void BookClass::displayBookInfo()

{

for (int i = 0; i < 7; i++)

{

cout << this->info[i] << endl;

}

}

void BookClass::storeBook(string *book)

{

for (int i = 0; i < 7; i++)

{

this->info[i] = book[i];

}

}

int getMenuChoice()

{

int choice;

cout << "i. 1: Display all books ii. 2: Display book titles iii. 3 : Find book iv. 4 : Check out v. 5 : Check in vi. 6 : Exit program" << endl;

cin >> choice;

return choice;

}

void getBooks(BookClass *books, int &totalBooks)

{

string fileName, line, tempInfo[7];

int info;

totalBooks = 0;

cout << "Enter the file name to load book information (with extension): ";

cin >> fileName;

ifstream file(fileName);

if (file.is_open())

{

info = 0;

while (getline(file, line))

{

if (line != "")

{

tempInfo[info++] = line;

if (info == 7)

{

books[totalBooks++].storeBook(tempInfo);

info = 0;

tempInfo->clear();

}

}

}

}

}

void showBooks(BookClass *books, int totalBooks)

{

for (int i = 0; i < totalBooks; i++)

{

books[i].displayBookInfo();

}

}

void showTitles(BookClass *books, int totalBooks)

{

for (int i = 0; i < totalBooks; i++)

{

cout << books[i].getTitle() << endl;

}

}

int findBook(string book, BookClass *books, int totalBooks)

{

for (int i = 0; i < totalBooks; i++)

{

if (strcmp(books[i].getTitle().c_str(), book.c_str()) == 0)

{

return i;

}

}

return -1;

}

int main()

{

BookClass books[MAX_BOOKS];

string book;

bool exit = false;

int totalBooks, idx;

getBooks(books, totalBooks);

while (!exit)

{

switch (getMenuChoice())

{

case 1:

showBooks(books, totalBooks);

break;

case 2:

showTitles(books, totalBooks);

break;

case 3:

cout << "enter the name of the book to find : ";

cin >> book;

idx = findBook(book, books, totalBooks);

if (idx == -1)

{

cout << "This book is not available ";

}

else

{

cout << books[idx].getCopiesLeft() << " copies of " << books[idx].getTitle() << " are in stock " << endl;

}

break;

case 4:

cout << "Enter title of book to check out ";

cin >> book;

idx = findBook(book, books, totalBooks);

if (idx == -1)

{

cout << "Sorry we do not stock that book ";

}

else

{

cout << "Book is available to check out ";

books[idx].IncreaseDecreaseCopies(1);

cout << books[idx].getCopiesLeft() << " copies of " << books[idx].getTitle() << " are in stock after checkout" << endl;

}

break;

case 5:

cout << "Enter tittle of book to check in ";

cin >> book;

idx = findBook(book, books, totalBooks);

if (idx == -1)

{

cout << "Sorry we do not stock that book ";

}

else

{

cout << books[idx].getTitle() << " returned ";

books[idx].IncreaseDecreaseCopies(0);

}

break;

case 6:

return 0;

default:

cout << "Enter correct choice : ";

}

}

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!