Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <ostream>
- #include <string>
- #include <utility>
- #include <set>
- #include <fstream>
- #include <vector>
- #include <sstream>
- const std::string filename = "E:\\TempProject\\Database.dat";
- using namespace std;
- class Book {
- private:
- vector<string> author_names;
- string date;
- bool is_available;
- public:
- Book(string date, bool is_available);
- Book();
- vector<string> get_author_names() const;
- void add_author(const string& author_name);
- string get_date() const;
- void set_date(const string& date);
- bool get_is_available() const;
- void set_is_available(bool is_available);
- friend ostream& operator<<(ostream& stream, const Book& other);
- bool operator<(const Book& other) const;
- };
- Book::Book(string date, bool is_available)
- : date(std::move(date)),
- is_available(is_available) {}
- Book::Book()
- : date(""),
- is_available(false) { }
- vector<string> Book::get_author_names() const {
- return this->author_names;
- }
- void Book::add_author(const string& author_name) {
- this->author_names.push_back(author_name);
- }
- string Book::get_date() const {
- return this->date;
- }
- void Book::set_date(const string& date) {
- this->date = date;
- }
- bool Book::get_is_available() const {
- return this->is_available;
- }
- void Book::set_is_available(bool is_available) {
- this->is_available = is_available;
- }
- ostream& operator<<(ostream& stream, const Book& other) {
- for (const string& author : other.author_names) {
- stream << author << " ";
- }
- stream << other.date << " " << other.is_available;
- return stream;
- }
- bool Book::operator<(const Book& other) const {
- return this->author_names[0] < other.author_names[0];
- }
- class Library {
- private:
- int count_books = 0;
- public:
- set<Book> books;
- };
- int main() {
- Library library = Library();
- ifstream input(filename);
- string line;
- while (getline(input, line)) {
- istringstream reader(line);
- string str;
- std::vector<string> parsedWords;
- while (reader >> str) {
- parsedWords.push_back(str);
- }
- Book book(parsedWords[parsedWords.size() - 2], parsedWords[parsedWords.size() - 1] == "true");
- for (int i = 0; i < parsedWords.size() - 2; ++i) {
- book.add_author(parsedWords[i]);
- }
- library.books.insert(book);
- }
- std::cout << "Books by authors: " << std::endl;
- for (const auto& item : library.books) {
- cout << item << endl;
- }
- std::cout << std::endl;
- std::cout << "Enter date in following format -> dd.MM.yyyy: ";
- string date;
- cin >> date;
- std::cout << "Books taken by given date: " << std::endl;
- for (const auto& item : library.books) {
- if (item.get_date() == date) {
- cout << item << endl;
- }
- }
- ofstream out("ManyAuthorsOut.dat");
- for (const auto& item : library.books) {
- if (item.get_author_names().size() > 1) {
- out << item;
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement