Advertisement
Guest User

Problem 2

a guest
May 26th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.20 KB | None | 0 0
  1. #include <iostream>
  2. #include <string.h>
  3. #include <map>
  4.  
  5. using namespace std;
  6.  
  7. class Book {
  8. private:
  9.     string title;
  10. public:
  11.     Book(string title = " ") {
  12.         this->title = title;
  13.     }
  14.  
  15.     string getTitle() {return title;}
  16. };
  17.  
  18. class BookLibrary {
  19. private:
  20.     multimap<string, Book> books;
  21. public:
  22.     BookLibrary(multimap<string, Book> input) {
  23.         multimap<string, Book>::iterator it;
  24.  
  25.         for(it = input.begin(); it != input.end(); it++) {
  26.             books.insert(pair<string, Book>(it->first, it->second));
  27.         }
  28.  
  29.     }
  30.  
  31.     void searchBook(string input) {
  32.         multimap<string, Book>::iterator it;
  33.  
  34.         for(it = books.begin(); it != books.end(); it++) {
  35.             if(it->second.getTitle() == input) {
  36.                 cout << "Book found: " << it->second.getTitle() << "; Author: " << it->first << endl;
  37.             }
  38.         }
  39.     }
  40.  
  41.     void addBook(string author, string name) {
  42.         books.insert(pair<string, Book>(author, name));
  43.         cout << "New book has been added" << endl;
  44.     }
  45.  
  46.     void printBooks() {
  47.         multimap<string, Book>::iterator it;
  48.         int i = 1;
  49.         for(it = books.begin(); it != books.end(); it++) {
  50.             cout << i << ". Author: " << it->first << "; Title: " << it->second.getTitle() << endl;
  51.             i++;
  52.         }
  53.     }
  54.  
  55.     void removeBook(Book &b) {
  56.         multimap<string, Book>::iterator it;
  57.  
  58.         for(it = books.begin(); it != books.end(); it++) {
  59.             if(b.getTitle() == it->second.getTitle()) {
  60.                 books.erase(it);
  61.                 cout << b.getTitle() << " found and removed." << endl;
  62.             }
  63.         }
  64.     }
  65.  
  66. };
  67.  
  68. int main()
  69. {
  70.     multimap<string, Book> books;
  71.  
  72.     Book b1("Book1");
  73.     Book b2("Book2");
  74.     Book b3("Book3");
  75.  
  76.     books.insert(pair<string, Book>("Maki", b1));
  77.     books.insert(pair<string, Book>("Slaki", b2));
  78.     books.insert(pair<string, Book>("Traki", b3));
  79.  
  80.     BookLibrary bl1(books);
  81.  
  82.     bl1.printBooks();
  83.     cout << endl;
  84.     bl1.addBook("Zaki", "Book4");
  85.     cout << endl;
  86.     bl1.printBooks();
  87.     cout << endl;
  88.     bl1.removeBook(b1);
  89.     bl1.printBooks();
  90.     bl1.searchBook("Book2");
  91.  
  92.     return 0;
  93. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement