根据案例,我们还是先在computerRoom.txt中直接添加点数据
//几机房 机器数量
1 20
2 50
3 100
同样我们在头文件下新建一个computerRoom.h文件
添加如下代码:
#pragma once
#include
using namespace std;
class ComputerRoom {
public:
//机房id
int m_ComId;
//机房最大容量
int m_MaxNum;
};
在Manager管理类下,添加机房的容器,用于保存信息
//机房容器
#include "computerRoom.h"
vector 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::iterator it = vCom.begin(); it != vCom.end();it++) {
cout << "机房编号:" << it->m_ComId << " 机房最大容量:" << it->m_MaxNum << endl;
}
system("pause");
system("cls");
}
效果图:
接着我们来实现cleanFile成员方法
由于我们order文件中没有东西,如果需要测试可以自己添加一点
//清空预约系统
void Manager::clearFile()
{
ofstream ofs(ORDER_FILE, ios::trunc);
ofs.close();
cout << "清空成功!" << endl;
system("pause");
system("cls");
}
接下来我们对学生有参构造进行实现
student.cpp
//有参构造
Student::Student(int id, string name, string pwd)
{
this->m_Id = id;
this->m_Name = name;
this->m_Pwd = pwd;
}
首先添加一个学生菜单函数,在机房预约系统.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
在这里添加方法,测试一下
然后我们也要来获取一下机房信息,在studen.h中
#include"computerRoom.h"
#include "globalFile.h"
#include
//机房信息
vector 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号机房容量:" <> 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");
}
接着我们来创建一个预约类,用于获取所有记录和显示,以及更新
文件名为orderFile.h 和 orderFile.cpp文件
orderFile.h
#pragma once
#include
#include
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 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>::iterator it = m_orderData.begin(); it != m_orderData.end(); it++) {
cout << "条数为:" << it->first << "value = " << endl;
for (map::iterator mit = (*it).second.begin(); mit != it->second.end(); mit++) {
cout << "key = " << mit->first << " value = " << mit->second << " ";
}
cout << endl;
}
}
//截取字符串
void OrderFile::insertInfo(map &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));
}
}
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");
}
//查看所有预约
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;
}
}
student.cpp
//取消预约
void Student::cancelOrder()
{
OrderFile of;
if (of.m_size == 0)
{
cout << "无预约记录" << endl;
system("pause");
system("cls");
}
cout << "审核中或预约成功的记录可以取消,请输入取消的记录" << endl;
vector 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");
}
//有参构造
Teacher::Teacher(int empId, string name, string pwd)
{
//初始化属性
this->m_EmpId = empId;
this->m_Name = name;
this->m_Pwd = pwd;
}
首先我们还是回到机房预约系统.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 << "请输入序号进行操作:";
根据学生的申请进行审核
teacher.cpp
//审核预约
void Teacher::validOrder()
{
OrderFile of;
if (of.m_size == 0)
{
cout << "无预约记录" << endl;
system("pause");
system("cls");
}
cout << "审核中或预约成功的记录可以取消,请输入取消的记录" << endl;
vector 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");
}