8.4、查看机房
8.4.1、添加机房信息
根据案例,我们还是先在computerRoom.txt中直接添加点数据
//几机房 机器数量
1 20
2 50
3 100
8.4.2、机房类创建
同样我们在头文件下新建一个computerRoom.h文件
添加如下代码:
#pragma once
#include<iostream>
using namespace std;class ComputerRoom {
public://机房idint m_ComId;//机房最大容量int m_MaxNum;
};
8.4.3、初始化机房信息并显示
在Manager管理类下,添加机房的容器,用于保存信息
//机房容器
#include "computerRoom.h"
vector<ComputerRoom> vCom;
在Manager有参构造函数中,追加如下代码,初始化机房信息
//获取机房信息
void Manager::getComputerNumber()
{// 文件名string computerFile = COMPUTER_FILE; // 替换为机房文件名ifstream ifs;ifs.open(COMPUTER_FILE, ios::in);ComputerRoom c;while (ifs>>c.m_ComId && ifs>>c.m_MaxNum){vCom.push_back(c);}cout << "当前机房数量为: " << vCom.size() << endl;ifs.close();
}
然后我们修改showComputer成员方法
void Manager::showComputer()
{cout << "机房信息如下: " << endl;for (vector<ComputerRoom>::iterator it = vCom.begin(); it != vCom.end();it++) {cout << "机房编号:" << it->m_ComId << " 机房最大容量:" << it->m_MaxNum << endl;}system("pause");system("cls");
}
效果图:
8.5、清空预约功能
接着我们来实现cleanFile成员方法
由于我们order文件中没有东西,如果需要测试可以自己添加一点
//清空预约系统
void Manager::clearFile()
{ofstream ofs(ORDER_FILE, ios::trunc);ofs.close();cout << "清空成功!" << endl;system("pause");system("cls");
}
9、学生模块
9.1、初始化信息
接下来我们对学生有参构造进行实现
student.cpp
//有参构造
Student::Student(int id, string name, string pwd)
{this->m_Id = id;this->m_Name = name;this->m_Pwd = pwd;
}
9.2、菜单实现
首先添加一个学生菜单函数,在机房预约系统.cpp中添加
void studentMenu(Indentity*& student);
然后我们来实现一下具体的步骤
void Student::studentMenu(Indentity*& student)
{while (true){//学生菜单student->operMenu();Student* stu = (Student*)student;//接受用户选项int select = 0;cin >> select;switch (select){case 1: //申请预约cout << "申请预约" << endl;stu->applyOrder();break;case 2://查看预约cout << "查看预约" << endl;stu->showMyOrder();break;case 3://查看所有预约cout << "查看所有预约" << endl;stu->showAllOrder();break;case 4://取消预约cout << "取消预约" << endl;stu->cancelOrder();break;default://注销登录//销毁掉堆区对象delete student;cout << "注销成功" << endl;system("pause");system("cls");return;}}
}
接着我们来添加一下菜单,回到student.cpp
void Student::operMenu()
{cout << "欢迎:" << this->m_Name << "同学登录!" << endl;cout << endl;cout << "-------------- 1、申请预约 -----------------" << endl;cout << endl;cout << "-------------- 2、查看预约 -----------------" << endl;cout << endl;cout << "-------------- 3、查看所有预约 -----------------" << endl;cout << endl;cout << "-------------- 4、取消预约 -----------------" << endl;cout << endl;cout << "-------------- 输入其他数字退出 -----------------" << endl;cout << "-------------------------------------------------------" << endl;cout << "请输入序号进行操作:";}
然后回到机房预约系统.cpp
在这里添加方法,测试一下
9.3、申请预约实现
然后我们也要来获取一下机房信息,在studen.h中
#include"computerRoom.h"
#include "globalFile.h"
#include<fstream>
//机房信息
vector<ComputerRoom> vCom;
回到student.cpp中的学生构造中填一下以下代码
//有参构造
Student::Student(int id, string name, string pwd)
{this->m_Id = id;this->m_Name = name;this->m_Pwd = pwd;// 文件名string computerFile = COMPUTER_FILE; // 替换为机房文件名ifstream ifs;ifs.open(COMPUTER_FILE, ios::in);ComputerRoom c;while (ifs >> c.m_ComId && ifs >> c.m_MaxNum){vCom.push_back(c);}ifs.close();
}
然后我们来实现预约功能在applyOrder()中实现
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 (true){cin >> date;if (date>=1&&date<=5){break;}cout << "输入有误,请重新输入" << endl;}cout << "请输入申请预约时间段" << endl;cout << "1、上午" << endl;cout << "2、下午" << endl;while (true){cin >> interval;if (interval >= 1 && interval <= 2){break;}cout << "输入有误,请重新输入" << endl;}cout << "请选择机房:" << endl;cout << "1号机房容量:" <<vCom[0].m_MaxNum << endl;cout << "2号机房容量:" << vCom[1].m_MaxNum << endl;cout << "3号机房容量:" << vCom[2].m_MaxNum << endl;while (true){cin >> room;if (room>=1&&room<=3){break;}cout << "输入有误,请重新输入" << endl;}cout << "预约成功!等待审核" << endl;ofstream ofs(ORDER_FILE, ios::app);ofs << "data:" << 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");
}
9.4、显示预约
接着我们来创建一个预约类,用于获取所有记录和显示,以及更新
文件名为orderFile.h 和 orderFile.cpp文件
orderFile.h
#pragma once
#include<iostream>
#include<map>
#include "globalFile.h"
#include<string>
using namespace std;class OrderFile {
public://构造函数OrderFile();//更新预约记录void updateOrder();//记录容器 key 记录条数 value -- 具体记录的键值对map<int, map<string, string>>m_orderData;//记录预约的条数int m_size;
};
orderFile.cpp
OrderFile::OrderFile()
{ifstream ifs;ifs.open(ORDER_FILE, ios::in);string date; //日期string interval; //时间段string stuId; //学生编号string stuName; //学生姓名string roomId; //机房号string status; //预约状态this->m_size = 0; //记录条数while (ifs>>date && ifs >> interval&&ifs>>stuId&&ifs>>stuName&&ifs>>roomId&&ifs>>status){map<string, string> m;insertInfo(m, date);insertInfo(m, interval);insertInfo(m, stuId);insertInfo(m, stuName);insertInfo(m, roomId);insertInfo(m, status);//将小mpa容器放到大的map容器中this->m_orderData.insert(make_pair(this->m_size, m));this->m_size++;//清空小容器m.clear();}ifs.close();//测试代码for (map<int, map<string, string>>::iterator it = m_orderData.begin(); it != m_orderData.end(); it++) {cout << "条数为:" << it->first << "value = " << endl;for (map<string, string>::iterator mit = (*it).second.begin(); mit != it->second.end(); mit++) {cout << "key = " << mit->first << " value = " << mit->second << " ";}cout << endl;}
}
//截取字符串
void OrderFile::insertInfo(map<string, string> &m,string keys) {string key;string value;int pos = keys.find(":");if (pos != -1){key = keys.substr(0, pos);value = keys.substr(pos + 1);m.insert(make_pair(key, value));}
}
9.4.1、查看自身预约
student.cpp
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++) {//找到自身预约if (this->m_Id == stoi(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{status += "预约已经取消";}cout << status << endl;}}system("pause");system("cls");
}
9.4.2、显示所有预约
//查看所有预约
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 << "预约的日期:周" << 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 {status += "预约已经取消";}cout << status << endl;}
}
9.5、取消预约
student.cpp
//取消预约
void Student::cancelOrder()
{OrderFile of;if (of.m_size == 0){cout << "无预约记录" << endl;system("pause");system("cls");}cout << "审核中或预约成功的记录可以取消,请输入取消的记录" << endl;vector<int> v;int index = 1;for (int i = 0; i< of.m_size; i++){if (this->m_Id == stoi(of.m_orderData[i]["stuId"])) {//再筛选状态 审核中或预约成功的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 += "预约成功";}else if (of.m_orderData[i]["status"] == "-1") {status += "预约失败,审核未通过";}else {status += "预约已经取消";}cout << status << endl;}}}cout << "请输入取消的记录,0代表返回" << endl;int select = 0;while (true){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");
}
10、教师模块
10.1、教师登录和注销
10.1.1、构造函数
- 在Teacher类的构造函数中,初始化教师信息,代码如下:
//有参构造
Teacher::Teacher(int empId, string name, string pwd)
{//初始化属性this->m_EmpId = empId;this->m_Name = name;this->m_Pwd = pwd;
}
10.1.2、教师子菜单
- 在机房预约系统.cpp中,当用户登录的是教师,添加教师菜单接口
- 将不同的分支提供出来
- 查看所有预约
- 审核预约
- 注销登录
- 实现注销功能
首先我们还是回到机房预约系统.cpp中,创建一个teacherMenu方法,同样的在LoginIn中创建类并链接此方法
//老师子菜单界面
void teacherMenu(Indentity*& teacher)
{while (true){//学生菜单teacher->operMenu();Teacher* tea = (Teacher*)teacher;//接受用户选项int select = 0;cin >> select;switch (select){case 1: //查看所有预约cout << "查看所有预约" << endl;tea->showAllOrder();break;case 2://审核预约cout << "审核预约" << endl;tea->validOrder();break;default://注销登录//销毁掉堆区对象delete teacher;cout << "注销成功" << endl;system("pause");system("cls");return;}}
}
teacher.cpp中 operMenu()
cout << "欢迎:" << this->m_Name << "老师!" << endl;cout << endl;cout << "-------------- 1、查看所有预约 -----------------" << endl;cout << endl;cout << "-------------- 2、审核预约 -----------------" << endl;cout << endl;cout << "-------------- 输入其他数字退出 -----------------" << endl;cout << "-------------------------------------------------------" << endl;cout << "请输入序号进行操作:";
10.1.2、审核预约
根据学生的申请进行审核
teacher.cpp
//审核预约
void Teacher::validOrder()
{OrderFile of;if (of.m_size == 0){cout << "无预约记录" << endl;system("pause");system("cls");}cout << "审核中或预约成功的记录可以取消,请输入取消的记录" << endl;vector<int> v;int index = 1;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]["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 (true){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");
}