Guest User

Untitled

a guest
May 24th, 2018
80
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 12.01 KB | None | 0 0
  1. #include <string>
  2. #include <iostream>
  3. #include <iomanip>
  4.  
  5. #include "Library.h"
  6.  
  7. void displayMenu();
  8. bool menuSelection( int option, Library* lib );
  9. void addBookInformation( Library* lib );
  10. void removeBook( Library* lib );
  11. void displayBook( Library* lib );
  12.  
  13. int main() {
  14. Library lib;
  15.  
  16. std::cout << "Welcome to the Library!n";
  17. displayMenu();
  18.  
  19. int option = 0;
  20. do {
  21. std::cout << "nChoose an option from the menu ";
  22. std::cin >> option;
  23. std::cout << 'n';
  24. } while( menuSelection( option, &lib ) );
  25.  
  26. std::cout << "nPress any key and enter to quit.n";
  27. std::cin.get();
  28. return 0;
  29. }
  30.  
  31. void displayMenu() {
  32. std::cout << "================================================n";
  33. std::cout << "1: Add a book to the libraryn";
  34. std::cout << "2: Remove book from the libraryn";
  35. std::cout << "3: Display the number of books in the libraryn";
  36. std::cout << "4: Display a books informationn";
  37. std::cout << "5: Display the list of all booksn";
  38. std::cout << "6: Display menu optionn";
  39. std::cout << "7: Exit the library and quitn";
  40. std::cout << "================================================n";
  41. }
  42.  
  43. bool menuSelection( int option, Library* lib ) {
  44. switch( option ) {
  45. case 1: {
  46. addBookInformation( lib );
  47. std::cout.flush();
  48. system( "clear" );
  49. displayMenu();
  50. std::cout << "nYou have entered a book into the libray.n";
  51. break;
  52. }
  53. case 2: {
  54. removeBook( lib );
  55. std::cout.flush();
  56. system( "clear" );
  57. displayMenu();
  58. std::cout << "nYou have removed a book from the library.n";
  59. break;
  60. }
  61. case 3: {
  62. unsigned int numBooks = lib->totalBooks();
  63. if( numBooks != 1 ) {
  64. std::cout << "nThere are " << numBooks << " books in our library.n";
  65. } else {
  66. std::cout << "nThere is 1 book in our library.n";
  67. }
  68. break;
  69. }
  70. case 4: {
  71. displayBook( lib );
  72. break;
  73. }
  74. case 5: {
  75. unsigned int numBooks = lib->totalBooks();
  76. if( numBooks > 0 ) {
  77. std::cout << *lib;
  78. } else {
  79. std::cout << "nThere are no books to display.n";
  80. }
  81. break;
  82. }
  83. case 6: {
  84. std::cin.ignore();
  85. std::cout.flush();
  86. system( "clear" );
  87. displayMenu();
  88. break;
  89. }
  90. case 7: {
  91. return false;
  92. }
  93. default: {
  94. std::cout << "nInvalid selection please try again.n";
  95. break;
  96. }
  97. }
  98. return true;
  99. }
  100.  
  101. void addBookInformation( Library* lib ) {
  102. static unsigned int bookId = 0;
  103. unsigned int year = 0;
  104.  
  105. std::string title, author;
  106. std::cin.ignore();
  107. std::cout << "Please enter the books title: ";
  108. std::getline( std::cin, title );
  109. std::cout << "Please enter the books author: ";
  110. std::getline( std::cin, author );
  111. std::cout << "Please enter the books year: ";
  112. std::cin >> year;
  113.  
  114. bookId++; // increment our book id so each one is unique TODO: this can be replaced to have same id for multiple books if the books are exact matches.
  115.  
  116. Book book( title, author, year );
  117. lib->addBook( std::to_string( bookId ), book );
  118. }
  119.  
  120. void removeBook( Library* lib ) {
  121. unsigned int numBooks = lib->totalBooks();
  122. if( numBooks == 0 ) {
  123. std::cout << "nThere are 0 books in library; nothing to remove.n";
  124. return;
  125. }
  126.  
  127. std::cout << "nRemove book by ID(I) or by Book(B)n";
  128. char choice;
  129. std::cin >> choice;
  130. if( choice == 'i' || choice == 'I' ) {
  131. std::cout << "Enter the books ID ";
  132. unsigned int id;
  133. std::cin >> id;
  134. lib->removeBook( std::to_string( id ) );
  135. } else if( choice == 'b' || choice == 'B' ) {
  136. std::cin.ignore();
  137. std::cout << "What is the title of the book? ";
  138. std::string title;
  139. std::getline( std::cin, title );
  140. std::cout << "Who is the author of the book? ";
  141. std::string author;
  142. std::getline( std::cin, author );
  143. std::cout << "What year was the book published ";
  144. unsigned int year;
  145. std::cin >> year;
  146. Book book( title, author, year );
  147. lib->removeBook( book );
  148. } else {
  149. std::cout << "nYou entered an invalid selectionn";
  150. }
  151. }
  152.  
  153. void displayBook( Library* lib ) {
  154. unsigned int numBooks = lib->totalBooks();
  155. if( numBooks == 0 ) {
  156. std::cout << "nThere are 0 books in the library; nothing to display.n";
  157. return;
  158. }
  159.  
  160. std::cout << "nFind book by ID(I) or by Book(B)n";
  161. char choice;
  162. std::cin >> choice;
  163. if( choice == 'i' || choice == 'I' ) {
  164. std::cout << "Enter the books ID ";
  165. unsigned int id;
  166. std::cin >> id;
  167. Book* book = lib->findBook( std::to_string( id ) );
  168. if( book ) {
  169. std::cout << *book;
  170. } else {
  171. std::cout << "nBook was not found.n";
  172. }
  173.  
  174. } else if( choice == 'b' || choice == 'B' ) {
  175. std::cin.ignore();
  176. std::cout << "What is the title of the book? ";
  177. std::string title;
  178. std::getline( std::cin, title );
  179. std::cout << "Who is the author of the book? ";
  180. std::string author;
  181. std::getline( std::cin, author );
  182. std::cout << "What year was the book published? ";
  183. unsigned int year;
  184. std::cin >> year;
  185.  
  186. Book bookToFind( title, author, year );
  187. Book* actualBook = lib->findBook( bookToFind );
  188.  
  189. if( actualBook ) {
  190. std::cout << *actualBook;
  191. } else {
  192. std::cout << "nBook was not found.n";
  193. }
  194.  
  195. } else {
  196. std::cout << "nYou entered an invalid selectionn";
  197. }
  198. }
  199.  
  200. #ifndef BOOK_H
  201. #define BOOK_H
  202.  
  203. #include <string>
  204. #include <iostream>
  205. #include <iomanip>
  206.  
  207. class Book {
  208. private:
  209. std::string title_;
  210. std::string author_;
  211. unsigned int year_;
  212.  
  213. public:
  214. Book(); // default
  215. Book( const std::string& title, const std::string& author, unsigned int year );
  216.  
  217. std::string titleIs() const;
  218. std::string authorIs() const;
  219. unsigned int yearPublished() const;
  220.  
  221. void updateTitle( const std::string& title );
  222. void updateAuthor( const std::string& author );
  223. void updateYear( unsigned int year );
  224.  
  225. bool operator==( const Book& other ) const;
  226. };
  227.  
  228. std::ostream& operator<<( std::ostream& out, const Book& book );
  229.  
  230. #endif // BOOK_H
  231.  
  232. #include "Book.h"
  233.  
  234. Book::Book() {
  235. } // default
  236.  
  237. Book::Book( const std::string& title, const std::string& author, unsigned int year ) :
  238. title_( title ),
  239. author_( author ),
  240. year_( year ) {
  241. }
  242.  
  243. std::string Book::titleIs() const {
  244. return title_;
  245. }
  246.  
  247. std::string Book::authorIs() const {
  248. return author_;
  249. }
  250.  
  251. unsigned int Book::yearPublished() const {
  252. return year_;
  253. }
  254.  
  255. void Book::updateTitle( const std::string& title ) {
  256. title_ = title;
  257. }
  258.  
  259. void Book::updateAuthor( const std::string& author ) {
  260. author_ = author;
  261. }
  262.  
  263. void Book::updateYear( unsigned int year ) {
  264. year_ = year;
  265. }
  266.  
  267. bool Book::operator==( const Book& other ) const {
  268. return ( title_ == other.title_ &&
  269. author_ == other.author_ &&
  270. year_ == other.year_ );
  271. }
  272.  
  273. std::ostream& operator<<( std::ostream& out, const Book& book ) {
  274. out << std::setw( 15 ) << "Title: " << book.titleIs() << 'n'
  275. << std::setw( 15 ) << "Author: " << book.authorIs() << 'n'
  276. << std::setw( 15 ) << "Year: " << book.yearPublished() << 'n';
  277. return out;
  278. }
  279.  
  280. #ifndef LIBRARY_H
  281. #define LIBRARY_H
  282.  
  283. #include "Book.h"
  284. #include <map>
  285.  
  286. class Library {
  287. private:
  288. std::multimap<std::string, Book> books_;
  289. // This is used if the library has several copies of the same book
  290. // that have the same ID in the multimap above.
  291. std::map<std::string, unsigned int> inventory_;
  292.  
  293. public:
  294. Library(); // deafault
  295.  
  296. void addBook( const std::string& id, Book& book );
  297. void removeBook( const std::string& id );
  298. void removeBook( Book& book );
  299.  
  300. Book* findBook( const std::string& id );
  301. Book* findBook( Book& book );
  302.  
  303. std::size_t totalBooks() const;
  304. std::size_t totalUniqueBooks() const;
  305.  
  306. // Three different ways to return the library back to user
  307. std::multimap<std::string, Book> mapOfBooks() const;
  308.  
  309. // Currently Supports List and Vector
  310. template< template < class ... > class Container, class ... Args >
  311. void listOfBooks( Container<Book, Args...>& c ) const;
  312.  
  313. private:
  314. // Helper function to calculate the number of unique books.
  315. std::size_t calculateUniqueNumberOfBooks() const;
  316. };
  317.  
  318.  
  319. template<template<class...> class Container, class...Args>
  320. void Library::listOfBooks( Container<Book, Args...>& c ) const {
  321. auto it = books_.begin();
  322. while ( it != books_.end() ) {
  323. c.emplace_back( it->second );
  324. }
  325. }
  326.  
  327. std::ostream& operator<<( std::ostream& out, const Library& library );
  328.  
  329. void displayLibrary( const Library& library );
  330.  
  331. #endif // LIBRARY_H
  332.  
  333. #include "Library.h"
  334. #include <vector>
  335.  
  336. Library::Library() {
  337. } // deafault
  338.  
  339. void Library::addBook( const std::string& id, Book& book ) {
  340. books_.insert( std::pair<std::string,Book>( id, book ) );
  341. }
  342.  
  343. void Library::removeBook( const std::string& id ) {
  344. auto it = books_.begin();
  345. while( it != books_.end() ) {
  346. if( id == it->first ) {
  347. // found match so remove it
  348. it = books_.erase( it );
  349. } else {
  350. it++;
  351. }
  352. }
  353. }
  354.  
  355. void Library::removeBook( Book& book ) {
  356. auto it = books_.begin();
  357. while( it != books_.end() ) {
  358. if( book == it->second ) {
  359. // found match so remove it
  360. it = books_.erase( it );
  361. } else {
  362. it++;
  363. }
  364. }
  365. }
  366.  
  367. Book* Library::findBook( const std::string& id ) {
  368. auto it = books_.begin();
  369. while( it != books_.end() ) {
  370. if( id == it->first ) {
  371. return &it->second;
  372. } else{
  373. it++;
  374. }
  375. }
  376. return nullptr;
  377. }
  378.  
  379. Book* Library::findBook( Book& book ) {
  380. auto it = books_.begin();
  381. while( it != books_.end() ) {
  382. if( book == it->second ) {
  383. return &it->second;
  384. } else {
  385. it++;
  386. }
  387. }
  388. return nullptr;
  389. }
  390.  
  391. std::multimap<std::string, Book> Library::mapOfBooks() const {
  392. return books_;
  393. }
  394.  
  395.  
  396. std::size_t Library::totalBooks() const {
  397. return books_.size();
  398. }
  399.  
  400. std::size_t Library::totalUniqueBooks() const {
  401. //TODO: For now just return total number of books
  402. return books_.size();
  403. }
  404.  
  405. std::size_t Library::calculateUniqueNumberOfBooks() const {
  406. //TODO: For now just return total number of books
  407. return books_.size();
  408. }
  409.  
  410. std::ostream& operator<<( std::ostream& out, const Library& library ) {
  411. for( auto b : library.mapOfBooks() ) {
  412. out << "ID " << b.first << 'n'
  413. << b.second;
  414. }
  415. return out;
  416. }
  417.  
  418. void displayLibrary( const Library& library ) {
  419. std::cout << library;
  420. }
  421.  
  422. Library::Library() {
  423. } // deafault [sic]
  424.  
  425. Book::Book() {
  426. } // default
  427.  
  428. Book::Book( const std::string& title, const std::string& author, unsigned int year ) :
  429. title_( title ),
  430. author_( author ),
  431. year_( year ) {
  432. }
  433.  
  434. Book( std::string title, std::string author, unsigned int year ) :
  435. title_{ std::move(title) },
  436. author_{ std::move(author) },
  437. year_{ year }
  438. { }
  439.  
  440. // Three different ways to return the library back to user
  441. std::multimap<std::string, Book> mapOfBooks() const;
  442.  
  443. // Currently Supports List and Vector
  444. template< template < class ... > class Container, class ... Args >
  445. void listOfBooks( Container<Book, Args...>& c ) const;
  446.  
  447. template<template<class...> class Container, class...Args>
  448. void Library::listOfBooks( Container<Book, Args...>& c ) const {
  449. auto it = books_.begin();
  450. while ( it != books_.end() ) {
  451. c.emplace_back( it->second );
  452. }
  453. }
  454.  
  455. void Library::removeBook( const std::string& id ) {
  456. auto it = books_.begin();
  457. while( it != books_.end() ) {
  458. if( id == it->first ) {
  459. // found match so remove it
  460. it = books_.erase( it );
  461. } else {
  462. it++;
  463. }
  464. }
  465. }
Add Comment
Please, Sign In to add comment