From 9df19f31f2a90022aad2c655b952acec0bbda05d Mon Sep 17 00:00:00 2001 From: ZhuangYumin Date: Wed, 13 Dec 2023 12:48:58 +0000 Subject: [PATCH] writing book system --- backend/include/database.h | 11 +++--- backend/include/lexer.h | 1 + backend/src/database.cpp | 70 ++++++++++++++++++++++++++++++++++++-- backend/src/lexer.cpp | 16 +++++++-- 4 files changed, 90 insertions(+), 8 deletions(-) diff --git a/backend/include/database.h b/backend/include/database.h index 0532b86..64c5d7e 100644 --- a/backend/include/database.h +++ b/backend/include/database.h @@ -23,16 +23,19 @@ class UserDataBase { }; class BookDataBase { DriveArray full_book_data; - String2Index indexer; + String2Index ISBN2index, name2index, author2index, keyword2index; public: void Open(std::string file_name); + bool HaveISBN(const std::string &ISBN); + void CreateEmptyBook(const std::string &ISBN); void QueryBook(const std::string &ISBN, const std::string &name, const std::string &author, const std::string &keyword, std::vector &ret); - void ModifyInfo(const std::string &ISBN,const std::string &new_ISBN, const std::string &name, - const std::string &author, const std::string &keyword, - double price); + void ModifyInfo(const std::string &ISBN, const std::string &new_ISBN, + const std::string &name, const std::string &author, + const std::string &keyword, double price, + int quantity_remain); }; class LogDataBase { DriveArray finance_data; diff --git a/backend/include/lexer.h b/backend/include/lexer.h index 235884f..b14d7f8 100644 --- a/backend/include/lexer.h +++ b/backend/include/lexer.h @@ -27,4 +27,5 @@ bool CommandModifyLexer(const std::string &command, std::string &ISBN, bool CommandImportLexer(const std::string &command, int &quantity, double &total_cost); bool CommandShowfinanceLexer(const std::string &command,int &count); +bool KeyWordSpliter(const std::string &keyword, std::vector &words); #endif // PROTECTOR_LEXER_H \ No newline at end of file diff --git a/backend/src/database.cpp b/backend/src/database.cpp index 313a9f7..a2cf7cb 100644 --- a/backend/src/database.cpp +++ b/backend/src/database.cpp @@ -8,7 +8,10 @@ void UserDataBase::Open(std::string file_name) { void BookDataBase::Open(std::string file_name) { full_book_data.OpenFile(file_name + ".full"); - indexer.OpenFile(file_name + ".n2i"); + ISBN2index.OpenFile(file_name + ".isbn"); + name2index.OpenFile(file_name + ".name"); + author2index.OpenFile(file_name + ".author"); + keyword2index.OpenFile(file_name + ".keyword"); } void LogDataBase::Open(std::string file_name) { @@ -23,7 +26,8 @@ bool UserDataBase::PAM(const std::string &user_id, if (ret.size() != 1) return false; UserItemClass tmp; full_user_data.read(tmp, ret[0]); - // debugPrint("Correct password: ", tmp.password, " Input password: ", password); + // debugPrint("Correct password: ", tmp.password, " Input password: ", + // password); return tmp.password == password; } @@ -66,4 +70,66 @@ void UserDataBase::ChangePassword(const std::string &user_id, full_user_data.read(tmp, ret[0]); strcpy(tmp.password, password.c_str()); full_user_data.update(tmp, ret[0]); +} + +bool BookDataBase::HaveISBN(const std::string &ISBN) { + auto ret = ISBN2index.Find(ISBN); + return ret.size() == 1; +} + +void BookDataBase::CreateEmptyBook(const std::string &ISBN) { + BookItemClass tmp; + strcpy(tmp.ISBN, ISBN.c_str()); + tmp.quantity_remain = tmp.price = 0; + tmp.name[0] = 0; + tmp.author[0] = 0; + tmp.keyword[0] = 0; + int idx = full_book_data.write(tmp); + ISBN2index.Insert(ISBN, idx); + name2index.Insert("", idx); + author2index.Insert("", idx); + keyword2index.Insert("", idx); +} + +void BookDataBase::ModifyInfo(const std::string &ISBN, + const std::string &new_ISBN, + const std::string &name, + const std::string &author, + const std::string &keyword, double price, + int quantity_remain) { + auto ret = ISBN2index.Find(ISBN); + if (ret.size() != 1) return; + BookItemClass tmp; + full_book_data.read(tmp, ret[0]); + if (new_ISBN != "") { + ISBN2index.Delete(ISBN, ret[0]); + ISBN2index.Insert(new_ISBN, ret[0]); + strcpy(tmp.ISBN, new_ISBN.c_str()); + } + if (name != "") { + name2index.Delete(tmp.name, ret[0]); + name2index.Insert(name, ret[0]); + strcpy(tmp.name, name.c_str()); + } + if (author != "") { + author2index.Delete(tmp.author, ret[0]); + author2index.Insert(author, ret[0]); + strcpy(tmp.author, author.c_str()); + } + if (keyword != "") { + keyword2index.Delete(tmp.keyword, ret[0]); + keyword2index.Insert(keyword, ret[0]); + strcpy(tmp.keyword, keyword.c_str()); + } + if (price >= 0) tmp.price = price; + if (quantity_remain >= 0) tmp.quantity_remain = quantity_remain; + full_book_data.update(tmp, ret[0]); +} + +void BookDataBase::QueryBook(const std::string &ISBN, const std::string &name, + const std::string &author, + const std::string &keyword, + std::vector &ret) { + ret.clear(); + if (ISBN == "" && name == "" && author == "" && keyword == "") return; } \ No newline at end of file diff --git a/backend/src/lexer.cpp b/backend/src/lexer.cpp index 5635260..7de7cf1 100644 --- a/backend/src/lexer.cpp +++ b/backend/src/lexer.cpp @@ -361,8 +361,6 @@ bool CommandModifyLexer(const std::string &command, std::string &ISBN, } else return false; } - for (int i = 0; i < keyword.length(); i++) - if (keyword[i] == '|') return false; return true; } else return false; @@ -431,4 +429,18 @@ bool CommandShowfinanceLexer(const std::string &command, int &count) { return true; } else return false; +} + +bool KeyWordSpliter(const std::string &keyword, std::vector &words) { + int current_beg = 0; + for (int i = 0; i < keyword.length(); i++) { + if (keyword[i] == '|') { + if (i == current_beg) return false; + words.push_back(keyword.substr(current_beg, i - current_beg)); + current_beg = i + 1; + } + } + if (current_beg == keyword.length()) return false; + words.push_back(keyword.substr(current_beg, keyword.length() - current_beg)); + return true; } \ No newline at end of file