Question 1:
You have been given the header for class Book, the header for class Library, and test code for function main.    You may not change any of the code that has been provided.  However, you may add additional test cases to function main and add preprocessor directives to all files as needed. 
Your assignment is to write the member function definitions for classes Book and Library.  Your program must be broken up into the following files:

  • Book.h
  • Book.cpp
  • Library.h
  • Library.cpp
  • Prog4.cpp

Use the following guidelines when writing you code:

  • The Book constructor - Book(char*,char*,bool) – accepts three arguments.  The first is used to initialize the title member.  The second is used to initialize the callNumber member.  The last argument represents a flag used to indicate if the book object is circulating or non-circulating.  True for circulating, False for non-circulating.  The Book constructor is to increment bookCount. The Book destructor is to decrement bookCount.
  • setCirculating(bool) assigns a value - True  or False – to circulating. 
  • Member function getCount() is to return the value of bookCount.
  • The Library constructor - Library(int=10) – accepts a single argument that represents the maximum number of books that can be held on the bookShelf.  Use that value both to allocate storage for bookShelf to point to and to initialize the const value bookShelfSpace.  The pointers in the bookShelf array are to be initialized to NULL.
  • The Library destructor dynamically destroys all the book objects pointed to by the bookshelf array by de-allocating their storage.
  • getBookCount() returns the number of books currently on the shelf.
  • getBookShelfSpace() returns the number of elements in the bookshelf array.
  • addBook(char*,char*,bool) dynamically requests storage for  a new book, initializes its member data, and puts its address in the next available location in the bookshelf array.  If bookShelf is full an error message is displayed.
  • removeBookByTitle(char*) destroys the Book object having the indicated title.  After the Book object’s storage has been de-allocated, its pointer in bookShelf is assigned a NULL value.   If the book to be removed cannot be found an error message is displayed.
  • RemoveBookByCallNo(char*) destroys the Book object having the indicated call Number.  After the Book object’s storage has been de-allocated, its pointer in bookShelf is assigned a NULL value.   If the book to be removed cannot be found an error message is displayed.

//Book.h
class Book {
            public:
                        Book(char*, char*, int=true);
            ~Book();
                        int getCirculating()const;
                        void setCirculating(bool);
                        const char* getTitle()const;
                        const char* getCallNumber()const;
                        static int getCount();
            private:
                        char title[30];
                        char callNumber[25];
                        static int bookCount;
                        bool circulating;
};

//Library.h
class Library {
            public:
                        Library(int=10);
                        ~Library();
                        void addBook(char*,char*,bool);
                        void removeBookByTitle(char*);
                        void removeBookByCallNo(char*);
                        const Book* getBookByTitle(char*) const;
                        const Book* getBookByCallNo(char*) const;
                        void displayAllBooks() const;
                        static int getBookCount();
                        int getBookShelfSpace() const;
            private:
                        Book **bookShelf;
                        const int bookShelfSpace;
};

 

//Prog4.cpp
void main() {
            Library fretzPark(5);
            fretzPark.displayAllBooks();
cout << "Book count = " << Book::getCount() << endl;

            fretzPark.addBook("Rocky", "L123.54", true);
            fretzPark.addBook("Rocky2", "L123.55", false);
            fretzPark.addBook("Rocky3", "L123.56", true);
            fretzPark.addBook("Rocky4", "L123.57", true);
            fretzPark.addBook("Rocky5", "L123.58", true);

            fretzPark.displayAllBooks();
            cout << "Book count = " << Book::getCount() << endl;

            fretzPark.addBook("Rocky6", "L123.59", true);
            fretzPark.addBook("Rocky7", "Li23.510", false);

            fretzPark.displayAllBooks();
            cout << "Book count = " << Book::getCount() << endl;

            fretzPark.removeBookByTitle("Rocky2");
            fretzPark.removeBookByCallNo("L123.57");
            fretzPark.removeBookByCallNo("L999.66");

            fretzPark.displayAllBooks();
            cout << "Book count = " << Book::getCount() << endl;

            fretzPark.addBook("Rocky6", "L123.59", true);
            fretzPark.addBook("Rocky7", "L123.510", false);

            fretzPark.displayAllBooks();
            cout << "Book count = " << Book::getCount() << endl;

            const Book *myBook1=fretzPark.getBookByCallNo("L123.59");
            if (myBook1 != NULL){
                        cout << "Title: " << myBook1->getTitle() << endl;
                        cout << "Call number: "
        << myBook1->getCallNumber() << endl;
                        cout << "Circulating: "
                                <<(myBook1->getCirculating()? "Yes" : "No") << endl;
            }

            const Book *myBook2=fretzPark.getBookByTitle("Rocky7");
            if (myBook2 != NULL){
                        cout << "Title: " << myBook2->getTitle() << endl;
                        cout << "Call number: "
                             << myBook2->getCallNumber() << endl;
                        cout << "Circulating: "
                                << (myBook2->getCirculating()? "Yes" : "No") << endl;
            }
} //end main

Solution 1:

Save the following code in a file named Book.h

---------------------------------start---------------------------------------
#ifndef BOOK_H
#define BOOK_H

class Book {
public:
Book(char*, char*, bool =true);
~Book();
bool getCirculating()const;
void setCirculating(bool);
const char* getTitle()const;
const char* getCallNumber()const;
static int getCount();
private:
char title[30];
char callNumber[25];
static int bookCount;
bool circulating;
};

#endif
--------------------------------------------------------end---------------------------------

 

Save the following code in a file named Book.cpp

-----------------------------------------------------------start-----------------------------

#include "Book.h"

 

#include <CSTRING>

using namespace std;

 

#include <IOSTREAM>

using namespace std;

 

Book::Book(char* bookTitle,char* bookCallNumber,bool bookCirculating)

{

        strcpy(title,bookTitle);

        strcpy(callNumber,bookCallNumber);

        circulating=bookCirculating;

        Book::bookCount++;

}

 

Book::~Book()

{

        Book::bookCount--;

}

 

bool Book::getCirculating() const

{

        return circulating;

}

 

void Book::setCirculating(bool bookCirculating)

{

        circulating=bookCirculating;

}

 

const char* Book::getTitle() const

{

        return title;

}

 

const char* Book::getCallNumber() const

{

        return callNumber;

}

 

int Book::getCount()

{

        return Book::bookCount;

}

 

int Book::bookCount = 0;

----------------------------------------------------------end-------------------------------

 

 

Save the following code in a file named Library.h

 

---------------------------------start---------------------------------------

#ifndef LIBRARY_H

#define LIBRARY_H

#include "Book.h"

 

class Library {

            public:

                        Library(int=10);

                        ~Library();

                        void addBook(char*,char*,bool);

                        void removeBookByTitle(char*);

                        void removeBookByCallNo(char*);

                        const Book* getBookByTitle(char*) const;

                        const Book* getBookByCallNo(char*) const;

                        void displayAllBooks() const;

                        static int getBookCount();

                        int getBookShelfSpace() const;

            private:

                        Book **bookShelf;

                                              Book* bookPtr[10];

                        const int bookShelfSpace;

};

 

#endif

-----------------------------------------------------------------end------------------------
Save the following code in a file named Library.cpp

--------------------------------------------------------------start--------------------------

#include "Book.h"

#include "Library.h"

 

#include <CSTRING>

using namespace std;

 

#include <IOSTREAM>

using namespace std;

 

Library::Library(int libraryBookShelfSpace): bookShelfSpace(libraryBookShelfSpace)

{

        bookShelf=bookPtr;

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

        {

               bookPtr[i]=NULL;

        }

       

}

 

Library::~Library()

{

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

        {

               if(bookPtr[i]!=NULL)

               {

                       delete bookPtr[i];

                       bookPtr[i]=NULL;

                       return;

               }

        }

}

 

void Library::addBook(char *bookTitle, char *bookCallNumber, bool bookCirculatity)

{

        int numberOfBooksOnShelf=getBookCount();

        if(numberOfBooksOnShelf<bookShelfSpace)

        {

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

               {

                       if(bookPtr[i]==NULL)

                       {

                               bookPtr[i]=new Book(bookTitle,bookCallNumber,bookCirculatity);

                               return;

                       }

               }

        }

        else

               cout<<"\nBookshelf is full";

}

 

void Library::removeBookByTitle(char *bookTitle)

{

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

        {

               if(bookPtr[i]!=NULL)

               {

                       if(strcmp(bookPtr[i]->getTitle(),bookTitle)==0)

                       {

                               delete bookPtr[i];

                               bookPtr[i]=NULL;

                               cout<<"\nBook removed";

                               return;

                       }

               }

        }

        cout<<"\nNo book matching this title";

}

 

void Library::removeBookByCallNo(char *bookCallNo)

{

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

        {

               if(bookPtr[i]!=NULL)

               {

                       if(strcmp(bookPtr[i]->getCallNumber(),bookCallNo)==0)

                       {

                               delete bookPtr[i];

                               bookPtr[i]=NULL;

                               cout<<"\nBook removed";

                               return;

                       }

               }

        }

        cout<<"\nNo book matching this Call Number";

}

 

const Book* Library::getBookByTitle(char *bookTitle) const

{

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

        {

               if((bookPtr[i])!=NULL)

               {

                       if(strcmp((bookPtr[i])->getTitle(),bookTitle)==0)

                       {

                               cout<<"\nBook found";

                               return bookPtr[i];

                       }

               }

        }

        cout<<"\nNo book matching this title";

        return NULL;

}

 

const Book* Library::getBookByCallNo(char *bookCallNo) const

{

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

        {

               if(bookPtr[i]!=NULL)

               {

                       if(strcmp(bookPtr[i]->getCallNumber(),bookCallNo)==0)

                       {

                               cout<<"\nBook found";

                               return bookPtr[i];

                       }

               }

        }

        cout<<"\nNo book matching this Call Number";

        return NULL;

}

 

void Library::displayAllBooks() const

{

        int flag=0;

        cout<<"\nBooks available in library: \n";

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

        {

               if(bookPtr[i]!=NULL)

               {

                       flag++;

                       cout<<"\nBook Title: "<<bookPtr[i]->getTitle();

               }

        }

        if(flag==0)

               cout<<"No books in library";

 

}

 

int Library::getBookCount()

{

        return Book::getCount();

}

 

int Library::getBookShelfSpace() const

{

        return bookShelfSpace;

}

-------------------------------------------------------------end----------------------------

 

Save the following code in a file named Prog4.cpp
-----------------------------------------------------------start-----------------------------

#include "Library.h"

#include "Book.h"

#include <CONIO.H>

#include <IOSTREAM>

using namespace std;

 

void main() {

            Library fretzPark(5);

            fretzPark.displayAllBooks();

cout << "\nBook count = " << Book::getCount() << endl;

            fretzPark.addBook("Rocky", "L123.54", true);

            fretzPark.addBook("Rocky2", "L123.55", false);

            fretzPark.addBook("Rocky3", "L123.56", true);

            fretzPark.addBook("Rocky4", "L123.57", true);

            fretzPark.addBook("Rocky5", "L123.58", true);

            fretzPark.displayAllBooks();

            cout << "\nBook count = " << Book::getCount() << endl;

            fretzPark.addBook("Rocky6", "L123.59", true);

            fretzPark.addBook("Rocky7", "Li23.510", false);

            fretzPark.displayAllBooks();

            cout << "\nBook count = " << Book::getCount() << endl;

            fretzPark.removeBookByTitle("Rocky2");

            fretzPark.removeBookByCallNo("L123.57");

            fretzPark.removeBookByCallNo("L999.66");

            fretzPark.displayAllBooks();

            cout << "\nBook count = " << Book::getCount() << endl;

            fretzPark.addBook("Rocky6", "L123.59", true);

            fretzPark.addBook("Rocky7", "L123.510", false);

            fretzPark.displayAllBooks();

            cout << "\nBook count = " << Book::getCount() << endl;

            const Book *myBook1=fretzPark.getBookByCallNo("L123.59");

            if (myBook1 != NULL){

                        cout << "Title: " << myBook1->getTitle() << endl;

                        cout << "Call number: "

        << myBook1->getCallNumber() << endl;

                        cout << "Circulating: "

                                <<(myBook1->getCirculating()? "Yes" : "No") << endl;

                       }

           

 

            const Book *myBook2=fretzPark.getBookByTitle("Rocky7");

            if (myBook2 != NULL){

                        cout << "\n\nTitle: " << myBook2->getTitle() << endl;

                        cout << "Call number: "

                             << myBook2->getCallNumber() << endl;

                        cout << "Circulating: "

                                << (myBook2->getCirculating()? "Yes" : "No") << endl;

            }

                       getch();

}

 

-----------------------------------------------------------------end------------------------