You will be writing a Library simulator. It will have three classes: Book, Patro
ID: 3676530 • Letter: Y
Question
You will be writing a Library simulator. It will have three classes: Book, Patron and Library. To make things a little simpler for you, I am supplying you with the three .hpp files. You will write the three implementation files. You may add one-line "inline function" implementations to the .hpp files if you want, but implementation of the other functions should go in the corresponding .cpp files. Please put your function comments in the .cpp files (they would clutter up the .hpp files).
A couple of notes:
The vector::erase() function lets you delete an element of a vector, shifting over all the elements after it.
You'll see in Book.hpp a line that says "class Patron;". That is a forward declaration. It doesn't say anything about the definition of the Paton class, but it promises the compiler that there will be a type named Patron. The reason we don't just say "#include Patron.hpp" is that both Book and Patron need to know about each other, but they can't both #include the other because that would create a cyclic dependency.
#ifndef BOOK_HPP
#define BOOK_HPP
#include <string>
class Patron;
// These three locations are mutually exclusive, but note that
// a Book can be on request for a Patron while being checked
// out to another Patron. In that case the Book's location is
// CHECKED_OUT, and when it is returned, it's location will
// become ON_HOLD_SHELF.
enum Locale {ON_SHELF, ON_HOLD_SHELF, CHECKED_OUT};
class Book
{
private:
std::string idCode;
std::string title;
std::string author;
Locale location;
Patron* checkedOutBy;
Patron* requestedBy;
int dateCheckedOut;
public:
static const int CHECK_OUT_LENGTH = 21;
Book(std::string idc, std::string t, std::string a);
int getCheckOutLength();
std::string getIdCode();
std::string getTitle();
std::string getAuthor();
Locale getLocation();
void setLocation(Locale);
Patron* getCheckedOutBy();
void setCheckedOutBy(Patron*);
Patron* getRequestedBy();
void setRequestedBy(Patron*);
int getDateCheckedOut();
void setDateCheckedOut(int);
};
#endif
//Patron.hpp
#ifndef PATRON_HPP
#define PATRON_HPP
#include <string>
#include <vector>
#include "Book.hpp"
//class LibraryItem;
class Patron
{
private:
std::string idNum;
std::string name;
std::vector<Book*> checkedOutBooks;
double fineAmount;
public:
Patron(std::string idn, std::string n);
std::string getIdNum();
std::string getName();
std::vector<Book*> getCheckedOutBooks();
void addBook(Book* b);
void removeBook(Book* b);
double getFineAmount();
void amendFine(double amount);
};
#endif
//Library.hpp
#ifndef LIBRARY_HPP
#define LIBRARY_HPP
#include <string>
#include <vector>
#include "Patron.hpp"
class Library
{
private:
std::vector<Book*> holdings;
std::vector<Patron*> members;
int currentDate;
public:
Library();
void addBook(Book*);
void addPatron(Patron*);
std::string checkOutBook(std::string pID, std::string bID);
std::string returnBook(std::string bID);
std::string requestBook(std::string pID, std::string bID);
std::string payFine(std::string pID, double payment);
void incrementCurrentDate();
Patron* getPatron(std::string pID);
Book* getBook(std::string bID);
};
#endif
Here are descriptions of the three classes:
Book:
idCode - a unique identifier for a Book
title - cannot be assumed to be unique
author - the title and author don't need set methods, since they will never change after the object has been created, therefore these fields can be initialized directly within the constructor
location - a Book can be either on the shelf, on the hold shelf, or checked out
checkedOutBy - pointer to the Patron who has it checked out (if any)
requestedBy - pointer to the Patron who has requested it (if any); a Book can only be requested by one Patron at a time
dateCheckedOut - when a book is checked out, this will be set to the currentDate of the Library
CHECK_OUT_LENGTH - constant that gives how long a Book can be checked out for
constructor - takes an idCode, title and author; checkedOutBy and requestedBy should be initialized to NULL; a new Book should be on the shelf
some get and set methods
Patron:
idNum - a unique identifier for a Patron
name - cannot be assumed to be unique
checkedOutBooks - a vector of Books that Patron currently has checkedOut
fineAmount - how much the Patron owes the Library in late fines; this is allowed to go negative
a constructor that takes an idNum and name
some get and set methods
addBook - adds the specified Book to checkedOutBooks
removeBook - removes the specified Book from checkedOutBooks
amendFine - a positive argument increases the fineAmount, a negative one decreases it
Library:
holdings - a vector of pointers to Books in the Library
members - a vector of pointers to Patrons in the Library
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 - adds the parameter to holdings
addPatron - adds the parameter to members
getBook - returns a pointer to the Book corresponding to the ID parameter, or NULL if no such Book is in the holdings
getPatron - returns a pointer to the Patron corresponding to the ID parameter, or NULL if no such Patron is a member
checkOutBook
if the specified Book is not in the Library, return "book not found"
if the specified Patron is not in the Library, return "patron not found"
if the specified Book is already checked out, return "book already checked out"
if the specified Book is on hold by another Patron, return "book on hold by other patron"
otherwise update the Book's checkedOutBy, dateCheckedOut and Location; if the Book was on hold for this Patron, update requestedBy; update the Patron's checkedOutBooks; return "check out successful"
returnBook
if the specified Book is not in the Library, return "book not found"
if the Book is not checked out, return "book already in library"
update the Patron's checkedOutBooks; update the Book's location depending on whether another Patron has requested it; update the Book's checkedOutBy; return "return successful"
requestBook
if the specified Book is not in the Library, return "book not found"
if the specified Patron is not in the Library, return "patron not found"
if the specified Book is already requested by another Patron, return "book on hold by other patron"
update the Book's requestedBy; if the Book is on the shelf, update its location to on hold; return "request successful"
payFine
if the specified Patron is not in the Library, return "patron not found"
use amendFine to update the Patron's fine; return "payment successful"
incrementCurrentDate
increment current date; increase each Patron's fines by 10 cents for each overdue Book they have checked out (using amendFine)
be careful - a Book can be on request without its location being the hold shelf (if another Patron has it checked out);
In the main method you use for testing, you should only need to #include Library.hpp. Remember that your compile command needs to list all four .cpp files.
Just to think about: There are six possible changes in the location of a Book. Can all six occur?
Explanation / Answer
main.cpp
/**********************************************************
** Description: This file tests the implementations of the
** Book, Patron, and Library classes.
**********************************************************/
#include "Library.hpp"
#include <iostream>
#include <vector>
using std::cin;
using std::cout;
using std::endl;
using std::vector;
int main() {
Book b1("123", "War and Peace", "Tolstoy");
Book b2("234", "Moby Dick", "Melville");
Book b3("345", "Phantom Tollbooth", "Juster");
Book b4("666", "Sam City", "Hell");
cout << "A book's check out length should be 21: "
<< b1.getCheckOutLength() << endl;
cout << "b1's ID Code should be 123: " << b1.getIdCode() << endl;
cout << "b2's title should be Moby Dick: " << b2.getTitle() << endl;
cout << "b3's author should be Juster: " << b3.getAuthor() << endl;
cout << "All three books should be on the shelf." << endl;
if (b1.getLocation() != ON_SHELF) {
cout << "ERROR: b1 is off the shelf" << endl;
}
if (b2.getLocation() != ON_SHELF) {
cout << "ERROR: b2 is off the shelf" << endl;
}
if (b3.getLocation() != ON_SHELF) {
cout << "ERROR: b3 is off the shelf" << endl;
}
Patron p1("abc", "Felicity");
Patron p2("bcd", "Waldo");
Library lib;
lib.addBook(&b1);
lib.addBook(&b2);
lib.addBook(&b3);
lib.addPatron(&p1);
lib.addPatron(&p2);
lib.checkOutBook("bcd", "234");
cout << "Now b2 should be checked out by p2." << endl;
if (b2.getLocation() != CHECKED_OUT) {
cout << "ERROR: b2 is not checked out" << endl;
if (b2.getLocation() == ON_SHELF) {
cout << "b2 is on the shelf" << endl;
} else {
cout << "b2 is on hold" << endl;
}
}
if (b2.getCheckedOutBy() != &p2) {
cout << "ERROR: p2 is not checking out this book" << endl;
if (b2.getCheckedOutBy() == NULL) {
cout << "ERROR: nobody is checking out this book" << endl;
}
}
cout << "p1's total fine amount should be $0.00: "
<< p1.getFineAmount() << endl;
cout << "p2's total fine amount should be $0.00: "
<< p2.getFineAmount() << endl;
for (int i=0; i<7; i++)
lib.incrementCurrentDate();
cout << "It is now day 7." << endl;
cout << "p1's total fine amount should be $0.00: "
<< p1.getFineAmount() << endl;
cout << "p2's total fine amount should be $0.00: "
<< p2.getFineAmount() << endl;
cout << "p2 should have 1 book checked out." << endl;
if (p2.getCheckedOutBooks().size() != 1) {
cout << "ERROR: p2 has" << p2.getCheckedOutBooks().size() << endl;
}
lib.checkOutBook("bcd", "123");
cout << "Now b1 should be checked out by p2." << endl;
if (b1.getLocation() != CHECKED_OUT) {
cout << "ERROR: b1 is not checked out" << endl;
if (b1.getLocation() == ON_SHELF) {
cout << "b1 is on the shelf" << endl;
} else {
cout << "b1 is on hold" << endl;
}
}
if (b1.getCheckedOutBy() != &p2) {
cout << "ERROR: p2 is not checking out this book" << endl;
if (b1.getCheckedOutBy() == NULL) {
cout << "ERROR: nobody is checking out this book" << endl;
}
}
cout << "p1's total fine amount should be $0.00: "
<< p1.getFineAmount() << endl;
cout << "p2's total fine amount should be $0.00: "
<< p2.getFineAmount() << endl;
cout << "p2 should have 2 books checked out." << endl;
if (p2.getCheckedOutBooks().size() != 2) {
cout << "ERROR: p2 has" << p2.getCheckedOutBooks().size() << endl;
}
lib.checkOutBook("abc", "345");
cout << "Now b3 should be checked out by p1." << endl;
cout << "p1 should have 1 book checked out." << endl;
if (p1.getCheckedOutBooks().size() != 1) {
cout << "ERROR: p1 has" << p1.getCheckedOutBooks().size() << endl;
}
for (int i=0; i<24; i++)
lib.incrementCurrentDate();
cout << "It is now day 31." << endl;
cout << "p1's total fine amount should be $0.30: "
<< p1.getFineAmount() << endl;
cout << "p2's total fine amount should be $1.30: "
<< p2.getFineAmount() << endl;
lib.payFine("bcd", 0.4);
cout << "p2 just paid $0.40 in fines" << endl;
cout << "p1's total fine amount should be $0.30: "
<< p1.getFineAmount() << endl;
cout << "p2's total fine amount should be $0.90: "
<< p2.getFineAmount() << endl;
cout << "p2 wants to return b2." << endl;
cout << lib.returnBook("234") << endl;
cout << "p2 should have 1 book checked out." << endl;
if (p2.getCheckedOutBooks().size() != 1) {
cout << "ERROR: p2 has" << p2.getCheckedOutBooks().size() << endl;
}
cout << "p2 wants to return b2 again." << endl;
cout << lib.returnBook("234") << endl;
cout << "p2 should have 1 book checked out." << endl;
if (p2.getCheckedOutBooks().size() != 1) {
cout << "ERROR: p2 has" << p2.getCheckedOutBooks().size() << endl;
}
cout << "p1 wants to request b1 but it is checked out." << endl;
cout << lib.requestBook("abc", "123") << endl;
if (b1.getLocation() != CHECKED_OUT) {
cout << "ERROR: b1 is not checked out" << endl;
if (b1.getLocation() == ON_SHELF) {
cout << "b1 is on the shelf" << endl;
} else {
cout << "b1 is on hold" << endl;
}
}
if (b1.getCheckedOutBy() != &p2) {
cout << "ERROR: p2 is not checking out this book" << endl;
if (b1.getCheckedOutBy() == NULL) {
cout << "ERROR: nobody is checking out this book" << endl;
}
}
cout << "p1 wants to request b2." << endl;
cout << lib.requestBook("abc", "234") << endl;
if (b2.getLocation() != ON_HOLD_SHELF) {
cout << "ERROR: b2 is not on hold" << endl;
if (b2.getLocation() == CHECKED_OUT) {
cout << "b2 is checked out" << endl;
} else {
cout << "b2 is on the shelf" << endl;
}
}
if (b2.getCheckedOutBy() != NULL) {
cout << "ERROR: someone is checking out this book" << endl;
}
if (b2.getRequestedBy() != &p1) {
cout << "ERROR: b2 is not requested by p1" << endl;
}
cout << "p2 wants to request b2." << endl;
cout << lib.requestBook("bcd", "234") << endl;
cout << "Testing getPatron()." << endl;
if (&p1 != lib.getPatron("abc")) {
cout << "ERROR: cannot find patron abc" << endl;
}
if (lib.getPatron("test") != NULL) {
cout << "ERROR: patron test found when not there" << endl;
}
cout << "Testing getBook()." << endl;
if (&b2 != lib.getBook("234")) {
cout << "ERROR: cannot find book 234" << endl;
}
if (lib.getBook("test") != NULL) {
cout << "ERROR: book test found when not there" << endl;
}
cout << "Attempting to return a book that isn't checked out." << endl;
cout << lib.returnBook("234") << endl;
cout << "Attempting to check out a book not in holdings." << endl;
cout << lib.checkOutBook("abc", "456") << endl;
return 0;
}
Book.cpp
/**********************************************************
** Description: This file contains the Book class function
** implementation.
**********************************************************/
#include "Book.hpp"
using std::string;
/*********************************************************************
** Description: Constructs a Book given an ID code, a title, and an
** author name.
*********************************************************************/
Book::Book(string idc, string t, string a) {
idCode = idc;
title = t;
author = a;
// Book starts out neither checked out
// nor requested by a Patron
setLocation(ON_SHELF);
setCheckedOutBy(NULL);
setRequestedBy(NULL);
setDateCheckedOut(0);
}
/*********************************************************************
** Description: Returns the maximum number of days this Book may be
** checked out by a Patron.
*********************************************************************/
int Book::getCheckOutLength() {
return CHECK_OUT_LENGTH;
}
/*********************************************************************
** Description: Returns the ID code.
*********************************************************************/
string Book::getIdCode() {
return idCode;
}
/*********************************************************************
** Description: Returns the title.
*********************************************************************/
string Book::getTitle() {
return title;
}
/*********************************************************************
** Description: Returns the author name.
*********************************************************************/
string Book::getAuthor() {
return author;
}
/*********************************************************************
** Description: Returns ON_SHELF, ON_HOLD_SHELF, or CHECKED_OUT
** depending on the current status of this Book.
*********************************************************************/
Locale Book::getLocation() {
return location;
}
/*********************************************************************
** Description: Updates the current status of this Book.
*********************************************************************/
void Book::setLocation(Locale loc) {
location = loc;
}
/*********************************************************************
** Description: Returns a pointer to the Patron currently checking
** out this Book. Returns NULL if not checked out.
*********************************************************************/
Patron* Book::getCheckedOutBy() {
return checkedOutBy;
}
/*********************************************************************
** Description: Updates the Patron currently checking out this Book
** via a pointer.
*********************************************************************/
void Book::setCheckedOutBy(Patron *pat) {
checkedOutBy = pat;
}
/*********************************************************************
** Description: Returns a pointer to the Patron currently requesting
** this Book. Returns NULL if not requested.
*********************************************************************/
Patron* Book::getRequestedBy() {
return requestedBy;
}
/*********************************************************************
** Description: Updates the Patron currently requesting this Book
** via a pointer.
*********************************************************************/
void Book::setRequestedBy(Patron *pat) {
requestedBy = pat;
}
/*********************************************************************
** Description: Returns the date the book was checked out in the form
** of days since the Library was created.
*********************************************************************/
int Book::getDateCheckedOut() {
return dateCheckedOut;
}
/*********************************************************************
** Description: Updates the date the book was checked out in the form
** of days since the Library was created.
*********************************************************************/
void Book::setDateCheckedOut(int date) {
dateCheckedOut = date;
}
Book.hpp
#ifndef BOOK_HPP
#define BOOK_HPP
#include <string>
class Patron;
// These three locations are mutually exclusive, but note that
// a Book can be on request for a Patron while being checked
// out to another Patron. In that case the Book's location is
// CHECKED_OUT, and when it is returned, it's location will
// become ON_HOLD_SHELF.
enum Locale {ON_SHELF, ON_HOLD_SHELF, CHECKED_OUT};
class Book
{
private:
std::string idCode;
std::string title;
std::string author;
Locale location;
Patron* checkedOutBy;
Patron* requestedBy;
int dateCheckedOut;
public:
static const int CHECK_OUT_LENGTH = 21;
Book(std::string idc, std::string t, std::string a);
int getCheckOutLength();
std::string getIdCode();
std::string getTitle();
std::string getAuthor();
Locale getLocation();
void setLocation(Locale);
Patron* getCheckedOutBy();
void setCheckedOutBy(Patron*);
Patron* getRequestedBy();
void setRequestedBy(Patron*);
int getDateCheckedOut();
void setDateCheckedOut(int);
};
#endif
Patron.cpp
/**********************************************************
** Description: This file contains the Patron class
** function implementation.
**********************************************************/
#include "Patron.hpp"
using std::string;
using std::vector;
/*********************************************************************
** Description: Constructs a Patron given an ID number and a name.
*********************************************************************/
Patron::Patron(string idn, string n) {
idNum = idn;
name = n;
fineAmount = 0.0;
// vector already declared
}
/*********************************************************************
** Description: Returns the ID number.
*********************************************************************/
string Patron::getIdNum() {
return idNum;
}
/*********************************************************************
** Description: Returns the name.
*********************************************************************/
string Patron::getName() {
return name;
}
/*********************************************************************
** Description: Returns the list of books this Patron currently has
** checked out in the form of a vector of pointers to
** Books.
*********************************************************************/
vector<Book*> Patron::getCheckedOutBooks() {
return checkedOutBooks;
}
/*********************************************************************
** Description: Appends a pointer to a Book to the list of this
** Patron's checked out books.
*********************************************************************/
void Patron::addBook(Book *b) {
checkedOutBooks.push_back(b);
}
/*********************************************************************
** Description: Removes a book from the list of books checked out
** given a pointer to a Book.
*********************************************************************/
void Patron::removeBook(Book *b) {
// length of list of books
int numCheckedOutBooks = checkedOutBooks.size();
// search for the book passed as a parameter in the list of
// checked out books and remove it when it is found
for (int i = 0; i < numCheckedOutBooks; i++) {
Book *curBook = checkedOutBooks.at(i);
if (b == curBook) {
checkedOutBooks.erase(checkedOutBooks.begin() + i);
return;
}
}
}
/*********************************************************************
** Description: Returns the current total dollar amount this Patron
** owes the Library in fines.
*********************************************************************/
double Patron::getFineAmount() {
return fineAmount;
}
/*********************************************************************
** Description: Adds the given dollar amount to the total fines this
** Patron owes the Library.
*********************************************************************/
void Patron::amendFine(double amount) {
fineAmount += amount;
}
Patron.hpp
#ifndef PATRON_HPP
#define PATRON_HPP
#include <string>
#include <vector>
#include "Book.hpp"
//class LibraryItem;
class Patron
{
private:
std::string idNum;
std::string name;
std::vector<Book*> checkedOutBooks;
double fineAmount;
public:
Patron(std::string idn, std::string n);
std::string getIdNum();
std::string getName();
std::vector<Book*> getCheckedOutBooks();
void addBook(Book* b);
void removeBook(Book* b);
double getFineAmount();
void amendFine(double amount);
};
#endif
library.cpp
#include "Library.hpp"
using std::string;
using std::vector;
/*********************************************************************
** Description: Constructs a library and sets the current date to 0.
*********************************************************************/
Library::Library() {
currentDate = 0;
}
/*********************************************************************
** Description: Adds a Book to this Library's holdings via a pointer.
*********************************************************************/
void Library::addBook(Book *b) {
holdings.push_back(b);
}
/*********************************************************************
** Description: Adds a Patron to this Library's members via a pointer.
*********************************************************************/
void Library::addPatron(Patron *pat) {
members.push_back(pat);
}
/*********************************************************************
** Description: Allows a Patron to check out a Book from this Library
** given that certain conditions are met. Returns a
** string indicating the status of the check out attempt.
*********************************************************************/
string Library::checkOutBook(string pID, string bID) {
Book *book = getBook(bID); // Book desired
Patron *patron = getPatron(pID); // Patron seeking book
// if Book is not in holdings, do not allow check out
if (!book) {
return "book not found";
}
// if Patron is not a member, do not allow check out
if (!patron) {
return "patron not found";
}
// determine Book's status
Locale status = book->getLocation();
if (status == CHECKED_OUT) {
return "book already checked out";
}
// if this Patron wants to check out a Book someone else
// has on hold, do not allow check out
if (status == ON_HOLD_SHELF) {
if (book->getRequestedBy() != patron) {
return "book on hold by other patron";
} else {
// otherwise this Patron is allowed to check out the Book,
// so no one is now requesting it
book->setRequestedBy(NULL);
}
}
// update Book's status and other info
book->setCheckedOutBy(patron);
book->setDateCheckedOut(currentDate);
book->setLocation(CHECKED_OUT);
// add the Book to the list of Books checked out by this
// Patron
patron->addBook(book);
return "check out successful";
}
/*********************************************************************
** Description: Allows a Patron to return a Book to this Library.
** Returns a string indicating the status of the return
** attempt.
*********************************************************************/
string Library::returnBook(string bID) {
Book *book = getBook(bID); // Book to return
// if Book is not part of Library's holdings, do not accept return
if (!book) {
return "book not found";
}
// determine Book's status
Locale status = book->getLocation();
// only return the Book if it is currently checked out
if (status != CHECKED_OUT) {
return "book already in library";
}
// figure out who is returning the Book
Patron *patron = book->getCheckedOutBy();
// remove it from their list of checked out Books
patron->removeBook(book);
// if it was requested by someone else, put it on the hold shelf
// otherwise put it on the shelf
if (book->getRequestedBy()) {
book->setLocation(ON_HOLD_SHELF);
} else {
book->setLocation(ON_SHELF);
}
// no one is currently checking the book out now
book->setCheckedOutBy(NULL);
return "return successful";
}
/*********************************************************************
** Description: Allows a Patron to request a Book from this Library
** given that certain conditions are met. Returns a
** string indicating the status of the request attempt.
*********************************************************************/
string Library::requestBook(string pID, string bID) {
Book *book = getBook(bID); // Book desired
Patron *patron = getPatron(pID); // Patron seeking Book
// if Book is not part of Library's holdings, do not allow request
if (!book) {
return "book not found";
}
// if Patron is not a member, do not allow request
if (!patron) {
return "patron not found";
}
// determine Book's status
Locale status = book->getLocation();
// if Book is on hold, check to see if it's by this Patron
// if not, do not allow request
if (status == ON_HOLD_SHELF) {
if (book->getRequestedBy() != patron) {
return "book on hold by other patron";
}
}
// put the book on hold if it wasn't already
if (status == ON_SHELF) {
book->setLocation(ON_HOLD_SHELF);
}
book->setRequestedBy(patron);
return "request successful";
}
/*********************************************************************
** Description: Attempts to pay this Library a fine for the Patron
** with the given ID number and in the given dollar
** amount. The Patron's amount owed is updated. Returns
** a string indicating whether the transaction was
** successful.
*********************************************************************/
// takes amount being paid, not negative of that amount
string Library::payFine(string pID, double payment) {
Patron *patron = getPatron(pID);
// if Patron is not a Library member, does not pay fine
if (!patron) {
return "patron not found";
}
patron->amendFine(-payment);
return "payment successful";
}
/*********************************************************************
** Description: Increments the current date. Also adds 10 cents per
** overdue book to every Patron member's debt.
*********************************************************************/
void Library::incrementCurrentDate() {
// increment the current date
++currentDate;
// number of Books in the Library's holdings
int numHoldings = holdings.size();
// if the book is overdue, charge the Patron who has it checked out
// 10 cents
for (int i = 0; i < numHoldings; i++) {
Book *book = holdings.at(i);
int checkOutLength = book->getCheckOutLength();
int dateCheckedOut = book->getDateCheckedOut();
int dueDate = dateCheckedOut + checkOutLength;
int daysOverdue = currentDate - dueDate;
if (daysOverdue > 0) {
Patron *patron = book->getCheckedOutBy();
if (patron != NULL) {
patron->amendFine(0.10);
}
}
}
}
/*********************************************************************
** Description: Returns a pointer to the Patron with the given ID
** number. Returns NULL if the ID cannot be identified.
*********************************************************************/
Patron* Library::getPatron(string pID) {
int numMembers = members.size();
// search through all the members
for (int i = 0; i < numMembers; i++) {
string curID = members.at(i)->getIdNum();
// check for equality of strings
if (pID == curID) {
return members.at(i);
}
}
// given ID did not match a Patron member
return NULL;
}
/*********************************************************************
** Description: Returns a pointer to the Book with the given ID code.
** Returns NULL if the ID does not match a Book.
*********************************************************************/
Book* Library::getBook(string bID) {
int numHoldings = holdings.size();
// search through the Book holdings
for (int i = 0; i < numHoldings; i++) {
string curID = holdings.at(i)->getIdCode();
// check for equality of strings
if (bID == curID) {
return holdings.at(i);
}
}
// given ID did not match a Book in the holdings
return NULL;
}
library.hpp
#ifndef LIBRARY_HPP
#define LIBRARY_HPP
#include <string>
#include <vector>
#include "Patron.hpp"
class Library
{
private:
std::vector<Book*> holdings;
std::vector<Patron*> members;
int currentDate;
public:
Library();
void addBook(Book*);
void addPatron(Patron*);
std::string checkOutBook(std::string pID, std::string bID);
std::string returnBook(std::string bID);
std::string requestBook(std::string pID, std::string bID);
std::string payFine(std::string pID, double payment);
void incrementCurrentDate();
Patron* getPatron(std::string pID);
Book* getBook(std::string bID);
};
#endif
sample output
A book's check out length should be 21: 21
b1's ID Code should be 123: 123
b2's title should be Moby Dick: Moby Dick
b3's author should be Juster: Juster
All three books should be on the shelf.
Now b2 should be checked out by p2.
p1's total fine amount should be $0.00: 0
p2's total fine amount should be $0.00: 0
It is now day 7.
p1's total fine amount should be $0.00: 0
p2's total fine amount should be $0.00: 0
p2 should have 1 book checked out.
Now b1 should be checked out by p2.
p1's total fine amount should be $0.00: 0
p2's total fine amount should be $0.00: 0
p2 should have 2 books checked out.
Now b3 should be checked out by p1.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.