Student学生类
1、有参构造函数
在有参构造函数中,给Student类的三个成员属性赋值,即在创建Student对象时便给该对象赋值。然后打开computerRoom.txt文件,将机房信息导入容器vectorvCom中。
//有参构造函数(学号、姓名、密码)
Student::Student(int id, string name, string pwd)
{
this->m_Id = id;
this->m_Name = name;
this->m_Pwd = pwd;
ifstream ifs;
ifs.open(COMPUTER_FILE, ios::in);
if (!ifs.is_open())
{
cout << "文件打开失败!" << endl;
return;
}
ComputerRoom c;
while (ifs >> c.m_ComId && ifs >> c.m_MaxNum)
{
this->vCom.push_back(c);
}
//cout << "当前机房数量为:" << this->vCom.size() << endl;
ifs.close();
}
2、申请预约
首先向用户显示一些预约信息,然后进入大循环中,先让用户输入预约选择,再打开order.txt文件,将刚刚输入的预约信息直接写入文本文件中。此时写入的代表预约状态的“status”为1,即审核中。
//申请预约
void Student::applyOrder()
{
cout << "机房开放时间为周一至周五!" << endl;
cout << "请输入申请预约的日期:" << endl;
cout << "1、周一" << endl;
cout << "2、周二" << endl;
cout << "3、周三" << endl;
cout << "4、周四" << endl;
cout << "5、周五" << endl;
int date = 0;
int interval = 0;
int room = 0;
while (1)
{
cin >> date;
if (date >= 1 && date <= 5)
{
break;
}
cout << "输入有误,请重新输入:";
}
cout << "请输入申请预约的时间段:" << endl;
cout << "1、上午" << endl;
cout << "2、下午" << endl;
while (1)
{
cin >> interval;
if (interval >= 1 && interval <=2)
{
break;
}
cout << "输入有误,请重新输入:";
}
cout << "请选择机房:" << endl;
for (int i = 0; i < vCom.size(); i++)
{
cout <<vCom[i].m_ComId<< "号机房容量" << this->vCom[i].m_MaxNum << endl;
}
while (1)
{
cin >> room;
if (room >= 1 && room <= 3)
{
break;
}
cout << "输入有误,请重新输入:";
}
cout << "预约成功,审核中!" << endl;
ofstream ofs;
ofs.open(ORDER_FILE, ios::app);
ofs << "date:" << date << " ";
ofs << "interval:" << interval << " ";
ofs << "stuId:" << this->m_Id << " ";
ofs << "stuName:" << this->m_Name << " ";
ofs << "roomId:" << room << " ";
ofs << "status:" << 1 << endl;
ofs.close();
system("pause");
system("cls");
}
3、查看我的预约
创建OrderFile对象of,同时自动执行OrderFile的默认构造函数,将order.txt中预约信息写入m_OrderData容器。然后遍历该容器,检测本Student类对象的成员属性this->id(即学号)是否与容器中某条预约记录的ID号相等(此处把string型数据转为int型数据),若相等则输入该条记录的相关信息。
//查看我的预约
void Student::showMyOrder()
{
OrderFile of;
if (of.m_size == 0)
{
cout << "无预约记录" << endl;
system("pause");
system("cls");
return;
}
for (int i = 0; i < of.m_size; i++)
{
//string 转 int
//string 利用.c_str()转const char *
//利用 atoi(const char *)转int
if (this->m_Id == atoi(of.m_OrderData[i]["stuId"].c_str()))
{
cout << "预约日期:周" << of.m_OrderData[i]["date"] << " ";
cout << "时间段:" << (of.m_OrderData[i]["interval"] == "1" ? "上午" : "下午") << " ";
cout << "机房号:" << of.m_OrderData[i]["roomId"] << " ";
string status = "预约状态:";
//1-审核中 2-已预约 -1-预约失败 0-取消预约
if (of.m_OrderData[i]["status"] == "1")
{
status += "审核中";
}
else if (of.m_OrderData[i]["status"] == "2")
{
status += "已预约";
}
else if (of.m_OrderData[i]["status"] == "-1")
{
status += "预约失败,审核未通过";
}
else if (of.m_OrderData[i]["status"] == "0")
{
status += "预约已取消";
}
cout << status << endl;
}
}
system("pause");
system("cls");
}
4、查看所有预约
与查看我的预约类似,但不再需要判断id号是否相等。
//查看所有预约
void Student::showAllOrder()
{
OrderFile of;
if (of.m_size == 0)
{
cout << "无预约记录" << endl;
system("pause");
system("cls");
return;
}
for (int i = 0; i < of.m_size; i++)
{
cout << i + 1 << "、 ";
cout << "预约日期:周" << of.m_OrderData[i]["date"] << " ";
cout << "时间段:" << (of.m_OrderData[i]["interval"] == "1" ? "上午" : "下午") << " ";
cout << "学号:" << of.m_OrderData[i]["stuId"] << " ";
cout << "姓名:" << of.m_OrderData[i]["stuName"] << " ";
cout << "机房:" << of.m_OrderData[i]["roomId"] << " ";
string status = "预约状态:";
//1-审核中 2-已预约 -1-预约失败 0-取消预约
if (of.m_OrderData[i]["status"] == "1")
{
status += "审核中";
}
else if (of.m_OrderData[i]["status"] == "2")
{
status += "已预约";
}
else if (of.m_OrderData[i]["status"] == "-1")
{
status += "预约失败,审核未通过";
}
else if (of.m_OrderData[i]["status"] == "0")
{
status += "预约已取消";
}
cout << status << endl;
}
system("pause");
system("cls");
}
5、取消预约
创建OrderFile对象of,同时自动执行OrderFile的默认构造函数,将order.txt中预约信息写入m_OrderData容器。然后遍历该容器,检测本Student类对象的成员属性this->id(即学号)是否与容器中某条预约记录的ID号相等(此处把string型数据转为int型数据),若相等,再检测该条记录的预约状态是否为审核中或者已完成(即"status"是否为"1"或"2"),然后按顺序(1、2、3……)依次显示该同学的所有预约记录。同时创建容器vectorv,将所有该同学的预约记录在m_OrderData中的key值(即预约记录条次)放入v容器中。然后由用户输入要删除的记录条数(1、2、3……),由v容器中存储的对应key值连接m_OrderData容器,将对应的"status"改为"-1"(即代表预约失败)。最后调用of.updateOrder()函数,更新order.txt文本文件。
//取消预约
void Student::cancelOrder()
{
OrderFile of;
if (of.m_size == 0)
{
cout << "无预约记录" << endl;
system("pause");
system("cls");
return;
}
cout << "审核中或者预约成功的记录可以取消,请输入要取消的记录" << endl;
vector<int>v; //存放在最大容器中的下标编号
int index = 1;
for (int i = 0; i < of.m_size; i++)
{
//先判断是自身学号
if (this->m_Id == atoi(of.m_OrderData[i]["stuId"].c_str()))
{
//再筛选状态 审核中或预约成功
if (of.m_OrderData[i]["status"] == "1" || of.m_OrderData[i]["status"] == "2")
{
v.push_back(i);
cout << index++ << "、 ";
cout << "预约日期:周" << of.m_OrderData[i]["date"] << " ";
cout << "时间段:" << (of.m_OrderData[i]["interval"] == "1" ? "上午" : "下午") << " ";
cout << "机房号:" << of.m_OrderData[i]["roomId"] << " ";
string status = "预约状态:";
//1-审核中 2-已预约 -1-预约失败 0-取消预约
if (of.m_OrderData[i]["status"] == "1")
{
status += "审核中";
}
else if (of.m_OrderData[i]["status"] == "2")
{
status += "已预约";
}
cout << status << endl;
}
}
}
cout << "请输入要取消的记录,0代表返回:" << endl;
int select = 0;
while (1)
{
cin >> select;
if (select >= 0 && select <= v.size())
{
if (select == 0)
{
break;
}
else
{
of.m_OrderData[v[select - 1]]["status"] = "0";
of.updateOrder();
cout << "已取消预约" << endl;
break;
}
}
cout << "输入有误,请重新输入:" << endl;
}
system("pause");
system("cls");
}
Teacher类
1、有参构造函数
//有参构造函数(职工编号、姓名、密码)
Teacher::Teacher(int EmpId, string name, string pwd)
{
this->m_EmpId = EmpId;
this->m_Name = name;
this->m_Pwd = pwd;
}
2、查看所有预约
与学生类中代码相同
3、审核预约
创建OrderFile对象of,同时自动执行OrderFile的默认构造函数,将order.txt中预约信息写入m_OrderData容器。然后遍历该容器,检测该条记录的预约状态是否为审核中(即"status"是否为"1"),然后按顺序(1、2、3……)依次显示所有待审核的预约记录。同时创建容器vectorv,将所有待审核的预约记录在m_OrderData中的key值(即预约记录条次)放入v容器中。然后由用户输入要审核的记录条数(1、2、3……),再输入审核结果(通过或不通过),由v容器中存储的对应key值连接m_OrderData容器,将对应的"status"改为"-1"(即代表预约失败)或"2"(即代表已预约)。最后调用of.updateOrder()函数,更新order.txt文本文件。
//审核预约
void Teacher::validOrder()
{
OrderFile of;
if (of.m_size == 0)
{
cout << "无预约记录" << endl;
system("pause");
system("cls");
return;
}
vector<int>v; //存放在最大容器中的下标编号
int index = 0;
cout << "待审核的预约记录如下:" << endl;
for (int i = 0; i < of.m_size; i++)
{
if (of.m_OrderData[i]["status"] == "1")
{
v.push_back(i);
cout << ++index << "、 ";
cout << "预约日期:周" << of.m_OrderData[i]["date"] << " ";
cout << "时间段:" << (of.m_OrderData[i]["interval"] == "1" ? "上午" : "下午") << " ";
cout << "学号:" << of.m_OrderData[i]["stuId"] << " ";
cout << "姓名" << of.m_OrderData[i]["stuName"] << " ";
cout << "机房号:" << of.m_OrderData[i]["roomId"] << " ";
string status = "预约状态:";
//1-审核中 2-已预约 -1-预约失败 0-取消预约
if (of.m_OrderData[i]["status"] == "1")
{
status += "审核中";
}
cout << status << endl;
}
}
cout << "请输入审核的预约记录,按0返回" << endl;
int select = 0;
int ret = 0;
while (1)
{
cin >> select;
if (select >= 0 && select <= v.size())
{
if (select == 0)
{
break;
}
else
{
cout << "请输入审核结果" << endl;
cout << "1、通过" << endl;
cout << "2、不通过" << endl;
cin >> ret;
if (ret == 1)
{
of.m_OrderData[v[select-1]]["status"] = "2";
}
else
{
of.m_OrderData[v[select - 1]]["status"] = "-1";
}
of.updateOrder();
cout << "审核完毕!" << endl;
break;
}
}
cout << "输入有误,请重新输入:" << endl;
}
system("pause");
system("cls");
}