/* * @Author: Student author@example.com * @Date: 2025-01-01 14:58:43 * @LastEditors: Student author@example.com * @LastEditTime: 2025-01-03 10:50:41 * @FilePath: \LibraryManageSystem\include\Book.h * @Description: Coding with UTF-8 * * Copyright (c) 2025 by Student, All Rights Reserved. */ #ifndef BOOK_H #define BOOK_H #include "STL/LinkList.h" #include // 图书类别枚举 enum class BookCategory { LITERATURE = 1, // 文学 SCIENCE = 2, // 科学 TECHNOLOGY = 3, // 技术 HISTORY = 4, // 历史 PHILOSOPHY = 5, // 哲学 ART = 6, // 艺术 ECONOMICS = 7, // 经济 OTHER = 8 // 其他 }; class Book { private: std::string bookId; // 书号 std::string title; // 书名 std::string author; // 作者 std::string library; // 所在书库 std::string description; // 图书简介 BookCategory category; // 图书类别 int currentStock; // 现存量 int totalStock; // 库存量 LinkList borrowerPhones; // 借阅者手机号列表 LinkList borrowDates; // 借阅日期列表 LinkList ratings; // 用户评分列表 double averageRating; // 平均评分 public: // 构造函数 Book(const std::string &id, const std::string &title, const std::string &author, const std::string &library, BookCategory category, const std::string &desc = "", int stock = 1); // 创建一个新的图书对象 // Getter方法 std::string getBookId() const { return bookId; } // 获取图书ID std::string getTitle() const { return title; } // 获取图书标题 std::string getAuthor() const { return author; } // 获取图书作者 std::string getLibrary() const { return library; } // 获取所在图书馆 std::string getDescription() const { return description; } // 获取图书描述 BookCategory getCategory() const { return category; } // 获取图书类别 int getCurrentStock() const { return currentStock; } // 获取当前库存 int getTotalStock() const { return totalStock; } // 获取总库存 double getAverageRating() const { return averageRating; } // 获取平均评分 // 图书操作方法 bool borrow(const std::string &userPhone); // 借出图书 bool returnBook(const std::string &userPhone); // 归还图书 bool isOverdue(const std::string &userPhone, int maxDays = 30) const; // 检查是否超期 bool isAvailable() const; // 检查是否可借 // 评分相关 void addRating(int rating); // 添加评分(1-5) void calculateAverageRating(); // 计算平均评分 // 修改图书信息 void updateInfo(const std::string &newTitle, const std::string &newAuthor, const std::string &newLibrary, const std::string &newDesc); // 更新图书基本信息 void updateStock(int newStock); // 更新库存数量 // 获取借阅次数 int getBorrowCount() const { return borrowerPhones.length(); } // 获取借阅次数 int getRatingCount() const { return ratings.length(); } // 获取评分数量 }; // 图书管理类 class BookManager { private: LinkList books; // 存储所有图书 std::string dataFile; // 数据文件路径 unsigned int BKDRHash(const std::string &str) const; // 计算字符串的哈希值 public: BookManager(const std::string &filename = "books.dat"); // 构造函数 ~BookManager(); // 析构函数 // 基本操作 bool addBook(Book *book); // 添加新图书 bool removeBook(const std::string &bookId); // 删除图书 Book *findBook(const std::string &bookId) const; // 查找图书 void displayAllBooks() const; // 显示所有图书 // 文件操作 bool saveToFile() const; // 保存数据到文件 bool loadFromFile(); // 从文件加载数据 // 借阅相关 LinkList getOverdueBooks() const; // 获取所有超期图书 LinkList searchBooks(const std::string &keyword) const; // 搜索图书 bool borrowBook(const std::string &bookId, const std::string &userPhone); // 借出图书 bool returnBook(const std::string &bookId, const std::string &userPhone); // 归还图书 const LinkList &getBooks() const { return books; } // 获取所有图书列表 Book *findBookBinary(const std::string &bookId) const; // 二分查找图书 Book *findBookByTitleHash(const std::string &title) const; // 哈希查找图书 }; #endif // BOOK_H