Advertisement
desislava_topuzakova

Library

Jun 11th, 2020
1,917
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.11 KB | None | 0 0
  1. #include <iostream>
  2. #include <ostream>
  3. #include <string>
  4. #include <utility>
  5. #include <set>
  6. #include <fstream>
  7. #include <vector>
  8. #include <sstream>
  9.  
  10. const std::string filename = "E:\\TempProject\\Database.dat";
  11.  
  12. using namespace std;
  13.  
  14. class Book {
  15. private:
  16.     vector<string> author_names;
  17.     string date;
  18.     bool is_available;
  19. public:
  20.     Book(string date, bool is_available);
  21.  
  22.     Book();
  23.  
  24.     vector<string> get_author_names() const;
  25.  
  26.     void add_author(const string& author_name);
  27.  
  28.     string get_date() const;
  29.  
  30.     void set_date(const string& date);
  31.  
  32.     bool get_is_available() const;
  33.  
  34.     void set_is_available(bool is_available);
  35.  
  36.     friend ostream& operator<<(ostream& stream, const Book& other);
  37.  
  38.     bool operator<(const Book& other) const;
  39. };
  40.  
  41. Book::Book(string date, bool is_available)
  42.     : date(std::move(date)),
  43.     is_available(is_available) {}
  44.  
  45. Book::Book()
  46.     : date(""),
  47.     is_available(false) { }
  48.  
  49. vector<string> Book::get_author_names() const {
  50.     return this->author_names;
  51. }
  52.  
  53. void Book::add_author(const string& author_name) {
  54.     this->author_names.push_back(author_name);
  55. }
  56.  
  57. string Book::get_date() const {
  58.     return this->date;
  59. }
  60.  
  61. void Book::set_date(const string& date) {
  62.     this->date = date;
  63. }
  64.  
  65. bool Book::get_is_available() const {
  66.     return this->is_available;
  67. }
  68.  
  69. void Book::set_is_available(bool is_available) {
  70.     this->is_available = is_available;
  71. }
  72.  
  73. ostream& operator<<(ostream& stream, const Book& other) {
  74.  
  75.     for (const string& author : other.author_names) {
  76.         stream << author << " ";
  77.     }
  78.  
  79.     stream << other.date << " " << other.is_available;
  80.     return stream;
  81. }
  82.  
  83. bool Book::operator<(const Book& other) const {
  84.     return this->author_names[0] < other.author_names[0];
  85. }
  86.  
  87. class Library {
  88. private:
  89.     int count_books = 0;
  90. public:
  91.     set<Book> books;
  92. };
  93.  
  94. int main() {
  95.     Library library = Library();
  96.  
  97.     ifstream input(filename);
  98.  
  99.     string line;
  100.  
  101.     while (getline(input, line)) {
  102.  
  103.         istringstream reader(line);
  104.  
  105.         string str;
  106.  
  107.         std::vector<string> parsedWords;
  108.  
  109.         while (reader >> str) {
  110.             parsedWords.push_back(str);
  111.         }
  112.  
  113.         Book book(parsedWords[parsedWords.size() - 2], parsedWords[parsedWords.size() - 1] == "true");
  114.  
  115.         for (int i = 0; i < parsedWords.size() - 2; ++i) {
  116.             book.add_author(parsedWords[i]);
  117.         }
  118.  
  119.         library.books.insert(book);
  120.     }
  121.  
  122.     std::cout << "Books by authors: " << std::endl;
  123.  
  124.     for (const auto& item : library.books) {
  125.         cout << item << endl;
  126.     }
  127.  
  128.     std::cout << std::endl;
  129.     std::cout << "Enter date in following format -> dd.MM.yyyy: ";
  130.     string date;
  131.     cin >> date;
  132.  
  133.     std::cout << "Books taken by given date: " << std::endl;
  134.  
  135.     for (const auto& item : library.books) {
  136.         if (item.get_date() == date) {
  137.             cout << item << endl;
  138.         }
  139.     }
  140.  
  141.     ofstream out("ManyAuthorsOut.dat");
  142.  
  143.     for (const auto& item : library.books) {
  144.         if (item.get_author_names().size() > 1) {
  145.             out << item;
  146.         }
  147.     }
  148. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement