1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
| #pragma once #include "../storage/Database.hpp" #include "../utils/Logger.hpp" #include "../utils/Config.hpp" #include <memory> #include <string> #include <random> #include <sstream>
class Library { private: std::unique_ptr<Database> database_; std::unique_ptr<Logger> logger_; std::unique_ptr<Config> config_; std::string generate_record_id() { static std::random_device rd; static std::mt19937 gen(rd()); static std::uniform_int_distribution<> dis(100000, 999999); std::stringstream ss; ss << "REC" << dis(gen); return ss.str(); } public: Library(std::unique_ptr<Database> database, std::unique_ptr<Logger> logger, std::unique_ptr<Config> config) : database_(std::move(database)) , logger_(std::move(logger)) , config_(std::move(config)) { logger_->info("Library system initialized"); } bool register_user(const std::string& user_id, const std::string& name, const std::string& email, const std::string& phone, UserType type = UserType::READER) { if (database_->load_user(user_id)) { logger_->warning("User already exists:", user_id); return false; } User user(user_id, name, email, phone, type); bool success = database_->save_user(user); if (success) { logger_->info("User registered:", user_id, name); } else { logger_->error("Failed to register user:", user_id); } return success; } std::unique_ptr<User> get_user(const std::string& user_id) { return database_->load_user(user_id); } std::vector<std::unique_ptr<User>> get_all_users() { return database_->load_all_users(); } bool update_user(const User& user) { logger_->info("Updating user:", user.user_id()); return database_->save_user(user); } bool delete_user(const std::string& user_id) { auto records = database_->load_user_borrow_records(user_id); for (const auto& record : records) { if (!record->is_returned()) { logger_->warning("Cannot delete user with unreturned books:", user_id); return false; } } bool success = database_->delete_user(user_id); if (success) { logger_->info("User deleted:", user_id); } return success; } bool add_book(const std::string& isbn, const std::string& title, const std::string& author, const std::string& publisher, int year, const std::string& category) { if (database_->load_book(isbn)) { logger_->warning("Book already exists:", isbn); return false; } Book book(isbn, title, author, publisher, year, category); bool success = database_->save_book(book); if (success) { logger_->info("Book added:", isbn, title); } else { logger_->error("Failed to add book:", isbn); } return success; } std::unique_ptr<Book> get_book(const std::string& isbn) { return database_->load_book(isbn); } std::vector<std::unique_ptr<Book>> get_all_books() { return database_->load_all_books(); } std::vector<std::unique_ptr<Book>> search_books_by_title(const std::string& title) { return database_->search_books([&title](const Book& book) { return book.title().find(title) != std::string::npos; }); } std::vector<std::unique_ptr<Book>> search_books_by_author(const std::string& author) { return database_->search_books([&author](const Book& book) { return book.author().find(author) != std::string::npos; }); } std::vector<std::unique_ptr<Book>> search_books_by_category(const std::string& category) { return database_->search_books([&category](const Book& book) { return book.category() == category; }); } bool update_book(const Book& book) { logger_->info("Updating book:", book.isbn()); return database_->save_book(book); } bool delete_book(const std::string& isbn) { auto book = database_->load_book(isbn); if (!book) { return false; } if (book->is_borrowed()) { logger_->warning("Cannot delete borrowed book:", isbn); return false; } bool success = database_->delete_book(isbn); if (success) { logger_->info("Book deleted:", isbn); } return success; } bool borrow_book(const std::string& user_id, const std::string& isbn) { auto user = database_->load_user(user_id); if (!user || !user->can_borrow_books()) { logger_->warning("User cannot borrow books:", user_id); return false; } auto book = database_->load_book(isbn); if (!book || !book->is_available()) { logger_->warning("Book not available for borrowing:", isbn); return false; } auto user_records = database_->load_user_borrow_records(user_id); int active_borrows = 0; for (const auto& record : user_records) { if (!record->is_returned()) { active_borrows++; } } int max_borrows = config_->get_int("max_books_per_user", 5); if (active_borrows >= max_borrows) { logger_->warning("User has reached borrowing limit:", user_id); return false; } std::string record_id = generate_record_id(); DateTime borrow_date = DateTime::now(); int borrow_days = config_->get_int("default_borrow_days", 14); DateTime due_date = borrow_date.add_days(borrow_days); BorrowRecord record(record_id, user_id, isbn, borrow_date, due_date); book->borrow(user_id); bool record_saved = database_->save_borrow_record(record); bool book_updated = database_->save_book(*book); if (record_saved && book_updated) { logger_->info("Book borrowed:", user_id, isbn, "due:", due_date.to_string()); return true; } else { logger_->error("Failed to complete borrowing:", user_id, isbn); return false; } } bool return_book(const std::string& user_id, const std::string& isbn) { auto user_records = database_->load_user_borrow_records(user_id); BorrowRecord* active_record = nullptr; for (const auto& record : user_records) { if (record->isbn() == isbn && !record->is_returned()) { active_record = record.get(); break; } } if (!active_record) { logger_->warning("No active borrow record found:", user_id, isbn); return false; } auto book = database_->load_book(isbn); if (!book || !book->is_borrowed()) { logger_->warning("Book is not currently borrowed:", isbn); return false; } active_record->return_book(); book->return_book(); bool record_updated = database_->save_borrow_record(*active_record); bool book_updated = database_->save_book(*book); if (record_updated && book_updated) { if (active_record->is_overdue()) { logger_->info("Book returned (overdue):", user_id, isbn, "fine:", active_record->fine_amount()); } else { logger_->info("Book returned:", user_id, isbn); } return true; } else { logger_->error("Failed to complete return:", user_id, isbn); return false; } } std::vector<std::unique_ptr<BorrowRecord>> get_user_borrow_history(const std::string& user_id) { return database_->load_user_borrow_records(user_id); } std::vector<std::unique_ptr<BorrowRecord>> get_overdue_books() { auto all_records = database_->load_all_borrow_records(); std::vector<std::unique_ptr<BorrowRecord>> overdue; for (auto& record : all_records) { if (!record->is_returned() && record->is_overdue()) { overdue.push_back(std::move(record)); } } return overdue; } struct LibraryStats { int total_books = 0; int available_books = 0; int borrowed_books = 0; int total_users = 0; int active_users = 0; int overdue_books = 0; double total_fines = 0.0; }; LibraryStats get_statistics() { LibraryStats stats; auto books = database_->load_all_books(); stats.total_books = static_cast<int>(books.size()); for (const auto& book : books) { if (book->is_available()) { stats.available_books++; } else if (book->is_borrowed()) { stats.borrowed_books++; } } auto users = database_->load_all_users(); stats.total_users = static_cast<int>(users.size()); for (const auto& user : users) { if (user->is_active()) { stats.active_users++; } } auto records = database_->load_all_borrow_records(); for (const auto& record : records) { if (!record->is_returned() && record->is_overdue()) { stats.overdue_books++; } stats.total_fines += record->fine_amount(); } return stats; } };
|