#include "booklist.h" #include #include #include #include bool BookCollection::addBook(const Book & b) { pair::iterator, bool> returnPair; returnPair = bookList.insert(make_pair(b.getIsbn(), b)); return returnPair.second; } Book BookCollection::findBook(const string & isbn) const { map::const_iterator it = bookList.find(isbn); if(it == bookList.end()){ return Book(notFound,"","",""); }else{ return Book(it -> second.getTitle(), it -> second.getAuthor(), it -> second.getIsbn(), it -> second.getPrice()); } } bool BookCollection::removeBook(const string & isbn) { return bookList.erase(isbn); } void BookCollection::print(ostream & os) const { string str(60,'-'); if(size() == 0){ os << "Store is empty" << endl; } for(map::const_iterator it = bookList.begin(); it != bookList.end(); ++it ){ // calling overloaded << for Book os << it -> second << str << endl; } } bool BookCollection::loadData() { string title, author, isbn, price; ifstream fin(filename.c_str()); if(!fin){ return false; } while(!fin.eof()){ getline(fin,title); getline(fin,author); getline(fin,isbn); getline(fin,price); Book book(title,author,isbn,price); addBook(book); } fin.close(); return true; } void BookCollection::saveData(){ ofstream out(filename.c_str()); for(map::const_iterator it = bookList.begin(); it != bookList.end(); ++it ){ out << it -> second; } out.close(); } ostream& operator << (ostream & os, const Book & b) { os << b.getTitle() << endl; if(b.getAuthor().size() != 0 ) { os << b.getAuthor() << endl << b.getIsbn() << endl << b.getPrice() << endl; } return os; } bool isValid(const string & isbn){ const char ZERO = '0'; string copy(10,'*'); copyNoHyphen(isbn,copy); if(copy.size() != 10){ return false; } int sum = copy[0] - ZERO; for(int i = 1; i < copy.size() - 1; ++i){ int temp = copy[i] - ZERO; sum += temp * (i + 1); } int remainder = sum % 11; if(remainder == 10) { if(copy[copy.size() - 1] == 'X') { return true; }else{ return false; } } else if(remainder != (copy[copy.size() -1] - ZERO)){ return false; } return true; } void copyNoHyphen(const string & isbn, string & copy){ const char HYPHEN = '-'; int j = 0; for(int i = 0; i < 13; ++i){ if(isbn[i] != HYPHEN){ copy[j++] = isbn[i]; } } }