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

1. Create LibraryMain.cpp that includes BookClass.h 2. Declare an array of BookC

ID: 3606100 • Letter: 1

Question

1. Create LibraryMain.cpp that includes BookClass.h

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

3. Use the following function from Assignment 10 main program

a. 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. Similar to getBookInfo from Assignment 10, except 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. void showBooks (BookClass[], int); Lops 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:

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

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 users’ 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 labelled, organized and easy to read.

BOOK.txt info

Starting Out with C++

Gaddis, Tony

Pearson

978-0-13-257625-3

129.98

2014

25

The World is Flat

Friedman, Thomas

Farrar, Straus and Giroux

0-374-29279-5

30.00

2006

12

Good to Great

Collins, Jim

Collins Business

978-0-06-662099-2

29.99

2001

10

You Cant Make this Stuff Up

Caputo, Theresa

Atrai Books

978-1-4767-6443-6

25.00

2014

8

Winners

Steel, Danielle

Dell

978-0-440-24525-4

7.99

2013

6

The Success Principles

Canfield, Jack

Harper Collins

0-06-059488-8

24.95

2005

4

Explanation / Answer

Full working code in C++

Enter all the function definations and the class defination in a header file and the rest in the main cpp file.

#include <iostream>

#include <string>

#include <fstream>

using namespace std;

#define MAX_BOOKS 200


int getMenuChoice();

class BookClass
{
private:
   //   0-title,1- author, 2-publisher,3- ISBN,4- price, 5-yearPublished,6- numberOfCopies;
   string info[7];


public:

   string getTitle(); // get title of book

   void storeBook(string *book); // store the book

   void displayBookInfo(); // dispaly all the book info

   string getCopiesLeft(); // display the copies left

   void IncreaseDecreaseCopies(int flag); // manipulate the number of copies left

};

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 tittle 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;
}