#include "stdstuff.h" #include "LibraryCard.h" // constructs a LibraryCard object (for a patron). The patron is allowed up to 20 books and currently has none borrowed. LibraryCard::LibraryCard() { maxBooks = 20; numBooks = 0; // no books out } // constructs a LibraryCard object (for a patron). The patron is allowed to borrow up to the given number of books, and // currently has none signed out. // Throws invalid_argument exception if argument is less than or equal to 0. LibraryCard::LibraryCard(int maxBooks) { if (maxBooks <= 0) { // **must** initialize fields for full marks this->maxBooks = 20; numBooks = 0; // no books out // I chose to use the default -- doesn't matter as long as they are initialized to something reasonabls throw invalid_argument("LibraryCard::LibraryCard - maxBooks must be greater than 0"); } this->maxBooks = maxBooks; numBooks = 0; // no books out } // numBooks books are signed out on the given library card. // if numBooks will cause the patron to have more than the maximum number of books, this method has no effect. // Throws invalid_argument exception if argument is less than 0. void LibraryCard::borrowBooks(int numBooks) { if (numBooks < 0) throw invalid_argument("LibraryCard::borrowBooks - numBooks must be greater than or equal to 0"); if (numBooks > maxBooks-this->numBooks) return; // attempt to take out too many books this->numBooks += numBooks; } // numBooks books are returned on the given library card. // if numBooks is larger than the number of books this patron has, this method has no effect. // Throws invalid_argument exception if argument is less than 0. void LibraryCard::returnBooks(int numBooks) { if (numBooks < 0) throw invalid_argument("LibraryCard::returnBooks - numBooks must be greater than or equal to 0"); if (numBooks > this->numBooks) return; // attempt to return too many books this->numBooks -= numBooks; } // prints the number of books that this LibraryCard has currently signed out. // (Does not change the state of the LibraryCard.) void LibraryCard::booksOut() const { cout << numBooks; } // returns the percent level of the LibraryCard usage, i.e. 100 = max number of books signed out; 0 = no books signed out, etc. // (Does not change the state of the LibraryCard.) double LibraryCard::currentUsage() const { return 100.0 * numBooks / maxBooks; }