// lbooklst.cpp // ---------------------------------------------------------------------- // Do not modify this file. // definition for class LibraryBookList // modified copy of ListA.h/cpp, Carrano et al. // // CSE 143 // Homework 3 // http://www.cs.washington.edu/education/courses/143/00su/homework/ // 05 Jul 2000; Ken Yasuhara #include #include #include #include #include "lbooklst.h" #include "library.h" // just for waitForChar() const int MAX_LINE_LENGTH = 120; // when reading from a file ////////////////////////////////////////////////////////////////////// // constructors LibraryBookList::LibraryBookList() { itemCount = 0; } LibraryBookList::LibraryBookList(char dataFileName[]) { itemCount = 0; readFile(dataFileName); } ////////////////////////////////////////////////////////////////////// // methods bool LibraryBookList::isEmpty() { return (itemCount == 0); } // end isEmpty int LibraryBookList::getLength() { return itemCount; } // end getLength void LibraryBookList::insertItem(int newPosition, LibraryBook newItem, bool &success) { success = ( (newPosition >= 0) && (newPosition <= itemCount) && (itemCount < MAX_BOOK_COUNT) ); if (success) { // make room for new item by shifting all items at // positions >= newPosition toward the end of the // list (no shift if newPosition == getLength()) for (int i = itemCount - 1; i >= newPosition; i--) { itemArray[i + 1] = itemArray[i]; } // insert new item itemArray[newPosition] = newItem; itemCount++; } // end if } // end insertItem void LibraryBookList::deleteItem(int position, bool &success) { success = ( (position >= 0) && (position < getLength()) ); if (success) { // delete item by shifting all items at positions > // position toward the beginning of the list // (no shift if position == itemCount) for (int i = position + 1; i < itemCount; i++) { itemArray[i - 1] = itemArray[i]; } itemCount--; } // end if } // end deleteItem void LibraryBookList::updateItem(int position, LibraryBook updatedItem, bool &success) { success = ( (position >= 0) && (position < getLength()) ); if (success) { itemArray[position] = updatedItem; } } // end updateItem void LibraryBookList::retrieveByPosition(int position, LibraryBook &retrievedItem, bool &success) { success = bool( (position >= 0) && (position < itemCount) ); if (success) retrievedItem = itemArray[position]; } // end retrieveByPosition int LibraryBookList::findPositionByID(int ID, bool &success) { for (int i = 0; i < getLength(); i++) { LibraryBook b; bool retrieveSuccess; retrieveByPosition(i, b, retrieveSuccess); if (b.getID() == ID) { success = true; return i; } } success = false; return -1; } // end findPositionByID void LibraryBookList::retrieveByID(int ID, LibraryBook &retrievedItem, bool &success) { int position = findPositionByID(ID, success); if (success) { retrieveByPosition(position, retrievedItem, success); } } // end retrieveByID ////////////////////////////////////////////////////////////////////// // I/O methods void LibraryBookList::readFile(char fileName[]) { // 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; } int bookCount = 0; // counts number of book records read so far while (inFile && (bookCount < MAX_BOOK_COUNT)) { int ID; inFile >> ID; // need to skip the trailing whitespace after ID, // i.e. trailing characters, including endline (\n) char line[MAX_LINE_LENGTH]; inFile.getline(line, MAX_LINE_LENGTH); char title[MAX_TITLE_LENGTH]; inFile.getline(title, MAX_TITLE_LENGTH); if (strlen(title) == 0) { // stop at first blank line break; } char author[MAX_AUTHOR_LENGTH]; inFile.getline(author, MAX_AUTHOR_LENGTH); // done reading data for this book; construct LibraryBook and // insert at end of list LibraryBook newBook(ID, title, author); bool success; insertItem(getLength(), newBook, success); if (!success) { cerr << "LibraryBookList::readFile(): ListInsert failed" << endl; return; } bookCount++; } inFile.close(); } // end readFile void LibraryBookList::print() { for (int i = 0; i < itemCount; i++) { itemArray[i].printRecord(); cout << endl; if ((i + 1) % BOOK_ENTRIES_PER_SCREEN == 0) { waitForChar(); } } } // end print // End of implementation file.