LMS
unknown
c_cpp
a year ago
9.7 kB
6
Indexable
class Book { public: Book(const string& title, const string& author, const string& genre, const string& isbn) : title(title), author(author), genre(genre), isbn(isbn), total_copies(0), available_copies(0) {} // Getters string getTitle() const { return title; } string getAuthor() const { return author; } string getGenre() const { return genre; } string getIsbn() const { return isbn; } int getTotalCopies() const { return total_copies; } int getAvailableCopies() const { return available_copies; } // Setters void setTitle(const string& title) { this->title = title; } void setAuthor(const string& author) { this->author = author; } void setGenre(const string& genre) { this->genre = genre; } void setIsbn(const string& isbn) { this->isbn = isbn; } void addCopy() { ++total_copies; ++available_copies; } void removeCopy() { if (total_copies > 0) { --total_copies; if (available_copies > 0) { --available_copies; } } } void borrowCopy() { if (available_copies > 0) { --available_copies; } } void returnCopy() { if (available_copies < total_copies) { ++available_copies; } } private: string title; string author; string genre; string isbn; int total_copies; int available_copies; }; class Patron { public: Patron(const string& name, int id) : name(name), patron_id(id) {} // Getters string getName() const { return name; } int getPatronId() const { return patron_id; } unordered_map<int, time_t> getBorrowedBooks() const { return borrowed_books; } // Setters void setName(const string& name) { this->name = name; } void setPatronId(int id) { this->patron_id = id; } void addBorrowedBook(int copy_id, time_t due_date) { borrowed_books[copy_id] = due_date; } void removeBorrowedBook(int copy_id) { borrowed_books.erase(copy_id); } private: string name; int patron_id; unordered_map<int, time_t> borrowed_books; // Copy ID and due date }; class Library { public: void addBook(const Book& book) { books_by_isbn[book.getIsbn()] = book; books_by_title[book.getTitle()].insert(book.getIsbn()); books_by_author[book.getAuthor()].insert(book.getIsbn()); books_by_genre[book.getGenre()].insert(book.getIsbn()); } void addBookCopy(const string& isbn) { if (books_by_isbn.find(isbn) != books_by_isbn.end()) { books_by_isbn[isbn].addCopy(); } else { cerr << "Book does not exist in the catalog.\n"; } } void addPatron(const Patron& patron) { patrons[patron.getPatronId()] = patron; } void borrowBook(int patron_id, const string& isbn) { if (patrons.find(patron_id) == patrons.end()) { cerr << "Patron does not exist.\n"; return; } if (books_by_isbn.find(isbn) == books_by_isbn.end()) { cerr << "Book does not exist in the catalog.\n"; return; } Book& book = books_by_isbn[isbn]; if (book.getAvailableCopies() <= 0) { cerr << "No available copy of the book.\n"; return; } book.borrowCopy(); time_t now = time(0); time_t due_date = now + 14 * 24 * 60 * 60; // 2 weeks patrons[patron_id].addBorrowedBook(patron_next_copy_id++, due_date); } void returnBook(int patron_id, int copy_id) { if (patrons.find(patron_id) == patrons.end()) { cerr << "Patron does not exist.\n"; return; } Patron& patron = patrons[patron_id]; if (patron.getBorrowedBooks().find(copy_id) == patron.getBorrowedBooks().end()) { cerr << "Book copy was not borrowed by this patron.\n"; return; } int book_copy_id = copy_id; // Use a dummy copy_id, real copies aren't tracked for (auto& pair : books_by_isbn) { Book& book = pair.second; if (book.getAvailableCopies() < book.getTotalCopies()) { book.returnCopy(); patron.removeBorrowedBook(copy_id); return; } } } vector<Book> searchBooks(const string& title = "", const string& author = "", const string& genre = "", const string& isbn = "") { vector<Book> results; if (!isbn.empty() && books_by_isbn.find(isbn) != books_by_isbn.end()) { results.push_back(books_by_isbn[isbn]); } else { unordered_set<string> isbn_set; if (!title.empty() && books_by_title.find(title) != books_by_title.end()) { isbn_set = books_by_title[title]; } if (!author.empty() && books_by_author.find(author) != books_by_author.end()) { if (isbn_set.empty()) { isbn_set = books_by_author[author]; } else { unordered_set<string> temp_set; for (const auto& isbn : books_by_author[author]) { if (isbn_set.find(isbn) != isbn_set.end()) { temp_set.insert(isbn); } } isbn_set = temp_set; } } if (!genre.empty() && books_by_genre.find(genre) != books_by_genre.end()) { if (isbn_set.empty()) { isbn_set = books_by_genre[genre]; } else { unordered_set<string> temp_set; for (const auto& isbn : books_by_genre[genre]) { if (isbn_set.find(isbn) != isbn_set.end()) { temp_set.insert(isbn); } } isbn_set = temp_set; } } for (const auto& isbn : isbn_set) { if (books_by_isbn.find(isbn) != books_by_isbn.end()) { results.push_back(books_by_isbn[isbn]); } } } return results; } void listBorrowedBooks(int patron_id) { if (patrons.find(patron_id) == patrons.end()) { cerr << "Patron does not exist.\n"; return; } Patron& patron = patrons[patron_id]; for (const auto& pair : patron.getBorrowedBooks()) { int copy_id = pair.first; time_t due_date = pair.second; cout << "Copy ID: " << copy_id << ", Due Date: " << put_time(localtime(&due_date), "%Y-%m-%d %H:%M:%S") << '\n'; } } void listOverdueBooks() { time_t now = time(0); for (const auto& pair : patrons) { const Patron& patron = pair.second; for (const auto& book_pair : patron.getBorrowedBooks()) { int copy_id = book_pair.first; time_t due_date = book_pair.second; if (due_date < now) { cout << "Patron: " << patron.getName() << ", Copy ID: " << copy_id << ", Due Date: " << put_time(localtime(&due_date), "%Y-%m-%d %H:%M:%S") << '\n'; } } } } private: unordered_map<string, Book> books_by_isbn; // ISBN to Book unordered_map<string, unordered_set<string>> books_by_title; // Title to set of ISBNs unordered_map<string, unordered_set<string>> books_by_author; // Author to set of ISBNs unordered_map<string, unordered_set<string>> books_by_genre; // Genre to set of ISBNs unordered_map<int, Patron> patrons; // Patron ID to Patron int patron_next_copy_id = 0; // Simulate copy ID for borrowing }; int main() { Library library; // Create books Book book1("The Great Gatsby", "F. Scott Fitzgerald", "Fiction", "9780743273565"); Book book2("1984", "George Orwell", "Dystopian", "9780451524935"); // Add books to the library library.addBook(book1); library.addBook(book2); // Add copies of books library.addBookCopy("9780743273565"); // Add one copy of "The Great Gatsby" library.addBookCopy("9780743273565"); // Add another copy of "The Great Gatsby" library.addBookCopy("9780451524935"); // Add one copy of "1984" // Create patrons Patron patron1("Alice", 1); Patron patron2("Bob", 2); // Add patrons to the library library.addPatron(patron1); library.addPatron(patron2); // Borrow books library.borrowBook(1, "9780743273565"); // Alice borrows "The Great Gatsby" library.borrowBook(2, "9780451524935"); // Bob borrows "1984" // List borrowed books for a patron cout << "Books borrowed by Alice:\n"; library.listBorrowedBooks(1); // List overdue books (none should be overdue yet) cout << "\nOverdue books:\n"; library.listOverdueBooks(); // Return books library.returnBook(1, 0); // Alice returns "The Great Gatsby" copy ID 0 (simulated) // List borrowed books for a patron after returning a book cout << "\nBooks borrowed by Alice after returning:\n"; library.listBorrowedBooks(1); // Search for books cout << "\nSearching for books by title '1984':\n"; vector<Book> search_results = library.searchBooks("1984"); for (const auto& book : search_results) { cout << "Found Book: " << book.getTitle() << ", Author: " << book.getAuthor() << ", Genre: " << book.getGenre() << ", ISBN: " << book.getIsbn() << ", Total Copies: " << book.getTotalCopies() << ", Available Copies: " << book.getAvailableCopies() << '\n'; } return 0; }
Editor is loading...
Leave a Comment