Write a portion of a class called Library. To be called Library.cpp and will inc
ID: 644612 • Letter: W
Question
Write a portion of a class called Library. To be called Library.cpp and will include (#include "Library.hpp") Library.hpp which is included below.
Library:
*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
Here is the Library.hpp file:
Explanation / Answer
// File: Library.hpp
#ifndef LIBRARY_HPP
#define LIBRARY_HPP
#include <string>
#include <vector>
#include "Book.h"
#include "Patron.h"
class Patron;
class Book;
static const double DAILY_FINE = 0.1;
class Library
{
private:
std::vector<Book> holdings;
std::vector<Patron> members;
int currentDate;
Book* getBook(std::string idNum);
Patron* getPatron(std::string idNum);
public:
Library();
void addBook();
void addMember();
void checkOutBook(std::string patronID, std::string bookID);
void returnBook(std::string bookID);
void requestBook(std::string patronID, std::string bookID);
void incrementCurrentDate();
void payFine(std::string patronID, double payment);
void viewPatronInfo(std::string patronID);
void viewBookInfo(std::string bookID);
int getCurrentDate();
};
#endif
------------------------------------------------------------------------------------------------------------
// File: Library.cpp
#include "Library.h"
#include <iostream>
using namespace std;
#include <iomanip>
#include <iostream>
#include <string>
Library::Library()
{
holdings.reserve(100);
members.reserve(100);
currentDate = 0;
}
void Library::addBook()
{
string idCodeNew;
string titleNew;
string authorNew;
cin.ignore(1000, ' ');
cout << "Enter the book's idCode: ";
getline(cin, idCodeNew);
cout << "Enter the book's title: ";
getline(cin, titleNew);
cout << "Enter the book's author: ";
getline(cin, authorNew);
if(getBook(idCodeNew) != NULL)
{
cout << "This book is already exists!" << endl;
return;
}
Book tmpBook(idCodeNew, titleNew, authorNew);
holdings.push_back(tmpBook);
}
void Library::addMember()
{
cin.ignore(1000, ' ');
string newIdNumber;
string newName;
cout << "Enter the patron's ID number: ";
getline(cin, newIdNumber);
cout << "Enter the patron's ID name: ";
getline(cin, newName);
if(getPatron(newIdNumber) != NULL)
{
cout << "This patron is already exists!" << endl;
return;
}
Patron ptrn(newIdNumber, newName);
members.push_back(ptrn);
}
void Library::checkOutBook(string aPatronID, string aBookID)
{
Patron* ptrn = getPatron(aPatronID);
Book* bk = getBook(aBookID);
if(bk == NULL || ptrn == NULL)
{
if(bk == NULL)
{
cout << endl << "No such book in holdings." << endl;
}
if(ptrn == NULL)
{
cout << endl << "No such patron in records." << endl;
}
return;
}
if(bk->getLocation() == CHECKED_OUT)
{
cout << "This book is already checked out." << endl << endl;
}
else if(bk->getLocation() == ON_HOLD_SHELF)
{
if(bk->getRequestedBy()->getIdNum() == ptrn->getIdNum())
{
bk->setLocation(CHECKED_OUT);
bk->setCheckedOutBy(getPatron(aPatronID));
bk->setDateCheckedOut(currentDate);
bk->setRequestedBy(NULL);
ptrn->addBook(bk);
}
else
{
cout << "This book is on hold and can only be checked out to requestee." << endl <<endl;
}
}
else
{
bk->setLocation(CHECKED_OUT);
bk->setCheckedOutBy(ptrn);
bk->setDateCheckedOut(currentDate);
ptrn->addBook(bk);
}
}
int Library::getCurrentDate()
{
return currentDate;
}
Patron* Library::getPatron(string aIdNum)
{
for(int i = 0; i < members.size(); i++)
{
if(members.at(i).getIdNum() == aIdNum)
{
return &members.at(i);
}
}
return NULL;
}
Book* Library::getBook(string aIdNum)
{
for(int i = 0; i < holdings.size(); i++)
{
if(holdings.at(i).getIdCode() == aIdNum)
{
return &holdings.at(i);
}
}
return NULL;
}
void Library::incrementCurrentDate()
{
currentDate++;
for(int i = 0; i < members.size(); i++)
{
for(int j = 0; j < members.at(i).getCheckedOutBooks().size(); j++)
{
if((members.at(i).getCheckedOutBooks().at(j)->getDateCheckedOut() + 21) < currentDate)
{
members.at(i).amendFine(DAILY_FINE);
}
}
}
}
void Library::payFine(string aPatronID, double anAmound)
{
Patron* ptrn = getPatron(aPatronID);
if(ptrn == NULL)
{
cout << endl << "No such patron exists. No adjustment made." << endl;
return;
}
for(int i = 0; i < members.size(); i++)
{
if(members.at(i).getIdNum() == aPatronID)
{
members.at(i).amendFine(-(anAmound));
cout << "Fines for " << members.at(i).getName() << " are now $" <<
setprecision(2) << fixed << members.at(i).getFineAmount() << endl;
break;
}
}
}
void Library::requestBook(string aPatronID, string aBookID)
{
Book* bk = getBook(aBookID);
Patron* ptrn = getPatron(aPatronID);
if(bk == NULL || ptrn == NULL)
{
if(bk == NULL)
{
cout << endl << "No such book in holdings." << endl;
}
if(ptrn == NULL)
{
cout << endl << "No such patron in records." << endl;
}
return;
}
if(bk->getRequestedBy() == NULL)
{
bk->setRequestedBy(ptrn);
if(bk->getLocation() == ON_SHELF)
{
bk->setLocation(ON_HOLD_SHELF);
}
}
else
{
cout << "This book is already requested!";
}
}
void Library::returnBook(string aBookID)
{
Book* bk = getBook(aBookID);
if(bk == NULL)
{
cout << endl << "No such book in holdings." << endl << endl;
return;
}
if(bk->getLocation() == ON_SHELF)
{
cout << "This book is already on the shelf." << endl;
}
else if(bk->getLocation() == ON_HOLD_SHELF)
{
cout << "This book is on the hold shelf and cannot be returned." << endl;
}
else
{
Patron* ptrn = bk->getCheckedOutBy();
if(bk->getRequestedBy() == NULL)
{
bk->setLocation(ON_SHELF);
bk->setCheckedOutBy(NULL);
bk->setDateCheckedOut(-1);
ptrn->removeBook(bk);
}
else
{
bk->setLocation(ON_HOLD_SHELF);
bk->setCheckedOutBy(NULL);
bk->setDateCheckedOut(-1);
ptrn->removeBook(bk);
}
}
}
void Library::viewBookInfo(string aBookID)
{
Book* bk = getBook(aBookID);
if(bk == NULL)
{
cout << "No such holding exists." << endl;
}
else
{
cout << endl << "ID: " << bk->getIdCode() << endl;
cout << "Title: " << bk->getTitle() << endl;
cout << "Author: " << bk->getAuthor() << endl;
if(bk->getLocation() == ON_SHELF)
{
cout << "Location: ON SHELF" << endl;
}
else if(bk->getLocation() == ON_HOLD_SHELF)
{
cout << "Location: ON HOLD" << endl;
cout << "Requested By: " << bk->getRequestedBy()->getName() << endl;
cout << "Requested By ID: " << bk->getRequestedBy()->getIdNum() << endl;
}
else
{
cout << "Location: CHECKED OUT" << endl;
cout << "Checked out by: " << bk->getCheckedOutBy()->getName() << endl;
cout << "Checked out by ID: " << bk->getCheckedOutBy()->getIdNum() << endl;
cout << "Due date: " << bk->getDateCheckedOut() + 21 << endl;
if(bk->getRequestedBy() != NULL)
{
cout << "This book is also requested by patron: " << bk->getRequestedBy()->getIdNum()
<< endl;
cout << "It should be put on the hold shelf when checked back in." << endl;
}
}
}
}
void Library::viewPatronInfo(string aPatronID)
{
Patron* ptrn = getPatron(aPatronID);
if(ptrn == NULL)
{
cout << endl << "That patron does not exist!" << endl;
}
else
{
cout << endl << "Patron ID: " << ptrn->getIdNum() << endl;
cout << "Name: " << ptrn->getName() << endl;
cout << "Fines: $" << setprecision(2) << fixed << ptrn->getFineAmount() << endl;
if(ptrn->getCheckedOutBooks().size() == 0)
{
cout << "No checked out books." << endl << endl;
}
else
{
cout << "Checked out books:" << endl << endl;
for(int i = 0; i < ptrn->getCheckedOutBooks().size(); i++)
{
viewBookInfo(ptrn->getCheckedOutBooks().at(i)->getIdCode());
}
cout << endl << endl;
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.