// library.cpp // ---------------------------------------------------------------------- // incomplete; see comments marked that w/ *** // misc. library-related functions for use in main.cpp // // CSE 143 // Homework 3 // http://www.cs.washington.edu/education/courses/143/00su/homework/ // 03 Jul 2000 // 12 Jul 2000: modified to uncomment #include #include #include #include #include #include #include "library.h" const int MAX_LINE_LENGTH = 120; // when reading from a file ////////////////////////////////////////////////////////////////////// // misc. utility functions void waitForChar(void) { char junkchar; cout << endl << "Type a character and ENTER to continue... "; cin >> junkchar; } ////////////////////////////////////////////////////////////////////// // finances int computeFine(int daysOverdue) { assert(daysOverdue > 0); return (FIXED_FINE + PER_DAY_FINE * daysOverdue); } ////////////////////////////////////////////////////////////////////// // I/O void processTransactionsFile(char fileName[], LibraryBookList &bookList, LibraryPatronList &patronList) { // Mac and UNIX users: omit ios::nocreate argument assert(strlen(fileName) > 0); ifstream inFile(fileName, ios::nocreate); if (!inFile) { cerr << endl << "unable to open file: " << fileName << endl; return; } // process one transaction per loop iteration while (inFile) { // dec is not a local variable, but a manipulator that forces cin // to read values in decimal (base 10) format int month, day, year, userID, bookID; char transactionCode; inFile >> dec >> month >> day >> year >> userID >> transactionCode >> bookID; Date transactionDate(month, day, year); // need to skip the trailing whitespace after bookID, // i.e. extract trailing characters, including \n char line[MAX_LINE_LENGTH]; inFile.getline(line, MAX_LINE_LENGTH); if (!transactionIsValid(transactionDate, userID, transactionCode)) { cerr << "invalid transaction" << endl; return; } processTransaction(bookList, patronList, transactionDate, userID, transactionCode, bookID); waitForChar(); } // end loop over lines in file inFile.close(); } // end processTransactionsFile bool transactionIsValid(Date transactionDate, int userID, char transactionCode) { if (!transactionDate.isValid()) { cerr << "invalid transaction date" << endl; return false; } if (userID <= 0 || !(transactionCode == 'i' || transactionCode == 'o')) { cerr << "invalid transaction ID or code" << endl; return false; } return true; } // updating this book in bookList entails retrieving a copy of it // (transactionBook), updating the copy, and replacing the original in // the list w/ the updated copy void processTransaction(LibraryBookList &bookList, LibraryPatronList &patronList, Date transactionDate, int userID, char transactionCode, int bookID) { // find position of book with given ID (bookID) in bookList, then // retrieve copy of book object // *** your code here // find position of patron with given ID (userID) in patronList, // then retrieve copy of patron object // *** your code here // begin printing record of transaction; output date of transaction // and whether it is a check-in or -out // *** your code here // depending on transactionCode, call appropriate function (either // processCheckOut or processCheckIn) to process transaction and // update the book and patron objects retrieved above // *** your code here // update original book object in the list, i.e. replace the object // in the list w/ the copy of the object updated by the call to // processCheckOut or processCheckIn above // *** your code here // do the same w/ the patron record // *** your code here // print out updated book and patron records // *** your code here } void processCheckOut(LibraryBook &book, LibraryPatron &patron, Date date) { // verify book is not checked out if (book.isCheckedOut()) { cerr << "transaction invalid; cannot check out a book " << "which is already checked out" << endl; return; } // verify patron hasn't reached max. number of allowed check-outs if (!patron.canCheckOutMore()) { cerr << "transaction invalid for patron; " << "max. number of check-outs reached" << endl; return; } book.checkOut(date, patron, LOAN_DURATION); } void processCheckIn(LibraryBook &book, LibraryPatron &patron, Date date) { // verify book is checked out if (!book.isCheckedOut()) { cerr << "transaction invalid; cannot check in a book " << "which is not checked out" << endl; return; } // verify that patron actually has this book checked out if (!patron.hasCheckedOut(book.getID())) { cerr << "transaction invalid; book not checked out by patron" << endl; return; } if (book.isOverdue(date)) { int daysPastDue = book.daysOverdue(date); cout << "overdue by " << daysPastDue << " days, "; cout << "overdue fine: " << computeFine(daysPastDue) << " cents" << endl; } // end overdue book book.checkIn(patron); }