From e4c7c1b8889dcee7a0b126aa1a41cabbc6c9af5c Mon Sep 17 00:00:00 2001 From: happyZYM Date: Thu, 23 May 2024 02:31:23 +0000 Subject: [PATCH] write parser for train_system and transaction_system --- src/include/data.h | 4 +- src/include/engine.h | 10 ++--- src/train_system.cpp | 75 ++++++++++++++++++++++++++++++++++---- src/transaction_system.cpp | 22 +++++++++-- 4 files changed, 94 insertions(+), 17 deletions(-) diff --git a/src/include/data.h b/src/include/data.h index 7336cd3..b7cde8d 100644 --- a/src/include/data.h +++ b/src/include/data.h @@ -29,7 +29,7 @@ struct FullTrainData { StationData stations[100]; }; -class TrainDataDrive: public DataDriverBase { +class TrainDataDrive : public DataDriverBase {}; -}; +class TransactionDataDrive : public DataDriverBase {}; #endif \ No newline at end of file diff --git a/src/include/engine.h b/src/include/engine.h index 3f1be69..cba62f7 100644 --- a/src/include/engine.h +++ b/src/include/engine.h @@ -26,27 +26,27 @@ class TicketSystemEngine { data_directory + "/user_data.val") {} std::string Execute(const std::string &command); - // 用户相关函数 + // User system std::string AddUser(const std::string &command); std::string LoginUser(const std::string &command); std::string LogoutUser(const std::string &command); std::string QueryProfile(const std::string &command); std::string ModifyProfile(const std::string &command); - // 车次相关函数 + // Train System std::string AddTrain(const std::string &command); std::string DeleteTrain(const std::string &command); std::string ReleaseTrain(const std::string &command); std::string QueryTrain(const std::string &command); - // 订单相关函数 + // Transaction System std::string BuyTicket(const std::string &command); std::string QueryOrder(const std::string &command); std::string RefundTicket(const std::string &command); - - // 其他函数 std::string QueryTransfer(const std::string &command); std::string QueryTicket(const std::string &command); + + // Other functions std::string Clean(); std::string Exit(const std::string &command); }; diff --git a/src/train_system.cpp b/src/train_system.cpp index bf0ec01..394cf98 100644 --- a/src/train_system.cpp +++ b/src/train_system.cpp @@ -24,6 +24,7 @@ std::string TicketSystemEngine::AddTrain(const std::string &command) { char type[2]; command_stream >> token >> token; while (command_stream >> token) { + LOG->debug("token: {}", token); switch (token[1]) { case 'i': { command_stream >> trainID; @@ -40,43 +41,96 @@ std::string TicketSystemEngine::AddTrain(const std::string &command) { case 's': { std::string station_raw; command_stream >> station_raw; - // TODO + std::stringstream station_raw_stream(station_raw); + // the sperator is '|' in the raw string + for (int i = 0; i < stationNum; i++) { + std::getline(station_raw_stream, stations[i], '|'); + } break; } case 'p': { std::string price_raw; command_stream >> price_raw; - // TODO + std::stringstream price_raw_stream(price_raw); + for (int i = 0; i < stationNum - 1; i++) { + std::string tmp; + std::getline(price_raw_stream, tmp, '|'); + sscanf(tmp.c_str(), "%d", &prices[i]); + } break; } case 'x': { - command_stream >> startTime; + std::string startTime_raw; + command_stream >> startTime_raw; + int hh, mm; + sscanf(startTime_raw.c_str(), "%d:%d", &hh, &mm); + startTime = hh * 60 + mm; break; } case 't': { std::string travelTime_raw; command_stream >> travelTime_raw; - // TODO + std::stringstream travelTime_raw_stream(travelTime_raw); + for (int i = 0; i < stationNum - 1; i++) { + std::string tmp; + std::getline(travelTime_raw_stream, tmp, '|'); + sscanf(tmp.c_str(), "%d", &travelTimes[i]); + } break; } case 'o': { std::string stopoverTime_raw; command_stream >> stopoverTime_raw; - // TODO + std::stringstream stopoverTime_raw_stream(stopoverTime_raw); + for (int i = 1; i < stationNum - 1; i++) { + std::string tmp; + std::getline(stopoverTime_raw_stream, tmp); + sscanf(tmp.c_str(), "%d", &stopoverTimes[i]); + } break; } case 'd': { std::string saleDate_raw; command_stream >> saleDate_raw; - // TODO + int beg_mm, beg_dd, end_mm, end_dd; + sscanf(saleDate_raw.c_str(), "%d-%d|%d-%d", &beg_mm, &beg_dd, &end_mm, &end_dd); + saleDate_begin = GetCompactDate(beg_mm, beg_dd); + saleDate_end = GetCompactDate(end_mm, end_dd); break; } case 'y': { command_stream >> type; break; } + default: { + throw std::runtime_error("fatal error: unknown token"); + } } } + LOG->debug("trainID: {}", trainID); + LOG->debug("stationNum: {}", stationNum); + LOG->debug("seatNum: {}", seatNum); + LOG->debug("stations:"); + for (int i = 0; i < stationNum; i++) { + LOG->debug("{} {}", i, stations[i]); + } + LOG->debug("prices:"); + for (int i = 0; i < stationNum - 1; i++) { + LOG->debug("{} {}", i, prices[i]); + } + LOG->debug("startTime: {}={}:{}", startTime, startTime / 60, startTime % 60); + LOG->debug("travelTimes:"); + for (int i = 0; i < stationNum - 1; i++) { + LOG->debug("{} {}", i, travelTimes[i]); + } + LOG->debug("stopoverTimes:"); + for (int i = 1; i < stationNum - 1; i++) { + LOG->debug("{} {}", i, stopoverTimes[i]); + } + LOG->debug("saleDate: from {}={}-{} to {}={}-{}", saleDate_begin, RetrieveReadableDate(saleDate_begin).first, + RetrieveReadableDate(saleDate_begin).second, saleDate_end, RetrieveReadableDate(saleDate_end).first, + RetrieveReadableDate(saleDate_end).second); + LOG->debug("type: {}", type); response_stream << '[' << command_id << "] AddTrain"; return response_stream.str(); } @@ -97,6 +151,7 @@ std::string TicketSystemEngine::DeleteTrain(const std::string &command) { } } } + // TODO response_stream << '[' << command_id << "] DeleteTrain"; return response_stream.str(); } @@ -117,6 +172,7 @@ std::string TicketSystemEngine::ReleaseTrain(const std::string &command) { } } } + // TODO response_stream << '[' << command_id << "] ReleaseTrain"; return response_stream.str(); } @@ -139,11 +195,16 @@ std::string TicketSystemEngine::QueryTrain(const std::string &command) { case 'd': { std::string date_raw; command_stream >> date_raw; - // TODO + int mm, dd; + sscanf(date_raw.c_str(), "%d-%d", &mm, &dd); + date = GetCompactDate(mm, dd); break; } } } + LOG->debug("trainID: {}", trainID); + LOG->debug("date: {}={}-{}", date, RetrieveReadableDate(date).first, RetrieveReadableDate(date).second); + // TODO response_stream << '[' << command_id << "] QueryTrain"; return response_stream.str(); } \ No newline at end of file diff --git a/src/transaction_system.cpp b/src/transaction_system.cpp index 932eb45..c109a5a 100644 --- a/src/transaction_system.cpp +++ b/src/transaction_system.cpp @@ -23,7 +23,9 @@ std::string TicketSystemEngine::QueryTicket(const std::string &command) { case 'd': { std::string date_raw; command_stream >> date_raw; - // TODO: parse date + int mm, dd; + sscanf(date_raw.c_str(), "%d-%d", &mm, &dd); + date = GetCompactDate(mm, dd); break; } case 's': { @@ -43,6 +45,9 @@ std::string TicketSystemEngine::QueryTicket(const std::string &command) { } } } + LOG->debug("date {}={}-{}, from {}, to {}, order by {}", date, RetrieveReadableDate(date).first, + RetrieveReadableDate(date).second, from, to, order_by); + // TODO response_stream << "[" << command_id << "] QueryTicket"; return response_stream.str(); } @@ -62,7 +67,9 @@ std::string TicketSystemEngine::QueryTransfer(const std::string &command) { case 'd': { std::string date_raw; command_stream >> date_raw; - // TODO: parse date + int mm, dd; + sscanf(date_raw.c_str(), "%d-%d", &mm, &dd); + date = GetCompactDate(mm, dd); break; } case 's': { @@ -82,6 +89,9 @@ std::string TicketSystemEngine::QueryTransfer(const std::string &command) { } } } + LOG->debug("date {}={}-{}, from {}, to {}, order by {}", date, RetrieveReadableDate(date).first, + RetrieveReadableDate(date).second, from, to, order_by); + // TODO response_stream << "[" << command_id << "] QueryTransfer"; return response_stream.str(); } @@ -112,7 +122,9 @@ std::string TicketSystemEngine::BuyTicket(const std::string &command) { case 'd': { std::string date_raw; command_stream >> date_raw; - // TODO: parse date + int mm, dd; + sscanf(date_raw.c_str(), "%d-%d", &mm, &dd); + date = GetCompactDate(mm, dd); break; } case 'f': { @@ -133,6 +145,10 @@ std::string TicketSystemEngine::BuyTicket(const std::string &command) { } } } + LOG->debug("user {}, train {}, date {}={}-{}, from {}, to {}, ticket num {}, accept queue {}", user_name, train_id, + date, RetrieveReadableDate(date).first, RetrieveReadableDate(date).second, from, to, ticket_num, + accept_queue); + // TODO response_stream << "[" << command_id << "] BuyTicket"; return response_stream.str(); }