writing book system

This commit is contained in:
2023-12-13 12:48:58 +00:00
parent b7a17f2997
commit 9df19f31f2
4 changed files with 90 additions and 8 deletions

View File

@ -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<BookItemClass> &ret) {
ret.clear();
if (ISBN == "" && name == "" && author == "" && keyword == "") return;
}

View File

@ -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<std::string> &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;
}