// lbook.cpp // ---------------------------------------------------------------------- // Do not modify this file. // defines class LibraryBook // // CSE 143 // Homework 2, 3 // http://www.cs.washington.edu/education/courses/143/00su/homework/ // 25 Jun 2000; Ken Yasuhara #include #include #include #include "lbook.h" ////////////////////////////////////////////////////////////////////// // constructors LibraryBook::LibraryBook() { strcpy(title, ""); strcpy(author, ""); ID = 0; borrowerID = 0; } LibraryBook::LibraryBook(int newID, char newTitle[MAX_TITLE_LENGTH], char newAuthor[MAX_AUTHOR_LENGTH]) { assert(strlen(newTitle) > 0); assert(strlen(newAuthor) > 0); assert(newID > 0); strcpy(title, newTitle); strcpy(author, newAuthor); ID = newID; borrowerID = 0; } ////////////////////////////////////////////////////////////////////// // accessors int LibraryBook::getBorrowerID() { assert(isCheckedOut()); return borrowerID; } void LibraryBook::getTitle(char copiedTitle[]) { strcpy(copiedTitle, title); } void LibraryBook::getAuthor(char copiedAuthor[]) { strcpy(copiedAuthor, author); } int LibraryBook::getID() { return ID; } bool LibraryBook::isCheckedOut() { return (borrowerID != 0); } ////////////////////////////////////////////////////////////////////// // I/O void LibraryBook::printRecord() { cout << "book #" << getID() << endl; cout << "title:\t" << title << endl; cout << "author:\t" << author << endl; cout << "out:\t"; if (isCheckedOut()) { cout << "yes" << endl; cout << "user:\t" << getBorrowerID() << endl; cout << "due:\t" << getDueDate().getMonth() << "-" << getDueDate().getDay() << "-" << getDueDate().getYear() << endl; } else { cout << "no" << endl; } } ////////////////////////////////////////////////////////////////////// // recording book-borrowing // preconditions: this book is checked out bool LibraryBook::isOverdue(Date today) { assert(isCheckedOut()); if (today.compare(dueDate) < 0) { return true; } return false; } // preconditions: this book is overdue // // given current date, returns number of days by which this book is // overdue int LibraryBook::daysOverdue(Date today) { assert(isOverdue(today)); int days = dueDate.dayDifference(today); assert(dueDate.dayDifference(today) == today.dayDifference(dueDate)); assert(days > 0); return days; } // preconditions: this book is checked out Date LibraryBook::getDueDate() { assert(isCheckedOut()); return dueDate; } // preconditions: this book is not checked out; loan duration is positive; // borrower has not reached limit on allowed number of borrowed books void LibraryBook::checkOut(Date today, LibraryPatron &borrower, int loanDuration) { assert(!isCheckedOut()); assert(loanDuration > 0); assert(borrower.canCheckOutMore()); borrowerID = borrower.getID(); borrower.registerCheckOut(ID); dueDate = today; dueDate.addDays(loanDuration); } // preconditions: this book must be checked out; patron must be the // one who has this book checked out, i.e. you can only return a book // that you previousl8y borrowed void LibraryBook::checkIn(LibraryPatron &returner) { assert(isCheckedOut()); assert(returner.hasCheckedOut(ID)); borrowerID = 0; returner.registerCheckIn(ID); }