// lpatron.cpp // ---------------------------------------------------------------------- // Do not modify this file. // implements class LibraryPatron // // CSE 143 // Homework 3 // http://www.cs.washington.edu/education/courses/143/00su/homework/ // 02 Jul 2000; Ken Yasuhara #include #include #include #include "lpatron.h" ////////////////////////////////////////////////////////////////////// // constructors LibraryPatron::LibraryPatron() { ID = 0; strcpy(name, ""); // initially, no books checked out, so all entries 0 for (int i = 0; i < MAX_CHECKOUTS; i++) { borrowedBookIDs[i] = 0; } } LibraryPatron::LibraryPatron(int newID, char newName[]) { assert(strlen(newName) <= (unsigned int)MAX_NAME_LENGTH); assert(newID > 0); ID = newID; strcpy(name, newName); // initially, no books checked out, so all entries 0 for (int i = 0; i < MAX_CHECKOUTS; i++) { borrowedBookIDs[i] = 0; } } ////////////////////////////////////////////////////////////////////// // accessors, related methods void LibraryPatron::getName(char spaceForCopy[]) { strcpy(spaceForCopy, name); } int LibraryPatron::getID() { return ID; } void LibraryPatron::getBorrowedBooks(int bookIDs[MAX_CHECKOUTS]) { for (int i = 0, j = 0; i < MAX_CHECKOUTS; i++) { // only positive elements in borrowedBookIDs indicate borrowed // books; copy those into bookIDs if (borrowedBookIDs[i] > 0) { bookIDs[j] = borrowedBookIDs[i]; j++; } } } int LibraryPatron::getBorrowedCount() { int count = 0; // count of nonzero entries in borrowedBookIDs for (int i = 0; i < MAX_CHECKOUTS; i++) { if (borrowedBookIDs[i] > 0) { count++; } } return count; } int LibraryPatron::compare(LibraryPatron otherPatron) { char otherName[MAX_NAME_LENGTH]; otherPatron.getName(otherName); // currently only compares name strings return strcmp(name, otherName); } ////////////////////////////////////////////////////////////////////// // tracking borrowed books bool LibraryPatron::canCheckOutMore() { return (getBorrowedCount() < MAX_CHECKOUTS); } bool LibraryPatron::hasCheckedOut(int bookID) { assert(bookID > 0); // if this patron has the book w/ the given ID bookID checked out, // this book ID will be in the borrowedBookIDs array for (int i = 0; i < MAX_CHECKOUTS; i++) { if (borrowedBookIDs[i] == bookID) { return true; } } return false; } void LibraryPatron::registerCheckOut(int bookID) { assert(bookID > 0); assert(canCheckOutMore()); // find empty slot (zero element) in borrowedBookIDs and store given // book ID there for (int i = 0; i < MAX_CHECKOUTS; i++) { if (borrowedBookIDs[i] == 0) { borrowedBookIDs[i] = bookID; return; } } // should never reach here, since canCheckOutMore() is asserted // before attempting check-out assert(false); // (recall: assert false *always* fails) } void LibraryPatron::registerCheckIn(int bookID) { assert(bookID > 0); assert(hasCheckedOut(bookID)); // find this book's ID in borrowedBookIDs and clear it, i.e. zero // the element for (int i = 0; i < MAX_CHECKOUTS; i++) { if (borrowedBookIDs[i] == bookID) { borrowedBookIDs[i] = 0; return; } } // should never reach here, since we assert that this patron has // this book checked out first assert(false); } ////////////////////////////////////////////////////////////////////// // I/O void LibraryPatron::printRecord() { cout << "patron #" << ID << endl; cout << "name:\t" << name << endl; cout << "books:\t"; for (int i = 0; i < MAX_CHECKOUTS; i++) { // only list nonzero elements of borrowedBookIDs if (borrowedBookIDs[i] > 0) { cout << borrowedBookIDs[i] << " "; } } cout << endl; }