机房预约系统项目简介
机房预约系统
机房预约系统需求
身份简介
- 学生:申请预约机房
- 教师:审核学生预约
- 管理员:创建、注销学生、教师账号
机房简介
- 1号机房:最大容量20人
- 2号机房:最大容量5人
- 3号机房:最大容量100人
申请简介
- 管理员每周清理预约
- 学生申请一周内的预约,申请时间为周一至周五,时段为上午或下午
- 教师审核学生预约是否通过
系统具体需求
- 进入主菜单选择身份登录
- 学生:学号、姓名、密码
- 教师:工号、姓名、密码
- 管理员:姓名、密码
- 退出
- 进入各身份子菜单
- 学生子菜单
- 申请预约:申请时间(周一至周五)、申请时段(上午、下午)、申请机房号(1-3号)、预约状态(审核中、通过、失败、取消)
- 查看自身预约:查看自己预约状态
- 查看所有预约:查看所有预约状态
- 取消预约:取消预约记录
- 退出登录
- 教师子菜单
- 查看所有预约:查看学生所有预约
- 审核预约:审核学生预约是否通过
- 退出登录
- 管理员子菜单
- 添加账号:添加学生或者教师账号
- 查看账号:查看所有账号
- 撤销账号:注销账号,并删除所有注销账号有关的订单信息
- 查看机房信息:查看机房信息
- 清空预约:清空所有预约
- 退出登录
- 学生子菜单
创建主菜单
class menu
{
public:
void main_menu();//主菜单
void manager_menu(identity* Manager);//管理员子菜单
void student_menu(identity* Student);//学生子菜单
void teacher_menu(identity* Teacher);//教师子菜单
};
void menu::main_menu()//主菜单
{
cout << "\t\t==============欢迎来到机房预约系统====================" << endl;
cout << "\t\t -----------------------------------------------------" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 1.学生代表 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 2.教师代表 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 3. 管理员 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 0. 退出 |" << endl;
cout << "\t\t| |" << endl;
cout << " \t\t----------------------------------------------------- " << endl;
cout << endl << "请输入您的身份:";
}
int main()
{
int select=0;
while (1)
{
menu Menu;
Menu.main_menu();
cin >> select;
switch (select) {
case 1://学生子菜单
break;
case 2://教师子菜单
break;
case 3://管理员子菜单
break;
case 0://退出系统
return 0;
break;
default:
cout << "输入有误!请重新输入!" << endl;
system("pause");
system("cls");
}
}
return 1;
}
创建身份类
身份类基类
class identity
{
public:
string name;//身份姓名
string password;//身份密码
virtual void order_menu() = 0;//定义子菜单纯虚函数,子身份继承父类身份类时重写子菜单
};
学生身份子类
student.h
class student:public identity
{
public:
string id;//学号
student();
student(string id, string s_name, string s_password);
void order_menu();//学生子菜单
void apply_order();//申请预约
void show_order();//查看预约
void show_all_order();//查看所有预约
void cancel_order();//取消预约
};
student.cpp
student::student() {}
student::student(string id, string s_name, string s_password) {}
void student::order_menu(){}
void student::apply_order(){}
void student::show_order(){}
void student::show_all_order(){}
void student::cancel_order(){}
教师身份子类
teacher.h
class teacher:public identity
{
public:
string id;//工号
teacher();
teacher(string id, string t_name, string t_password);
void order_menu();//显示教师子菜单
void show_allt_order();//查看所有预约
void valid_order();//审核预约
};
teacher.cpp
teacher::teacher() {};
teacher::teacher(string id, string t_name, string t_password) {}
void teacher::order_menu(){}
void teacher::show_allt_order(){}
void teacher::valid_order(){}
管理员身份子类
manager.h
class manager :public identity
{
public:
manager();
manager( string m_name, string m_password);
void order_menu();//显示管理员菜单
void add_person();//添加账号
void show_person();//查看账号
void show_computer();//查看机房信息
void clean_file();//清空账号
};
manager.cpp
manager::manager() {};
manager::manager(string m_name,string m_password) {}
void manager::order_menu() {}
void manager::add_person() {}
void manager::show_person(){}
void manager::show_computer(){}
void manager::clean_file(){}
添加后台数据文件
定义全局文件:
global_file.h
#define ADMINFILE "admin.txt"
#define STUDENTFILE "student.txt"
#define TEACHERFILE "teacher.txt"
#define COMPUTERROOMFILRE "computer_room.txt"
#define ORDERFILE "order.txt"
登录模块
在menu.h文件中给menu类中添加成员函数:
void login_in(string file_name, int type);//登录模块
在menu.cpp文件中实现登录模块功能:
void menu::login_in(string file_name, int type)
{
ifstream ifs;
ifs.open(file_name, ios::in);
if (!ifs.is_open())
{
cout << "文件打开失败!" << endl;
ifs.close();
return;
}
cout << endl;
string id;
if (type == 1)
{
cout << "请输入学号:";
cin >> id;
}
else
{
if (type == 2)
{
cout << "请输入工号:";
cin >> id;
}
}
string name, password;
cout << "请输入姓名:";
cin >> name;
cout << "请输入密码:";
cin >> password;
string fid, fname, fpassword;
identity* person = NULL;//定义多态父类身份指针
if (type == 1)//学生登录验证
{
while (ifs >> fid && ifs >> fname && ifs >> fpassword)//c++编码方式为ANSI(gbk)
{
if (fid == id && fname == name && fpassword == password)
{
cout << "登陆成功!" << endl;
ifs.close();
system("pause");
system("cls");
person = new student(id, name, password);//父类指针指向子类对象
//进入学生子菜单
student_menu(person);
return;
}
}
}
else {
if (type == 2)//教师登录验证
{
while (ifs >> fid && ifs >> fname && ifs >> fpassword)
{
if (fid == id && fname == name && fpassword == password)
{
cout << "登陆成功!" << endl;
system("pause");
system("cls");
person = new teacher(id, name, password);
//进入教师子菜单
teacher_menu(person);
return;
}
}
}
else
if (type == 3)//管理员登录验证
{
while (ifs >> fname && ifs >> fpassword)
{
if (fname == name && fpassword == password)
{
cout << "登录成功!" << endl;
system("pause");
system("cls");
person = new manager(name, password);
//进入管理员子菜单
manager_menu(person);
return;
}
}
}
}
cout << "登录验证失败!" << endl;
system("pause");
system("cls");
return;
}
管理员模块
管理员菜单搭建
void manager::order_menu() {
cout << "欢迎管理员:"<<this->name<<"登录!" << endl;
cout << "\t\t -----------------------------------------------------" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 1.添加账号 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 2.查看账号 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 3.查看机房 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 4.清空预约 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 0.注销登录 |" << endl;
cout << "\t\t| |" << endl;
cout << " \t\t----------------------------------------------------- " << endl;
cout << "请选择您的操作:" ;
}
添加账号
输入账号信息并写入后台文件student.txt文件中
void manager::add_person() {
int type = 0;
string p_id, p_name, p_password;
ofstream ofs;
string p_filename;
while (1)
{
cout << endl << "1.学生" << endl;
cout << "2.教师" << endl << endl;
cout << "请输入添加的账户类型:";
cin >> type;
if (type == 1)
{
p_filename = STUDENTFILE;
cout << "请输入学号:";
break;
}
else if (type == 2)
{
p_filename = TEACHERFILE;
cout << "请输入工号:";
break;
}
else
{
cout << "输入错误!请重新输入!" << endl;
}
}
cin >> p_id;
cout << "请输入姓名:";
cin >> p_name;
cout << "请输入密码:";
cin >> p_password;
ofs.open(p_filename, ios::out | ios::app);//以追加方式写入账户账户信息
if (!ofs.is_open())
{
cout << "文件打开失败!" << endl;
}
else
{
ofs << p_id << " " << p_name << " " << p_password << endl;
}
ofs.close();
cout << "添加成功!" << endl;
system("pause");
system("cls");
return;
}
处理检测账号是否重复问题
获取账号信息
在manager.h文件中添加成员变量:
vector<student> vstu;//获取学生账户信息
vector<teacher> vtea;//获取教师账户信息
在manager.h文件中读取后台账号数据信息与检测账号是否重复的成员函数:
void init_vector();//初始化容器,从后台数据库中获取当前账号信息
bool check_repeat(string id,int type);//检测是否有重复账户
在manager.cpp文件中实现账号信息读取功能并将该函数写入构造函数中:
void manager::init_vector()
{
ifstream ifs;
string f_id, f_name, f_passord;
vstu.clear();//容器初始化
vtea.clear();
ifs.open(STUDENTFILE, ios::in);//读取学生信息
if (!ifs.is_open())
{
cout << "文件打开失败!"<<endl;
ifs.close();
}
else
{
while (ifs >> f_id && ifs >> f_name && ifs >> f_passord)
{
vstu.push_back(student(f_id, f_name, f_passord));
}
cout << "当前学生数量为:" << vstu.size() << endl;
ifs.close();
}
ifs.open(TEACHERFILE, ios::in);
if (!ifs.is_open())
{
cout << "文件打开失败!"<<endl;
ifs.close();
}
else
{
while (ifs >> f_id && ifs >> f_name && ifs >> f_passord)
{
//cout << f_id << " " << f_name << " " << f_passord << endl;
vtea.push_back(teacher(f_id, f_name, f_passord));
}
cout << "当前教师数量为:" << vtea.size() << endl;
ifs.close();
}
}
添加检测账号重复函数
在manager.cpp文件中实现账号信息读取功能账号(id)重复检测功能:
bool manager::check_repeat(string id, int type)
{
if (type == 1)
{
for (vector<student>::iterator it = vstu.begin(); it != vstu.end(); it++)
{
if (it->id == id)
{
return true;
}
}
}
else if (type == 2)
{
for (vector<teacher>::iterator it = vtea.begin(); it != vtea.end(); it++)
{
if (it->id == id)
{
return true;
}
}
}
return false;
}
在add_person()函数中添加错误提示信息变量:
string err;//错误提示信息
追加以下代码检测输入id是否有重复:
while (1)
{
cin >> p_id;
if (this->check_repeat(p_id, type))
{
cout << err << endl;
}
else
break;
}
添加账号功能完善
void manager::add_person() {
int type = 0;
string p_id, p_name, p_password;
ofstream ofs;
string p_filename;
string err;//错误提示信息
while (1)
{
cout << endl << "1.学生" << endl;
cout << "2.教师" << endl << endl;
cout << "请输入添加的账户类型:";
cin >> type;
if (type == 1)
{
p_filename = STUDENTFILE;
err = "学号已存在!请重新输入!";
cout << "请输入学号:";
break;
}
else if (type == 2)
{
p_filename = TEACHERFILE;
err = "工号已经存在!请重新输入!";
cout << "请输入工号:";
break;
}
else
{
cout << "输入错误!请重新输入!" << endl;
}
}
while (1)
{
cin >> p_id;
if (this->check_repeat(p_id, type))
{
cout << err << endl;
}
else
break;
}
cout << "请输入姓名:";
cin >> p_name;
cout << "请输入密码:";
cin >> p_password;
ofs.open(p_filename, ios::out | ios::app);//以追加方式写入账户账户信息
if (!ofs.is_open())
{
cout << "文件打开失败!" << endl;
}
else
{
ofs << p_id << " " << p_name << " " << p_password << endl;
}
ofs.close();
cout << "添加成功!" << endl;
this->init_vector();//每次添加成功后就更新获取后台数据的容器中的数据,以保证检测账户重复机制的完整性
system("pause");
system("cls");
return;
}
查看账号
void manager::show_person()
{
int select = 0;
cout << endl << "1.学生" << endl;
cout << "2.教师" << endl << endl;
cout << "请输入查看的账户类型:";
while (1)
{
cin >> select;
if (select == 1)
{
for (vector<student>::iterator it = vstu.begin(); it != vstu.end(); it++)
{
cout << "工号:" << (*it).id << "\t"
<< "姓名:" <<(*it).name << "\t"
<< "密码" <<(*it).password<< endl;
}
system("pause");
system("cls");
break;
}
else if (select == 2)
{
for (vector<teacher>::iterator t_it = vtea.begin(); t_it != vtea.end(); t_it++)
{
cout<<"工号:" << (*t_it).id << "\t"
<<"姓名:"<< (*t_it).name << "\t"
<<"密码"<< (*t_it).name << endl;
}
system("pause");
system("cls");
break;
}
else
{
cout << "输入错误!请重新输入!" << endl;
}
}
}
查看机房信息
添加机房信息文件
computer_file.h
class computer
{
public:
string c_id;
int capcity;
vector<computer> vc;//获取机房信息
computer();
computer(string id, int capcity);
};
实现加载机房信息功能
computer_file.cpp
computer::computer()
{
ifstream ifs;
ifs.open(COMPUTERROOMFILRE, ios::in);
while (!ifs.is_open())
{
cout << "文件打开失败!" << endl;
ifs.close();
}
string c_id;
int capcity;
while (ifs >> c_id && ifs >> capcity)
{
vc.push_back(computer(c_id, capcity));
}
}
computer::computer(string id, int capcity)
{
this->c_id = id;
this->capcity = capcity;
}
查看机房信息功能实现
manager.cpp
void manager::show_computer()
{
computer com;
for (vector<computer>::iterator it = com.vc.begin(); it!= com.vc.end(); it++)
{
cout << "机房号:" << it->c_id << "\t机房数量:" << it->capcity << endl;
}
system("pause");
system("cls");
return;
}
清空账号
void manager::clean_file()
{
ofstream ofs;
ofs.open(ORDERFILE, ios::trunc);
ofs.close();
cout << "清空成功!" << endl;
system("pause");
system("cls");
}
学生模块
学生子菜单搭建
void student::order_menu()
{
cout << "欢迎:" << this->name << "登录!" << endl;
cout << "\t\t -----------------------------------------------------" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 1.申请预约 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 2.查看预约 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 3.查看所有预约 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 4.取消预约 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 0.注销登录 |" << endl;
cout << "\t\t| |" << endl;
cout << " \t\t----------------------------------------------------- " << endl;
cout << "请选择您的操作:";
}
申请预约
void student::apply_order()
{
cout << endl;
cout << "1.周一" << endl;
cout << "2.周二" << endl;
cout << "3.周三" << endl;
cout << "4.周四" << endl;
cout << "5.周五" << endl;
cout << "请选择申请日期:";
int date=0;
while (1)
{
cin >> date;
if (date < 1 || date>5)
cout << "输入错误!请重新输入!" << endl;
else
break;
}
cout << endl;
cout << "1.上午" << endl;
cout << "2.下午" << endl;
cout << "请选择时间段:";
int interval=0;
while (1)
{
cin >> interval;
if (interval>=1 && interval<=2)
break;
else
cout << "输入错误!请重新输入!" << endl;
}
computer c;//加载机房信息
cout << endl;
cout <<"1号机房:"<< c.vc[0].capcity << endl;
cout <<"2号机房:"<< c.vc[1].capcity << endl;
cout <<"3号机房:"<< c.vc[2].capcity << endl;
cout << "请选择机房号:";
int num = 0;
while (1)
{
cin >> num;
if (num < 1 || num>3)
cout << "输入错误!请重新输入!" << endl;
else
break;
}
ofstream ofs;
ofs.open(ORDERFILE, ios::out | ios::app);
ofs << "date:" << date << " ";
ofs << "interval:" << interval << " ";
ofs << "number:" << num << " ";
ofs << "stu_id:" << this->id << " ";
ofs << "stu_name:" << this->name << " ";
ofs << "status:" << 1 << endl;//审核中
ofs.close();
cout << "预约成功!审核中!" << endl;
system("pause");
system("cls");
return;
}
查看自身预约
order_file.h
class order_file
{
public:
map<int, map<string, string>> m;//存放所有订单记录
int size;//订单记录数
void sub_order(map<string, string>& message,string order_message);//获取每条订单记录信息并将其存放在this->message中
order_file();
};
order_file.cpp
获取订单信息
void order_file::sub_order(map<string, string>& message,string order_message)
{
int pos = order_message.find(":");
if (pos != -1)
{
string key = order_message.substr(0, pos);//截取关键字
string value = order_message.substr(pos + 1, order_message.size() - pos-1);//截取值
message.insert(make_pair(key, value));
}
}
order_file::order_file()
{
this->size = 0;
string date;//预约日期
string interval;//预约时间段
string com_number;//预约机房号
string stu_id;//预约学生学号
string stu_name;//预约学生姓名
string status;//预约状态
ifstream ifs;
ifs.open(ORDERFILE, ios::in);
while (ifs >> date && ifs >> interval && ifs >> com_number
&& ifs >> stu_id && ifs >> stu_name && ifs >> status)
{
map<string, string> message;
sub_order(message,date);//截取日期放入小容器中
sub_order(message,interval);//截取时间段放入小容器中
sub_order(message,com_number);//截取机房号放入小容器中
sub_order(message,stu_id);//截取学号放入小容器中
sub_order(message,stu_name);//截取学生姓名放入小容器中
sub_order(message,status);//截取日期审核状态小容器中
this->m.insert(make_pair(this->size++, message));
}
ifs.close();
}
查看预约功能实现(不显示取消订单记录)
void student::show_order()
{
order_file order;
if (order.size == 0)
{
cout << "无预约记录!" << endl;
system("pause");
system("cls");
return;
}
for (int i = 0; i < order.size; i++)
{
if (order.m[i]["stu_id"] == this->id && order.m[i]["status"]!="0")
{
cout << "日期:周" << order.m[i]["date"] << "\t";
cout << "时间段:" << (order.m[i]["interval"]=="1"?"上午":"下午")<< "\t";
cout << "机房号:" << order.m[i]["number"] << "号" << "\t";
string status="预约状态:";
if (order.m[i]["status"] == "1")
status += "审核中";
else if (order.m[i]["status"] == "2")
status += "预约成功!";
else if (order.m[i]["status"] == "-1")
status += "预约失败!";
cout << status << endl;
}
}
system("pause");
system("cls");
return;
}
查看所有预约(不显示取消订单记录)
void student::show_all_order()
{
order_file order;
if (order.size == 0)
{
cout << "无预约记录!" << endl;
system("pause");
system("cls");
return;
}
for (int i = 0; i < order.size; i++)
{
if (order.m[i]["status"] != "0")
{
cout << "学号:" << order.m[i]["stu_id"] << "\t";
cout << "姓名:" << order.m[i]["stu_name"] << "\t";
cout << "日期:周" << order.m[i]["date"] << "\t";
cout << "时间段:" << (order.m[i]["interval"] == "1" ? "上午" : "下午") << "\t";
cout << "机房号:" << order.m[i]["number"] << "号" << "\t";
string status = "预约状态:";
if (order.m[i]["status"] == "1")
status += "审核中";
else if (order.m[i]["status"] == "2")
status += "预约成功!";
else if (order.m[i]["status"] == "-1")
status += "预约失败!";
cout << status << endl;
}
}
system("pause");
system("cls");
return;
}
取消预约
添加更新订单记录功能
在order_file.h文件中给order_file类添加更新订单成员函数
void update_order();//更新预约记录
在order_file.cpp文件中实现更新订单记录功能:
void order_file::update_order()
{
if (this->size == 0)
return;
ofstream ofs;
ofs.open(ORDERFILE, ios::out | ios::trunc);
for (int i = 0; i < this->size; i++)
{
ofs << "date:" << this->m[i]["date"] << " ";
ofs << "interval:" << this->m[i]["interval"] << " ";
ofs << "number:" << this->m[i]["number"] << " ";
ofs << "stu_id:" << this->m[i]["stu_id"] << " ";
ofs << "stu_name:" << this->m[i]["stu_name"] << " ";
ofs << "status:" << this->m[i]["status"] << endl;
}
}
取消预约功能实现
void student::cancel_order()
{
order_file order;//加载订单信息
vector<int> v_id;//存放要取消的订单记录号
if (order.size == 0)
{
cout << "无预约记录!" << endl;
system("pause");
system("cls");
return;
}
int index = 1;
for (int i = 0; i < order.size; i++)
{
if (order.m[i]["stu_id"] == this->id)
{
if (order.m[i]["status"] == "1" || order.m[i]["status"] == "2")
{
v_id.push_back(i);
cout << "(" << index++ << ") ";
cout << "学号:" << order.m[i]["stu_id"] << "\t";
cout << "姓名:" << order.m[i]["stu_name"] << "\t";
cout << "预约时间:周" << order.m[i]["date"] << "\t";
cout << "预约时段:" << (order.m[i]["interval"] == "1" ? "上午" : "下午") << "\t";
cout << "机房号:" << order.m[i]["number"] << "\t";
string status = "预约状态:";
cout << (order.m[i]["status"] == "1" ? (status += "审核中!") : (status += "预约成功!")) << endl;
}
}
}
int select=0;
while (1)
{
cout << "请选择取消预约记录条数,0代表返回:";
cin >> select;
if (select >= 0 && select <= order.size)
{
if (select == 0)
{
system("cls");
return;
}
order.m[v_id[select - 1]]["status"] = "0";
order.update_order();
cout << "取消预约成功!" << endl;
system("pause");
system("cls");
return;
}
else
{
cout << "输入有误!请重新输入!" << endl;
}
}
}
教师模块
显示教师子菜单
void teacher::order_menu()
{
cout << "欢迎教师:" << this->name << "登录!" << endl;
cout << "\t\t -----------------------------------------------------" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 1.查看所有预约 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 2.审核预约 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 0.注销登录 |" << endl;
cout << "\t\t| |" << endl;
cout << " \t\t----------------------------------------------------- " << endl;
cout << "请选择您的操作:";
}
查看所有预约
void teacher::show_allt_order()
{
order_file order;
if (order.size == 0)
{
cout << "无预约记录!" << endl;
system("pause");
system("cls");
return;
}
for (int i = 0; i < order.size; i++)
{
cout << "(" << i + 1 << ") ";
cout << "学号:" << order.m[i]["stu_id"] << "\t";
cout << "姓名:" << order.m[i]["stu_name"] << "\t";
cout << "日期:周" << order.m[i]["date"] << "\t";
cout << "时间段:" << (order.m[i]["interval"] == "1" ? "上午" : "下午") << "\t";
cout << "机房号:" << order.m[i]["number"] << "号" << "\t";
string status = "预约状态:";
if (order.m[i]["status"] == "1")
status += "审核中";
else if (order.m[i]["status"] == "2")
status += "预约成功!";
else if (order.m[i]["status"] == "-1")
status += "预约失败!";
else if (order.m[i]["status"] == "0")
status += "预约已取消!";
cout << status << endl;
}
system("pause");
system("cls");
return;
}
审核预约
void teacher::valid_order()
{
order_file order;
int index = 1;
vector<int> v_id;
if (order.size == 0)
{
cout << "无预约记录!" << endl;
system("pause");
system("cls");
return;
}
for (int i = 0; i < order.size; i++)
{
if (order.m[i]["status"] == "1")
{
v_id.push_back(i);
cout << "(" << index++ << ") ";
cout << "学号:" << order.m[i]["stu_id"] << "\t";
cout << "姓名:" << order.m[i]["stu_name"] << "\t";
cout << "时间:周" << order.m[i]["date"] << "\t";
cout << "时间段:" << ((order.m[i]["interval"] == "1") ? ("上午") : ("下午")) << "\t";
cout << "机房号:" << order.m[i]["number"] << "\t";
if (order.m[i]["status"] == "1")
{
cout << "预约状态:审核中!" << endl;
}
}
}
cout << endl;
int select = 0;
string res;
cout << "请选择审核记录,0代表返回:";
cin >> select;
while (1)
{
if (select >= 0 && select <= order.size)
{
if (select == 0)
{
system("cls");
return;
}
while (1)
{
cout << "\n1.通过\n2.不通过" << endl;
cout << "请选择审核结果:";
cin >> res;
if (res == "1")
{
order.m[v_id[select - 1]]["status"] = "2";
break;
}
else if (res == "2")
{
order.m[v_id[select - 1]]["status"] = "-1";
break;
}
else
{
cout << "输入错误!请重新输入!" << endl;
}
}
order.update_order();
cout << "审核成功!" << endl;
system("pause");
system("cls");
return;
}
else
{
cout << "输入错误!请重新输入!" << endl;
}
}
}
功能完善
添加管理员撤销账号功能
在manager.h文件中给manager类添加成员函数:
void erase_person();//注销账号
void update_person(int type);//更新账号信息
在manager.cpp文件中实现更新账号功能:
void manager::update_person(int type)
{
ofstream ofs;
if (type == 1)
{
ofs.open(STUDENTFILE, ios::out | ios::trunc);
for (vector<student>::iterator it=vstu.begin();it!=vstu.end();it++)
{
ofs << it->id << " " << it->name << " " << it->password << endl;
}
}
else if (type == 2)
{
ofs.open(TEACHERFILE, ios::out | ios::trunc);
for (vector<teacher>::iterator it = vtea.begin(); it != vtea.end(); it++)
{
ofs << it->id << " " << it->name << " " << it->password << endl;
}
}
ofs.close();
}
在manager.cpp文件中实现注销账号功能:
void manager::erase_person()
{
cout << "\n1.学生\n2.教师" << endl;
cout << "请输入注销账号类型:";
int type;
string era_sid;
while (1)
{
cin >> type;
if (type == 1)
{
if (vstu.size() == 0)
{
cout << "学生账户为空!" << endl;
system("pause");
system("cls");
return;
}
for (vector<student>::iterator it = vstu.begin(); it != vstu.end(); it++)
{
cout<< " 学号:" << it->id << "\t姓名:" << it->name << endl;
}
cout << "请输入注销学号:";
cin >> era_sid;
for (vector<student>::iterator it = vstu.begin(); it != vstu.end(); )
{
if (it->id == era_sid)
{
vstu.erase(it);
it = vstu.begin();//erase每次删除一个元素后vector迭代器就会失效,故需重置迭代器
}
else
it++;
}
break;
}
else if(type == 2)
{
if (vtea.size() == 0)
{
cout << "教师账户为空!" << endl;
system("pause");
system("cls");
return;
}
for (vector<teacher>::iterator it = vtea.begin(); it != vtea.end(); it++)
{
cout << " 工号:" << it->id << "\t姓名:" << it->name << endl;
}
cout << "请输入注销工号:";
cin >> era_sid;
for (vector<teacher>::iterator it = vtea.begin(); it != vtea.end();)
{
if (it->id == era_sid)
{
vtea.erase(it);
it = vtea.begin();//erase每次删除一个元素后vector迭代器就会失效,故需重置迭代器
}
else
it++;
}
break;
}
else
{
cout << "输入错误!请重新输入!" << endl;
system("pause");
system("cls");
return;
}
}
this->update_person(type);//更新后台数据库
//更新订单信息
order_file order;
if (type == 1)
{
for (map<int, map<string, string>>::iterator it = order.m.begin(); it != order.m.end();)
{
if (it->second["stu_id"] == era_sid)
{
order.m.erase(it);
it = order.m.begin();
order.size--;
}
else
it++;
}
}
order.update_order();
cout << "注销成功!" << endl;
system("pause");
system("cls");
return;
}
添加学生申请预约检测功能
在student.cpp文件中给apply_order()函数中,在学生输入预约信息后追加以下代码:
order_file order;//加载订单信息
for (int i = 0; i < order.size; i++)
{
if (order.m[i]["stu_id"] == this->id && order.m[i]["stu_name"] == this->name
&&order.m[i]["date"] ==to_string(date)
&& order.m[i]["interval"] == to_string(interval)
&& order.m[i]["number"] == to_string(num))
{
if (order.m[i]["status"] == "1")
{
cout << "您已预约该时段,教师正在审核中,请耐心等待!" << endl;
system("pause");
system("cls");
return;
}
else if (order.m[i]["status"] == "2")
{
cout << "您已成功预约该时段,不能重复预约!" << endl;
system("pause");
system("cls");
return;
}
}
}
完整代码
global_file.h
#pragma once
#include<iostream>
using namespace std;
#define ADMINFILE "admin.txt"//"D:\VSwork\VS_work\机房预约系统(再版)\机房预约系统(再版)\admin.txt"
#define STUDENTFILE "student.txt"//"D:\VSwork\VS_work\机房预约系统(再版)\机房预约系统(再版)\student.txt"
#define TEACHERFILE "teacher.txt"//"D:\VSwork\VS_work\机房预约系统(再版)\机房预约系统(再版)\teacher.txt"
#define COMPUTERROOMFILRE "computer_room.txt"//"D:\VSwork\VS_work\机房预约系统(再版)\机房预约系统(再版)\computer_room.txt"
#define ORDERFILE "order.txt"//"D:\VSwork\VS_work\机房预约系统(再版)\机房预约系统(再版)\order.txt"
computer_file.h
#pragma once
#include<iostream>
#include<string>
#include<vector>
#include"global_file.h"
#include<fstream>
using namespace std;
class computer
{
public:
string c_id;
int capcity;
vector<computer> vc;//获取机房信息
computer();
computer(string id, int capcity);
};
computer_file.cpp
#include"computer_room.h"
computer::computer()
{
ifstream ifs;
ifs.open(COMPUTERROOMFILRE, ios::in);
while (!ifs.is_open())
{
cout << "文件打开失败!" << endl;
ifs.close();
}
string c_id;
int capcity;
while (ifs >> c_id && ifs >> capcity)
{
vc.push_back(computer(c_id, capcity));
}
}
computer::computer(string id, int capcity)
{
this->c_id = id;
this->capcity = capcity;
}
order_file.h
#pragma once
#include<iostream>
#include<map>
#include<string>
#include<fstream>
#include"global_file.h"
using namespace std;
class order_file
{
public:
map<int, map<string, string>> m;//存放所有订单记录
int size;//订单记录数
void sub_order(map<string, string>& message,string order_message);//获取每条订单记录信息并将其存放在this->message中
order_file();
void update_order();//更新预约记录
};
order_file.cpp
#include"order_file.h"
void order_file::sub_order(map<string, string>& message,string order_message)
{
int pos = order_message.find(":");
if (pos != -1)
{
string key = order_message.substr(0, pos);//截取关键字
string value = order_message.substr(pos + 1, order_message.size() - pos-1);//截取值
message.insert(make_pair(key, value));
}
}
order_file::order_file()
{
this->size = 0;
string date;//预约日期
string interval;//预约时间段
string com_number;//预约机房号
string stu_id;//预约学生学号
string stu_name;//预约学生姓名
string status;//预约状态
ifstream ifs;
ifs.open(ORDERFILE, ios::in);
while (ifs >> date && ifs >> interval && ifs >> com_number
&& ifs >> stu_id && ifs >> stu_name && ifs >> status)
{
map<string, string> message;
sub_order(message,date);//截取日期放入小容器中
sub_order(message,interval);//截取时间段放入小容器中
sub_order(message,com_number);//截取机房号放入小容器中
sub_order(message,stu_id);//截取学号放入小容器中
sub_order(message,stu_name);//截取学生姓名放入小容器中
sub_order(message,status);//截取日期审核状态小容器中
this->m.insert(make_pair(this->size++, message));
}
ifs.close();
}
void order_file::update_order()
{
if (this->size == 0)
return;
ofstream ofs;
ofs.open(ORDERFILE, ios::out | ios::trunc);
for (int i = 0; i < this->size; i++)
{
ofs << "date:" << this->m[i]["date"] << " ";
ofs << "interval:" << this->m[i]["interval"] << " ";
ofs << "number:" << this->m[i]["number"] << " ";
ofs << "stu_id:" << this->m[i]["stu_id"] << " ";
ofs << "stu_name:" << this->m[i]["stu_name"] << " ";
ofs << "status:" << this->m[i]["status"] << endl;
}
}
identity.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
class identity
{
public:
string name;
string password;
virtual void order_menu() = 0;
};
manager.h
#pragma once
#include"identity.h"
#include<fstream>
#include<vector>
#include"student.h"
#include"teacher.h"
#include"global_file.h"
#include"computer_room.h"
#include"order_file.h"
using namespace std;
class manager :public identity
{
public:
vector<student> vstu;//获取学生账户信息
vector<teacher> vtea;//获取教师账户信息
manager();
manager( string m_name, string m_password);
void order_menu();//显示管理员菜单
void add_person();//添加账号
void show_person();//查看账号
void erase_person();//注销账号
void show_computer();//查看机房信息
void clean_file();//清空账号
void update_person(int type);
void init_vector();//初始化容器,从后台数据库中获取当前信息
bool check_repeat(string id,int type);//检测是否有重复账户
};
manager.cpp
#pragma once
#include"manager.h"
#include<algorithm>
using namespace std;
class computer;
manager::manager() {};
manager::manager(string m_name,string m_password) {
this->name = m_name;
this->password = m_password;
init_vector();//初始化容器,获取当前数据库信息
computer com;
cout << "当前机房数量为:" << com.vc.size() << endl;
}
void manager::order_menu() {
cout << "欢迎管理员:"<<this->name<<"登录!" << endl;
cout << "\t\t -----------------------------------------------------" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 1.添加账号 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 2.查看账号 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 3.注销账号 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 4.查看机房 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 5.清空预约 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 0.注销登录 |" << endl;
cout << "\t\t| |" << endl;
cout << " \t\t----------------------------------------------------- " << endl;
cout << "请选择您的操作:" ;
}
void manager::init_vector()
{
ifstream ifs;
string f_id, f_name, f_passord;
vstu.clear();//容器初始化
vtea.clear();
ifs.open(STUDENTFILE, ios::in);//读取学生信息
if (!ifs.is_open())
{
cout << "文件打开失败!"<<endl;
ifs.close();
}
else
{
while (ifs >> f_id && ifs >> f_name && ifs >> f_passord)
{
vstu.push_back(student(f_id, f_name, f_passord));
}
cout << "当前学生数量为:" << vstu.size() << endl;
ifs.close();
}
ifs.open(TEACHERFILE, ios::in);
if (!ifs.is_open())
{
cout << "文件打开失败!"<<endl;
ifs.close();
}
else
{
while (ifs >> f_id && ifs >> f_name && ifs >> f_passord)
{
//cout << f_id << " " << f_name << " " << f_passord << endl;
vtea.push_back(teacher(f_id, f_name, f_passord));
}
cout << "当前教师数量为:" << vtea.size() << endl;
ifs.close();
}
}
bool manager::check_repeat(string id, int type)
{
if (type == 1)
{
for (vector<student>::iterator it = vstu.begin(); it != vstu.end(); it++)
{
if (it->id == id)
{
return true;
}
}
}
else if (type == 2)
{
for (vector<teacher>::iterator it = vtea.begin(); it != vtea.end(); it++)
{
if (it->id == id)
{
return true;
}
}
}
return false;
}
void manager::add_person() {
int type = 0;
string p_id, p_name, p_password;
ofstream ofs;
string p_filename;
string err;//错误提示信息
while (1)
{
cout << endl << "1.学生" << endl;
cout << "2.教师" << endl << endl;
cout << "请输入添加的账户类型:";
cin >> type;
if (type == 1)
{
p_filename = STUDENTFILE;
err = "学号已存在!请重新输入!";
cout << "请输入学号:";
break;
}
else if (type == 2)
{
p_filename = TEACHERFILE;
err = "工号已经存在!请重新输入!";
cout << "请输入工号:";
break;
}
else
{
cout << "输入错误!请重新输入!" << endl;
}
}
while (1)
{
cin >> p_id;
if (this->check_repeat(p_id, type))
{
cout << err << endl;
}
else
break;
}
cout << "请输入姓名:";
cin >> p_name;
cout << "请输入密码:";
cin >> p_password;
ofs.open(p_filename, ios::out | ios::app);//以追加方式写入账户账户信息
if (!ofs.is_open())
{
cout << "文件打开失败!" << endl;
}
else
{
ofs << p_id << " " << p_name << " " << p_password << endl;
}
ofs.close();
cout << "添加成功!" << endl;
this->init_vector();//每次添加成功后就更新获取后台数据的容器中的数据,以保证检测账户重复机制的完整性
system("pause");
system("cls");
return;
}
void manager::show_person()
{
int select = 0;
cout << endl << "1.学生" << endl;
cout << "2.教师" << endl << endl;
cout << "请输入查看的账户类型:";
while (1)
{
cin >> select;
if (select == 1)
{
if (vstu.size() == 0)
{
cout << "学生账户为空!" << endl;
}
for (vector<student>::iterator it = vstu.begin(); it != vstu.end(); it++)
{
cout << "工号:" << (*it).id << "\t"
<< "姓名:" <<(*it).name << "\t"
<< "密码" <<(*it).password<< endl;
}
system("pause");
system("cls");
break;
}
else if (select == 2)
{
if (vtea.size() == 0)
{
cout << "教师账户为空!" << endl;
}
for (vector<teacher>::iterator t_it = vtea.begin(); t_it != vtea.end(); t_it++)
{
cout<<"工号:" << (*t_it).id << "\t"
<<"姓名:"<< (*t_it).name << "\t"
<<"密码"<< (*t_it).name << endl;
}
system("pause");
system("cls");
break;
}
else
{
cout << "输入错误!请重新输入!" << endl;
}
}
}
void manager::update_person(int type)
{
ofstream ofs;
if (type == 1)
{
ofs.open(STUDENTFILE, ios::out | ios::trunc);
for (vector<student>::iterator it=vstu.begin();it!=vstu.end();it++)
{
ofs << it->id << " " << it->name << " " << it->password << endl;
}
}
else if (type == 2)
{
ofs.open(TEACHERFILE, ios::out | ios::trunc);
for (vector<teacher>::iterator it = vtea.begin(); it != vtea.end(); it++)
{
ofs << it->id << " " << it->name << " " << it->password << endl;
}
}
ofs.close();
}
void manager::erase_person()
{
cout << "\n1.学生\n2.教师" << endl;
cout << "请输入注销账号类型:";
int type;
string era_sid;
while (1)
{
cin >> type;
if (type == 1)
{
if (vstu.size() == 0)
{
cout << "学生账户为空!" << endl;
system("pause");
system("cls");
return;
}
for (vector<student>::iterator it = vstu.begin(); it != vstu.end(); it++)
{
cout<< " 学号:" << it->id << "\t姓名:" << it->name << endl;
}
cout << "请输入注销学号:";
cin >> era_sid;
for (vector<student>::iterator it = vstu.begin(); it != vstu.end(); )
{
if (it->id == era_sid)
{
it=vstu.erase(it);
//it = vstu.begin();//erase每次删除一个元素后vector迭代器就会失效,故需重置迭代器
}
else
it++;
}
break;
}
else if(type == 2)
{
if (vtea.size() == 0)
{
cout << "教师账户为空!" << endl;
system("pause");
system("cls");
return;
}
for (vector<teacher>::iterator it = vtea.begin(); it != vtea.end(); it++)
{
cout << " 工号:" << it->id << "\t姓名:" << it->name << endl;
}
cout << "请输入注销工号:";
cin >> era_sid;
for (vector<teacher>::iterator it = vtea.begin(); it != vtea.end(); it++)
{
if (it->id == era_sid)
{
vtea.erase(it);
it = vtea.begin();//erase每次删除一个元素后vector迭代器就会失效,故需重置迭代器
}
else
it++;
}
break;
}
else
{
cout << "输入错误!请重新输入!" << endl;
system("pause");
system("cls");
return;
}
}
this->update_person(type);//更新后台数据库
//更新订单信息
order_file order;
if (type == 1)
{
for (map<int, map<string, string>>::iterator it = order.m.begin(); it != order.m.end();)
{
if (it->second["stu_id"] == era_sid)
{
order.m.erase(it);
it = order.m.begin();
order.size--;
}
else
it++;
}
}
order.update_order();
cout << "注销成功!" << endl;
system("pause");
system("cls");
return;
}
void manager::show_computer()
{
computer com;
for (vector<computer>::iterator it = com.vc.begin(); it!= com.vc.end(); it++)
{
cout << "机房号:" << it->c_id << "\t机房数量:" << it->capcity << endl;
}
system("pause");
system("cls");
return;
}
void manager::clean_file()
{
ofstream ofs;
ofs.open(ORDERFILE, ios::trunc);
ofs.close();
cout << "清空成功!" << endl;
system("pause");
system("cls");
}
student.h
#pragma once
#include"identity.h"
#include"computer_room.h"
#include<vector>
#include<fstream>
#include"global_file.h"
#include"order_file.h"
using namespace std;
class student:public identity
{
public:
string id;
student();
student(string id, string s_name, string s_password);
void order_menu();//学生子菜单
void apply_order();//申请预约
void show_order();//查看预约
void show_all_order();//查看所有预约
void cancel_order();//取消预约
};
student.cpp
#pragma once
#include<iostream>
#include"student.h"
using namespace std;
student::student() {}
student::student(string id, string s_name, string s_password) {
this->id = id;
this->name = s_name;
this->password = s_password;
}
void student::order_menu()
{
cout << "欢迎:" << this->name << "登录!" << endl;
cout << "\t\t -----------------------------------------------------" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 1.申请预约 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 2.查看预约 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 3.查看所有预约 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 4.取消预约 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 0.注销登录 |" << endl;
cout << "\t\t| |" << endl;
cout << " \t\t----------------------------------------------------- " << endl;
cout << "请选择您的操作:";
}
void student::apply_order()
{
cout << endl;
cout << "1.周一" << endl;
cout << "2.周二" << endl;
cout << "3.周三" << endl;
cout << "4.周四" << endl;
cout << "5.周五" << endl;
cout << "请选择申请日期:";
int date=0;
while (1)
{
cin >> date;
if (date < 1 || date>5)
cout << "输入错误!请重新输入!" << endl;
else
break;
}
cout << endl;
cout << "1.上午" << endl;
cout << "2.下午" << endl;
cout << "请选择时间段:";
int interval=0;
while (1)
{
cin >> interval;
if (interval>=1 && interval<=2)
break;
else
cout << "输入错误!请重新输入!" << endl;
}
computer c;//加载机房信息
cout << endl;
cout <<"1号机房:"<< c.vc[0].capcity << endl;
cout <<"2号机房:"<< c.vc[1].capcity << endl;
cout <<"3号机房:"<< c.vc[2].capcity << endl;
cout << "请选择机房号:";
int num = 0;
while (1)
{
cin >> num;
if (num < 1 || num>3)
cout << "输入错误!请重新输入!" << endl;
else
break;
}
order_file order;//加载订单信息
for (int i = 0; i < order.size; i++)
{
if (order.m[i]["stu_id"] == this->id && order.m[i]["stu_name"] == this->name
&&order.m[i]["date"] ==to_string(date)
&& order.m[i]["interval"] == to_string(interval)
&& order.m[i]["number"] == to_string(num))
{
if (order.m[i]["status"] == "1")
{
cout << "您已预约该时段,教师正在审核中,请耐心等待!" << endl;
system("pause");
system("cls");
return;
}
else if (order.m[i]["status"] == "2")
{
cout << "您已成功预约该时段,不能重复预约!" << endl;
system("pause");
system("cls");
return;
}
}
}
ofstream ofs;
ofs.open(ORDERFILE, ios::out | ios::app);
ofs << "date:" << date << " ";
ofs << "interval:" << interval << " ";
ofs << "number:" << num << " ";
ofs << "stu_id:" << this->id << " ";
ofs << "stu_name:" << this->name << " ";
ofs << "status:" << 1 << endl;//审核中
ofs.close();
cout << "预约成功!审核中!" << endl;
system("pause");
system("cls");
return;
}
void student::show_order()
{
order_file order;
if (order.size == 0)
{
cout << "无预约记录!" << endl;
system("pause");
system("cls");
return;
}
for (int i = 0; i < order.size; i++)
{
if (order.m[i]["stu_id"] == this->id && order.m[i]["status"]!="0")
{
cout << "日期:周" << order.m[i]["date"] << "\t";
cout << "时间段:" << (order.m[i]["interval"]=="1"?"上午":"下午")<< "\t";
cout << "机房号:" << order.m[i]["number"] << "号" << "\t";
string status="预约状态:";
if (order.m[i]["status"] == "1")
status += "审核中";
else if (order.m[i]["status"] == "2")
status += "预约成功!";
else if (order.m[i]["status"] == "-1")
status += "预约失败!";
cout << status << endl;
}
}
system("pause");
system("cls");
return;
}
void student::show_all_order()
{
order_file order;
if (order.size == 0)
{
cout << "无预约记录!" << endl;
system("pause");
system("cls");
return;
}
for (int i = 0; i < order.size; i++)
{
if (order.m[i]["status"] != "0")
{
cout << "学号:" << order.m[i]["stu_id"] << "\t";
cout << "姓名:" << order.m[i]["stu_name"] << "\t";
cout << "日期:周" << order.m[i]["date"] << "\t";
cout << "时间段:" << (order.m[i]["interval"] == "1" ? "上午" : "下午") << "\t";
cout << "机房号:" << order.m[i]["number"] << "号" << "\t";
string status = "预约状态:";
if (order.m[i]["status"] == "1")
status += "审核中";
else if (order.m[i]["status"] == "2")
status += "预约成功!";
else if (order.m[i]["status"] == "-1")
status += "预约失败!";
cout << status << endl;
}
}
system("pause");
system("cls");
return;
}
void student::cancel_order()
{
order_file order;//加载订单信息
vector<int> v_id;//存放要取消的订单记录号
if (order.size == 0)
{
cout << "无预约记录!" << endl;
system("pause");
system("cls");
return;
}
int index = 1;
for (int i = 0; i < order.size; i++)
{
if (order.m[i]["stu_id"] == this->id)
{
if (order.m[i]["status"] == "1" || order.m[i]["status"] == "2")
{
v_id.push_back(i);
cout << "(" << index++ << ") ";
cout << "学号:" << order.m[i]["stu_id"] << "\t";
cout << "姓名:" << order.m[i]["stu_name"] << "\t";
cout << "预约时间:周" << order.m[i]["date"] << "\t";
cout << "预约时段:" << (order.m[i]["interval"] == "1" ? "上午" : "下午") << "\t";
cout << "机房号:" << order.m[i]["number"] << "\t";
string status = "预约状态:";
cout << (order.m[i]["status"] == "1" ? (status += "审核中!") : (status += "预约成功!")) << endl;
}
}
}
int select=0;
while (1)
{
cout << "请选择取消预约记录条数,0代表返回:";
cin >> select;
if (select >= 0 && select <= order.size)
{
if (select == 0)
{
system("cls");
return;
}
order.m[v_id[select - 1]]["status"] = "0";
order.update_order();
cout << "取消预约成功!" << endl;
system("pause");
system("cls");
return;
}
else
{
cout << "输入有误!请重新输入!" << endl;
}
}
}
teacher.h
#pragma once
#include<vector>
#include"identity.h"
#include"order_file.h"
#include"student.h"
using namespace std;
class teacher:public identity
{
public:
string id;
teacher();
teacher(string id, string t_name, string t_password);
void order_menu();//显示教师子菜单
void show_allt_order();//查看所有预约
void valid_order();//审核预约
};
teacher.cpp
#pragma once
#include"teacher.h"
using namespace std;
teacher::teacher() {};
teacher::teacher(string id, string t_name, string t_password) {
this->id = id;
this->name = t_name;
this->password = t_password;
}
void teacher::order_menu()
{
cout << "欢迎教师:" << this->name << "登录!" << endl;
cout << "\t\t -----------------------------------------------------" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 1.查看所有预约 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 2.审核预约 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 0.注销登录 |" << endl;
cout << "\t\t| |" << endl;
cout << " \t\t----------------------------------------------------- " << endl;
cout << "请选择您的操作:";
}
void teacher::show_allt_order()
{
order_file order;
if (order.size == 0)
{
cout << "无预约记录!" << endl;
system("pause");
system("cls");
return;
}
for (int i = 0; i < order.size; i++)
{
cout << "(" << i + 1 << ") ";
cout << "学号:" << order.m[i]["stu_id"] << "\t";
cout << "姓名:" << order.m[i]["stu_name"] << "\t";
cout << "日期:周" << order.m[i]["date"] << "\t";
cout << "时间段:" << (order.m[i]["interval"] == "1" ? "上午" : "下午") << "\t";
cout << "机房号:" << order.m[i]["number"] << "号" << "\t";
string status = "预约状态:";
if (order.m[i]["status"] == "1")
status += "审核中";
else if (order.m[i]["status"] == "2")
status += "预约成功!";
else if (order.m[i]["status"] == "-1")
status += "预约失败!";
else if (order.m[i]["status"] == "0")
status += "预约已取消!";
cout << status << endl;
}
system("pause");
system("cls");
return;
}
void teacher::valid_order()
{
order_file order;
int index = 1;
vector<int> v_id;
if (order.size == 0)
{
cout << "无预约记录!" << endl;
system("pause");
system("cls");
return;
}
for (int i = 0; i < order.size; i++)
{
if (order.m[i]["status"] == "1")
{
v_id.push_back(i);
cout << "(" << index++ << ") ";
cout << "学号:" << order.m[i]["stu_id"] << "\t";
cout << "姓名:" << order.m[i]["stu_name"] << "\t";
cout << "时间:周" << order.m[i]["date"] << "\t";
cout << "时间段:" << ((order.m[i]["interval"] == "1") ? ("上午") : ("下午")) << "\t";
cout << "机房号:" << order.m[i]["number"] << "\t";
if (order.m[i]["status"] == "1")
{
cout << "预约状态:审核中!" << endl;
}
}
}
cout << endl;
int select = 0;
string res;
cout << "请选择审核记录,0代表返回:";
cin >> select;
while (1)
{
if (select >= 0 && select <= order.size)
{
if (select == 0)
{
system("cls");
return;
}
while (1)
{
cout << "\n1.通过\n2.不通过" << endl;
cout << "请选择审核结果:";
cin >> res;
if (res == "1")
{
order.m[v_id[select - 1]]["status"] = "2";
break;
}
else if (res == "2")
{
order.m[v_id[select - 1]]["status"] = "-1";
break;
}
else
{
cout << "输入错误!请重新输入!" << endl;
}
}
order.update_order();
cout << "审核成功!" << endl;
system("pause");
system("cls");
return;
}
else
{
cout << "输入错误!请重新输入!" << endl;
}
}
}
menu.h
#include<string>
#include<fstream>
#include"global_file.h"
#include"identity.h"
#include"student.h"
#include"teacher.h"
#include"manager.h"
using namespace std;
class menu
{
public:
void main_menu();//主菜单
void manager_menu(identity* Manager);//管理员子菜单
void student_menu(identity* Student);//学生子菜单
void teacher_menu(identity* Teacher);//教师子菜单
void login_in(string file_name, int type);//登录模块
};
menu.cpp
#pragma once
#include"menu.h"
void menu::main_menu()//主菜单
{
cout << "\t\t==============欢迎来到机房预约系统====================" << endl;
cout << "\t\t -----------------------------------------------------" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 1.学生代表 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 2.教师代表 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 3. 管理员 |" << endl;
cout << "\t\t| |" << endl;
cout << "\t\t| 0. 退出 |" << endl;
cout << "\t\t| |" << endl;
cout << " \t\t----------------------------------------------------- " << endl;
cout << endl << "请输入您的身份:";
}
void menu::manager_menu(identity* Manager)
{
while (1)
{
Manager->order_menu();//父类指针只能调用公共接口
//将父类指针强制转为子类指针,以调用子类接口
manager* man = (manager*)Manager;
int select = 0;
cin >> select;
if (select == 1)
{
//cout << "添加账号" << endl;
man->add_person();
}
else if (select == 2)
{
//cout << "查看账号" << endl;
man->show_person();
}
else if (select == 3)
{
//cout << "注销账号" << endl;
man->erase_person();
}
else if (select == 4)
{
//cout << "查看机房信息" << endl;
man->show_computer();
}
else if (select == 5)
{
//cout << "清空预约" << endl;
man->clean_file();
}
else if (select == 0)
{
delete Manager;
cout << "注销成功!" << endl;
system("pause");
system("cls");
return;
}
else {
cout << "输入错误!请重新输入!" << endl;
system("pause");
system("cls");
}
}
}
void menu::student_menu(identity* Student)
{
while (1)
{
Student->order_menu();
student* stu = (student*)Student;
int select = 0;
cin >> select;
if (select == 1)
{
stu->apply_order();//申请预约
}
else if (select == 2)
{
stu->show_order();//查看预约
}
else if (select == 3)
{
stu->show_all_order();//查看所有预约
}
else if (select == 4)
{
stu->cancel_order();//取消预约
}
else if (select == 0)//退出登录
{
delete Student;
cout << "注销成功!" << endl;
system("pause");
system("cls");
return;
}
else {
cout << "输入错误!请重新输入!" << endl;
system("pause");
system("cls");
}
}
}
void menu::teacher_menu(identity* Teacher)
{
teacher* tea = (teacher*)Teacher;
int select = 0;
while (1)
{
tea->order_menu();
cin >> select;
if (select == 1)
{
tea->show_allt_order();//显示所有预约
}
else if (select == 2)
{
tea->valid_order();//审核预约
}
else if (select == 0)
{
delete Teacher;
cout << "注销登录成功!" << endl;
system("pause");
system("cls");
return;
}
else
{
cout << "输入错误!请重新输入!" << endl;
system("pause");
system("cls");
}
}
}
void menu::login_in(string file_name, int type)
{
ifstream ifs;
ifs.open(file_name, ios::in);
if (!ifs.is_open())
{
cout << "文件打开失败!" << endl;
ifs.close();
return;
}
cout << endl;
string id;
if (type == 1)
{
cout << "请输入学号:";
cin >> id;
}
else
{
if (type == 2)
{
cout << "请输入工号:";
cin >> id;
}
}
string name, password;
cout << "请输入姓名:";
cin >> name;
cout << "请输入密码:";
cin >> password;
string fid, fname, fpassword;
identity* person = NULL;
if (type == 1)//学生登录验证
{
while (ifs >> fid && ifs >> fname && ifs >> fpassword)//c++编码方式为ANSI(gbk)
{
//cout << fid << " " << fname << " " << fpassword << endl;
if (fid == id && fname == name && fpassword == password)
{
cout << "登陆成功!" << endl;
ifs.close();
system("pause");
system("cls");
person = new student(id, name, password);
//进入学生子菜单
student_menu(person);
return;
}
}
}
else {
if (type == 2)//教师登录验证
{
while (ifs >> fid && ifs >> fname && ifs >> fpassword)
{
if (fid == id && fname == name && fpassword == password)
{
cout << "登陆成功!" << endl;
system("pause");
system("cls");
person = new teacher(id, name, password);
//进入教师子菜单
teacher_menu(person);
return;
}
}
}
else
if (type == 3)//管理员登录验证
{
while (ifs >> fname && ifs >> fpassword)
{
if (fname == name && fpassword == password)
{
cout << "登录成功!" << endl;
system("pause");
system("cls");
person = new manager(name, password);
//进入管理员子菜单
manager_menu(person);
return;
}
}
}
}
cout << "登录验证失败!" << endl;
system("pause");
system("cls");
return;
}
机房预约系统.cpp
#include<iostream>
#include"menu.h"
using namespace std;
int main()
{
int select=0;
while (1)
{
menu Menu;
Menu.main_menu();
cin >> select;
switch (select) {
case 1://学生子菜单
Menu.login_in(STUDENTFILE, 1);
break;
case 2://教师子菜单
Menu.login_in(TEACHERFILE, 2);
break;
case 3://管理员子菜单
Menu.login_in(ADMINFILE, 3);
break;
case 0://退出系统
cout << "欢迎下次使用!" << endl;
return 0;
break;
default:
cout << "输入有误!请重新输入!" << endl;
system("pause");
system("cls");
}
}
return 1;
}