Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<fstream>
- #include<string>
- #include<map>
- #include<utility>
- #include<vector>
- #include<algorithm>
- #include<stdlib.h>
- using namespace std;
- void clear_screen() { system("CLS"); }
- void pause(istream& in)
- {
- if(in == cin) system("pause");
- }
- class Exception
- {
- private:
- string content;
- public:
- Exception(string message) : content(message) {}
- string Message()
- {
- return content;
- }
- };
- class Book
- {
- private:
- string name;
- string content;
- public:
- Book(string name, string content) : name(name), content(content) {}
- string Name()
- {
- return name;
- }
- string Text()
- {
- return content;
- }
- bool operator== (const Book &b) const
- {
- return content == b.content;
- }
- bool operator< (const Book &b) const
- {
- return content < b.content;
- }
- };
- class AuthorBooks
- {
- private:
- string author;
- map<string, Book> content;
- public:
- AuthorBooks(string author) : author(author), content() {}
- inline bool HaveBook(string book_name)
- {
- return content.find(book_name) != content.end();
- }
- Book GetBook(string book_name)
- {
- map<string, Book>::iterator res = content.find(book_name);
- if(res == content.end()) throw Exception("Have not \"" + book_name + "\" book.");
- return (*res).second;
- }
- bool AddBook(string book_name, string book)
- {
- if(HaveBook(book_name)) return false;
- content.insert(pair<string, Book>(book_name, Book(book_name, book)));
- return true;
- }
- bool AddBook(Book book)
- {
- if(HaveBook(book.Name())) return false;
- content.insert(pair<string, Book>(book.Name(), book));
- return true;
- }
- bool RemoveBook(string book_name)
- {
- map<string, Book>::iterator i = content.find(book_name);
- if(i == content.end()) return false;
- content.erase(i);
- return true;
- }
- bool ChangeBook(string book_name, string book)
- {
- if(!RemoveBook(book_name)) return false;
- content.insert(pair<string, Book>(book_name, Book(book_name, book)));
- return true;
- }
- bool ChangeBook(string book_name, Book book)
- {
- if(!RemoveBook(book_name)) return false;
- content.insert(pair<string, Book>(book_name, book));
- return true;
- }
- vector<string> BooksNames()
- {
- vector<string> res;
- for(map<string, Book>::iterator i = content.begin(); i != content.end(); i++)
- {
- res.push_back((*i).first);
- }
- return res;
- }
- vector<string> FindInName(string word)
- {
- vector<string> res;
- for(map<string, Book>::iterator i = content.begin(); i != content.end(); i++)
- {
- if((*i).first.find(" " + word) != (*i).first.npos)
- {
- res.push_back((*i).first);
- continue;
- }
- if((*i).first.find(word + " ") != (*i).first.npos)
- {
- res.push_back((*i).first);
- continue;
- }
- if((*i).first == word)
- {
- res.push_back((*i).first);
- continue;
- }
- }
- return res;
- }
- bool operator== (const AuthorBooks &b) const
- {
- return content == b.content;
- }
- bool operator< (const AuthorBooks &b) const
- {
- return content < b.content;
- }
- };
- class Author
- {
- private:
- string name;
- int death_year;
- public:
- Author(string name) : name(name), death_year(-1) {}
- Author(string name, int year) : name(name), death_year(year) {}
- string Name()
- {
- return name;
- }
- int Year()
- {
- return death_year;
- }
- bool operator== (const Author &b) const
- {
- return name == b.name && death_year == b.death_year;
- }
- bool operator< (const Author &b) const
- {
- return (name < b.name) || ((name == b.name) && death_year < b.death_year);
- }
- };
- class Library
- {
- private:
- map<Author, AuthorBooks*> content;
- struct check_author
- {
- private:
- string t;
- public:
- check_author(string check_name)
- {
- t = check_name;
- }
- bool operator()(pair<Author, AuthorBooks*> a) const
- {
- return a.first.Name() == t;
- }
- };
- public:
- Library() : content() {}
- inline bool HaveAuthor(string author_name)
- {
- return find_if(content.begin(), content.end(), check_author(author_name)) != content.end();
- }
- AuthorBooks* GetBooks(string author_name)
- {
- map<Author, AuthorBooks*>::iterator res = find_if(content.begin(), content.end(), check_author(author_name));
- if(res == content.end()) throw Exception("Have not author " + author_name + ".");
- return res->second;
- }
- bool AddAuthor(string author_name)
- {
- if(HaveAuthor(author_name)) return false;
- content.insert(pair<Author, AuthorBooks*>(Author(author_name), new AuthorBooks(author_name)));
- return true;
- }
- bool UpdateAuthor(string author_name, int year)
- {
- map<Author, AuthorBooks*>::iterator i = find_if(content.begin(), content.end(), check_author(author_name));
- if(i == content.end()) return false;
- AuthorBooks* t = (*i).second;
- content.erase(i);
- content.insert(pair<Author, AuthorBooks*>(Author(author_name, year), t));
- return true;
- }
- bool RemoveAuthor(string author_name)
- {
- map<Author, AuthorBooks*>::iterator i = find_if(content.begin(), content.end(), check_author(author_name));
- if(i == content.end()) return false;
- content.erase(i);
- return true;
- }
- vector<string> AuthorsNames()
- {
- vector<string> res;
- for(map<Author, AuthorBooks*>::iterator i = content.begin(); i != content.end(); i++)
- {
- pair<Author, AuthorBooks*> p = *i;
- res.push_back(p.first.Name());
- }
- return res;
- }
- map<string, vector<string> > FindAllBooksContain(string word)
- {
- map<string, vector<string> > res;
- for(map<Author, AuthorBooks*>::iterator i = content.begin(); i != content.end(); i++)
- {
- pair<Author, AuthorBooks*> p = *i;
- vector<string> author_res = p.second->FindInName(word);
- if(author_res.size()) res.insert(pair<string, vector<string> >(p.first.Name(), author_res));
- }
- return res;
- }
- };
- class LibUI
- {
- private:
- Library lib;
- vector<string> print_author_list()
- {
- vector<string> res = lib.AuthorsNames();
- for(int i = 0; i < res.size(); i++)
- {
- cout << " " << i + 1 << ") " << res[i] << "\n";
- }
- return res;
- }
- void lib_start_page(istream& in)
- {
- while(true)
- {
- cout << " In our Archives presents the work of various authors.\n";
- cout << " What do you want? \n";
- cout << " 1) See the list of authors \n";
- cout << " 2) Add new author \n";
- cout << " 3) Read the author\'s books \n";
- cout << " 4) Update author \n";
- cout << " 5) Remove author \n";
- cout << " 6) Find books containing the word \n";
- cout << " 7) Return \n";
- cout << "> ";
- int input;
- in >> input;
- while(input < 1 || input > 7)
- {
- cout << " We have not this variant.\n";
- cout << " Please, select something from the above list.\n";
- cout << "> ";
- in >> input;
- }
- clear_screen();
- if(input == 1)
- {
- cout << " Archives contain the following authors: \n";
- vector<string> v = lib.AuthorsNames();
- if(v.size() == 0)
- {
- cout << " No authors. \n";
- }
- for(int i = 0; i < v.size(); i++)
- {
- cout << " - " << v[i] << "\n";
- }
- pause(in);
- clear_screen();
- continue;
- }
- if(input == 2)
- {
- cout << " Enter author name:\n";
- cout << "> ";
- string s, ts;
- in >> s;
- getline(in, ts);
- s = s + " " + ts;
- add_new_author(in, s);
- continue;
- }
- if(input == 6)
- {
- cout << " Enter word:\n";
- cout << "> ";
- string s, ts;
- in >> s;
- getline(in, ts);
- s = s + " " + ts;
- map<string, vector<string> > res = lib.FindAllBooksContain("of");
- for(map<string, vector<string> >::iterator i = res.begin(); i != res.end(); i++)
- {
- vector<string> ts = i->second;
- for(int ti = 0; ti < ts.size(); ti++)
- {
- cout << " - " << i->first << ", " << ts[ti] << "\n";
- }
- }
- pause(in);
- clear_screen();
- continue;
- }
- if(input == 7)
- {
- clear_screen();
- break;
- }
- cout << " Choose the author you are interested in:\n";
- vector<string> authors = print_author_list();
- cout << " " << authors.size() + 1 << ") Return\n";
- cout << "> ";
- int input_2;
- in >> input_2;
- while(input_2 < 1 || input_2 > authors.size() + 1)
- {
- cout << " We have not this variant.\n";
- cout << " Please, select something from the above list.\n";
- cout << "> ";
- in >> input_2;
- }
- if(input_2 == authors.size() + 1)
- {
- clear_screen();
- continue;
- }
- switch(input)
- {
- case 3:
- clear_screen();
- use_author_books(in, authors[input_2 - 1]);
- break;
- case 4:
- update_author(in, authors[input_2 - 1]);
- break;
- case 5:
- remove_author(in, authors[input_2 - 1]);
- break;
- }
- }
- }
- void add_new_author(istream& in, string author_name)
- {
- if(lib.AddAuthor(author_name))
- {
- cout << " " << author_name << " added!\n";
- }
- else
- {
- cout << " Don't added.\n";
- }
- pause(in);
- clear_screen();
- }
- void update_author(istream& in, string author_name)
- {
- cout << " Enter the year of death:\n";
- cout << "> ";
- int year;
- in >> year;
- if(lib.UpdateAuthor(author_name, year))
- {
- cout << " " << author_name << " updated!\n";
- }
- else
- {
- cout << " Don't updated.\n";
- }
- pause(in);
- clear_screen();
- }
- void remove_author(istream& in, string author_name)
- {
- if(lib.RemoveAuthor(author_name))
- {
- cout << " " << author_name << " removed!\n";
- }
- else
- {
- cout << " Don't removed.\n";
- }
- pause(in);
- clear_screen();
- }
- vector<string> print_books(AuthorBooks* books)
- {
- vector<string> res = books->BooksNames();
- for(int i = 0; i < res.size(); i++)
- {
- cout << " " << i + 1 << ") " << res[i] << "\n";
- }
- return res;
- }
- void use_author_books(istream& in, string author_name)
- {
- AuthorBooks* books = lib.GetBooks(author_name);
- while(true)
- {
- cout << " You are in room of " << author_name << " \n";
- cout << " What do you want? \n";
- cout << " 1) View books \n";
- cout << " 2) Add book \n";
- cout << " 3) Read book \n";
- cout << " 4) Update book \n";
- cout << " 5) Remove book \n";
- cout << " 6) Return \n";
- cout << "> ";
- int input;
- in >> input;
- while(input < 1 || input > 6)
- {
- cout << " We have not this variant.\n";
- cout << " Please, select something from the above list.\n";
- cout << "> ";
- in >> input;
- }
- clear_screen();
- if(input == 1)
- {
- cout << " Room contain the following books: \n";
- vector<string> v = books->BooksNames();
- if(v.size() == 0)
- {
- cout << " No books. \n";
- }
- for(int i = 0; i < v.size(); i++)
- {
- cout << " - " << v[i] << "\n";
- }
- pause(in);
- clear_screen();
- continue;
- }
- if(input == 2)
- {
- add_book(in, books);
- pause(in);
- clear_screen();
- continue;
- }
- if(input == 6)
- {
- clear_screen();
- break;
- }
- cout << " Choose the book you are interested in:\n";
- vector<string> books_names = print_books(books);
- cout << " " << books_names.size() + 1 << ") Return\n";
- cout << "> ";
- int input_2;
- in >> input_2;
- while(input_2 < 1 || input_2 > books_names.size() + 1)
- {
- cout << " We have not this variant.\n";
- cout << " Please, select something from the above list.\n";
- cout << "> ";
- in >> input_2;
- }
- if(input_2 == books_names.size() + 1)
- {
- clear_screen();
- continue;
- }
- switch(input)
- {
- case 3:
- clear_screen();
- read_book(in, books, books_names[input_2 - 1]);
- break;
- case 4:
- update_book(in, books, books_names[input_2 - 1]);
- break;
- case 5:
- remove_book(in, books, books_names[input_2 - 1]);
- break;
- }
- }
- clear_screen();
- }
- void add_book(istream& in, AuthorBooks* books)
- {
- cout << " Enter book name: \n";
- cout << "> ";
- string s, ts;
- in >> s;
- getline(in, ts);
- string name = s + " " + ts;
- cout << " Enter book:\n";
- cout << "> ";
- in >> s;
- getline(in, ts);
- string book = s + " " + ts;
- if(books->AddBook(name, book))
- {
- cout << " " << name << " added! \n";
- }
- else
- {
- cout << " " << name << " not added. \n";
- }
- pause(in);
- clear_screen();
- }
- void read_book(istream& in, AuthorBooks* books, string name)
- {
- cout << books->GetBook(name).Text() << "\n";
- pause(in);
- clear_screen();
- }
- void update_book(istream& in, AuthorBooks* books, string name)
- {
- cout << " Enter new text for " << name << ":\n";
- cout << "> ";
- string s, ts;
- in >> s;
- getline(in, ts);
- string text = s + " " + ts;
- if(books->ChangeBook(name, text))
- {
- cout << " " << name << " updated! \n";
- }
- else
- {
- cout << " " << name << " not updated! \n";
- }
- pause(in);
- clear_screen();
- }
- void remove_book(istream& in, AuthorBooks* books, string name)
- {
- if(books->RemoveBook(name))
- {
- cout << " " << name << " removed! \n";
- }
- else
- {
- cout << " " << name << " not removed! \n";
- }
- pause(in);
- clear_screen();
- }
- public:
- LibUI() : lib() {}
- void Run(istream& in)
- {
- cout << " Greetings traveler. You are in the Eternal Archives, \n";
- cout << " the repository of the wisdom of many generations.\n";
- while(true)
- {
- cout << " Before you the door of the Archive. \n";
- cout << " What do you want? \n";
- cout << " 1) Go in Archive \n";
- cout << " 2) Exit \n";
- cout << "> ";
- int input;
- in >> input;
- while(input < 1 || input > 2)
- {
- cout << " We have not this variant.\n";
- cout << " Please, select something from the above list.\n";
- cout << "> ";
- in >> input;
- }
- clear_screen();
- if(input == 1)
- {
- lib_start_page(in);
- continue;
- }
- break; // if(input == 2)
- }
- cout << " Be careful traveler.\n";
- }
- };
- int main()
- {
- ifstream in("input.txt");
- /*
- Library lib;
- lib.AddAuthor("J.R.R.Tolkien");
- lib.GetBooks("J.R.R.Tolkien")->AddBook("Lord of the Rings I", "LOTR 1");
- lib.GetBooks("J.R.R.Tolkien")->AddBook("Lord of the Rings II", "LOTR 2");
- lib.GetBooks("J.R.R.Tolkien")->AddBook("Lord of the Rings III", "LOTR 3");
- lib.AddAuthor("L.N.Tolstoy");
- lib.GetBooks("L.N.Tolstoy")->AddBook("War and World", "WaW");
- lib.GetBooks("L.N.Tolstoy")->AddBook("Anna Karenina", "AK");
- lib.GetBooks("L.N.Tolstoy")->AddBook("Book of Letters", "BoL");
- lib.AddAuthor("A.P.4ehov");
- lib.GetBooks("A.P.4ehov")->AddBook("Garden of Cherry", "GoC");
- lib.GetBooks("A.P.4ehov")->AddBook("Men", "M");
- lib.GetBooks("A.P.4ehov")->AddBook("OliGofren", "OG");
- map<string, vector<string> > res = lib.FindAllBooksContain("of");
- for(map<string, vector<string> >::iterator i = res.begin(); i != res.end(); i++)
- {
- vector<string> ts = i->second;
- for(int ti = 0; ti < ts.size(); ti++)
- {
- cout << i->first << ", " << ts[ti] << "\n";
- }
- }
- */
- /*
- Input:
- 1
- 2
- J.R.R.Tolkien
- 3
- 1
- 2
- Lord of the Rings I
- LOTR 1
- 2
- Lord of the Rings II
- LOTR 2
- 2
- Lord of the Rings III
- LOTR 3
- 6
- 2
- L.N.Tolstoy
- 3
- 2
- 2
- War and World
- WaW
- 2
- Anna Karenina
- AK
- 2
- Book of Letters
- BoL
- 6
- 2
- A.P.4ehov
- 3
- 1
- 2
- Garden of Cherry
- GoC
- 2
- Men
- M
- 2
- OliGofren
- OG
- 6
- 7
- 2
- */
- LibUI lib;
- lib.Run(in);
- clear_screen();
- lib.Run(cin);
- //getc(stdin);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement