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

*holdings - a list of Books the Library has *members - a list of Patrons the Lib

ID: 644400 • Letter: #

Question

*holdings - a list of Books the Library has
*members - a list of Patrons the Library has
*currentDate - stores the current date represented as an integer number of "days" since the Library object was created
*a constructor that initializes the currentDate to zero
*addBook - prompts the user for book info (including the ID, which can be any arbitrary string) and uses it to create a Book, which is added to holdings
*addMember - prompts the user for patron info (including the ID, which can be any arbitrary string) and uses it to create a Patron, which is added to members
*checkOutBook
-if the specified Book or Patron are not in the Library (an ID doesn't match an object in the Library), print out that message and return to the menu
-if a Book is already checked out, print out that message and return to the menu
-if a Book is on hold by another Patron, print out that message and return to the menu
-otherwise update the Book's checkedOutBy, dateCheckedOut and Location; if the Book was on hold for this Patron, update requestedBy; update the Patron's list; print out that Book title has been checked out to Patron name
*returnBook
-
if the specified Book is not in the Library, print out that message and return to the menu
-if the Book is not checked out, print out that message and return to the menu
-update the Patron's list; update the Book's location depending on whether another Patron has requested it; update the Book's checkedOutBy; print out that Book title has been returned

*requestBook
-if the specified Book or Patron are not in the Library, print out that message and return to the menu
-if the Book is already requested by another Patron, print out that message and return to the menu
-If the Book is currently checked out by the same Patron who is trying to request it, print out that message and return to the menu
-update the Book's requestedBy; if the Book is on the shelf, update its location to on hold; print that Book title is on request for Patron name
*
payFine
-if the specified Patron is not in the Library, print out that message and return to the menu
-use amendFine to update the Patron's fine; print out that the fines for Patron name are now Patron fineAmount
*
incrementCurrentDate
-
increment current date; increase each Patron's fines by 10 cents for each overdue Book they have checked out (using amendFine)
*viewPatronInfo
-if the specified Patron is not in the Library, print out that message and return to the menu
-print the Patron's ID, name, the titles of any checked out Books, and their current fines
*
viewBookInfo
-
if the specified Book is not in the Library, print out that message and return to the menu
-
print the Book's ID, title, author and location; the name of the Patron whom it's requested by (if any); the name of the Patron whom it's checked out by (if any), and the number of days left till it's due (or the word "overdue").

be careful - a Book can be on request without its location being the hold shelf (if another Patron has it checked out);

Here is the HPP file:

This will be called Library.cpp

Explanation / Answer

Code
/* MyBook.hpp */
#ifndef BOOKHPP
#define BOOKHPP
#include <string>
using namespace std;
class MyPatron;
enum Location
{
    ONSHELF, ONHOLDSHELF, CHECKEDOUT
};
class MyBook
{
    private:
        string idCode1;
        string title1;
        string author1;
        Location location1;
        MyPatron* checkedOutBy1;
        MyPatron* requestedBy1;
        int dateCheckedOut1;
    public:
        static const int CHECKOUTLENGTH = 21;
        MyBook();
        MyBook(string idc1, string t, string a);
        string getIdCode1();
        string getTitle1();
        string getAuthor1();
        Location getLocation1();
        void setLocation1(Location lo);
        MyPatron* getCheckedOutBy1();
        void setCheckedOutBy1(MyPatron* p);
        MyPatron* getRequestedBy1();
        void setRequestedBy1(MyPatron* p);
        int getDateCheckedOut1();
        void setDateCheckedOut1(int d);
};
#endif

/* MyBook.cpp */
#include "MyPatron.hpp"
#include "MyBook.hpp"
using namespace std;
static int cinc = 1;
MyBook::MyBook()
{
    cinc++;
    idCode1 = "C" + cinc;
    title1 = "";
    author1 = "";
    setLocation1(ONSHELF);
    setCheckedOutBy1(NULL);
    setRequestedBy1(NULL);
    setDateCheckedOut1(0);
}
MyBook::MyBook(string idc1, string t, string a)
{
    idCode1 = idc1;
    title1 = t;
    author1 = a;
    setLocation1(ONSHELF);
    setCheckedOutBy1(NULL);
    setRequestedBy1(NULL);
    setDateCheckedOut1(0);   
}
string MyBook::getIdCode1()
{
    return idCode1;
}
string MyBook::getTitle1()
{
    return title1;
}
string MyBook::getAuthor1()
{
    return author1;
}
Location MyBook::getLocation1()
{
    return location1;
}
void MyBook::setLocation1(Location lo)
{
    location1 = lo;
}
MyPatron* MyBook::getCheckedOutBy1()
{
    return checkedOutBy1;
}
void MyBook::setCheckedOutBy1(MyPatron* p)
{
    checkedOutBy1 = p;
}
MyPatron* MyBook::getRequestedBy1()
{
    return requestedBy1;
}
void MyBook::setRequestedBy1(MyPatron* p)
{
    requestedBy1 = p;
}
int MyBook::getDateCheckedOut1()
{
    return dateCheckedOut1;
}
void MyBook::setDateCheckedOut1(int d)
{
    dateCheckedOut1 = d;
}

/* MyLibrary.hpp */
#ifndef LIBRARYHPP
#define LIBRARYHPP
#include <string>
#include <vector>
#include <iostream>
#include <iomanip>
using namespace std;
class MyPatron;
class MyBook;
class MyLibrary
{
    private:
        vector<MyBook> holdings;
        vector<MyPatron> members;
        int currentDate1;
    public:
        MyLibrary();
        void addBook1();
        void addMember1();
        void checkOutBook1(string patronID1, string bookID1);
        void returnBook1(string bookID1);
        void requestBook1(string patronID1, string bookID1);
        void incrementCurrentDate1();
        void payFine1(string patronID1, double payment1);
        void viewPatronInfo1(string patronID1);
        void viewBookInfo1(string bookID1);
};
#endif


/* MyLibrary.cpp */
#include "MyBook.hpp"
#include "MyPatron.hpp"
#include "MyLibrary.hpp"
using namespace std;
MyLibrary::MyLibrary()
{
    currentDate1 = 0;
    holdings.reserve(100);
    members.reserve(100);
}
void MyLibrary::addBook1()
{
    string idCode1;
    string title1;
    string author1;
    cout <<endl<< "New Book"<<endl;
    cout <<endl<< "--------"<<endl;
    cout <<endl<< "Enter ID-Code: ";
    getline(cin, idCode1);
    cout << "Enter Title: ";
    getline(cin, title1);
    cout << "Enter Author: ";
    getline(cin, author1);
    for(int i = 0; i < holdings.size(); i++)
    {
        if(holdings[i].getIdCode1() == idCode1)
        {
            cout <<endl<< "Book ID-Code is in use"<<endl;
            return;
        }
    }
    MyBook newBook(idCode1, title1, author1);
    holdings.push_back(newBook);
}
void MyLibrary::addMember1()
{
    string idNum1;
    string name1;
    cout <<endl<< "New Patron";
    cout <<endl<<"----------"<<endl;
    cout << "Enter ID-Number: ";
    getline(cin, idNum1);
    cout << "Enter Name: ";
    getline(cin, name1);
    for(int i = 0; i < members.size(); i++)
    {
        if(members[i].getIdNum1() == idNum1)
        {
            cout <<endl<< "Patron ID-Number is in use"<<endl;
            return;
        }
    }
    MyPatron newPatron(idNum1, name1);
    members.push_back(newPatron);
}
void MyLibrary::checkOutBook1(string patronID1, string bookID1)
{
    int counts = 0;
    for(int i = 0; i < members.size(); i++)
    {
        if(members[i].getIdNum1() == patronID1)
        {
            counts++;
        }
    }
    if(counts == 0)
    {
        cout <<endl<<"Patron doesn't exist"<<endl;
        return;
    }
    else
        counts = 0;
    for(int i = 0; i < holdings.size(); i++)
    {
        if(holdings[i].getIdCode1() == bookID1)
        {
            counts++;
        }
    }
    if(counts == 0)
    {
        cout <<endl<< "Book doesn't exist"<<endl;
        return;
    }
    else
        counts = 0;
    for(int i = 0; i < members.size(); i++)
    {
        for(int y = 0; y < members[i].getCheckedOutBooks1().size(); y++)
        {
            if(members[i].getCheckedOutBooks1()[y]->getIdCode1() == bookID1)
            {
                cout <<endl<< "Book is already checkedout"<<endl;
                return;
            }
        }
    }
    for(int i = 0; i < holdings.size(); i++)
    {
        if(holdings[i].getIdCode1() == bookID1)
        {
            for(int y = 0; y < members.size(); y++)
            {
                if(members[y].getIdNum1() == patronID1)
                {
                    if(holdings[i].getRequestedBy1() != NULL)
                    {
                        if(holdings[i].getRequestedBy1()->getIdNum1() == patronID1)
                        {
                            holdings[i].setRequestedBy1(NULL);
                        }
                        else
                        {
                            cout <<endl<<"Book is onhold by another patron"<<endl;
                            return;
                        }
                    }
                    holdings[i].setCheckedOutBy1(&members[y]);
                    holdings[i].setDateCheckedOut1(currentDate1);
                    holdings[i].setLocation1(CHECKEDOUT);
                    members[y].addBook1(&holdings[i]);
                    cout <<endl<< "The book " << holdings[i].getTitle1() << " has been checked "<<endl;
                    cout << "out to " << members[y].getName1() << endl;
                    return;
                }
            }
        }
    }
}
void MyLibrary::returnBook1(string bookID1)
{
    int counts = 0;
    for(int i = 0; i < holdings.size(); i++)
    {
        if(holdings[i].getIdCode1() == bookID1)
        {
            counts++;
        }
    }
    if(counts == 0)
    {
        cout <<endl<<"That book is not in the library"<<endl;
        return;
    }
    else
        counts = 0;
    for(int i = 0; i < holdings.size(); i++)
    {
        if(holdings[i].getIdCode1() == bookID1)
        {
            if(holdings[i].getLocation1() == CHECKEDOUT)
            {
                holdings[i].getCheckedOutBy1()->removeBook1(&holdings[i]);
                holdings[i].setCheckedOutBy1(NULL);
                if(holdings[i].getRequestedBy1() == NULL)
                {
                    holdings[i].setLocation1(ONSHELF);
                }
                else
                {
                    holdings[i].setLocation1(ONHOLDSHELF);
                }
                cout <<endl<< "Book " << holdings[i].getTitle1() << " has been returned"<<endl;
                return;
            }
            else
            {
                cout <<endl<< "Book isn't checkedout"<<endl;
                return;
            }
        }
    }
}
void MyLibrary::requestBook1(string patronID1, string bookID1)
{
    int counts = 0;
    for(int i = 0; i < members.size(); i++)
    {
        if(members[i].getIdNum1() == patronID1)
        {
            counts++;
        }
    }
    if(counts == 0)
    {
        cout <<endl<< "Patron doesn't exist"<<endl;
        return;
    }
    else
        counts = 0;
    for(int i = 0; i < holdings.size(); i++)
    {
        if(holdings[i].getIdCode1() == bookID1)
        {
            counts++;
        }
    }
    if(counts == 0)
    {
        cout <<endl<< "Book doesn't exist"<<endl;
        return;
    }
    else
        counts = 0;
    for(int i = 0; i < holdings.size(); i++)
    {
        if(holdings[i].getIdCode1() == bookID1)
        {
            if(holdings[i].getRequestedBy1() != NULL)
            {
                cout <<endl<< "Book " << holdings[i].getTitle1() << " is already requested";
                cout << " by " << holdings[i].getRequestedBy1()->getName1() << endl;
                return;
            }
            if(holdings[i].getCheckedOutBy1() != NULL)
            {
                if(holdings[i].getCheckedOutBy1()->getIdNum1() == patronID1)
                {
                    cout <<endl<< "Book " << holdings[i].getTitle1() << " is already checked";
                    cout << " out to " << holdings[i].getCheckedOutBy1()->getName1() << endl;
                    return;
                }
            }
            if(holdings[i].getRequestedBy1() == NULL)
            {
                for(int y = 0; y < members.size(); y++)
                {
                    if(members[y].getIdNum1() == patronID1)
                    {
                        holdings[i].setRequestedBy1(&members[y]);
                        if(holdings[i].getLocation1() == ONSHELF)
                        {
                            holdings[i].setLocation1(ONHOLDSHELF);
                        }
                        cout <<endl<< "Book " << holdings[i].getTitle1() << " has been requested";
                        cout << " by " << members[y].getName1() << endl;
                        return;
                    }
                }
            }
        }
    }
}
void MyLibrary::payFine1(string patronID1, double payment1)
{
    int counts = 0;
    for(int i = 0; i < members.size(); i++)
    {
        if(members[i].getIdNum1() == patronID1)
        {
            counts++;
        }
    }
    if(counts == 0)
    {
        cout <<endl<< "Patron doesn't exist"<<endl;
        return;
    }
    else
        counts = 0;
    for(int i = 0; i < members.size(); i++)
    {
        if(members[i].getIdNum1() == patronID1)
        {
            members[i].amendFine1(payment1);
            cout << "The fines for " << members[i].getName1();
            cout << fixed << setprecision(2);
            cout << " are now $" << members[i].getFineAmount1()<< " ";
        }
    }
}
void MyLibrary::incrementCurrentDate1()
{
    currentDate1++;
    for(int i = 0; i < members.size(); i++)
    {
        for(int y = 0; y < members[i].getCheckedOutBooks1().size(); y++)
        {
            if((currentDate1 - members[i].getCheckedOutBooks1()[y]->getDateCheckedOut1()) > MyBook::CHECKOUTLENGTH)
            {
                members[i].amendFine1(0.10);
            }
        }
    }
}
void MyLibrary::viewPatronInfo1(string patronID1)
{
    int counts = 0;
    for(int i = 0; i < members.size(); i++)
    {
        if(members[i].getIdNum1() == patronID1)
        {
            counts++;
        }
    }
    if(counts == 0)
    {
        cout <<endl<< "Patron doesn't exist"<<endl;
        return;
    }
    else
        counts = 0;
    for(int i = 0; i < members.size(); i++)
    {
        if(members[i].getIdNum1() == patronID1)
        {
            cout <<endl<< "Patron Details:"<<endl;
            cout << "------------"<<endl;
            cout << "ID: " << members[i].getIdNum1() << endl;
            cout << "Name: " << members[i].getName1() << endl;
            cout << fixed << setprecision(2);
            cout << "Current Fines: $" << members[i].getFineAmount1() << endl;
            cout << endl<<"Checked Out Books:"<<endl;
            if(members[i].getCheckedOutBooks1().size() == 0)
                cout << "none"<<endl;
            else
            {
                for(int y = 0; y < members[i].getCheckedOutBooks1().size(); y++)
                {
                    cout << members[i].getCheckedOutBooks1()[y]->getTitle1() << endl;
                }
            }
        }
    }
}
void MyLibrary::viewBookInfo1(string bookID1)
{
    int counts = 0;
    for(int i = 0; i < holdings.size(); i++)
    {
        if(holdings[i].getIdCode1() == bookID1)
        {
            counts++;
        }
    }
    if(counts == 0)
    {
        cout <<endl<< "Book does not exist"<<endl;
        return;
    }
    else
        counts = 0;
    for(int i = 0; i < holdings.size(); i++)
    {
        if(holdings[i].getIdCode1() == bookID1)
        {
            cout <<endl<< "Book Details:"<<endl;
            cout << "----------"<<endl;
            cout << "ID: " << holdings[i].getIdCode1() <<endl;
            cout << "Title: " << holdings[i].getTitle1() << endl;
            cout << "Author: " << holdings[i].getAuthor1() << endl;
            cout << "Location: ";
            switch(holdings[i].getLocation1())
            {
            case 0:
                cout << "on-shelf"<<endl;
                break;
            case 1:
                cout << "on-hold-shelf"<<endl;
                break;
            case 2:
                cout << "checkedout"<<endl;
                break;
            }
            cout << "RequestedBy: ";
            if(holdings[i].getRequestedBy1() != NULL)
            {
                cout << holdings[i].getRequestedBy1()->getName1() << endl;
            }
            else
                cout << "none"<<endl;
                cout << "CheckedOut By: ";
            if(holdings[i].getCheckedOutBy1() != NULL)
            {
                cout << holdings[i].getCheckedOutBy1()->getName1() << endl;
            }
            else
                cout << "none"<<endl;
            cout << "No. of Days Left Till Due: ";
            if((currentDate1 - holdings[i].getDateCheckedOut1()) > MyBook::CHECKOUTLENGTH)
            {
                cout << "overdue"<<endl;
            }
            else
                cout << (MyBook::CHECKOUTLENGTH - (currentDate1 - holdings[i].getDateCheckedOut1()));
                cout << endl;
        }
    }
}

/* MyPatron.hpp */
#ifndef PATRONHPP
#define PATRONHPP
#include <string>
#include <vector>
using namespace std;
class MyBook;
class MyPatron
{
    private:
        string idNum1;
        string name1;
        vector<MyBook*> checkedOutBooks1;
        double fineAmount1;
    public:
        MyPatron();
        MyPatron(string idn, string n);
        string getIdNum1();
        string getName1();
        vector<MyBook*> getCheckedOutBooks1();
        void addBook1(MyBook* b);
        void removeBook1(MyBook* b);
        double getFineAmount1();
        void amendFine1(double amount);
};
#endif

/* MyPatron.cpp */
#include "MyPatron.hpp"
#include "MyBook.hpp"
using namespace std;
static int ninc = 1;
MyPatron::MyPatron()
{
    ninc++;
    idNum1 = "N" + ninc;
    name1 = "";
    fineAmount1 = 0.00;
}
MyPatron::MyPatron(string idn, string n)
{
    ninc++;
    idNum1 = idn;
    name1 = n;
    fineAmount1 = 0.00;
}
string MyPatron::getIdNum1()
{
    return idNum1;
}
string MyPatron::getName1()
{
    return name1;
}
vector<MyBook*> MyPatron::getCheckedOutBooks1()
{
    return checkedOutBooks1;
}
void MyPatron::addBook1(MyBook* b)
{
    checkedOutBooks1.push_back(b);
}
void MyPatron::removeBook1(MyBook* b)
{
    for(int i = 0; i < checkedOutBooks1.size(); i++)
    {
        if(checkedOutBooks1[i]->getIdCode1() == b->getIdCode1())
            checkedOutBooks1.erase(checkedOutBooks1.begin() + i);
    }
}
double MyPatron::getFineAmount1()
{
    return fineAmount1;
}
void MyPatron::amendFine1(double amount)
{
        fineAmount1 += amount;
}

/* Menu.cpp */
#include <iostream>
#include "MyBook.hpp"
#include "MyPatron.hpp"
#include "MyLibrary.hpp"
using namespace std;
int main()
{
    int options = 0;
    string patronID1, bookID1;
    double payment1;
    MyLibrary puyallup1;
    cout <<endl<< "Welcome to Library!"<<endl;
    while(options != 10)
    {
        cout <<endl<< "Choose options:"<<endl;
        cout << "-----------------"<<endl;
        cout << "1. Add book 2 Inventory"<<endl;
        cout << "2. Add new member 2 library"<<endl;
        cout << "3. Checkout book"<<endl;
        cout << "4. Return book"<<endl;
        cout << "5. Request book"<<endl;
        cout << "6. Payfine"<<endl;
        cout << "7. Increment currentDate"<<endl;
        cout << "8. View patron details"<<endl;
        cout << "9. View book info ";
        cout << "10. Quit"<<endl;
        cout <<endl<< "Selection: ";
        cin >> options;
        cin.ignore();
        switch(options)
        {
            case 1:
                puyallup1.addBook1();
                break;
            case 2:
                puyallup1.addMember1();
                break;
            case 3:
                cout <<endl<< "CheckOut a Book"<<endl;
                cout << "----------------"<<endl;
                cout << "Enter Patron-ID: ";
                getline(cin, patronID1);
                cout << "Enter Book-ID: ";
                getline(cin, bookID1);
                puyallup1.checkOutBook1(patronID1, bookID1);
                break;
            case 4:
                cout << "Return Book"<<endl;
                cout << "-------------"<<endl;
                cout << "Enter Book-ID: ";
                getline(cin, bookID1);
                puyallup1.returnBook1(bookID1);
                break;
            case 5:
                cout <<endl<< "Request a Book"<<endl;
                cout << "--------------"<<endl;
                cout << "Enter Patron-ID: ";
                getline(cin, patronID1);
                cout << "Enter Book-ID: ";
                getline(cin, bookID1);
                puyallup1.requestBook1(patronID1, bookID1);
                break;
            case 6:
                cout <<endl<< "Pay Fine"<<endl;
                cout << "--------"<<endl;
                cout << "Enter Patron-ID: ";
                getline(cin, patronID1);
                cout << "Enter Payment Amount: ";
                cin >> payment1;
                puyallup1.payFine1(patronID1, payment1);
                break;
            case 7:
                cout <<endl<<"Date incremented by 1-day"<<endl;
                puyallup1.incrementCurrentDate1();
                break;
            case 8:
                cout <<endl<< "View Patron Details"<<endl;
                cout << "----------------"<<endl;
                cout << "Enter Patron-ID: ";
                getline(cin, patronID1);
                puyallup1.viewPatronInfo1(patronID1);
                break;
            case 9:
                cout <<endl<< "View Book Details"<<endl;
                cout << "--------------"<<endl;
                cout << "Enter Book-ID: ";
                getline(cin, bookID1);
                puyallup1.viewBookInfo1(bookID1);
                break;
        }
    }
    cout << endl<<endl<<"U have selected 2 quit, plz click enter!"<<endl;
    cin.ignore();
    cin.get();
    return 0;
}

Output

Welcome to Library!

Choose options:

-----------------

1. Add book 2 Inventory

2. Add new member 2 library

3. Checkout book

4. Return book

5. Request book

6. Payfine

7. Increment currentDate

8. View patron details

9. View book details

10. Quit

Selection: 1

New Book

--------

Enter ID-Code: 01

Enter Title: A

Enter Author: Ami

Choose options:

-----------------

1. Add book 2 Inventory

2. Add new member 2 library

3. Checkout book

4. Return book

5. Request book

6. Payfine

7. Increment currentDate

8. View patron details

9. View book details

10. Quit

Selection: 2

New Patron

----------

Enter ID-Number: 001

Enter Name: Amith

Choose options:

-----------------

1. Add book 2 Inventory

2. Add new member 2 library

3. Checkout book

4. Return book

5. Request book

6. Payfine

7. Increment currentDate

8. View patron details

9. View book details

10. Quit

Selection: 5

Request a Book

--------------

Enter Patron-ID: 001

Enter Book-ID: 01

Book A has been requested by Amith

Choose options:

-----------------

1. Add book 2 Inventory

2. Add new member 2 library

3. Checkout book

4. Return book

5. Request book

6. Payfine

7. Increment currentDate

8. View patron details

9. View book details

10. Quit

Selection: