write finance
This commit is contained in:
@ -125,6 +125,7 @@ class BookItemClass {
|
|||||||
char ISBN[21], name[61], author[61], keyword[61];
|
char ISBN[21], name[61], author[61], keyword[61];
|
||||||
double price;
|
double price;
|
||||||
int quantity_remain;
|
int quantity_remain;
|
||||||
|
int bid;
|
||||||
};
|
};
|
||||||
|
|
||||||
class FinanceItemClass {
|
class FinanceItemClass {
|
||||||
|
@ -28,7 +28,7 @@ class BookDataBase {
|
|||||||
public:
|
public:
|
||||||
void Open(std::string file_name);
|
void Open(std::string file_name);
|
||||||
bool HaveISBN(const std::string &ISBN);
|
bool HaveISBN(const std::string &ISBN);
|
||||||
bool HaveISBN(const std::string &ISBN,BookItemClass &ret);
|
bool HaveISBN(const std::string &ISBN, BookItemClass &ret);
|
||||||
void CreateEmptyBook(const std::string &ISBN);
|
void CreateEmptyBook(const std::string &ISBN);
|
||||||
void QueryBook(const std::string &ISBN, const std::string &name,
|
void QueryBook(const std::string &ISBN, const std::string &name,
|
||||||
const std::string &author, const std::string &keyword,
|
const std::string &author, const std::string &keyword,
|
||||||
@ -41,10 +41,16 @@ class BookDataBase {
|
|||||||
class LogDataBase {
|
class LogDataBase {
|
||||||
DriveArray<FinanceItemClass> finance_data;
|
DriveArray<FinanceItemClass> finance_data;
|
||||||
DriveArray<OperationLogItemClass> operation_log_data;
|
DriveArray<OperationLogItemClass> operation_log_data;
|
||||||
|
int finance_operation_count;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
|
~LogDataBase() { finance_data.write_info(finance_operation_count, 1); }
|
||||||
void Open(std::string file_name);
|
void Open(std::string file_name);
|
||||||
|
void AddSell(int book_id, int quantity, double total_price);
|
||||||
|
void AddImport(int book_id, int quantity, double total_price);
|
||||||
|
std::pair<double, double> QueryFinance(int count = -1);
|
||||||
void QueryFinance(int count, std::vector<FinanceItemClass> &ret);
|
void QueryFinance(int count, std::vector<FinanceItemClass> &ret);
|
||||||
|
int TotalFinanceOperationCount() noexcept { return finance_operation_count; }
|
||||||
};
|
};
|
||||||
|
|
||||||
#endif // PROTECTOR_DATABASE_HPP
|
#endif // PROTECTOR_DATABASE_HPP
|
@ -17,6 +17,7 @@ void BookDataBase::Open(std::string file_name) {
|
|||||||
|
|
||||||
void LogDataBase::Open(std::string file_name) {
|
void LogDataBase::Open(std::string file_name) {
|
||||||
finance_data.OpenFile(file_name + ".finance");
|
finance_data.OpenFile(file_name + ".finance");
|
||||||
|
finance_data.get_info(finance_operation_count, 1);
|
||||||
operation_log_data.OpenFile(file_name + ".log");
|
operation_log_data.OpenFile(file_name + ".log");
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -94,6 +95,8 @@ void BookDataBase::CreateEmptyBook(const std::string &ISBN) {
|
|||||||
tmp.author[0] = 0;
|
tmp.author[0] = 0;
|
||||||
tmp.keyword[0] = 0;
|
tmp.keyword[0] = 0;
|
||||||
int idx = full_book_data.write(tmp);
|
int idx = full_book_data.write(tmp);
|
||||||
|
tmp.bid = idx;
|
||||||
|
full_book_data.update(tmp, idx);
|
||||||
ISBN2index.Insert(ISBN, idx);
|
ISBN2index.Insert(ISBN, idx);
|
||||||
name2index.Insert("", idx);
|
name2index.Insert("", idx);
|
||||||
author2index.Insert("", idx);
|
author2index.Insert("", idx);
|
||||||
@ -235,3 +238,35 @@ void BookDataBase::QueryBook(const std::string &ISBN, const std::string &name,
|
|||||||
return strcmp(a.ISBN, b.ISBN) < 0;
|
return strcmp(a.ISBN, b.ISBN) < 0;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void LogDataBase::AddImport(int book_id, int quantity, double total_price) {
|
||||||
|
FinanceItemClass tmp;
|
||||||
|
tmp.book_id = book_id;
|
||||||
|
tmp.quantity = quantity;
|
||||||
|
tmp.total_price = -total_price;
|
||||||
|
finance_data.write(tmp);
|
||||||
|
finance_operation_count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
void LogDataBase::AddSell(int book_id, int quantity, double total_price) {
|
||||||
|
FinanceItemClass tmp;
|
||||||
|
tmp.book_id = book_id;
|
||||||
|
tmp.quantity = quantity;
|
||||||
|
tmp.total_price = total_price;
|
||||||
|
finance_data.write(tmp);
|
||||||
|
finance_operation_count++;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::pair<double, double> LogDataBase::QueryFinance(int count) {
|
||||||
|
double total_sell = 0, total_import = 0;
|
||||||
|
if (count == -1) count = finance_operation_count;
|
||||||
|
for (int i = 1; i <= count; i++) {
|
||||||
|
FinanceItemClass tmp;
|
||||||
|
finance_data.read(tmp, finance_operation_count + 1 - i);
|
||||||
|
if (tmp.total_price > 0)
|
||||||
|
total_sell += tmp.total_price;
|
||||||
|
else
|
||||||
|
total_import -= tmp.total_price;
|
||||||
|
}
|
||||||
|
return std::make_pair(total_sell, total_import);
|
||||||
|
}
|
@ -269,6 +269,7 @@ std::vector<std::string> BookStoreEngineClass::ExecuteBuy(
|
|||||||
return std::vector<std::string>({"Invalid"});
|
return std::vector<std::string>({"Invalid"});
|
||||||
book_data_base.ModifyInfo(ISBN, "", "", "", "", -1,
|
book_data_base.ModifyInfo(ISBN, "", "", "", "", -1,
|
||||||
tmp.quantity_remain - quantity);
|
tmp.quantity_remain - quantity);
|
||||||
|
log_data_base.AddSell(tmp.bid, quantity, tmp.price * quantity);
|
||||||
/*浮点数输出购买图书所需的总金额,小数点后有且仅有两位小数*/
|
/*浮点数输出购买图书所需的总金额,小数点后有且仅有两位小数*/
|
||||||
std::vector<std::string> ans;
|
std::vector<std::string> ans;
|
||||||
unsigned long long cost_tmp = tmp.price * quantity * 100 + 0.5;
|
unsigned long long cost_tmp = tmp.price * quantity * 100 + 0.5;
|
||||||
@ -355,13 +356,32 @@ std::vector<std::string> BookStoreEngineClass::ExecuteImport(
|
|||||||
book_data_base.HaveISBN(login_stack.top().second, tmp);
|
book_data_base.HaveISBN(login_stack.top().second, tmp);
|
||||||
book_data_base.ModifyInfo(login_stack.top().second, "", "", "", "", -1,
|
book_data_base.ModifyInfo(login_stack.top().second, "", "", "", "", -1,
|
||||||
tmp.quantity_remain + quantity);
|
tmp.quantity_remain + quantity);
|
||||||
|
log_data_base.AddImport(tmp.bid, quantity, total_cost);
|
||||||
return std::vector<std::string>();
|
return std::vector<std::string>();
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> BookStoreEngineClass::ExecuteShowFinance(
|
std::vector<std::string> BookStoreEngineClass::ExecuteShowFinance(
|
||||||
const std::string &cmd,
|
const std::string &cmd,
|
||||||
std::stack<std::pair<std::string, std::string>> &login_stack) {
|
std::stack<std::pair<std::string, std::string>> &login_stack) {
|
||||||
return std::vector<std::string>();
|
int count;
|
||||||
|
if (!CommandShowfinanceLexer(cmd, count))
|
||||||
|
return std::vector<std::string>({"Invalid"});
|
||||||
|
if (login_stack.empty() ||
|
||||||
|
user_data_base.GetPrevilege(login_stack.top().first) < 7)
|
||||||
|
return std::vector<std::string>({"Invalid"});
|
||||||
|
if (count > log_data_base.TotalFinanceOperationCount())
|
||||||
|
return std::vector<std::string>({"Invalid"});
|
||||||
|
if (count == 0) return std::vector<std::string>({""});
|
||||||
|
std::pair<double, double> ret = log_data_base.QueryFinance(count);
|
||||||
|
std::string ans;
|
||||||
|
unsigned long long income_tmp = ret.first * 100 + 0.5;
|
||||||
|
unsigned long long outcome_tmp = ret.second * 100 + 0.5;
|
||||||
|
ans =
|
||||||
|
"+ " + std::to_string(income_tmp / 100) + '.' +
|
||||||
|
std::to_string(income_tmp % 100 / 10) + std::to_string(income_tmp % 10) +
|
||||||
|
" - " + std::to_string(outcome_tmp / 100) + '.' +
|
||||||
|
std::to_string(outcome_tmp % 100 / 10) + std::to_string(outcome_tmp % 10);
|
||||||
|
return std::vector<std::string>({ans});
|
||||||
}
|
}
|
||||||
|
|
||||||
std::vector<std::string> BookStoreEngineClass::ExecuteLog(
|
std::vector<std::string> BookStoreEngineClass::ExecuteLog(
|
||||||
|
@ -424,6 +424,7 @@ bool CommandShowfinanceLexer(const std::string &command, int &count) {
|
|||||||
std::stringstream ss(command);
|
std::stringstream ss(command);
|
||||||
std::string token;
|
std::string token;
|
||||||
ss >> token;
|
ss >> token;
|
||||||
|
ss >> token;
|
||||||
long long count_tmp = -1;
|
long long count_tmp = -1;
|
||||||
ss >> count_tmp;
|
ss >> count_tmp;
|
||||||
if (count_tmp > 2147483647) return false;
|
if (count_tmp > 2147483647) return false;
|
||||||
|
Reference in New Issue
Block a user