Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <string.h>
- #include <map>
- using namespace std;
- class Book {
- private:
- string title;
- public:
- Book(string title = " ") {
- this->title = title;
- }
- string getTitle() {return title;}
- };
- class BookLibrary {
- private:
- multimap<string, Book> books;
- public:
- BookLibrary(multimap<string, Book> input) {
- multimap<string, Book>::iterator it;
- for(it = input.begin(); it != input.end(); it++) {
- books.insert(pair<string, Book>(it->first, it->second));
- }
- }
- void searchBook(string input) {
- multimap<string, Book>::iterator it;
- for(it = books.begin(); it != books.end(); it++) {
- if(it->second.getTitle() == input) {
- cout << "Book found: " << it->second.getTitle() << "; Author: " << it->first << endl;
- }
- }
- }
- void addBook(string author, string name) {
- books.insert(pair<string, Book>(author, name));
- cout << "New book has been added" << endl;
- }
- void printBooks() {
- multimap<string, Book>::iterator it;
- int i = 1;
- for(it = books.begin(); it != books.end(); it++) {
- cout << i << ". Author: " << it->first << "; Title: " << it->second.getTitle() << endl;
- i++;
- }
- }
- void removeBook(Book &b) {
- multimap<string, Book>::iterator it;
- for(it = books.begin(); it != books.end(); it++) {
- if(b.getTitle() == it->second.getTitle()) {
- books.erase(it);
- cout << b.getTitle() << " found and removed." << endl;
- }
- }
- }
- };
- int main()
- {
- multimap<string, Book> books;
- Book b1("Book1");
- Book b2("Book2");
- Book b3("Book3");
- books.insert(pair<string, Book>("Maki", b1));
- books.insert(pair<string, Book>("Slaki", b2));
- books.insert(pair<string, Book>("Traki", b3));
- BookLibrary bl1(books);
- bl1.printBooks();
- cout << endl;
- bl1.addBook("Zaki", "Book4");
- cout << endl;
- bl1.printBooks();
- cout << endl;
- bl1.removeBook(b1);
- bl1.printBooks();
- bl1.searchBook("Book2");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement