// function to add book void Inventory::addBook() { try{ Book temp = getBook(); for (vector<Book>::iterator it = books.begin(); it != books.end(); it++){ //Allow for duplicate titles but not ISBN's if (it->getISBN() == temp.getISBN()) { cout << "\nBook already in library\n\n"; return; } } pushBook(temp); cout << "\nAdded book:\n\n" << temp << endl << endl; } catch (exception &e){ cout << endl << e.what() << endl << endl; } }
//The following function uses a non-recursive method to traverse the tree, adding books into a stack array. //As it comes back, it pops books from the stack and checks if they are the book to be sold. If they are, the quantity //parameter is subtracted by 1. If there is only one left, the function removes the book from the binary search tree //by rearranging it appropriately. void Library::SellBookbyISBN(Book *tmp, string aISBN) { //If there are no Books if (root == NULL) { cout << "There are no books in the library." << endl; } //If there is only one Book else if (tmp->getLeft() == 0 && tmp->getRight() == 0) { cout << "Only one book in the library." << endl; cout << "$" << tmp->getPrice() << " " << tmp->getTitle() << " by " << tmp->getAuthor() << endl; } else { Book *stack[200]; int L = 0; Book *current = tmp; bool done = false; while (done == false) { if (current != NULL) { stack[L] = current; L++; current = current->getLeft(current); } else { if (L != 0 && current == NULL) { L--; current = stack[L]; if (current->getISBN() == aISBN) { if (current->getQuantity() == 1) { Book* parent = stack[L-1]; cout << "Oh, looks like you are selling the last one. Removing from tree." << endl; if (current->getLeft() == NULL && current->getRight() == NULL) { if (current->getPrice() > parent->getPrice()) { parent->setRight(NULL); } else { parent->setLeft(NULL); } } else if (current->getLeft() != NULL && current->getRight() == NULL) { if (current->getPrice() > parent->getPrice()) { parent->setRight(current->getLeft()); } else { parent->setLeft(current->getRight()); } } else if (current->getLeft() == NULL && current->getRight() != NULL) { if (current->getPrice() > parent->getPrice()) { parent->setRight(current->getRight()); } else { parent->setLeft(current->getLeft()); } } else if (current->getLeft() != NULL && current->getRight() != NULL) { Book* prev = predecessor(current->getPrice(), current); if (prev == NULL) { prev = successor(current->getPrice(), current); } if (prev->getPrice() > prev->getParent()->getPrice()) { prev->getParent()->setRight(prev->getRight()); } else if (prev->getPrice() <= prev->getParent()->getPrice()) { prev->getParent()->setLeft(prev->getRight()); } current->setPrice(prev->getPrice()); delete prev; } } else { int new_quant = current->getQuantity()-1; current->setQuantity(new_quant); cout << "$" << current->getPrice() << " " << current->getTitle() << " by " << current->getAuthor() << endl; cout << "New quantity: " << current->getQuantity() << endl; } } current = current->getRight(current); } else { done = true; } } } } }
int main() { Book bookshelf; Member listofmembers; string file1; string file2; int numberofbooks; int numberofmembers; bool program = false; int choice1 = 0; int choice2=0; bool files=false; ///////////// Reading in Files////////////// while(files == false) { cout << "Please enter books.txt"<< endl; cin >> file1; cout <<"Please enter ratings.txt"<< endl; cin >> file2; numberofbooks = readBookfile(file1, bookshelf); numberofmembers=readRatingfile(file2, numberofbooks, listofmembers); if(numberofbooks == 0 or numberofmembers == 0) {cout << "Unable to read files. Please enter usuable files."<< endl; } else { files =true; } } ///////////////Running Program///////////// program = true; bool login= false; while(program == true){ cout << endl<<"Welcome to the Book Recommender!"<<endl; //Welcome screen cout << "Select an opion below:"<<endl; cout <<"1) Login:"******"2) Add new member:"<<endl<<"3) Add new book:"<< endl << "4) Quit"<<endl; cin >> choice1; while (choice1>4){ cout << "Please select a choice from above. Idoit." <<endl; cin >> choice1;} while(choice1 == 1){//login string name; int ID; Rating rateobject; if(login==false){ cout<< "Please enter user ID"<< endl; cin >>ID; while (ID> numberofmembers){ cout<<"That user doesn't exsist! Please enter a valid user ID."<<endl; cin >>ID; } login =true; } name= listofmembers.getName(ID); rateobject=listofmembers.getMemberRating(ID); cout << endl<<"Welcome " <<name<< "!"<< endl; cout<< "Select an opion below." << endl; cout<< "1) Rate book"<<endl<<"2) View your ratings"<<endl<<"3) Recommend Books"<<endl<<"4) Logout"<<endl; cin>>choice2; while(choice2 >4){ cout << "Please select a choice from above like how hard is it come on?"<<endl; cin >> choice2;} /////////logged in functions while(choice2 == 1){//rate book string YorN= "y"; int bookchoice=0; int bookrate=0; while(YorN == "y") { cout << "Here are the books avalible to rate:" <<endl; for(int i=0; i < numberofbooks; i++){ cout<< bookshelf.getISBN(i)<<") "<< bookshelf.getTitle(i)<<endl; } cout<< "Enter the number of the book to rate: "<< endl; cin >> bookchoice; cout << "What would you give this book? (-5, -3, 0, 1, 3, 5)"<<endl; cin >>bookrate; listofmembers.getMemberRating(ID).setrating(bookchoice, bookrate); cout<< "Would you like to rate another book?(y/n)"<<endl; cin>>YorN; } if (YorN != "n"){ cout << "Please enter y or n. " << endl; cin >> YorN; } else{ choice2=0; } }