write parser for train_system and transaction_system
This commit is contained in:
@ -29,7 +29,7 @@ struct FullTrainData {
|
|||||||
StationData stations[100];
|
StationData stations[100];
|
||||||
};
|
};
|
||||||
|
|
||||||
class TrainDataDrive: public DataDriverBase {
|
class TrainDataDrive : public DataDriverBase {};
|
||||||
|
|
||||||
};
|
class TransactionDataDrive : public DataDriverBase {};
|
||||||
#endif
|
#endif
|
@ -26,27 +26,27 @@ class TicketSystemEngine {
|
|||||||
data_directory + "/user_data.val") {}
|
data_directory + "/user_data.val") {}
|
||||||
std::string Execute(const std::string &command);
|
std::string Execute(const std::string &command);
|
||||||
|
|
||||||
// 用户相关函数
|
// User system
|
||||||
std::string AddUser(const std::string &command);
|
std::string AddUser(const std::string &command);
|
||||||
std::string LoginUser(const std::string &command);
|
std::string LoginUser(const std::string &command);
|
||||||
std::string LogoutUser(const std::string &command);
|
std::string LogoutUser(const std::string &command);
|
||||||
std::string QueryProfile(const std::string &command);
|
std::string QueryProfile(const std::string &command);
|
||||||
std::string ModifyProfile(const std::string &command);
|
std::string ModifyProfile(const std::string &command);
|
||||||
|
|
||||||
// 车次相关函数
|
// Train System
|
||||||
std::string AddTrain(const std::string &command);
|
std::string AddTrain(const std::string &command);
|
||||||
std::string DeleteTrain(const std::string &command);
|
std::string DeleteTrain(const std::string &command);
|
||||||
std::string ReleaseTrain(const std::string &command);
|
std::string ReleaseTrain(const std::string &command);
|
||||||
std::string QueryTrain(const std::string &command);
|
std::string QueryTrain(const std::string &command);
|
||||||
|
|
||||||
// 订单相关函数
|
// Transaction System
|
||||||
std::string BuyTicket(const std::string &command);
|
std::string BuyTicket(const std::string &command);
|
||||||
std::string QueryOrder(const std::string &command);
|
std::string QueryOrder(const std::string &command);
|
||||||
std::string RefundTicket(const std::string &command);
|
std::string RefundTicket(const std::string &command);
|
||||||
|
|
||||||
// 其他函数
|
|
||||||
std::string QueryTransfer(const std::string &command);
|
std::string QueryTransfer(const std::string &command);
|
||||||
std::string QueryTicket(const std::string &command);
|
std::string QueryTicket(const std::string &command);
|
||||||
|
|
||||||
|
// Other functions
|
||||||
std::string Clean();
|
std::string Clean();
|
||||||
std::string Exit(const std::string &command);
|
std::string Exit(const std::string &command);
|
||||||
};
|
};
|
||||||
|
@ -24,6 +24,7 @@ std::string TicketSystemEngine::AddTrain(const std::string &command) {
|
|||||||
char type[2];
|
char type[2];
|
||||||
command_stream >> token >> token;
|
command_stream >> token >> token;
|
||||||
while (command_stream >> token) {
|
while (command_stream >> token) {
|
||||||
|
LOG->debug("token: {}", token);
|
||||||
switch (token[1]) {
|
switch (token[1]) {
|
||||||
case 'i': {
|
case 'i': {
|
||||||
command_stream >> trainID;
|
command_stream >> trainID;
|
||||||
@ -40,43 +41,96 @@ std::string TicketSystemEngine::AddTrain(const std::string &command) {
|
|||||||
case 's': {
|
case 's': {
|
||||||
std::string station_raw;
|
std::string station_raw;
|
||||||
command_stream >> 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;
|
break;
|
||||||
}
|
}
|
||||||
case 'p': {
|
case 'p': {
|
||||||
std::string price_raw;
|
std::string price_raw;
|
||||||
command_stream >> 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;
|
break;
|
||||||
}
|
}
|
||||||
case 'x': {
|
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;
|
break;
|
||||||
}
|
}
|
||||||
case 't': {
|
case 't': {
|
||||||
std::string travelTime_raw;
|
std::string travelTime_raw;
|
||||||
command_stream >> 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;
|
break;
|
||||||
}
|
}
|
||||||
case 'o': {
|
case 'o': {
|
||||||
std::string stopoverTime_raw;
|
std::string stopoverTime_raw;
|
||||||
command_stream >> 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;
|
break;
|
||||||
}
|
}
|
||||||
case 'd': {
|
case 'd': {
|
||||||
std::string saleDate_raw;
|
std::string saleDate_raw;
|
||||||
command_stream >> 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;
|
break;
|
||||||
}
|
}
|
||||||
case 'y': {
|
case 'y': {
|
||||||
command_stream >> type;
|
command_stream >> type;
|
||||||
break;
|
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";
|
response_stream << '[' << command_id << "] AddTrain";
|
||||||
return response_stream.str();
|
return response_stream.str();
|
||||||
}
|
}
|
||||||
@ -97,6 +151,7 @@ std::string TicketSystemEngine::DeleteTrain(const std::string &command) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// TODO
|
||||||
response_stream << '[' << command_id << "] DeleteTrain";
|
response_stream << '[' << command_id << "] DeleteTrain";
|
||||||
return response_stream.str();
|
return response_stream.str();
|
||||||
}
|
}
|
||||||
@ -117,6 +172,7 @@ std::string TicketSystemEngine::ReleaseTrain(const std::string &command) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
// TODO
|
||||||
response_stream << '[' << command_id << "] ReleaseTrain";
|
response_stream << '[' << command_id << "] ReleaseTrain";
|
||||||
return response_stream.str();
|
return response_stream.str();
|
||||||
}
|
}
|
||||||
@ -139,11 +195,16 @@ std::string TicketSystemEngine::QueryTrain(const std::string &command) {
|
|||||||
case 'd': {
|
case 'd': {
|
||||||
std::string date_raw;
|
std::string date_raw;
|
||||||
command_stream >> date_raw;
|
command_stream >> date_raw;
|
||||||
// TODO
|
int mm, dd;
|
||||||
|
sscanf(date_raw.c_str(), "%d-%d", &mm, &dd);
|
||||||
|
date = GetCompactDate(mm, dd);
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
LOG->debug("trainID: {}", trainID);
|
||||||
|
LOG->debug("date: {}={}-{}", date, RetrieveReadableDate(date).first, RetrieveReadableDate(date).second);
|
||||||
|
// TODO
|
||||||
response_stream << '[' << command_id << "] QueryTrain";
|
response_stream << '[' << command_id << "] QueryTrain";
|
||||||
return response_stream.str();
|
return response_stream.str();
|
||||||
}
|
}
|
@ -23,7 +23,9 @@ std::string TicketSystemEngine::QueryTicket(const std::string &command) {
|
|||||||
case 'd': {
|
case 'd': {
|
||||||
std::string date_raw;
|
std::string date_raw;
|
||||||
command_stream >> 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;
|
break;
|
||||||
}
|
}
|
||||||
case 's': {
|
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";
|
response_stream << "[" << command_id << "] QueryTicket";
|
||||||
return response_stream.str();
|
return response_stream.str();
|
||||||
}
|
}
|
||||||
@ -62,7 +67,9 @@ std::string TicketSystemEngine::QueryTransfer(const std::string &command) {
|
|||||||
case 'd': {
|
case 'd': {
|
||||||
std::string date_raw;
|
std::string date_raw;
|
||||||
command_stream >> 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;
|
break;
|
||||||
}
|
}
|
||||||
case 's': {
|
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";
|
response_stream << "[" << command_id << "] QueryTransfer";
|
||||||
return response_stream.str();
|
return response_stream.str();
|
||||||
}
|
}
|
||||||
@ -112,7 +122,9 @@ std::string TicketSystemEngine::BuyTicket(const std::string &command) {
|
|||||||
case 'd': {
|
case 'd': {
|
||||||
std::string date_raw;
|
std::string date_raw;
|
||||||
command_stream >> 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;
|
break;
|
||||||
}
|
}
|
||||||
case 'f': {
|
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";
|
response_stream << "[" << command_id << "] BuyTicket";
|
||||||
return response_stream.str();
|
return response_stream.str();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user