头文件:
#ifndef USER_H
#define USER_H
#include <string>
class user
{
private:
std::string id;//id用于登陆系统(readonly)
std::string pwd;//密码(read and write)
std::string power;//权限(readonly)
std::string name;//用户真实姓名(read and write)
std::string identity;//证件号码(read and write)
public:
user();
~user();
private:
public://属性
//读属性
std::string get_id();
std::string get_pwd();
std::string get_power();
std::string get_name();
std::string get_identity();
//写属性
void set_power(std::string new_power);
void set_pwd(std::string new_pwd);
void set_name(std::string new_name);
void set_identity(std::string new_identity);
void set_id(std::string value);
public://方法
void powerup(user &u,std::string new_power);//提升用户权限
void load(std::string);
std::string toxml();
std::string face();
};
#endif
-------------------------------------------------------------------------------------------
#ifndef PLAN_H
#define PLAN_H
#include <string>
class plan
{
private:
std::string id;
std::string sta_adr;
std::string arv_adr;
std::string sta_time;
std::string arv_time;
std::string price;
std::string zhekou;
std::string total;
std::string selled;
public:
std::string get_id();
std::string get_sta_adr();
std::string get_arv_adr();
std::string get_sta_time();
std::string get_arv_time();
float get_price();
float get_zhekou();
int get_total();
int get_selled();
int get_remain();
void set_id(std::string);
void set_sta_adr(std::string);
void set_arv_adr(std::string);
void set_sta_time(std::string);
void set_arv_time(std::string);
void plan::set_price(std::string value);
void plan::set_zhekou(std::string value);
void plan::set_total(std::string value);
void plan::set_selled(std::string value);
public:
plan();
~plan();
std::string toxml();
void load(std::string &buf);
std::string face();
void change(std::string ,std::string );
};
#endif
-------------------------------------------------------------------------------
#ifndef SYSTEM_H
#define SYSTEM_H
#include <string>
#include <vector>
#include "user.h"
#include "plan.h"
#include "order.h"
class CSystem
{
private:
std::vector<user> users;//所有的用户
std::vector<plan> plans;//所有的航班
std::vector<order> orders;//所有的订单
user *cur_user;//当前用户
private://系统的一些设置
char *user_file;
char *plan_file;
char *order_file;
private:
//从文件中加载信息
void load_users();
void load_plans();
void load_orders();
void clear();
void welcome();//显示欢迎信息,和版本信息
void help();
void login();
void logout();
void badcmd(std::string cmd);
void signin();
void show(std::vector<std::string> &);
void insert(std::vector<std::string> &);
private://核心算法
void show_plans();
void show_users();
void show_orders();
void insert_plan(std::vector<std::string> &);
void insert_user(std::vector<std::string> &);
void insert_order(std::vector<std::string> &);
void alert(std::vector<std::string> &);
void alert_plan(std::vector<std::string> &);
void find(std::vector<std::string> &);
void find_plan(std::string );
void buy_tikets(std::string );
void return_tikets(std::string );
public:
std::string get_cmd();//获取命令
void parse_cmd(std::string);//解析命令
public://
CSystem();
~CSystem();
void startup();
void shutdown();
};
#endif
--------------------------------------------------------------------------------------------
源文件;
#include <string>
#include "user.h"
#include <iostream>
using namespace std;
user::user()
{
}
user::~user()
{
}
//读属性
string user::get_id(){ return id;}
string user::get_pwd(){return pwd;}
string user::get_power(){return power;}
string user::get_name(){ return name;}
string user::get_identity(){return identity;}
//写属性
void user::set_id(string value){id = value;}
void user::set_pwd(string new_pwd){pwd = new_pwd;}
void user::set_name(string new_name){name = new_name;}
void user::set_identity(string new_identity){identity = new_identity;}
void user::set_power(string new_power)
{
power = new_power;
}
void user::powerup(user &u,string new_power)
{
if(power != "admin")
{
cout<<"你不是管理员不能设置用户权限!"<<endl;
return;
}
u.set_power(new_power);
}
void user::load(string tem)
{
size_t sta,end;
sta = tem.find("<id>",0);
end = tem.find("</id>",sta);
sta += strlen("<id>");
id = tem.substr(sta,end-sta);
sta = tem.find("<pwd>",0);
end = tem.find("</pwd>",sta);
sta += strlen("<pwd>");
pwd = tem.substr(sta,end-sta);
sta = tem.find("<power>",0);
end = tem.find("</power>",sta);
sta += strlen("<power>");
power = tem.substr(sta,end-sta);
sta = tem.find("<name>",0);
end = tem.find("</name>",sta);
sta += strlen("<name>");
name = tem.substr(sta,end-sta);
sta = tem.find("<identity>",0);
end = tem.find("</identity>",sta);
sta += strlen("<identity>");
identity = tem.substr(sta,end-sta);
}
string user::toxml()
{
string tem;
tem += "<user><id>" + id + "</id>";
tem += "<power>" + power + "</power>";
tem += "<name>" + name + "</name>";
tem += "<pwd>" + pwd + "</pwd>";
tem += "<identity>" + identity + "</identity></user>";
return tem;
}
string user::face()
{
string result;
result += "user id:" + id + '/n';
result += "user pwd:" + pwd + '/n';
result += "ture name:" + name +'/n';
result += "power:" + power +'/n';
result += "identity:" + identity + '/n';
return result;
}
-------------------------------------------------------------------------------------
#include "plan.h"
#include <string>
using namespace std;
plan::plan()
{
}
plan::~plan()
{
}
string plan::get_id(){return id;}
string plan::get_sta_adr(){return sta_adr;}
string plan::get_arv_adr(){return arv_adr;}
string plan::get_sta_time(){return sta_time;}
string plan::get_arv_time(){return arv_time;}
float plan::get_price(){return atof(price.c_str());}
float plan::get_zhekou(){return atof(zhekou.c_str());}
int plan::get_total(){return atoi(total.c_str());}
int plan::get_selled(){return atoi(selled.c_str());}
int plan::get_remain(){return get_total()-get_selled();};
void plan::set_id(string value){id = value;}
void plan::set_sta_adr(string new_saddr){sta_adr = new_saddr;}
void plan::set_arv_adr(string new_aaddr){arv_adr = new_aaddr;}
void plan::set_sta_time(string new_stime){sta_time = new_stime;}
void plan::set_arv_time(string new_atime){arv_time = new_atime;}
void plan::set_price(string value){price = value;}
void plan::set_zhekou(string value){zhekou = value;}
void plan::set_total(string value){total=value;}
void plan::set_selled(string value){selled=value;}
string plan::toxml()
{
string result;
result += "<plan><id>" + id + "</id>";
result += "<sta_adr>" + sta_adr + "</sta_adr>";
result += "<arv_adr>" + arv_adr + "</arv_adr>";
result += "<sta_time>" + sta_time + "</sta_time>";
result += "<arv_time>" + arv_time + "</arv_time>";
result += "<price>" + price + "</price>";
result += "<zhekou>" + zhekou + "</zhekou>";
result += "<total>" + total + "</total>";
result += "<selled>" + selled + "</selled></plan>";
return result;
}
void plan::load(string &buf)
{
size_t sta=0,end=0;
sta = buf.find("<id>",0);
end = buf.find("</id>",0);
sta += strlen("<id>");
id = buf.substr(sta,end-sta);
sta = buf.find("<sta_adr>",0);
end = buf.find("</sta_adr>",0);
sta += strlen("<sta_adr>");
sta_adr = buf.substr(sta,end-sta);
sta = buf.find("<arv_adr>",0);
end = buf.find("</arv_adr>",0);
sta += strlen("<arv_adr>");
arv_adr = buf.substr(sta,end-sta);
sta = buf.find("<sta_time>",0);
end = buf.find("</sta_time>",0);
sta += strlen("<sta_time>");
sta_time = buf.substr(sta,end-sta);
sta = buf.find("<arv_time>",0);
end = buf.find("</arv_time>",0);
sta += strlen("<arv_time>");
arv_time = buf.substr(sta,end-sta);
sta = buf.find("<price>",0);
end = buf.find("</price>",0);
sta += strlen("<price>");
price = buf.substr(sta,end-sta);
sta = buf.find("<zhekou>",0);
end = buf.find("</zhekou>",0);
sta += strlen("<zhekou>");
zhekou = buf.substr(sta,end-sta);
sta = buf.find("<total>",0);
end = buf.find("</total>",0);
sta += strlen("<total>");
total = buf.substr(sta,end-sta);
sta = buf.find("<selled>",0);
end = buf.find("</selled>",0);
sta += strlen("<selled>");
selled = buf.substr(sta,end-sta);
}
string plan::face()
{
string result;
int remain;
remain = get_total() - get_selled();
char cnum[10];
itoa(remain,cnum,10);
result += "plan id:" + id + '/n';
result += "start address:" + sta_adr + '/n';
result += "arrive address:" + arv_adr + '/n';
result += "start time:" + sta_time + '/n';
result += "arrive time:" + arv_time + '/n';
result += "price:" + price +"rmb/n";
result += "zhekou:"+ zhekou +'/n';
result += "total tikets:" + total + '/n';
result += "tikets selled:" + selled + '/n';
result += "tikets remain:";
result += cnum ;
result += '/n';
return result;
}
void plan::change(string str1,string str2)
{
if(str1 == "id")
{
id = str2;
return;
}
if(str1 == "sta_adr")
{
sta_adr = str2;
return;
}
if(str1 == "arv_adr")
{
arv_adr = str2;
return;
}
if(str1 == "sta_time")
{
sta_time = str2;
return;
}
if(str1 == "arv_time")
{
arv_time = str2;
return;
}
if(str1 == "price")
{
price = str2;
return;
}
if(str1 == "zhekou")
{
zhekou = str2;
return;
}
if(str1 == "total")
{
total = str2;
return;
}
}
------------------------------------------------------------------------------------
#include "order.h"
#include <string>
using namespace std;
order::order()
{
}
order::~order()
{
}
string order::get_id()
{
return id;
}
string order::get_pid()
{
return pid;
}
string order::get_uid()
{
return uid;
}
string order::get_date()
{
return date;
}
int order::get_num()
{
return atoi(num.c_str());
}
void order::set_id(string value)
{
id = value;
}
void order::set_pid(string value)
{
pid = value;
}
void order::set_uid(string value)
{
uid = value;
}
void order::set_date(string value)
{
date = value;
}
void order::set_num(string value)
{
num = value;
}
string order::toxml()
{
string result;
result += "<order><id>" + id + "</id>";
result += "<pid>" + pid + "</pid>";
result += "<uid>" + uid + "</uid>";
result += "<date>" + date + "</date>";
result += "<num>" + num + "</num>";
return result;
}
void order::load(string &buf)
{
size_t sta=0,end=0;
sta = buf.find("<id>",0);
end = buf.find("</id>",0);
sta += strlen("<id>");
id = buf.substr(sta,end-sta);
sta = buf.find("<pid>",0);
end = buf.find("</pid>",0);
sta += strlen("<pid>");
pid = buf.substr(sta,end-sta);
sta = buf.find("<uid>",0);
end = buf.find("</uid>",0);
sta += strlen("<uid>");
uid = buf.substr(sta,end-sta);
sta = buf.find("<date>",0);
end = buf.find("</date>",0);
sta += strlen("<date>");
date = buf.substr(sta,end-sta);
sta = buf.find("<num>",0);
end = buf.find("</num>",0);
sta += strlen("<num>");
num = buf.substr(sta,end-sta);
}
string order::face()
{
string result;
result += "order id:" + id + '/n';
result += "user id:" + uid + '/n';
result += "plan id:" + pid + '/n';
result += "date:" + date + '/n';
result += "tikets number:" + num + '/n';
return result;
}
------------------------------------------------------------------------------------
#include "system.h"
#include "user.h"
#include "plan.h"
#include "order.h"
#include <vector>
#include <string>
#include <fstream>
#include <iostream>
using namespace std;
CSystem::CSystem()
{
order_file = "order.xml";
plan_file = "plan.xml";
user_file = "user.xml";
cur_user = NULL;
}
CSystem::~CSystem()
{
}
void CSystem::load_orders()
{
ifstream in(this->order_file);
string line;
string temp;
string anorder;
size_t sta,end;
order *porder;
while(getline(in,line))
{
temp += line;
}
in.close();
for(sta = temp.find("<order>",0);sta != string::npos;sta = temp.find("<order>",sta))
{
end = temp.find("</order>",sta);
end += strlen("</order>");
anorder = temp.substr(sta,end - sta);
porder = new order;
porder->load(anorder);
orders.push_back(*porder);
sta ++;
}
}
void CSystem::load_plans()
{
ifstream in(this->plan_file);
string line;
string temp;
string aplan;
size_t sta,end;
plan *pplan;
while(getline(in,line))
{
temp += line;
}
in.close();
for(sta = temp.find("<plan>",0);sta != string::npos;sta = temp.find("<plan>",sta))
{
end = temp.find("</plan>",sta);
end += strlen("</plan>");
aplan = temp.substr(sta,end - sta);
pplan = new plan;
pplan->load(aplan);
plans.push_back(*pplan);
sta ++;
}
}
void CSystem::load_users()
{
ifstream in(this->user_file);
string line;
string temp;
string auser;
size_t sta,end;
user *puser;
while(getline(in,line))
{
temp += line;
}
in.close();
for(sta = temp.find("<user>",0);sta != string::npos;sta = temp.find("<user>",sta))
{
end = temp.find("</user>",sta);
end += strlen("</user>");
auser = temp.substr(sta,end - sta);
puser = new user;
puser->load(auser);
users.push_back(*puser);
sta ++;
}
}
void CSystem::welcome()
{
cout<<"欢迎进入航班订票系统"<<endl;
cout<<" made by efish "<<endl;
cout<<"type /"help/" or '?' to get help"<<endl;
getchar();
system("cls");
}
void CSystem::startup()
{
cout<<"系统正在启动,请稍后..."<<endl;
system("cls");
cout<<"加载用户信息";
load_users();
cout<<" OK"<<endl;
cout<<"加载航班信息";
load_plans();
cout<<" OK"<<endl;
cout<<"加载订单信息";
load_orders();
cout<<" OK"<<endl;
system("cls");
welcome();
}
void CSystem::shutdown()
{
unsigned int i;
string temp;
string save;
cout<<"系统正在退出,是否要保存修改?( yes/no)"<<endl;
while(getline(cin,save))
{
if(save == "no")exit(0);
else if(save == "yes")break;
else cout<<"请回答yes or no?"<<endl;
}
cout<<"保存修改..."<<endl;
ofstream fuser(user_file);
temp = "<xml><users>";
for(i=0;i < users.size();i++)
{
temp +=users[i].toxml();
}
temp += "</users>";
fuser<<temp;
fuser.close();
ofstream fplan(plan_file);
temp = "<xml><plans>";
for(i=0;i<plans.size();i++)
{
temp +=plans[i].toxml();
}
temp += "</plans>";
fplan<<temp;
fplan.close();
ofstream forder(order_file);
temp = "<xml><orders>";
for(i=0;i<orders.size();i++)
{
temp +=orders[i].toxml();
}
temp += "</orders>";
forder<<temp;
cout<<"保存完毕"<<endl;
exit(0);
}
string CSystem::get_cmd()//获取命令
{
string result;
if(cur_user != NULL)
cout<<cur_user->get_name().c_str()<<":";
else
cout<<"COMMAND:";
getline(cin,result);
return result;
}
void CSystem::parse_cmd(std::string cmd)//解析命令
{
size_t sta=0,end=0;
vector<string> cmd_arr;
unsigned int pcmd=0;
while(1)
{
sta = cmd.find_first_not_of(" ",end);
if(sta == string::npos) break;
end = cmd.find_first_of(" ",sta);
if(end == string::npos) end = cmd.size();
cmd_arr.push_back(cmd.substr(sta,end-sta));
}
if(cmd_arr[0] == "goodbey" || cmd_arr[0] == "bey" || cmd_arr[0] == "beybey")
{
shutdown();//退出
return;
}
else if(cmd_arr[0] == "help" || cmd_arr[0] == "?")
{
help();
return;
}
else if(cmd_arr[0] == "login")
{
login();
return;
}
else if(cmd_arr[0] == "sign")
{
if(pcmd < cmd.size() && cmd[1])
{
signin();
return;
}
}
else if(cmd_arr[0] == "clear")
{
clear();
return;
}
else if(cmd_arr[0] == "show")
{
show(cmd_arr);
return;
}
else if(cmd_arr[0] == "insert")
{
insert(cmd_arr);
return;
}
else if(cmd_arr[0] == "logout")
{
logout();
return;
}
else if(cmd_arr[0] == "alert")
{
alert(cmd_arr);
return;
}
else if(cmd_arr[0] == "find")
{
find(cmd_arr);
return;
}
else if(cmd_arr[0] == "buy"&&cmd_arr[1] == "tikets")
{
if(cmd_arr.size() != 3)
{
cout<<"bad use of command."<<endl;
}
buy_tikets(cmd_arr[2]);
return;
}
else if(cmd_arr[0] == "return" && cmd_arr[1] == "tikets")
{
if(cmd_arr.size() != 3)
{
cout<<"bad use of command."<<endl;
}
return_tikets(cmd_arr[2]);
return;
}
badcmd(cmd_arr[0]);
}
void CSystem::help()
{
}
void CSystem::login()
{
string uid,pwd;
string temp;
unsigned int i;
system("cls");
cout<<"用户登录..."<<endl<<endl;
while(1)
{
cout<<"uid:";
getline(cin,uid);
cout<<"pwd:";
getline(cin,pwd);
for(i=0;i<users.size();i++)
{
if(users[i].get_id() == uid)
{
if(users[i].get_pwd() == pwd)
{
cur_user = &users[i];
system("cls");
cout<<cur_user->get_name().c_str()<<",欢迎进入系统!"<<endl;
return;
}
else
{
break;
}
}
}
cout<<"用户id不存在,或者密码不正确!"<<endl<<endl;
cout<<"是否注册?(输入'yes'注册,'no'重新输入,'exit'退出登入)"<<endl;
getline(cin,temp);
if(temp == "no");
if(temp == "yes")
{
system("cls");
signin();return;
}
if(temp == "exit")
{
return;
}
system("cls");
cout<<"重新输入:"<<endl;
}
}
void CSystem::logout()
{
string temp;
while(1)
{
cout<<"你确定要登出?(yes/no) "<<endl;
getline(cin,temp);
if(temp == "no")return;
else if(temp == "yes")
{
cur_user = NULL;
cout<<"你已经退出系统!"<<endl;
return;
}
cout<<"my god,我english不好,只认识yes,no"<<endl;
}
}
void CSystem::badcmd(string cmd)
{
cout<<cmd.c_str()<<"命令无法识别,或者使用语法错误"<<endl;
}
void CSystem::signin()
{
user new_user;
vector<string> info;
string temp;
unsigned int i;
int ok=0;
cout<<"清输入你的信息:"<<endl;
while(!ok)
{
info.clear();
cout<<"user id:";
getline(cin,temp);
info.push_back(temp);
cout<<"user pwd:";
getline(cin,temp);
info.push_back(temp);
cout<<"pwd again:";
getline(cin,temp);
info.push_back(temp);
cout<<"ture name:";
getline(cin,temp);
info.push_back(temp);
cout<<"identity:";
getline(cin,temp);
info.push_back(temp);
ok = 1;
for(i=0;i<users.size();i++)
{
if(info[0] == users[i].get_id())
{
ok = 0;
cout<<"用户id已存在!"<<endl;
break;
}
}
if(info[1] != info[2])
{
cout<<"两次输入密码不一致:"<<endl;
ok = 0;
}
}
if(ok)
{
new_user.set_id(info[0]);
new_user.set_pwd(info[1]);
new_user.set_name(info[3]);
new_user.set_identity(info[4]);
temp = "ordinary";
new_user.set_power(temp);
users.push_back(new_user);
cout<<"注册成功!"<<endl;
}
}
void CSystem::clear()
{
system("cls");
}
void CSystem::show(std::vector<std::string> &cmd_vec)
{
switch(cmd_vec.size())
{
case 1:
{
cout<<"缺少参数"<<endl;
}break;
case 2:
{
if(cmd_vec[1] == "myself" || cmd_vec[1] == "me")
{
if(cur_user == NULL)
{
cout<<"你还没有登陆,请先登录系统"<<endl;
return;
}
else
{
cout<<"你的信息:"<<endl;
cout<<cur_user->face().c_str()<<endl;
return;
}
}
}break;
case 3:
{
if(cmd_vec[1] == "all" && cmd_vec[2] == "plan")
{
show_plans();
return;
}
if(cmd_vec[1] == "all" &&cmd_vec[2] == "user")
{
show_users();
return;
}
if(cmd_vec[1] == "all" && cmd_vec[2] == "order")
{
show_orders();
return;
}
}break;
default:
{
cout<<"参数太多!"<<endl;
}break;
}
cout<<"命令格式不对"<<endl;
}
void CSystem::show_users()
{
size_t i;
if(cur_user ==NULL)
{
cout<<"you are not login,please login at first..."<<endl;;
return;
}
cout<<"所有的用户信息如下:"<<endl;
for(i = 0;i<users.size();i++)
{
cout<<users[i].face().c_str()<<endl<<endl;
}
cout<<endl;
}
void CSystem::show_plans()
{
size_t i;
if(plans.size() == 0)
{
cout<<"these is no plan now"<<endl;
return;
}
cout<<"所有的机票信息如下:"<<endl;
for(i =0;i<plans.size();i++)
{
cout<<plans[i].face().c_str()<<endl<<endl;
}
cout<<endl;
}
void CSystem::show_orders()
{
size_t i;
if(orders.size() == 0)
{
cout<<"these is no order now"<<endl;
return;
}
cout<<"所有的订单信息如下:"<<endl;
for(i = 0;i<orders.size();i++)
{
cout<<orders[i].face().c_str()<<endl<<endl;
}
cout<<endl;
}
void CSystem::insert(vector<string> &vecarg)
{
if(vecarg.size()<2)
{
cout<<"参数太少!"<<endl;
return;
}
else if(vecarg[1] == "user")
{
insert_user(vecarg);
return;
}
else if(vecarg[1] == "plan")
{
insert_plan(vecarg);
return;
}
else if(vecarg[1] == "order")
{
insert_order(vecarg);
return;
}
else
{
cout<<"insert命令的试用不正确!"<<endl;
return;
}
}
void CSystem::insert_user(vector<string> &vecarg)
{
if(vecarg.size()<3)
{
cout<<"too few argments in command 'insert'"<<endl;
return;
}
cout<<"you can not insert user"<<endl;
}
void CSystem::insert_plan(vector<string> &vecarg)
{
if(cur_user == NULL)
{
cout<<"请先登入"<<endl;
return;
}
if(cur_user->get_power() != "admin" && cur_user->get_power() != "typer")
{
cout<<"权限太低!"<<endl;
return;
}
if(vecarg.size() <3)
{
cout<<"参数太少!"<<endl;
return;
}
if(vecarg.size() > 3)
{
cout<<"参数太多!"<<endl;
return;
}
vector<string> vectemp;
size_t sta=0,end =0;
while(1)
{
sta = vecarg[2].find_first_not_of("(,",sta);
if(sta == string::npos)
break;
end = vecarg[2].find_first_of("(,)",sta);
if(end == string::npos)
{
end = vecarg[2].size();
}
vectemp.push_back(vecarg[2].substr(sta,end-sta));
sta=end+1;
}
if(vectemp.size() <8)
{
cout<<"参数太少!"<<endl;
return;
}
if(vectemp.size() >8)
{
cout<<"参数太多!"<<endl;
return;
}
bool have=false;
size_t i;
for(i=0;i<plans.size();i++)
{
if(plans[i].get_id() == vectemp[0])
{
cout<<"此航班号已经存在,请换一个航班号!"<<endl;
return;
}
}
plan new_plan;
new_plan.set_id(vectemp[0]);
new_plan.set_sta_adr(vectemp[1]);
new_plan.set_arv_adr(vectemp[2]);
new_plan.set_sta_time(vectemp[3]);
new_plan.set_arv_time(vectemp[4]);
new_plan.set_price(vectemp[5]);
new_plan.set_zhekou(vectemp[6]);
new_plan.set_total(vectemp[7]);
new_plan.set_selled("0");
plans.push_back(new_plan);
cout<<"航班添加成功!"<<endl;
}
void CSystem::insert_order(vector<string> &vecarg)
{
cout<<"你不能添加订单!"<<endl;
}
void CSystem::alert(vector<string> &vecarg)
{
if(cur_user == NULL)
{
cout<<"please login at first!"<<endl;
return;
}
if(vecarg.size()<4)
{
cout<<"arguments too few!"<<endl;
return;
}
if(vecarg.size()>4)
{
cout<<"too many arguments,or the style of it is not recognised!"<<endl;
return;
}
if(vecarg[1] == "plan")
{
alert_plan(vecarg);
return;
}
}
void CSystem::alert_plan(vector<string> &vecarg)
{
vector<string> temp;
size_t sta=0,end=0;
sta = vecarg[2].find_first_not_of("( =)",sta);
end = vecarg[2].find_first_of("( = )",sta);
temp.push_back(vecarg[2].substr(sta,end-sta));
sta = vecarg[2].find_first_not_of("(= )",end);
end = vecarg[2].find_first_of("(= )",sta);
temp.push_back(vecarg[2].substr(sta,end-sta));
sta = vecarg[2].find_first_not_of("( =)",end);
end = vecarg[2].find_first_of("(= )",sta);
if(end == string::npos)end = vecarg[2].size();
temp.push_back(vecarg[2].substr(sta,end-sta));
if(temp[0] != "where" || temp[1] != "id")
{
cout<<"bad use of command alert"<<endl;
return;
}
size_t u=-1,i;
for(i=0;i<plans.size();i++)
{
if(plans[i].get_id() == temp[2])
{
u = i;
break;
}
}
if(u == -1)
{
cout<<"plan id is not exsit."<<endl;
return;
}
temp.clear();
sta=0;end=0;
while(1)
{
sta = vecarg[3].find_first_not_of("( =),",end);
if(sta == string::npos)break;
end = vecarg[3].find_first_of("( =),",sta);
if(end == string::npos)end = vecarg[3].size();
temp.push_back(vecarg[3].substr(sta,end - sta));
}
if(temp[0] != "set")
{
cout<<"bad use of command."<<endl;
return;
}
for(i=1;i<temp.size();i+=2)
{
plans[u].change(temp[i],temp[i+1]);
}
cout<<"修改完毕!"<<endl;
}
void CSystem::find(vector<string> &vecarg)
{
if(vecarg.size() != 3 && vecarg[1] != "plan")
{
cout<<"arguments not right!"<<endl;
return;
}
find_plan(vecarg[2]);
return;
}
void CSystem::buy_tikets(string str)
{
if(cur_user == NULL)
{
cout<<"请先登入!"<<endl;
return;
}
size_t sta=0,end = 0;
sta = str.find_first_not_of("( ,)",end);
end = str.find_first_of("( ,)",sta);
string pid = str.substr(sta,end-sta);
sta = str.find_first_not_of("( ,)",end);
end = str.find_first_of("( ,)",sta);
if(end == string::npos)end = str.size();
string num = str.substr(sta,end-sta);
//find plan by id
plan *p = NULL;
size_t i;
for(i=0;i<plans.size();i++)
{
if(plans[i].get_id() == pid)
{
p = &plans[i];
break;
}
}
if(p == NULL)
{
cout<<"plan not find."<<endl;
return;
}
else
{
if(p->get_selled() + atoi(num.c_str()) > p->get_total())
{
cout<<"sorry , these only "<<p->get_remain()<<" remain."<<endl;
return;
}
char cnum[10];
itoa(p->get_selled()+atoi(num.c_str()),cnum,10);
string snum;
snum += cnum;
p->set_selled(snum);
char tem[10];
itoa(orders.size()+1,tem,10);
string oid;
oid += tem;
order new_order;
new_order.set_id(oid);
new_order.set_pid(pid);
new_order.set_uid(cur_user->get_id());
new_order.set_date("can not get date");
new_order.set_num(num);
cout<<"buy tikets ok./nyour order id is "<<new_order.get_id()<<endl;
cout<<endl<<new_order.face()<<endl<<endl;
orders.push_back(new_order);
}
return;
}
void CSystem::return_tikets(string str)
{
if(cur_user == NULL)
{
cout<<"请先登录!"<<endl;
}
size_t sta=0,end=0;
sta = str.find_first_not_of("( ,)",end);
end = str.find_first_of("( ,)",sta);
string oid = str.substr(sta,end-sta);
sta = str.find_first_not_of("( ,)",end);
end = str.find_first_of("( ,)",sta);
if(end == string::npos)end = str.size();
string num = str.substr(sta,end-sta);
order *o= NULL;
unsigned i;
for(i=0;i<orders.size();i++)
{
if(orders[i].get_id() == oid)
o = &orders[i];
}
if(o == NULL)
{
cout<<"can not find the order as your requst"<<endl;
return;
}
if(o->get_uid() != cur_user->get_id())
{
cout<<"你不能退别人的票!"<<endl;
}
int inum = atoi(num.c_str());
if(inum > o->get_num())
{
cout<<"the number of tikets is to big"<<endl;
return;
}
char stemp[10];
itoa(o->get_num() - inum,stemp,10);
string strnum(stemp);
o->set_num(strnum);
plan *p = NULL;
for(i=0;i<plans.size();i++)
{
if(plans[i].get_id() == o->get_pid())
p = &plans[i];
}
if(p == NULL)
{
cout<<"plan not find while return order"<<endl;
return;
}
itoa(p->get_selled() - inum,stemp,10);
string sselled(stemp);
p->set_selled(sselled);
cout<<"tikets return ok."<<endl;
return;
}
void CSystem::find_plan(string str)
{
size_t sta,end;
vector<string> temp;
if(str.substr(0,5) != "where")
{
cout<<"arguments not right."<<endl;
return;
}
sta = 0;end = 5;
while(1)
{
sta = str.find_first_not_of("( =),",end);
if(sta == string::npos)break;
end = str.find_first_of("( = ),",sta);
if(end == string::npos)end = str.size();
temp.push_back(str.substr(sta,end-sta));
}
vector<string>::iterator i;
vector<plan>::iterator j;
vector<plan> result;
result = plans;
for(i=temp.begin();i != temp.end();i+=2)
{
if(*(i) == "id")
{
for(j = result.begin();j!=result.end();j++)
{
if((*j).get_id() != *(i+1))
{
result.erase(j);
j--;
}
}
}
else if(*(i) == "sta_adr")
{
for(j = result.begin();j!=result.end();j++)
if((*j).get_sta_adr() != *(i+1))
{
result.erase(j);
j--;
}
}
else if(*(i) == "arv_adr")
{
for(j = result.begin();j!=result.end();j++)
if((*j).get_arv_adr() != *(i+1))
{
result.erase(j);
j--;
}
}
else if(*(i) == "sta_time")
{
for(j = result.begin();j!=result.end();j++)
if((*j).get_sta_time() != *(i+1))
{
result.erase(j);
j--;
}
}
else if(*(i) == "arv_time")
{
for(j = result.begin();j!=result.end();j++)
if((*j).get_arv_time() != *(i+1))
{
result.erase(j);
j--;
}
}
}
if(result.size() == 0)
{
cout<<"can not find the plan as your requst."<<endl;
return;
}
cout<<"find "<<result.size()<<" as your requst."<<endl<<endl;
for(j = result.begin();j!=result.end();j++)
{
cout<<(*j).face()<<endl<<endl;
}
return;
}
---------------------------------------------------------------------------------------
#include "system.h"
#include <fstream>
#include <iostream>
#include <vector>
#include <string>
using namespace std;
int main(int argc,char **argv)
{
CSystem sys ;
string cmd;
sys.startup();
while(1)
{
cmd = sys.get_cmd();
sys.parse_cmd(cmd);
}
sys.shutdown();
return 0;
}