文章目录
- 机房预约系统
- 一、系统需求
- 1、系统简介
- 2、身份简介
- 3、机房简介
- 4、申请简介
- 5、系统需求
- 6、创建文件
- 二 、创建主菜单
- 1、菜单实现&接口搭建
- 三、退出功能实现
- 四、创建身份类
- 1、身份基类
- 2、学生类
- 2.1功能分析
- 2.2类的创建
- 3、老师类
- 3.1功能分析
- 3.2类的创建
- 4、管理员类
- 4.1功能分析
- 4.2类的创建
- 五、登录模块
- 1、全局文件添加
- 2、登陆函数封装
- 六、管理员模块
- 1、管理员登陆和注销
- 1.1构造函数
- 1.2管理员子菜单
- 2、添加账号
- 2.1添加功能实现
- 2.2去重操作
- 2.2.1读取信息
- 2.2.2去重函数封装
- 3、显示账号功能
- 4、查看机房
- 4.1添加机房信息
- 4.2机房类创建
- 4.3初始化机房信息
- 4.4显示机房信息
- 5、清空预约
- 5.1功能实现
- 七、学生模块
- 1、学生登陆和注销
- 1.1构造函数
- 1.2学生子菜单
- 2、申请预约
- 2.1获取机房信息
- 2.2预约功能实现
- 3、显示预约
- 3.1创建预约类
- 3.2显示自身预约
- 3.3显示所有预约
- 4、取消预约
- 八、教师模块
- 1、教师登陆和注销
- 1.1构造函数
- 1.2教师子菜单
- 1.3菜单功能实现
- 2、查看所有预约
- 2.1所有预约功能实现
- 3、审核预约
- 3.1审核功能实现
机房预约系统
一、系统需求
1、系统简介
学校现有几个规格不同的机房,为了让大家在申请的时候不发生冲突,,让每个人都能提前知晓机房的使用情况从而有效地申请机位,现开发这套机房预约系统
2、身份简介
分别有三种身份使用该程序
学生代表:申请使用机房
教师:审核学生的预约申请
管理员:给学生、教师创建账号
3、机房简介
机房总共有三间
1 号机房 – 最大容量 20人
2 号机房 – 最多容量 50 人
3 号机房 – 最多容量 100 人
4、申请简介
申请的订单每周有管理员负责清空
学生可以预约未来一周内的机房使用,预约的日期为周一到周五,预约时需要选择预约时段(上午、下午)
教师来审核预约,依据实际情况审核通过或者不通过
5、系统需求
首先进入登录界面,可选登录身份有:
学生代表
老师
管理员
每个身份都需要进行验证后,进入子菜单
学生需要输入:学号、姓名、登录密码
老师需要输入:职工号、姓名、登录密码
管理员需要输入:姓名、登录密码
学生具体功能
申请预约 – 预约机房
查看自身预约 – 查看自己的预约状态
查看所有预约 – 查看全部预约信息以及预约状态
取消预约 – 取消自身的预约,预约成功或审核中的预约可以取消
教师具体功能
查看所有预约 – 查看全部预约信息以及预约状态
审核预约 – 对学生的预约进行审核
注销登录 – 退出登录
管理员具体功能
添加账号 – 添加学生或教师的账号,需要检测学生编号或教师职工号是否重复
查看账号 – 可以选择查看学生或教师的全部信息
查看机房 – 查看所有机房的信息
6、创建文件
创建一个机房预约系统的项目,同时创建一个 机房预约系统.cpp 文件作为主要运行文件
二 、创建主菜单
功能描述:
设计主菜单,与用户进行交互
1、菜单实现&接口搭建
在主函数 main 中添加菜单提示,代码如下
int main()
{
int select = 0;//用于接收用户的选择
while (true)
{
cout << "==================欢迎来到机房预约系统=================" << endl;
cout << endl << "请输入您的身份" << endl;
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 << "请输入您的选择:";
cin >> select;//接受用户选择
switch (select)//根据用户选择,实现不同接口
{
case 1://学生身份
LoginIn(STUDENT_FILE, 1);
break;
case 2://教师身份
LoginIn(TEACHER_FILE, 2);
break;
case 3://管理员身份
LoginIn(ADMIN_FILE, 3);
break;
case 0://退出系统
cout << "欢迎下一次使用" << endl;
system("pause");
return 0;
break;
default:
cout << "输入有误,请重新选择!" << endl;
system("pause");
system("cls");
break;
}
}
system("pause");
return 0;
}
三、退出功能实现
在 main() 函数分支选项中,添加退出程序功能
cout << "欢迎下一次使用" << endl;
system("pause");
return 0;
break;
四、创建身份类
1、身份基类
在整个系统中,有三种身份,分别为:学生代表、老师以及管理员
三种身份有其共性也有其特性,因此,我们可以将三种身份抽象出一个身份基类 identity
在头文件中创建 identity.h 文件
identity.h 中添加如下代码
#pragma once
#include<iostream>
using namespace std;
class Identify {
public:
virtual void showMenu() = 0;
string m_Name;
string m_Pwd;
};
2、学生类
2.1功能分析
学生类主要功能是可以通过类中成员函数,实现预约实验室操作
学生类中主要功能有
提示学生操作的菜单界面
申请预约
查看自身预约
查看所有预约
取消预约
2.2类的创建
在头文件以及源文件下创建 student.h 和 student.cpp 文件
student.h 中
#pragma once
#include<iostream>
using namespace std;
#include"Identify.h"
#include"computerRoom.h"
#include<vector>
#include"globalFile.h"
#include"orderFile.h"
class Student:public Identify {
public:
Student();
Student(int id, string name, string pwd);
virtual void showMenu();
void applyOrder();
void showMyOrder();
void showAllOrdder();
void cancleOrdedr();
int m_Id;
vector<ComputerRoom>vCom;
};
student.cpp
#include"student.h"
#include<fstream>
Student::Student()
{
}
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);
ComputerRoom c;
while (ifs >> c.m_ComId && ifs >> c.m_MaxNum)
{
vCom.push_back(c);
}
ifs.close();
}
void Student::showMenu()
{
cout << "欢迎学生代表:" << this->m_Name << "登陆" << endl;
cout << "-----------------------------------" << endl;
cout << "------------1、申请预约------------" << endl;
cout << "----------2、查看我的预约----------" << endl;
cout << "----------3、查看所有预约----------" << endl;
cout << "------------4、取消预约------------" << endl;
cout << "------------0、注销登陆------------" << endl;
cout << "-----------------------------------" << endl;
}
void Student::applyOrder()
{
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 << "date:" << date << " ";
ofs << "interval:" << interval << " ";
ofs << "stuId:" << this->m_Id << " ";
ofs << "stuName:" << this->m_Name << " ";
ofs << "roomId:" << room << " ";
ofs << "status" << 1 << endl;//默认待审核状态为1
ofs.close();
system("pause");
system("cls");
}
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转成char*,再用atoi给他转为c风格的int
if (atoi(of.m_orderData[i]["stuId"].c_str()) == this->m_Id)
{
//如果遍历出来的id和自身id相同,说明遍历出来的是自身的信息
cout << "预约日期:周" << of.m_orderData[i]["date"];
cout << "时段:" << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
cout << "机房:" << of.m_orderData[i]["roomId"];
string status = "状态:";//0取消预约1审核中2已预约-1预约失败
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::showAllOrdder()
{
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]["roomId"];
string status = "状态:";//0取消预约1审核中2已预约-1预约失败
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::cancleOrdedr()
{
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 (atoi(of.m_orderData[i]["stuId"].c_str()) == this->m_Id)
{
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 = "状态:";
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 (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");
}
3、老师类
3.1功能分析
教师主要功能是查看学生的预约,并进行审核
教师类中主要功能有:
显示教师操作的菜单界面
查看所有预约
审核预约
3.2类的创建
在头文件以及源文件中创建 teacher.h 和 teacher.cpp 文件
teacher.h
#pragma once
#define _CRT_SECURE_NO_WARINGS
#include<iostream>
using namespace std;
#include"Identify.h"
#include"orderFile.h"
#include"globalFile.h"
#include"computerRoom.h"
#include<vector>
#include<string>
class Teacher :public Identify
{
public:
Teacher();
Teacher(int empId, string name, string pwd);
virtual void showMenu();
void showAllOrder();
void validOrder();
int m_EmpId;
};
teacher.cpp
#include"teacher.h"
Teacher::Teacher()
{
}
Teacher::Teacher(int empId,string name,string pwd)
{
this->m_EmpId = empId;
this->m_Name = name;
this->m_Pwd = pwd;
}
void Teacher::showMenu()
{
cout << "欢迎教师:" << this->m_Name << "登陆" << endl;
cout << "-----------------------------------" << endl;
cout << "----------1、查看所有预约----------" << endl;
cout << "------------2、审核预约------------" << endl;
cout << "------------0、注销登陆------------" << endl;
cout << "-----------------------------------" << endl;
}
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 << i + 1 << "、";
cout << "预约日期:周" << of.m_orderData[i]["date"];
cout << "时段:" << of.m_orderData[i]["interval"];
cout << "学号:" << of.m_orderData[i]["stuId"];
cout << "姓名:" << of.m_orderData[i]["stuName"];
cout << "机房:" << of.m_orderData[i]["roomId"];
string status = " 状态";
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 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_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 = " 状态";
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;
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";
}
if (ret == 2)
{
of.m_orderData[v[select - 1]]["status"] = "-1";
}
of.updateOrder();
cout << "审核完毕" << endl;
break;
}
}
cout << "输入有误,请重新输入" << endl;
}
system("pause");
system("cls");
}
4、管理员类
4.1功能分析
管理类主要功能是对于学生和老师账户进行管理,直接查看机房信息以及清空预约记录
管理员类中主要功能有:
显示管理员操作界面
添加账号
查看机房信息
清空预约记录
4.2类的创建
在头文件以及源文件下创建 manager.h 和 manager.cpp 文件
manager.h
#pragma once
#include<iostream>
using namespace std;
#include"Identify.h"
#include<string>
#include<vector>
#include<algorithm>
#include"student.h"
#include"teacher.h"
#include"computerRoom.h"
class Manager :public Identify {
public:
Manager();
Manager(string name, string pwd);
virtual void showMenu();
void addPerson();
void showPerson();
void showComputer();
void cleanFile();
void initVector();
vector<Student>vStu;
vector<Teacher>vTea;
//检测重复 参数:(传入id和类型)返回值:(true代表有重复,false代表无重复
//type==1,检测学生,type==2,检测老师
bool checkRepeat(int id, int type);
vector<ComputerRoom>vCom;
};
manager.cpp
#include"Cmanager.h"
#include"globalFile.h"
#include<fstream>
Manager::Manager()
{
}
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 c;
while (ifs >> c.m_ComId && ifs >> c.m_MaxNum)
{
vCom.push_back(c);
}
cout << "当前机房数量为:" << vCom.size() << endl;
ifs.close();
}
void Manager::showMenu()
{
cout << "欢迎管理员:" << this->m_Name << "登陆" << endl;
cout << "\t\t--------------------------------------\n";
cout << "\t\t| 1、添加账号 | \n" << endl;
cout << "\t\t| 2、查看账号 | \n" << endl;
cout << "\t\t| 3、查看机房 | \n" << endl;
cout << "\t\t| 4、清空预约 | \n" << endl;
cout << "\t\t| 0、注销登录 | \n" << endl;
cout << "\t\t--------------------------------------\n";
}
void Manager::addPerson()
{
cout << "请输入添加账号的类型" << endl;
cout << "1、添加学生" << endl;
cout << "2、添加老师" << endl;
string fileName;
string tip;//提示id号
ofstream ofs;
int select = 0;
cin >> select;
string errorTip;//重复错误提示
if (select == 1)
{
fileName = STUDENT_FILE;
tip = "请输入学号:";
errorTip = "学号重复,请重新输入";
}
else if (select == 2)
{
fileName = TEACHER_FILE;
tip = "请输入职工号:";
errorTip = "职工号重复,请重新输入";
}
else
{
cout << "输入有误,请重新输入" << endl;
system("pause");
system("cls");
return;
}
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();
}
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)
{
cout << "所有学生信息如下:" << endl;
for_each(vStu.begin(), vStu.end(), printStudent);
}
else
{
cout << "所有老师信息如下:" << endl;
for_each(vTea.begin(), vTea.end(), printTeacher);
}
system("pause");
system("cls");
}
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");
}
void Manager::cleanFile()
{
ofstream ofs(ORDER_FILE, ios::trunc);
ofs.close();
cout << "清空成功" << endl;
system("pause");
system("cls");
}
void Manager::initVector()
{
ifstream ifs;
ifs.open(STUDENT_FILE, ios::in);
if (!ifs.is_open())
{
cout << "文件读取失败" << endl;
return;
}
vStu.clear();
vTea.clear();
Student s;
while (ifs >> s.m_Id && ifs >> s.m_Name && ifs >> s.m_Pwd)
{
vStu.push_back(s);
}
cout << "当前学生数量为:" << vStu.size() << endl;
ifs.close();
ifs.open(TEACHER_FILE, ios::in);
Teacher t;
while (ifs >> t.m_EmpId && ifs >> t.m_Name && ifs >> t.m_Pwd)
{
vTea.push_back(t);
}
cout << "当前老师数量为:" << vTea.size() << endl;
ifs.close();
}
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;
}
五、登录模块
1、全局文件添加
功能描述:
不同的身份可能会用到不同的文件操作,我们可以将所有的文件名定义到一个文件中
创建 globalFile.h 并在其中添加以下代码
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"
2、登陆函数封装
功能描述:
根据用户的选择,进入不同的身份登录
在预约系统的主运行文件中添加全局函数
参数:
fileName – 操作文件名
type – 登录的身份(1代表学生,2代表老师,3代表管理员)
void LoginIn(string fileName, int type)
{
//多态 用父类指针创建子类对象 父类指针指向子类对象
Identify* person = NULL;
//读文件
ifstream ifs;
ifs.open(fileName, ios::in);
//文件不存在情况
if (!ifs.is_open())
{
cout << "文件不存在" << endl;
ifs.close();
return;
}
int id = 0;//管理员没有id号
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;
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;
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 (fName == name && fPwd == pwd)
{
cout << "管理员验证登陆成功" << endl;
system("pause");
system("cls");
person = new Manager(name, pwd);
//创建管理员子菜单
managerMenu(person);
return;
}
}
}
cout << "验证登陆失败!" << endl;
system("pause");
system("cls");
return;
}
六、管理员模块
1、管理员登陆和注销
1.1构造函数
在 Manager 类的构造函数中,初始化管理员信息
Manager::Manager(string name,string pwd)
{
this->m_Name = name;
this->m_Pwd = pwd;
}
1.2管理员子菜单
在机房预约系统.cpp 中,当用户登录的是管理员,添加管理员菜单接口
将不同的分支提供出来
添加账号
查看账号
查看机房
清空预约
注销登录
实现注销功能
添加全局函数
void managerMenu(Identify*& manager)//用父类指针传进来
{
while (true)
{
//管理员菜单
manager->showMenu();
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->cleanFile();
}
else
{
delete manager;//这个对象是new出来的,需要手动释放,delete
//cout << "注销成功" << endl;
system("psuse");
system("cls");
return;
}
}
}
在 Manager.cpp 的 showMenu 成员函数中,实现界面的功能
void Manager::showMenu()
{
cout << "欢迎管理员:" << this->m_Name << "登陆" << endl;
cout << "\t\t--------------------------------------\n";
cout << "\t\t| 1、添加账号 | \n" << endl;
cout << "\t\t| 2、查看账号 | \n" << endl;
cout << "\t\t| 3、查看机房 | \n" << endl;
cout << "\t\t| 4、清空预约 | \n" << endl;
cout << "\t\t| 0、注销登录 | \n" << endl;
cout << "\t\t--------------------------------------\n";
}
2、添加账号
功能描述:
给学生或教师添加新的账号
功能要求:
添加学生时学生学号不能重复,教师职工号不能重复
2.1添加功能实现
在 Manager.cpp 的 addPerson 成员函数中,实现添加新账号的功能
void Manager::addPerson()
{
cout << "请输入添加账号的类型" << endl;
cout << "1、添加学生" << endl;
cout << "2、添加老师" << endl;
string fileName;
string tip;//提示id号
ofstream ofs;
int select = 0;
cin >> select;
string errorTip;//重复错误提示
if (select == 1)
{
fileName = STUDENT_FILE;
tip = "请输入学号:";
errorTip = "学号重复,请重新输入";
}
else if (select == 2)
{
fileName = TEACHER_FILE;
tip = "请输入职工号:";
errorTip = "职工号重复,请重新输入";
}
else
{
cout << "输入有误,请重新输入" << endl;
system("pause");
system("cls");
return;
}
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();
}
2.2去重操作
功能描述:
添加新账号时,如果是重复的学生编号,或是重复的教师职工编号,提示有误
2.2.1读取信息
要去除重复账号,首先先将学生和教师的账号信息获取到程序中,方可检测
在 manager.h 中添加两个容器,用于存放学生和教师信息
添加一个新的成员函数,初始化容器
void initVector();
在 Manager 的showPerson函数中,获取目前学生和教师的信息
void Manager::showPerson()
{
cout << "请选择查看内容:" << endl;
cout << "1、查看所有学生" << endl;
cout << "2、查看所有老师" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
cout << "所有学生信息如下:" << endl;
for_each(vStu.begin(), vStu.end(), printStudent);
}
else
{
cout << "所有老师信息如下:" << endl;
for_each(vTea.begin(), vTea.end(), printTeacher);
}
system("pause");
system("cls");
}
在构造函数中调用接口
Manager::Manager(string name,string pwd)
{
this->m_Name = name;
this->m_Pwd = pwd;
this->initVector();
}
2.2.2去重函数封装
在 manager.h 文件中添加成员函数
bool checkRepeat(int id, int type);
在 manager.cpp 中实现函数
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;
}
在 addPerson 函数中,在输入id时调用checkRepeat函数
void Manager::addPerson()
{
cout << "请输入添加账号的类型" << endl;
cout << "1、添加学生" << endl;
cout << "2、添加老师" << endl;
string fileName;
string tip;//提示id号
ofstream ofs;
int select = 0;
cin >> select;
string errorTip;//重复错误提示
if (select == 1)
{
fileName = STUDENT_FILE;
tip = "请输入学号:";
errorTip = "学号重复,请重新输入";
}
else if (select == 2)
{
fileName = TEACHER_FILE;
tip = "请输入职工号:";
errorTip = "职工号重复,请重新输入";
}
else
{
cout << "输入有误,请重新输入" << endl;
system("pause");
system("cls");
return;
}
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();
}
3、显示账号功能
在 Manager 的 showPerson 成员函数中,实现显示账号功能
void Manager::showPerson()
{
cout << "请选择查看内容:" << endl;
cout << "1、查看所有学生" << endl;
cout << "2、查看所有老师" << endl;
int select = 0;
cin >> select;
if (select == 1)
{
cout << "所有学生信息如下:" << endl;
for_each(vStu.begin(), vStu.end(), printStudent);
}
else
{
cout << "所有老师信息如下:" << endl;
for_each(vTea.begin(), vTea.end(), printTeacher);
}
system("pause");
system("cls");
}
4、查看机房
4.1添加机房信息
案例需求中,机房一共有三个,其中 1 号机房容量 20 台机器,2 号 50台,3 号 100 台
我们可以将信息录入到 computerRoom.txt 中
4.2机房类创建
在头文件中,创建新的文件 computerRoom.h
#pragma once
#include<iostream>
using namespace std;
class ComputerRoom {
public:
int m_ComId;
int m_MaxNum;
};
4.3初始化机房信息
在 manager.h 中添加存放机房信息的容器
vector<ComputerRoom>vCom;
在 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 c;
while (ifs >> c.m_ComId && ifs >> c.m_MaxNum)
{
vCom.push_back(c);
}
cout << "当前机房数量为:" << vCom.size() << endl;
ifs.close();
}
4.4显示机房信息
在 manager.cpp 的成员函数中,添加以下代码
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");
}
5、清空预约
清空生成的 order.txt 预约文件
5.1功能实现
在 Manager 的 cleanFile 成员函数中添加如下代码:
void Manager::cleanFile()
{
ofstream ofs(ORDER_FILE, ios::trunc);
ofs.close();
cout << "清空成功" << endl;
system("pause");
system("cls");
}
七、学生模块
1、学生登陆和注销
1.1构造函数
在 Student 类的构造函数中,初始化学生信息
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);
ComputerRoom c;
while (ifs >> c.m_ComId && ifs >> c.m_MaxNum)
{
vCom.push_back(c);
}
ifs.close();
}
1.2学生子菜单
在 机房预约系统.cpp 中,当用户登录的是学生,添加学生菜单接口
将不同的分支提供出来
申请预约
查看我的预约
查看所有预约
取消预约
注销登录
实现注销功能
添加全局函数
void studentMenu(Identify*& student)//传的是父类指针
{
while (true)
{
//学生菜单
student->showMenu();
Student* stu = (Student*)student;//把父类指针强转成子类指针
int select = 0;
cin >> select;
if (select == 1)//申请预约
{
stu->applyOrder();
}
else if (select == 2)//查看自身预约
{
stu->showMyOrder();
}
else if (select == 3)//查看所有预约
{
stu->showAllOrdder();
}
else if (select == 4)//取消预约
{
stu->cancleOrdedr();
}
else
{
delete student;
cout << "注销成功!" << endl;
system("pause");
system("cls");
return;
}
}
}
在 student.cpp 的 showMenu 成员函数中,实现界面的功能
void Student::showMenu()
{
cout << "欢迎学生代表:" << this->m_Name << "登陆" << endl;
cout << "-----------------------------------" << endl;
cout << "------------1、申请预约------------" << endl;
cout << "----------2、查看我的预约----------" << endl;
cout << "----------3、查看所有预约----------" << endl;
cout << "------------4、取消预约------------" << endl;
cout << "------------0、注销登陆------------" << endl;
cout << "-----------------------------------" << endl;
}
2、申请预约
2.1获取机房信息
在申请预约时,学生可以看到机房的信息,因此我们需要让学生获取到机房的信息
在 student.h 中添加新的成员属性
vector<ComputerRoom> vCom;//vCom容器中保存了所有机房的信息
2.2预约功能实现
在 student.cpp 中实现成员函数
void Student::applyOrder()
{
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 << "date:" << date << " ";
ofs << "interval:" << interval << " ";
ofs << "stuId:" << this->m_Id << " ";
ofs << "stuName:" << this->m_Name << " ";
ofs << "roomId:" << room << " ";
ofs << "status" << 1 << endl;//默认待审核状态为1
ofs.close();
system("pause");
system("cls");
}
3、显示预约
功能描述:
显示预约记录时,需要从文件中获取到所有记录,用来显示,创建预约的类来管理记录以及更新
在头文件和源文件下分别创建 orderFile.h 和 orderFile.cpp 文件
在 orderFile.h 中添加以下代码
#pragma once
#include<iostream>
using namespace std;
#include<map>
#include"globalFile.h"
#include<fstream>
class OrderFile {
public:
OrderFile();
void updateOrder();//更新预约记录
//记录的容器key,记录的条数value,具体记录的键值对信息
//嵌套容器map<string,string>中key代表的属性是date、interval之类的
//value代表的是实值
map<int, map<string, string>>m_orderData;
//预约记录条数
int m_Size;
};
3.1创建预约类
在 orderFile.cpp 中添加以下代码
#include"orderFile.h"
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)
{
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));
}
this->m_orderData.insert(make_pair(this->m_Size, m));
this->m_Size++;
}
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_orderData[i]["date"] << " ";
ofs << "interval:" << this->m_orderData[i]["interval"] << " ";
ofs << "stuId:" << this->m_orderData[i]["stuId"] << " ";
ofs << "stuName:" << this->m_orderData[i]["stuName"] << " ";
ofs << "roomId:" << this->m_orderData[i]["roomId"] << " ";
ofs << "status:" << this->m_orderData[i]["status"] << " ";
}
}
3.2显示自身预约
首先,我们先添加几条记录,可以用程序添加或者直接修改 order.txt 文件
order.txt 文件内容如下:比如我们有三名同学分别产生了 3 条预约记录
在 student 类的成员函数 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++)
{
//这里面是string和int对比,先把string转成char*,再用atoi给他转为c风格的int
if (atoi(of.m_orderData[i]["stuId"].c_str()) == this->m_Id)
{
//如果遍历出来的id和自身id相同,说明遍历出来的是自身的信息
cout << "预约日期:周" << of.m_orderData[i]["date"];
cout << "时段:" << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
cout << "机房:" << of.m_orderData[i]["roomId"];
string status = "状态:";//0取消预约1审核中2已预约-1预约失败
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");
}
3.3显示所有预约
在 student 类中的成员函数 showAllOrder 中添加代码
void Student::showAllOrdder()
{
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]["roomId"];
string status = "状态:";//0取消预约1审核中2已预约-1预约失败
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");
}
4、取消预约
在 student 类的 cancelOrder 成员函数中,添加如下代码
void Student::cancleOrdedr()
{
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 (atoi(of.m_orderData[i]["stuId"].c_str()) == this->m_Id)
{
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 = "状态:";
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 (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");
}
八、教师模块
1、教师登陆和注销
1.1构造函数
在 teacher 类的函数中,初始化教师信息,代码如下
Teacher::Teacher(int empId,string name,string pwd)
{
this->m_EmpId = empId;
this->m_Name = name;
this->m_Pwd = pwd;
}
1.2教师子菜单
在 机房预约系统.cpp 中,当用户登录的是教师,添加教师菜单接口
将不同的分支提供出来
查看所有预约
审核预约
注销登录
实现注销功能
添加全局函数 Tmenu ,代码如下
void teacherMenu(Identify*& teacher)
{
while (true)
{
teacher->showMenu();
Teacher* tea = (Teacher*)teacher;
int select = 0;
cin >> select;
if (select == 1)
{
//查看所有预约
tea->showAllOrder();
}
else if (select == 2)
{
//审核预约
tea->validOrder();
}
else
{
delete teacher;
cout << "注销成功" << endl;
system("pause");
system("cls");
return;
}
}
}
1.3菜单功能实现
在实现成员函数 showMenu ,代码如下
void Teacher::showMenu()
{
cout << "欢迎教师:" << this->m_Name << "登陆" << endl;
cout << "-----------------------------------" << endl;
cout << "----------1、查看所有预约----------" << endl;
cout << "------------2、审核预约------------" << endl;
cout << "------------0、注销登陆------------" << endl;
cout << "-----------------------------------" << endl;
}
2、查看所有预约
2.1所有预约功能实现
该功能与学生身份的查看所有预约功能相似,用于显示所有预约记录
在 Teacher.cpp 中实现成员函数 showAllOrder
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 << i + 1 << "、";
cout << "预约日期:周" << of.m_orderData[i]["date"];
cout << "时段:" << of.m_orderData[i]["interval"];
cout << "学号:" << of.m_orderData[i]["stuId"];
cout << "姓名:" << of.m_orderData[i]["stuName"];
cout << "机房:" << of.m_orderData[i]["roomId"];
string status = " 状态";
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");
}
3、审核预约
3.1审核功能实现
功能描述:
教师审核学生预约,依据实际情况审核
在 teacher.cpp 中实现成员函数 validOrder
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_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 = " 状态";
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;
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";
}
if (ret == 2)
{
of.m_orderData[v[select - 1]]["status"] = "-1";
}
of.updateOrder();
cout << "审核完毕" << endl;
break;
}
}
cout << "输入有误,请重新输入" << endl;
}
system("pause");
system("cls");
}