/********************************************************************* ** Description: ** returns a pointer to a memeber pased off their ID *********************************************************************/ Patron* Library:: getPatron(std::string pID) { Patron* patron; if(checkMember(pID)!=true) { for(int i = 0; i < members.size(); i++) { if(members[i]->getIdNum() == pID) { cout << "Name : " << members[i]->getName() << endl; cout << "ID : " << members[i]->getIdNum() << endl; //Set the dollar format cout << "Fines $" << members[i]->getFineAmount() << endl; //checkouted books cout << "Books Checked Out" << endl; for( int j = 0; j < holdings.size(); j++) { patron = holdings[j]->getCheckedOutBy(); if(pID == patron->getIdNum()) { cout << "Book Name " << holdings[j]->getTitle() << endl; } } } } } else { cout << "Patron not in system" << endl; } return 0; }
//Same as above. Uses viewBookInfo to print books checked out to a patron. void Library::viewPatronInfo(std::string patronID) { Patron* patron = GetPatron(patronID); if(patron == NULL) { std::cout << std::endl << "That patron does not exist!" << std::endl; } else { std::cout << std::endl << "Patron ID: " << patron->getIdNum() << std::endl; std::cout << "Name: " << patron->getName() << std::endl; std::cout << "Fines: $" << std::setprecision(2) << std::fixed << patron->getFineAmount() << std::endl; if(patron->getCheckedOutBooks().size() == 0) { std::cout << "No checked out books." << std::endl << std::endl; } else { std::cout << "Checked out books:" << std::endl << std::endl; for(int i = 0; i < patron->getCheckedOutBooks().size(); i++) { viewBookInfo(patron->getCheckedOutBooks().at(i)->getIdCode()); } std::cout << std::endl << std::endl; } } }
/********************************************************************* ** Description: ** applies 10 cent fine to each overdue book by patron per pay *********************************************************************/ void Library::incrementCurrentDate() { int update; Patron* checkOut; string patID; double fineAMT; fineAMT = .10; for( int i = 0; i < holdings.size(); i++) { if(holdings[i]->getLocation() != 0) { update = holdings[i]->getDateCheckedOut() + 1; holdings[i]->setDateCheckedOut(update); if((holdings[i]->getDateCheckedOut() - currentDate) > 21) { // fine applied. checkOut = holdings[i]->getCheckedOutBy(); patID = checkOut->getIdNum(); members[i]->amendFine(fineAMT); } } } currentDate++; cout << "The new date is " << currentDate; }
/********************************************************************* ** Description: ** Allows a patron to request a book not on shelf based off enums *********************************************************************/ string Library::requestBook(string patronID, string bookID) { Patron* patID; if(checkBook(bookID) != true) { for( int i = 0; i < holdings.size(); i++) { //Puts book on hold if(holdings[i]->getIdCode() == bookID) { // if currently on shelf if(holdings[i]->getLocation() == 0) { holdings[i]->setLocation(ON_HOLD_SHELF); //Connects Hold to a Patron for(int j = 0; j < members.size(); j++) { if(members[j]->getIdNum() == patronID) { holdings[i]->setRequestedBy(members[j]); cout << holdings[i]->getTitle() << " is now on hold for " << members[j]->getName() << endl; } } } // if currently on hold else if(holdings[i]->getLocation() == 1) { cout << "Already on hold by " << holdings[i]->getRequestedBy() << endl; } else { for(int j = 0; j < members.size(); j++) { //if person already has book checked out patID = holdings[i]->getCheckedOutBy(); if(patID->getIdNum() == patronID) { cout << "Already checked out by " << (holdings[i])->getRequestedBy() << endl; } else { holdings[i]->setRequestedBy(members[j]); cout << holdings[i]->getTitle() << " is now on hold for " << members[j]->getName() << endl; } } } } } } else { cout << "book not found" << endl; } return 0; }
//Takes a patron and book ID //Checks to make sure that the patron and book exist, returns if they dont //If book is on hold, only check out to that patron //If the book is on the shelf, it's fair game, set proper values. void Library::checkOutBook(std::string patronID, std::string bookID) { Book* book = GetBook(bookID); Patron* patron = GetPatron(patronID); if(book == NULL || patron == NULL) { if(book == NULL) { std::cout << std::endl << "No such book in holdings." << std::endl; } if(patron == NULL) { std::cout << std::endl << "No such patron in records." << std::endl; } return; } if(book->getLocation() == CHECKED_OUT) { std::cout << "This book is already checked out." << std::endl << std::endl; } else if(book->getLocation() == ON_HOLD) { if(book->getRequestedBy()->getIdNum() == patron->getIdNum()) { book->setLocation(CHECKED_OUT); book->setCheckedOutBy(GetPatron(patronID)); book->setDateCheckedOut(currentDate); book->setRequestedBy(NULL); patron->addBook(book); } else { std::cout << "This book is on hold and can only be checked out to requestee." << std::endl <<std::endl; } } else { book->setLocation(CHECKED_OUT); book->setCheckedOutBy(patron); book->setDateCheckedOut(currentDate); patron->addBook(book); } }
void Library::requestBook(string patronID, string bookID) //6 { int match=checkIdMatch(patronID, bookID); //match = location of book in holdings or -2 (no match) if (match >=0) // match and nested ifs //else return to menu; { string idn; //to hold dereferenced pointer string= idNum of requestedBy Patron* p;// if (holdings[match].getRequestedBy()!=NULL) //if book is on hold, get pointer to that idNum to compare to patron ID, otherwise if value is null { p=holdings[match].getRequestedBy(); //moved this to inside 'if ' so that pointer only assigned to non-NULL location idn = p->getIdNum(); //dereferenced pointer to requested by idNUm } //TESTING if (holdings[match].getRequestedBy()==NULL) //if requestedBy is NULL (nobody requested) { //cout<<"//TESTING: getRequestedBy() is NULL."<<endl; } if ( (holdings[match].getLocation() == ON_HOLD ) && (idn != patronID) ) //if book is on hold and idn (requested by patron'sID isn't = to patron requesting, then say book is already on hold { cout<<"Book is already ON_HOLD by another patron"<<endl; } ////////////////////////// UPDATE REQUESTED BY ///////////////////// //may need to re-address this function in previous application (chechOutBook) //cout<< "TEST THIS: (before update) RequestedBy (pointer location): "<<holdings[match].getRequestedBy() <<endl; //TESTING int k=-1; //accepts i value in for loop to be used in assigning vector location address to ptrToPatronRequesting //-1 indicates no match, but we already know there is a match if we got this far. //get patronID location in members for (int i=0; i< members.size(); i++) //check for match in members to establish pointer location to update checkedOut By { if (members[i].getIdNum()== patronID) { //cout<< "//TESTING: members vector [location] that RequestedBy will point to: "<<i<<endl; //TESTING k=i; break; } } Patron *ptrToPatronRequesting; ptrToPatronRequesting=&members[k]; holdings[match].setRequestedBy(ptrToPatronRequesting); //update pointer in requestedBy to current patron's location in members //cout<< "TEST THIS: RequestedBy UPDATED to (pointer location): "<<holdings[match].getRequestedBy() <<endl; //TESTING //////////////////////////// UPDATE LOCATION IF ON_SHELF to ON_HOLD ///////////////////////////// if (holdings[match].getLocation()==ON_SHELF) { //cout<< "//TESTING: Location before request update to (0= on shelf): "<< holdings[match].getLocation()<<endl; //TESTING holdings[match].setLocation(ON_HOLD); //cout<< "//TESTING: Location UPDATED to (1= on hold): "<< holdings[match].getLocation()<<endl; //TESTING } ///////////////////////////// PRINT MESSAGE ///////////////////////////// cout<<" Book: "<< holdings[match].getTitle()<<", is on request for: "<<members[k].getName()<<endl; } }
void Library::returnBook(string bookID) //5 { int j=-3; //if -2 , no match /////if >=0 in library location is value of i //-3 is arbitrary int loc; // loc values: 0= ON_SHELF, 1= ON_HOLD, 2= CHECKED_OUT for (int i=0; i< holdings.size(); i++) //check for match in holdings { if (holdings[i].getIdCode() == bookID) { //cout<< "//TESTING: Match found, book in the system (holdings vector) @: "<<i<<endl; //TESTING j=i; break; } else if (i==holdings.size()-1) { cout<< "There is no match for this bookID in the system (holdings vector)."<<endl; j=-2; //no match (-2 is arbitrary) //return to main } } if (j>=0) //if book in holdings and if location shows it's NOT checked out, print message and return to main. { loc=holdings[j].getLocation(); // loc values: 0= ON_SHELF, 1= ON_HOLD, 2= CHECKED_OUT if (loc != 2) { cout<<"Book ID: "<< bookID<< " is not CHECKED_OUT. Did you steal it?" <<endl; } } if ( (j>=0) && (loc==2) ) //if book in holdings and is Checked out... update patron's list, update book's location (ON_SHELF), { //look for book ID in holdings (use j) //get idNum for patron who has it checked out string idn; //will be the member's idNum Patron* p; p=holdings[j].getCheckedOutBy(); //pointer to book in holdings that contains a location to a patron in members (that's who checked it out) idn = p->getIdNum(); //dereferenced pointer to idNUm of patron in members ///////////////////////////// UPDATE PATRON'S LIST ///////////////////////////// //look for idn (idNum) of Patron in members vector to get location. int k=-1; //accepts i value in for loop to be used in establishing patron's vector location in members //-1 indicates no match //get patronID location in members for (int i=0; i< members.size(); i++) //check for match in members to establish pointer location to update checkedOut By { if (members[i].getIdNum()== idn) { //cout<< "//TESTING: members vector [location] that checkedOutBy will point to: "<<i<<endl; //TESTING k=i; break; } } Book * bookPtr; bookPtr=&holdings[j]; //address of book in holdings members[k].removeBook(bookPtr); //removes book pointer from patron's checkedOutBooks vector. //members[k].removeBook(Book* b) //TESTING int sizeOfCOB= members[k].getCheckedOutBooks().size(); //cout<<"//TESTING: print list of checked out books."<<endl; for (int i=0; i<sizeOfCOB; i++) { //cout<< "//TESTING: Title: "; //cout<<members[k].getCheckedOutBooks()[i]->getTitle()<<endl; //cout<< "//TESTING: Location: "; //cout<<members[k].getCheckedOutBooks()[i]->getLocation()<<endl; } //cout<< "//TESTING: Patron's list (members.checkedOutBooks) UPDATED!"<<endl; //TESTING ///////////////////////////// UPDATE BOOK's LOCATION ///////////////////////////// holdings[j].setLocation(ON_SHELF); //cout<< "//TESTING: Location UPDATED to: "<< holdings[j].getLocation()<<endl; //TESTING ///////////////////////////// Print CONFIRMATION ///////////////////////////// cout<< "Book title: "<<holdings[j].getTitle()<< ", has been returned. Thank you." <<endl; //cout<<"//TESTING: Book ID: "<< bookID<< " has been returned. " <<endl; //cout<<"//TESTING: Thank you Patron (ID: "<< idn<< ")." <<endl; } }
void Library::checkOutBook(string patronID, string bookID) //4 //MOST COMPLICATED FUNCTION { int match=checkIdMatch(patronID, bookID); //match = location of book in holdings or -2 (no match) if (match >=0) // match and nested ifs //else return to menu; { //cout<<"//TESTING: match >=0, match exists, holding vector location returned"<<endl;//TESTING if (holdings[match].getLocation()== CHECKED_OUT) { cout<<"Book is already CHECKED_OUT"<<endl;//TESTING } //string idn= holdings[match].getRequestedBy()->getIdNum(); //chaining not working string idn; Patron* p;//=holdings[match].getRequestedBy(); if (holdings[match].getRequestedBy()!=NULL) //if book is on hold, get pointer to that idNum to compare to patron ID { p=holdings[match].getRequestedBy(); //moved this to inside if so that pointer only assigned to non-NULL location idn = p->getIdNum(); //dereferenced pointer to idNUm } if ((holdings[match].getLocation()== ON_HOLD) && (idn != patronID)) { cout<<"Book is ON_HOLD by another patron"<<endl; } else if( (holdings[match].getLocation()== ON_SHELF) || ((holdings[match].getLocation()== ON_HOLD) && (idn == patronID)) ) //adding catch in case patron checking out had on hold { //cout<<"//TESTING: ON_SHELF!"<<endl; //TESTING ////////////////// UPDATE CHECKED OUT BY //////////////////////// int k=-1; //accepts i value in for loop to be used in assigning vector location address to ptrToPatronCheckingOut //-1 indicates no match, but we already know there is a match if we got this far. //get patronID location in members for (int i=0; i< members.size(); i++) //check for match in members to establish pointer location to update checkedOut By { if (members[i].getIdNum()== patronID) { //cout<< "//TESTING: members vector [location] that checkedOutBy will point to: "<<i<<endl; //TESTING k=i; break; } } Patron *ptrToPatronCheckingOut; ptrToPatronCheckingOut=&members[k]; holdings[match].setCheckedOutBy(ptrToPatronCheckingOut); ///////////////////////////// UPDATE DATE OUT /////////////////////////////// holdings[match].setDateCheckedOut(currentDate); //cout<< "//TESTING:DateCheckedOut UPDATED to: "<<holdings[match].getDateCheckedOut() <<endl; //TESTING ///////////////////////////// UPDATE LOCATION ////////////////////////////// holdings[match].setLocation(CHECKED_OUT); ///may cause problems to change location inside else if statement //cout<< "//TESTING: Location UPDATED to: "<< holdings[match].getLocation()<<endl; //TESTING ////////////////////////// UPDATE REQUESTED BY ///////////////////// //may need to re-address this function. might need to move update location to after and include (idn==patronID && getLocation==ON_HOLD) if (idn == patronID) //if idn (string that came from dereferenced pointer to requestedBy.getIdNum or could be NULL) is == to patronID (was on hold for the patron) { holdings[match].setRequestedBy(ptrToPatronCheckingOut); //update pointer in requestedBy to current patron's location in members //cout<< "TEST THIS: RequestedBy UPDATED to (pointer location): "<<holdings[match].getRequestedBy() <<endl; //TESTING } else { //cout<< "TEST THIS: RequestedBy not needing to be UPDATED bc idn (possibly NULL):" <<idn<< " != "<< patronID<<" (patronID). "<<endl; //TESTING } ///////////////////////////// UPDATE PATRON'S LIST ///////////////////////////// //members[k].addBook(Book* b) Book * bookPtr; bookPtr=&holdings[match]; //address of book in holdings members[k].addBook(bookPtr); //adds book pointer to patron's vector of checkedOutBooks //cout<< "//TESTING: Patron's list (members.checkedOutBooks) UPDATED!"<<endl; //TESTING ////////////////////////////////////// PRINT BOOK CHECKED OUT & PATRON'S NAME /////////////////////////////////// cout<<"Title: " <<holdings[match].getTitle(); cout<< " has been check out to: "; cout<< members[k].getName()<<endl; } } else //TESTING { //cout<<"//TESTING: no match found, returned -2 from checkIdMatch. return to menu."<<endl; //TESTING } }