1 机房预约系统需求
1.1 身份简介
分别有三种身份使用该程序:
(1)学生代表:申请使用机房
(2)教师:审核学生的预约申请
(3)管理员:给学生、教师创建账号
1.2 机房简介
分机房公有3间房间
(1)1号机房——最大容量为20人
(2)2号机房——最大容量为50人
(3)3号机房——最大容量为100人
1.3 申请简介
申请的订单每周由管理员负责清空
学生可以预约未来一周内机房的使用,预约的日期为周一至周五,预约时需要选择预约时段
教师来审核预约,依据实际情况审核预约通过或者不通过
1.4 系统具体需求
首先进入登录页面,可选择登录身份:
学生代表
老师
管理员
退出
每个身份都需要进行验证后,进入子菜单:
学生需要输入:学好、姓名、登录密码
老师需要输入:职工号、姓名、登录密码
管理员需要输入:管理员姓名、登录密码
学生具体功能:
申请预约——预约机房
查看预约——查看自己的预约状态
查看所有预约——查看全部预约信息以及预约状态
取消预约——取消自身预约,预约陈宫或审核中的预约均可以取消
注销登录——退出登录
教师具体功能:
查看所有预约——查看全部预约信息、预约状态
审核预约——对学生的预约进行审核
注销登录——退出登录
管理员具体功能:
添加账号——添加学生或教师账号,需要检测学生编号或教师编号是否重复
查看账号——可以选择查看学生或教师的全部信息
查看机房——查看机房所有信息
2 创建项目
2.1 创建项目
2.2 添加文件
3 创建主菜单
功能描述:
设计主菜单,与用户进行交互(分支功能未实现,只是搭建框架)
#include<iostream>
using namespace std;
int main()
{
int select = 0;//用于接收用户的选择
while (true)
{
cout << "-------------------------欢迎来到机房预约系统--------------------------" << endl;
cout << "请输入你的身份" << endl;
cout << "\t\t----------------------------------------------------\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 1.学生代表 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 2.老 师 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 3.管 理 员 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 0.退 出 |\n";
cout << "\t\t| |\n";
cout << "\t\t----------------------------------------------------\n";
cout << "请输入你的选择" << endl;
cin >> select;//接收用户选择
switch (select)
{
case 0:
break;
case 1:
break;
case 2:
break;
case 3:
break;
default:
cout << "输入有误,请重新选择!" << endl;
system("pause");
system("cls");
break;
}
}
system("pause");
system("cls");
}
4 退出功能
4.1 退出功能实现
在main函数分支0选项中,添加退出程序的代码
case 0:
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
4.2 退出功能测试
main函数当前完整代码
#include<iostream>
using namespace std;
int main()
{
int select = 0;//用于接收用户的选择
while (true)
{
cout << "-------------------------欢迎来到机房预约系统--------------------------" << endl;
cout << "请输入你的身份" << endl;
cout << "\t\t----------------------------------------------------\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 1.学生代表 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 2.老 师 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 3.管 理 员 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 0.退 出 |\n";
cout << "\t\t| |\n";
cout << "\t\t----------------------------------------------------\n";
cout << "请输入你的选择" << endl;
cin >> select;//接收用户选择
switch (select)
{
case 0:
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
case 1:
break;
case 2:
break;
case 3:
break;
default:
cout << "输入有误,请重新选择!" << endl;
system("pause");
system("cls");
break;
}
}
system("pause");
system("cls");
}
5 创建身份类
5.1 身份基类
在整个系统中,有三种身份。分别为:学生代表、老师、管理员
三种身份有其共性也有其特性,因此我们可以将三种身份抽象出一个身份基类identity
在头文件下创建identity.h
#pragma once
#include<iostream>
using namespace std;
//身份抽象类
class Identity
{
public:
//操作菜单
virtual void operMenu()=0;
//属性
string m_Name;
string m_Pwd;
};
5.2 学生类
5.2.1 功能分析
学生类主要功能是通过类中成员函数,实现预约实验室操作
学生类主要功能:
显示学生操作的菜单界面
申请预约
查看自身预约
查看所有预约
取消预约
5.2.2 学生类创建
(1)创建student.h头文件
#pragma once
#include<iostream>
using namespace std;
#include "identity.h"
class Student : public Identity
{
public:
//默认构造函数
Student();
//有参构造
Student(int id, string name, string pwd);
//基类继承
//1.菜单界面
virtual void operMenu();
//学生类的特性
//1.查看我的预约
void showMyOrder();
//2.查看所有预约
void showAllOrder();
//3.取消预约
void cancelOrder();
//4.申请预约
void applyOrder();
//4.学生id
int m_Id;
};
(2)创建student.cpp源文件.(当前只搭建框架,没有具体实现)
#include<iostream>
using namespace std;
#include "student.h"
//把所有.h中的所有成员函数一一实现
//默认构造函数
Student::Student()
{
}
//有参构造
Student::Student(int id, string name, string pwd)
{
}
//基类继承
//1.菜单界面
void Student::operMenu()
{
}
//学生类的特性
//1.查看我的预约
void Student::showMyOrder()
{
}
//2.查看所有预约
void Student::showAllOrder()
{
}
//3.取消预约
void Student::cancelOrder()
{
}
//4.申请预约
void Student::applyOrder()
{
}
5.3 教师类
5.3.1 功能分析
教师类主要功能是查看学生预约,并进行审核
教师类主要功能:
显示教师操作的菜单界面
查看所有预约
审核预约
5.3.2 教师类创建
(1)创建teacher.h头文件
#pragma once
#include<iostream>
using namespace std;
#include"identity.h"
class Teacher :public Identity
{
public:
//默认构造
Teacher();
//有参构造
Teacher(int empId, string name, string pwd);
//基类继承
virtual void operMenu();
//教师特性
//1.查看所有预约
void showAllOrder();
//2.审核预约
void validOrder();
//3.教师编号
int m_EmpId;
};
(2)创建teacher.cpp源文件.(当前只搭建框架,没有具体实现)
#include<iostream>
using namespace std;
#include"teacher.h"
//默认构造
Teacher::Teacher()
{
}
//有参构造
Teacher::Teacher(int empId, string name, string pwd)
{
}
//基类继承
void Teacher::operMenu()
{
}
//教师特性
//1.查看所有预约
void Teacher::showAllOrder()
{
}
//2.审核预约
void Teacher::validOrder()
{
}
5.4 管理员类
5.4.1 功能分析
管理员类主要功能是对学生和老师账户进行管理,查看机房信息以及清空预约记录
管理员类主要功能:
显示管理员操作的菜单界面
添加账号
查看机房信息
清空预约记录
5.4.2 管理员类创建
(1)创建manager.h头文件
#pragma once
#include<iostream>
using namespace std;
#include"identity.h"
class Manager :public Identity
{
public:
//默认构造
Manager();
//有参构造
Manager(string name, string pwd);
//继承基类
virtual void operMenu();
//管理员特性
//1.添加账号
void addPerson();
//2.查看账号
void showPerson();
//3.查看机房信息
void showComputer();
//4.清空预约记录
void clearFile();
};
(2)创建managerr.cpp源文件.(当前只搭建框架,没有具体实现)
#include<iostream>
using namespace std;
#include"manager.h"
//默认构造
Manager::Manager()
{
}
//有参构造
Manager::Manager(string name, string pwd)
{
}
void Manager::operMenu()
{
}
//管理员特性
//1.添加账号
void Manager::addPerson()
{
}
//2.查看账号
void Manager::showPerson()
{
}
//3.查看机房信息
void Manager::showComputer()
{
}
//4.清空预约记录
void Manager::clearFile()
{
}
6 登录模块
6.1 全局文件添加
功能描述:
不同的身份可能会用到不同的文件操作,我们可以将所有文件名定义到一个全局文件中
创建globalFile.h文件
#pragma once
//管理员文件
#define ADMIN_FILE "admin.txt"
//学生文件
#define STUDENT_FILE "student.txt"
//教师文件
#define TEACHER_FILE "teacher.txt"
//机房信息文件
#define COMPUTER_FILE "computerRoom.txt"
//订单文件
#define ORDER_FILE "order.txt"
同级目录下创建txt文件
6.2 登录函数封装
功能描述:
根据用户的选择,进入不同的身份登录
在机房预约系统.cpp文件中添加全局函数void LoginIn(string fileName,inttype),不完整代码如下
#include"globalFile.h"
#include<fstream>
//登录功能:参数1 操作文件名 参数2 操作身份类型
void LoginIn(string fileName, int type)
{
//父类指针,用于指向子类对象
Identity* person = NULL;
//读文件
ifstream ifs;
ifs.open(fileName, ios::in);
//判断文件是否存在
if (!ifs.is_open())
{
cout << "文件不存在" << endl;
ifs.close();
return;
}
//准备接受用户的信息
int id = 0;
string name;
string pwd;
//判断身份
if (type == 1)
{
cout << "请输入你的学号:" << endl;
cin >> id;
}
else if (type == 2)
{
cout << "请输入你的职工号:" << endl;
cin >> id;
}
cout << "请输入用户名:" << endl;
cin >> name;
cout << "请输入密码:" << endl;
cin >> pwd;
if (type == 1)
{
//学生身份登录
}
else if(type==2)
{
//教师身份登录
}
else if (type == 3)
{
//管理员身份登录
}
cout << "验证登录失败" << endl;
system("pause");
system("cls");
return;
}
在机房预约系统.cpp文件中main函数补充代码,不完整代码如下
int main()
{
int select = 0;//用于接收用户的选择
while (true)
{
cout << "-------------------------欢迎来到机房预约系统--------------------------" << endl;
cout << "请输入你的身份" << endl;
cout << "\t\t----------------------------------------------------\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 1.学生代表 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 2.老 师 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 3.管 理 员 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 0.退 出 |\n";
cout << "\t\t| |\n";
cout << "\t\t----------------------------------------------------\n";
cout << "请输入你的选择" << endl;
cin >> select;//接收用户选择
switch (select)
{
case 0:
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
break;
case 1://学生身份
LoginIn(STUDENT_FILE, 1);
break;
case 2://老师身份
LoginIn(TEACHER_FILE, 2);
break;
case 3://管理员身份
LoginIn(ADMIN_FILE, 3);
break;
default:
cout << "输入有误,请重新选择!" << endl;
system("pause");
system("cls");
break;
}
}
system("pause");
system("cls");
}
6.3 学生、教师、管理员登录实现
在student.txt文件中添加两条学生信息,用于测试
1 zhangsan 123
2 lisi 123456
其中第一列代表学号;第二列代表学生姓名;第三列代表密码
在teachert.txt文件中添加一条教师信息,用于测试
1 laowang 123
其中第一列代表教师职工号;第二列代表教师姓名;第三列代表密码
在admin.txt文件中添加一条管理员信息,用于测试
admin 123
其中第一列代表姓名;第二列代表密码
在机房预约系统.cpp文件中的全局函数void LoginIn(string fileName,inttype)的登录验证分支中----补充代码,不完整代码如下
//登录功能:参数1 操作文件名 参数2 操作身份类型
void LoginIn(string fileName, int type)
{
//父类指针,用于指向子类对象
Identity* person = NULL;
//读文件
ifstream ifs;
ifs.open(fileName,ios::in);
//判断文件是否存在
if (!ifs.is_open())
{
cout << "文件不存在" << endl;
ifs.close();
return;
}
//准备接受用户的信息
int id = 0;
string name;
string pwd;
//判断身份
if (type == 1)
{
cout << "请输入你的学号:" << endl;
cin >> id;
}
else if (type == 2)
{
cout << "请输入你的职工号:" << endl;
cin >> id;
}
cout << "请输入用户名:" << endl;
cin >> name;
cout << "请输入密码:" << endl;
cin >> pwd;
//登录验证分支
if (type == 1)
{
//学生登录验证
int fId;//从文件中读取的ID号
string fName;//从文件中获取的姓名
string fPwd;//从文件中获取的密码
while (ifs >> fId && ifs >> fName && ifs >> fPwd)
{
if (id == fId && name == fName && pwd == fPwd)//文件内容与用户输入做对比
{
cout << "学生登录信息验证通过" << endl;
system("pause");
system("cls");
//创建一个学生在堆区
person = new Student(id, name, pwd);
//进入学生身份子菜单
studentMenu(person);
return;
}
}
}
else if (type == 2)
{
//教师身份登录
int fId;//从文件中读取的ID号
string fName;//从文件中获取的姓名
string fPwd;//从文件中获取的密码
while (ifs >> fId && ifs >> fName && ifs >> fPwd)
{
if (id == fId && name == fName && pwd == fPwd)//文件内容与用户输入做对比
{
cout << "教师登录信息验证通过" << endl;
system("pause");
system("cls");
//创建一个教师在堆区
person = new Teacher(id, name, pwd);
//进入教师身份子菜单
teacherMenu(person);
return;
}
}
}
else if (type == 3)
{
//管理员身份登录
string fName;//从文件中获取的姓名
string fPwd;//从文件中获取的密码
while (ifs >> fName && ifs >> fPwd)
{
if (name == fName && pwd == fPwd)//文件内容与用户输入做对比
{
cout << "管理员登录信息验证通过" << endl;
system("pause");
system("cls");
//创建一个管理员在堆区
person = new Manager(name, pwd);
//进入管理员身份子菜单
managerMenu(person);//该函数暂未声明和实现
return;
}
}
}
cout << "验证登录失败" << endl;
system("pause");
system("cls");
return;
}
7 管理员模块
7.1 管理员登录、注销
7.1.1 构造函数
在manager.cpp中实现构造函数,初始化管理员信息
//有参构造
Manager::Manager(string name, string pwd)
{
this->m_Name = name;
this->m_Pwd = pwd;
}
7.1.2 管理员菜单显示
manager.cpp中实现void Manager::operMenu()函数
void Manager::operMenu()
{
cout << "欢迎管理员:" << this->m_Name << "登录!" << endl;
cout << "\t\t----------------------------------------------------\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 1.添加账号 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 2.查看账号 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 3.查看机房 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 4.清空预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 5.注销登录 |\n";
cout << "\t\t| |\n";
cout << "\t\t----------------------------------------------------\n";
cout << "请选择你的操作" << endl;
}
7.1.3 管理员菜单功能实现
在机房预约系统.cpp中,当用户登录的是管理员,添加管理员菜单接口
将不同的分支提供出来:
添加账号
查看账号
查看机房
清空预约
注销登录
在机房预约系统.cpp中添加全局函数void managerMenu(Identity *manager)
//管理员子菜单
void managerMenu(Identity* manager)
{
while (true)
{
//调用管理员子菜单
manager->operMenu();
//将父类指针转换为子类指针,调用子类其他接口
Manager* man = (Manager*)manager;
int select = 0;
//接受用户选择
cin >> select;
if (select == 1)//添加账号
{
cout << "添加账号" << endl;
man->addPerson();
}
else if (select == 2)//查看账号
{
cout << "查看账号" << endl;
man->showPerson();
}
else if (select == 3)//查看机房
{
cout << "查看机房" << endl;
man->showComputer();
}
else if (select == 4)//清空预约
{
cout << "清空预约" << endl;
man->clearFile();
}
else
{
delete manager;//销毁堆区对象
cout << "注销成功" << endl;
system("pause");
system("cls");
return;
}
}
}
7.2 管理员添加账号
功能描述:给学生或教师添加新账号
功能要求:添加时学生学号不能重复、教师职工号不能重复
7.2.1 添加功能实现
在manager.cpp中实现addPerson()函数
#include "globalFile.h"
#include <fstream>
//1.添加账号
void Manager::addPerson()
{
cout << "请输入添加账号的类型" << endl;
cout << "1.添加学生" << endl;
cout << "2.添加老师" << endl;
string filename;
string tip;
//选择添加的用户类型
ofstream ofs;
int select = 0;
cin >> select;
if (select == 1)
{
filename = STUDENT_FILE;
tip = "请输入学号:";
}
else
{
filename = TEACHER_FILE;
tip = "请输入职工号:";
}
//输入添加的用户的信息
ofs.open(filename, ios::out | ios::app);
int id;
string name;
string pwd;
cout << tip << endl;
cin >> id;
cout << "请输入姓名" << endl;
cin >> name;
cout << "请输入密码" << endl;
cin >> pwd;
//用户写入相应文件
ofs << id << " " << name << " " << pwd << " " << endl;
cout << "添加成功!" << endl;
system("pause");
system("cls");
ofs.close();
}
7.2.2 去重操作
功能描述:添加账号是,如果是重复的学生编号,或者重复的教师职工号,提示有误
7.2.2.1 读取信息
要去除重复账号,首先要先将学生和教师的账号信息获取到程序中,方可检测
(1)在manager.h中舔几两个容器,用于存放学生和教师信息,添加一个成员函数void initVector()初始化容器
#include"student.h"
#include"teacher.h"
#include<vector>
//初始化容器
void initVector();
//学生容器
vector<Student>vStu;
//教师容器
vector<Teacher>vTea;
(2)在manager.cpp中实现成员函数void initVector()
//初始化容器
void Manager::initVector()
{
vStu.clear();
vTea.clear();
//读取信息:学生
ifstream ifs;
ifs.open(STUDENT_FILE, ios::in);
if (!ifs.is_open())
{
cout << "文件读取失败" << endl;
return;
}
Student s;
while (ifs >> s.m_Id && ifs >> s.m_Name && ifs >> s.m_Pwd)
{
vStu.push_back(s);
}
//cout << "当前学生的数量为" << vStu.size() << endl;
//读取信息:老师
ifs.open(TEACHER_FILE, ios::in);
if (!ifs.is_open())
{
cout << "文件读取失败" << endl;
return;
}
Teacher t;
while (ifs >> t.m_EmpId && ifs >> t.m_Name && ifs >> t.m_Pwd)
{
vTea.push_back(t);
}
/*cout << "当前老师的数量为" << vTea.size() << endl;*/
}
(3)在manager.cpp的有参构造中调用初始化容器的函数
//有参构造
Manager::Manager(string name, string pwd)
{
//初始化管理员信息
this->m_Name = name;
this->m_Pwd = pwd;
//初始化容器:获取到老师、学生文件中的信息
this->initVector();
}
7.2.2.2 去重操作
(1)在manager.h中添加成员函数 checkRepeat()
//检测重复 参数:(传入id,传入类型) 返回值:(true 代表有重复)
bool checkRepeat(int id,int type);
(2)在manager.cpp中实现成员函数 checkRepeat()
//检测新添加的id是否存在
bool Manager::checkRepeat(int id, int type)
{
if (type == 1)
{
for (vector<Student>::iterator it = vStu.begin(); it != vStu.end(); it++)
{
if (id == it->m_Id)
{
return true;//检测重复
}
}
}
else
{
for (vector<Teacher>::iterator it = vTea.begin(); it != vTea.end(); it++)
{
if (id == it->m_EmpId)
{
return true;//检测重复
}
}
}
return false;
}
(3)在manager.cpp中补充成员函数addPerson()
//1.添加账号
void Manager::addPerson()
{
cout << "请输入添加账号的类型" << endl;
cout << "1.添加学生" << endl;
cout << "2.添加老师" << endl;
string filename;
string tip;
string errorTip;//重复错误提示
//选择添加的用户类型
ofstream ofs;
int select = 0;
cin >> select;
if (select == 1)
{
filename = STUDENT_FILE;
tip = "请输入学号:";
errorTip = "学号重复,请重新输入!";
}
else
{
filename = TEACHER_FILE;
tip = "请输入职工号:";
errorTip = "职工号重复,请重新输入!";
}
//输入添加的用户的信息
ofs.open(filename, ios::out | ios::app);
int id;//学号或职工号
string name;//姓名
string pwd;//密码
cout << tip << endl;
while (true)//检测重复操作:
{
cin >> id;
bool ret = this->checkRepeat(id, select);
if (ret)//有重复
{
cout << errorTip << endl;
}
else
{
break;//退出循环
}
}
cout << "请输入姓名" << endl;
cin >> name;
cout << "请输入密码" << endl;
cin >> pwd;
//用户写入相应文件
ofs << id << " " << name << " " << pwd << " " << endl;
cout << "添加成功!" << endl;
system("pause");
system("cls");
ofs.close();
//读取文件:每次添加用户之前,先进行初始化,读取文件中用户信息
this->initVector();
}
7.3 管理员查看账号
功能描述:显示学生信息或教师信息
7.3.1 显示功能实现
(1)在manager.cpp中实现成员函数showPerson(),并添加输出学生、老师信息的函数printStudent()、printTeacher()
#include<algorithm>
//2.查看账号
void printStudent(Student& s)
{
cout << "学号:" << s.m_Id << " 姓名:" << s.m_Name << " 密码:" << s.m_Pwd << endl;
}
void printTeacher(Teacher& t)
{
cout << "职工号:" << t.m_EmpId << " 姓名:" << t.m_Name << " 密码:" << t.m_Pwd<< endl;
}
void Manager::showPerson()
{
cout << "请输入查看的内容:" << endl;
cout << "1.查看所有学生" << endl;
cout << "2.查看所有老师" << endl;
//用户选择操作
int select = 0;
cin >> select;
if (select == 1)
{
//查看学生
for_each(vStu.begin(), vStu.end(), printStudent);//提供普通函数,函数名
}
else
{
//查看老师
for_each(vTea.begin(), vTea.end(), printTeacher);//提供普通函数,函数名
}
system("pause");
system("cls");
}
7.4 管理员查看机房
7.4.1 添加机房信息
功能描述:案例需求,机房一共有3个,其中一号机房容量20,2号机房容量50,3号机房容量100,将信息录入到computerRoom.txt中
7.4.2 机房类创建
创建computerRoom.h头文件
#pragma once
#include<iostream>
using namespace std;
//机房类
class ComputerRoom
{
public:
int m_ComID;//机房ID
int m_MaxNum;//机房最大容量
};
7.4.3 查看机房信息
(1)在manager.h中增加机房容器,用于保存机房信息
#include"computerRoom.h"
//机房容器
vector<ComputerRoom>vCom;
(2)在manager.cpp有参构造中添加代码,初始化机房信息
//有参构造
Manager::Manager(string name, string pwd)
{
//初始化管理员信息
this->m_Name = name;
this->m_Pwd = pwd;
//初始化容器:获取到老师、学生文件中的信息
this->initVector();
//获取机房信息
ifstream ifs;
ifs.open(COMPUTER_FILE, ios::in);
ComputerRoom com;
while (ifs >> com.m_ComID && ifs >> com.m_MaxNum)
{
vCom.push_back(com);
}
ifs.close();
}
(3)在manager.cpp实现查看机房信息函数showComputer()
//3.查看机房信息
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");
}
7.5 管理员清空预约
功能描述:清空生成的order.txt预约文件
7.5.1 清空功能实现
在manager.cpp实现清空预约函数clearFile()
//4.清空预约记录
void Manager::clearFile()
{
ofstream ofs(ORDER_FILE, ios::trunc);//trunc打开方式:如果文件存在则删除,重新创建文件
ofs.close();
cout << "清空成功" << endl;
system("pause");
system("cls");
}
8 学生模块
8.1 学生登录、注销
8.1.1 构造函数
在student.cpp中实现构造函数,初始化学生信息
//有参构造
Student::Student(int id, string name, string pwd)
{
//初始化学生信息
this->m_Id = id;
this->m_Name = name;
this->m_Pwd = pwd;
}
8.1.2 学生菜单显示
student.cpp中实现void Student::operMenu())函数
//1.菜单界面
void Student::operMenu()
{
cout << "欢迎学生代表:" << this->m_Name << "登录!" << endl;
cout << "\t\t----------------------------------------------------\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 1.申请预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 2.查看预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 3.查看所有预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 4.取消预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 5.退出登录 |\n";
cout << "\t\t| |\n";
cout << "\t\t----------------------------------------------------\n";
cout << "请选择你的操作" << endl;
}
8.1.3 管理员菜单功能实现
在机房预约系统.cpp中,当用户登录的是学生,添学生菜单接口
将不同的分支提供出来:
申请预约
查看我的预约
查看所有
清空预约
注销登录
机房预约系统.cpp中实现学生菜单studentMenu()
//学生子菜单
void studentMenu(Identity*& student)
{
while (true)
{
//调用学生子菜单
student->operMenu();
//将父类指针转换为子类指针,调用子类其他接口
Student* stu = (Student*)student;
//接受用户选择
int select=0;
cin >> select;
if (select == 1)//申请预约
{
cout << "申请预约" << endl;
stu->applyOrder();
}
else if (select == 2)//查看预约
{
cout << "查看预约" << endl;
stu->showMyOrder();
}
else if (select == 3)//查看所有预约
{
cout << "查看所有预约" << endl;
stu->showAllOrder();
}
else if (select == 4)//取消预约
{
cout << "取消预约" << endl;
stu->cancelOrder();
}
else
{
delete student;//退出登录
cout << "退出登录" << endl;
system("pause");
system("cls");
return;
}
}
}
8.2 学生申请预约
8.2.1 获取机房信息
(1)student.h中添加成员函数
#include<vector>
#include"computerRoom.h"
//机房容量
vector<ComputerRoom>vCom;
(2)student.cpp构造函数添加代码容器中包含机房信息
#include"globalFile.h"
#include<fstream>
#include"computerRoom.h"
Student::Student(int id, string name, string pwd)
{
//初始化学生信息
this->m_Id = id;
this->m_Name = name;
//获取机房信息
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();
}
8.2.2 预约功能实现
student.app中实现成员函数applyOrder()
//4.申请预约
void Student::applyOrder()
{
cout << "------------机房开放的时间为周一至周五!---------" << endl;
//(1)用户输入预约信息
int data = 0;//日期
int interval = 0;//时间段
int room = 0;//机房
cout << "请输入申请预约的时间:" << endl;
cout << "1.周一" << endl;
cout << "2.周二" << endl;
cout << "3.周三" << endl;
cout << "4.周四" << endl;
cout << "5.周无" << endl;
while (true)
{
cin >> data;
if (data >= 1 && data <= 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;
for (int i = 0; i < vCom.size(); i++)
{
cout << vCom[i].m_ComID<< "号机房容量为:" << vCom[i].m_MaxNum << endl;
}
while (true)
{
cin >> room;
if (room >= 1 && room <= 3)
{
break;
}
cout << "输入有误,请重新输入!" << endl;
}
cout << "预约成功!审核中" << endl;
//(2)文件信息写入
ofstream ofs;
ofs.open(ORDER_FILE, ios::app);//追加的方式打开
ofs << "data:" << data << " ";
ofs << "interva:" << interval << " ";
ofs << "stuId:" << this->m_Id << " ";
ofs << "stuName:" << this->m_Name << " ";
ofs << "roomId" << room<< " ";
ofs << "status:" << 1 << endl;
ofs.close();
system("pause");
system("cls");
}
8.3 学生显示预约
8.3.1 创建预约类
功能描述:显示预约记录是,需要从文件中获取到所有记录,用于显示,创建预约类来灌流记录以及更新
(1)创建orderFile.h头文件
#pragma once
#include<iostream>
using namespace std;
#include <map>
#include"globalFile.h"
#include<string>
class OrderFile
{
public:
//构造函数
OrderFile();
//更新预约记录
void updateOrder();
//记录的容器:key记录条数,value记录的具体信息(日期、时间段、id、姓名、、、)
map<int, map<string, string>> m_orderDate;
//预约记录条数
int m_Size;
};
(2)创建orderFile.cpp源文件
#include<iostream>
using namespace std;
#include"globalFile.h"
#include<string>
#include"orderFile.h"
#include <fstream>
//构造函数:将预约信息放入m_rderDate中
OrderFile::OrderFile()
{
ifstream ifs;
ifs.open(ORDER_FILE, ios::in);
string date;//日期
string interval;//时间段
string stuId;//学生id
string stuName;//学生姓名
string roomId;//学生id
string status;//预约状态
this->m_Size = 0;//记录条数
while (ifs >> date && ifs >> interval && ifs >> stuId && ifs >> stuName && ifs >> roomId && ifs >> status)
{
/*cout << date << endl;//data:1
cout << interval << endl;
cout << stuId << endl;
cout << stuName << endl;
cout << roomId << endl;
cout << status << endl;*/
//(1)小map<string,string>
string key;
string value;
map<string, string>m;
int pos=date.find(":");
if (pos != -1)
{
key = date.substr(0, pos);
value = date.substr(pos + 1, date.size() - pos - 1);
m.insert(make_pair(key, value));
}
pos = interval.find(":");
if (pos != -1)
{
key = interval.substr(0, pos);
value = interval.substr(pos + 1, interval.size() - pos - 1);
m.insert(make_pair(key, value));
}
pos = stuId.find(":");
if (pos != -1)
{
key = stuId.substr(0, pos);
value = stuId.substr(pos + 1, stuId.size() - pos - 1);
m.insert(make_pair(key, value));
}
pos = stuName.find(":");
if (pos != -1)
{
key = stuName.substr(0, pos);
value = stuName.substr(pos + 1, stuName.size() - pos - 1);
m.insert(make_pair(key, value));
}
pos = roomId.find(":");
if (pos != -1)
{
key = roomId.substr(0, pos);
value = roomId.substr(pos + 1, roomId.size() - pos - 1);
m.insert(make_pair(key, value));
}
pos = status.find(":");
if (pos != -1)
{
key = status.substr(0, pos);
value = status.substr(pos + 1, status.size() - pos - 1);
m.insert(make_pair(key, value));
}
//(2)大map<int,map>:小map放入大map中
this->m_orderDate.insert(make_pair(this->m_Size, m));
this->m_Size++;
}
测试大容器
//for (map<int, map<string, string>>::iterator it = m_orderDate.begin(); it != m_orderDate.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;
//}
ifs.close();
}
//更新预约记录:容器中所有数据重新写入到文件中
void OrderFile::updateOrder()
{
if (this->m_Size == 0)
{
return;
}
ofstream ofs(ORDER_FILE, ios::out | ios::trunc);
for (int i = 0; i < m_Size; i++)
{
ofs << "date:" << this->m_orderDate[i]["date"] << " ";
ofs << "interval:" << this->m_orderDate[i]["interval"] << " ";
ofs << "stuId:" << this->m_orderDate[i]["stuId"] << " ";
ofs << "stuName:" << this->m_orderDate[i]["stuName"] << " ";
ofs << "roomId:" << this->m_orderDate[i]["roomId"] << " ";
ofs << "status:" << this->m_orderDate[i]["status"] << endl;
}
ofs.close();
}
8.3.2 显示自身预约
student.cpp中实现showMyOrder()函数
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 (atoi(of.m_orderDate[i]["stuId"].c_str())== this->m_Id)
{
//找到自身预约
cout << "日期:" << of.m_orderDate[i]["data"] << " 时间段:" << (of.m_orderDate[i]["interval"] == "1" ? "上午" : "下午")
<< " 机房号:" << of.m_orderDate[i]["roomId"];
string status = " 状态:";
//1审核中2已预约-1预约失败0取消预约
if (of.m_orderDate[i]["status"] == "1")
{
status += "审核中";
}
else if (of.m_orderDate[i]["status"] == "2")
{
status += "预约成功";
}
else if (of.m_orderDate[i]["status"] == "-1")
{
status += "预约失败";
}
else
{
status += "预约已取消";
}
cout << status << endl;
}
}
system("pause");
system("cls");
}
8.3.3 显示所有预约
student.cpp中实现showAllOrder()函数
//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_orderDate[i]["data"]
<< " 时间段:" << (of.m_orderDate[i]["interval"] == "1" ? "上午" : "下午")
<<" 学号:"<<of.m_orderDate[i]["stuId"]
<<" 姓名:"<<of.m_orderDate[i]["stuName"]
<< " 机房号:" << of.m_orderDate[i]["roomId"];
string status = " 状态:";
//1审核中2已预约-1预约失败0取消预约
if (of.m_orderDate[i]["status"] == "1")
{
status += "审核中";
}
else if (of.m_orderDate[i]["status"] == "2")
{
status += "预约成功";
}
else if (of.m_orderDate[i]["status"] == "-1")
{
status += "预约失败";
}
else
{
status += "预约已取消";
}
cout << status << endl;
}
system("pause");
system("cls");
}
8.4 学生取消预约
student.cpp中实现cancelOrder()函数
//3.取消预约
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_orderDate[i]["stuId"].c_str()))
{
//赛选状态:审核中或预约成功
if (of.m_orderDate[i]["status"] == "1" || of.m_orderDate[i]["status"] == "2")
{
v.push_back(i);
cout << index++ << "、";
cout << "预约日期:周" << of.m_orderDate[i]["data"];
cout << " 时间段:" << (of.m_orderDate[i]["interval"] == "1" ? "上午" : "下午")
<< " 机房号:" << of.m_orderDate[i]["roomId"];
string status = " 状态:";
//1审核中2已预约-1预约失败0取消预约
if (of.m_orderDate[i]["status"] == "1")
{
status += "审核中";
}
else if (of.m_orderDate[i]["status"] == "2")
{
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_orderDate[v[select - 1]]["status"] = "0";
of.updateOrder();
cout << "已取消预约!" << endl;
break;
}
}
cout << "输入有误,请重新输入!" << endl;
}
system("pause");
system("cls");
}
9 教师模块
9.1 教师登录和注销
9.1.1构造函数
teacher.cpp中实现构造函数
//有参构造
Teacher::Teacher(int empId, string name, string pwd)
{
//初始化属性
this->m_EmpId = empId;
this->m_Name = name;
this->m_Pwd = pwd;
}
9.1.2 教师菜单显示
teacher.cpp中实现operMenu()
//基类继承
void Teacher::operMenu()
{
cout << "欢迎老师:" << this->m_Name << "登录!" << endl;
cout << "\t\t----------------------------------------------------\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 1.查看预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 2.审核预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 0.退出登录 |\n";
cout << "\t\t| |\n";
cout << "\t\t----------------------------------------------------\n";
cout << "请选择你的操作" << endl;
}
9.1.3 教师子菜单实现
在机房预约系统.cpp中。当用户登录的是教师,添加教师菜单接口
将不同分支提供出来:
查看所有预约
审核预约
注销登录
机房预约系统.cpp中添加全局函数viod TeacherMenu(Person *&manager)
//教师子菜单
void teacherMenu(Identity*& teacher)
{
while (true)
{
//调用学生子菜单
teacher->operMenu();
//将父类指针转换为子类指针,调用子类其他接口
Teacher* tea = (Teacher*)teacher;
//接受用户选择
int select = 0;
cin >> select;
if (select == 1)//查看预约
{
cout << "查看预约" << endl;
tea->showAllOrder();
}
else if (select == 2)//审核预约
{
cout << "查看预约" << endl;
tea->validOrder();
}
else
{
delete teacher;//退出登录
cout << "退出登录" << endl;
system("pause");
system("cls");
return;
}
}
}
9.2 教师查看所有预约
teacher.cpp中实现函数void Teacher::showAllOrder()
#include"orderFile.h"
#include<vector>
//1.查看所有预约
void Teacher::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_orderDate[i]["data"]
<< " 时间段:" << (of.m_orderDate[i]["interval"] == "1" ? "上午" : "下午")
<< " 学号:" << of.m_orderDate[i]["stuId"]
<< " 姓名:" << of.m_orderDate[i]["stuName"]
<< " 机房号:" << of.m_orderDate[i]["roomId"];
string status = " 状态:";
//1审核中2已预约-1预约失败0取消预约
if (of.m_orderDate[i]["status"] == "1")
{
status += "审核中";
}
else if (of.m_orderDate[i]["status"] == "2")
{
status += "预约成功";
}
else if (of.m_orderDate[i]["status"] == "-1")
{
status += "预约失败";
}
else
{
status += "预约已取消";
}
cout << status << endl;
}
system("pause");
system("cls");
}
9.3 教师审核预约
teacher.cpp中实现函数void Teacher::showAllOrder()
//2.审核预约
void Teacher::validOrder()
{
OrderFile of;
if (of.m_Size == 0)
{
cout << "无预约记录" << endl;
system("pause");
system("cls");
return;
}
cout << "待审核的记录如下:" << endl;
vector<int>v;
int index = 0;
for (int i = 0; i < of.m_Size; i++)
{
if (of.m_orderDate[i]["status"] == "1")
{
v.push_back(i);
cout << ++index << "、";
cout << "预约日期:周" << of.m_orderDate[i]["data"];
cout << " 时间段:" << (of.m_orderDate[i]["interval"] == "1" ? "上午" : "下午")
<< " 机房号:" << of.m_orderDate[i]["roomId"];
string status = " 状态:";
//1审核中2已预约-1预约失败0取消预约
if (of.m_orderDate[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_orderDate[v[select - 1]]["status"] = "2";
}
else
{
of.m_orderDate[v[select - 1]]["status"] = "-1";
}
of.updateOrder();
cout << "审核完毕!" << endl;
break;
}
}
cout << "输入有误,请重新输入!" << endl;
}
system("pause");
system("cls");
}