STL经典案例(四)——实验室预约综合管理系统(项目涉及知识点很全面,内 ...

打印 上一主题 下一主题

主题 1016|帖子 1016|积分 3048

项目干货满满,内容有点过多,看起来可能会有点卡。系统提示读完凌驾2小时,发起多篇发布,我以为分篇就不完整了,失去了这个项目标魂魄
一、需求分析

高校实验室预约管理系统包罗三种差别身份:管理员、实验室西席、学生
管理员:给学生和实验室西席创建账号并分发
实验室西席:稽核学生的预约申请
学生:申请利用实验室
高校实验室包罗:超景深实验室(可容纳10人)、大数据实验室(可容纳20人)、物联网实验室(可容纳30人)等,管理员可以添加实验室
学生可以预约将来一周内的实验室,必要选择周一至周五的上午、下午或晚上时间段
实验室西席对学生的预约申请进行稽核,稽核分为通过或不通过
管理员负责每周对学生申请的实验室预约清单进行清空
二、功能详情

1,主页面登录系统选择:学生、实验室西席、管理员、退出
2,学生:输入学号、姓名和登录暗码
实验室西席:输入职工号、姓名和登录暗码
管理员:输入姓名和登录暗码
3,学生功能
申请预约:预约实验室
查看自身的预约:查看自身申请的预约状态
查看全部预约:取消自身的预约,预约成功或稽核中的预约都可以随时取消
退出登录
4,实验室西席功能
查看全部学生预约环境:查看全部的实验室预约信息及预约状态
稽核学生的预约:对学生申请的实验室预约进行稽核
退出登录
5,管理员功能
创建账号:为学生或实验室西席创建账号,必要检测学生学号和西席职工号是否重复
查看账号:查看学生或实验室西席的全部信息
添加实验室:添加实验室的信息,包罗实验室的id、名称和最大容量
查看实验室:查看全部实验室的信息
清空预约:清空全部的预约记录
退出登录
三、项目创建

1,新建C++空项目


2,创建主菜单及退出功能的实现

创建项目入口,即主页面main.cpp

通过switch case分支进行差别身份的登录,
退出功能没啥好说的,用户选择0之后直接return 0;即可
  1. #include <iostream>
  2. int main() {
  3.         int select = 0;//存放用户的选择
  4.         while (true)
  5.         {
  6.                 std::cout << "======================  Welcome to the Laboratory Management System  =====================" << std::endl;
  7.                 std::cout << "\t\t -------------------------------\n";
  8.                 std::cout << "\t\t|          1.Student            |\n";
  9.                 std::cout << "\t\t|          2.Teacher            |\n";
  10.                 std::cout << "\t\t|          3.Admin              |\n";
  11.                 std::cout << "\t\t|          0.Exit               |\n";
  12.                 std::cout << "\t\t -------------------------------\n";
  13.                 std::cout << std::endl << "Please enter your identity: " << std::endl;
  14.                 std::cin >> select; //接受用户选择
  15.                 switch (select)
  16.                 {
  17.                 case 1:  //学生身份
  18.                         break;
  19.                 case 2:  //老师身份
  20.                         break;
  21.                 case 3:  //管理员身份
  22.                         break;
  23.                 case 0:  //退出系统
  24.                         std::cout << "Welcome to you next time!" << std::endl;
  25.                         system("pause");
  26.                         return 0;
  27.                         break;
  28.                 default:
  29.                         std::cout << "You have entered the wrong information, please make a new selection!" << std::endl;
  30.                         system("pause");
  31.                         system("cls");
  32.                         break;
  33.                 }
  34.         }
  35.         system("pause");
  36.         return 0;
  37. }
复制代码
3,身份类创建——搭建框架

无论Student、Teacher还是Admin,都有相通的共性(姓名和暗码),抽取出来整合成一个基类
继承这个基类,差别的子类根据自身的特性再新增属性功能即可
①身份类基类——Identify

经过功能详情的分析,三种差别身份的共性是:name和pwd
因为差别身份登录成功必要进入到差别的页面,再基类中通过纯虚函数定义一个接口,子类根据自身环境实现即可
基类只必要声明,不必要实现
身份类基类:Identify.h
  1. #pragma once
  2. #include <iostream>
  3. class Identify
  4. {
  5. public:
  6.         virtual void selectMenu() = 0;//纯虚函数,不同用户登录成功之后显示的菜单均不同
  7.        
  8.         std::string getName()
  9.         {
  10.                 return this->name_;
  11.         }
  12.         std::string getPwd()
  13.         {
  14.                 return this->pwd_;
  15.         }
  16.         void setName(std::string name)
  17.         {
  18.                 this->name_ = name;
  19.         }
  20.         void setPwd(std::string pwd)
  21.         {
  22.                 this->pwd_ = pwd;
  23.         }
  24. private:
  25.         std::string name_;//姓名
  26.         std::string pwd_;//密码
  27. };
复制代码
②Student类


学生具有的功能:申请预约、体现我的预约、查看全部预约和取消预约
Student.h
  1. #pragma once
  2. #include "Identify.h"
  3. class Student:public Identify
  4. {
  5. public:
  6.         Student();
  7.         Student(int id,std::string name,std::string pwd);//学号、姓名、密码
  8.         virtual void Identify::selectMenu() override;//重写基类中的虚函数
  9.         void applyOrder();//申请预约功能
  10.         void showMyOrder();//显示我的预约功能
  11.         void showAllOrder();//查看所有预约功能
  12.         void cancelOrder();//取消预约功能
  13.         int getId()
  14.         {
  15.                 return this->student_id_;
  16.         }
  17.         void setId(int id)
  18.         {
  19.                 this->student_id_ = id;
  20.         }
  21. private:
  22.         int student_id_;
  23. };
复制代码
Student.cpp
  1. #include "Student.h"
  2. Student::Student() {}
  3. Student::Student(int id,std::string name,std::string pwd) {}
  4. void Student::selectMenu() {}//功能菜单实现
  5. void Student::applyOrder() {}//预约申请实现
  6. void Student::showMyOrder() {}//显示我的预约
  7. void Student::showAllOrder() {}//显示所有预约
  8. void Student::cancelOrder() {}//取消预约
复制代码
③Teacher类


西席具有的功能:体现全部预约和稽核预约
Teacher.h
  1. #pragma once
  2. #include "Identify.h"
  3. class Teacher:public Identify
  4. {
  5. public:
  6.         Teacher();
  7.         Teacher(int teacher_id,std::string name,std::string pwd);
  8.         virtual void Identify::selectMenu() override;//功能菜单实现
  9.         void showAllOrder();//显示所有预约
  10.         void validOrder();//审核预约
  11.         int getId()
  12.         {
  13.                 return this->teaher_id_;
  14.         }
  15.         void setId(int id)
  16.         {
  17.                 this->teaher_id_ = id;
  18.         }
  19. private:
  20.         int teaher_id_;//教职工编号
  21. };
复制代码
Teacher.cpp
  1. #include "Teacher.h"
  2. Teacher::Teacher() {}
  3. Teacher::Teacher(int teacher_id,std::string name,std::string pwd) {}
  4. void Teacher::selectMenu() {}//功能菜单实现
  5. void Teacher::showAllOrder() {}//显示所有预约
  6. void Teacher::validOrder() {}//审核预约
复制代码
④Admin类


管理员具有的功能:添加用户(包罗学生和教职工)、查看用户、看实验室信息和清空预约记录
Admin.h
  1. #pragma once
  2. #include <iostream>
  3. #include "Identify.h"
  4. class Admin:public Identify
  5. {
  6. public:
  7.         Admin();
  8.         Admin(std::string name,std::string pwd);
  9.         virtual void Identify::selectMenu() override;//功能菜单实现
  10.         void addUser();//添加用户
  11.         void showUser();//查看用户
  12.         void showLabInfo();//查看实验室信息
  13.         void cleanRecord();//清空预约记录
  14. };
复制代码
Admin.cpp
  1. #include "Admin.h"
  2. Admin::Admin() {}
  3. Admin::Admin(std::string name, std::string pwd) {}
  4. void Admin::selectMenu(){}//功能菜单实现
  5. void Admin::addUser() {}//添加用户功能实现
  6. void Admin::showUser() {}//查看用户功能实现
  7. void Admin::showLabInfo() {}//查看实验室信息功能实现
  8. void Admin::cleanRecord() {}//清空预约记录功能实现
复制代码
4,登录模块

①全局文件


将差别身份的账号暗码等信息存放到文件中,将全部的文件名都定义到全局文件中
通过宏定义进行关联文件,方便后续的操作
globalFile.h
  1. #pragma once
  2. //管理员文件
  3. #define ADMIN_FILE     "admin.txt"
  4. //学生文件
  5. #define STUDENT_FILE   "student.txt"
  6. //教师文件
  7. #define TEACHER_FILE   "teacher.txt"
  8. //实验室信息文件
  9. #define LABORATORY_FILE  "laboratoryRoom.txt"
  10. //订单文件
  11. #define ORDER_FILE     "order.txt"
复制代码
同级路径下创建对应的txt文本文件

②登录函数封装

在main.cpp中添加一个全局函数Login,根据用户的选择进行差别的身份登录操作
void Login(string fileName, int type)
参数一:操作的文件名称
参数二:身份(1为学生、2为实验室西席、3为管理员)
main.cpp
  1. #include <iostream>
  2. #include "Identify.h"
  3. #include <fstream>
  4. #include "globalFile.h"
  5. #include "Student.h"
  6. #include "Teacher.h"
  7. #include "Admin.h"
  8. //全局登录函数
  9. //参数一:操作的文件名称
  10. //参数二:身份(1为学生、2为实验室教师、3为管理员)
  11. void Login(std::string fileName,int type)
  12. {
  13.         //创建父类指针,将来通过多态指向子类对象
  14.         Identify* identify_p= NULL;
  15.         //读取文件
  16.         std::ifstream ifs;
  17.         ifs.open(fileName, std::ios::in);//以读的方式打开文件
  18.         //若文件不存在
  19.         if (!ifs.is_open()) //打开失败
  20.         {
  21.                 std::cout << "The file does not exist" << std::endl;
  22.                 ifs.close();
  23.                 return;
  24.         }
  25.         //存放用户将来输入的信息
  26.         int id = 0;//存储学号或职工号
  27.         std::string name;//学生或实验室教师的姓名
  28.         std::string pwd;//学生或实验室教师的密码
  29.         if (type == 1)
  30.         {
  31.                 std::cout << "Please input your student_id:" << std::endl;
  32.                 std::cin >> id;
  33.         }
  34.         else if (type == 2)
  35.         {
  36.                 std::cout << "Please input your teacher_id:" << std::endl;
  37.                 std::cin >> id;
  38.         }
  39.         std::cout << "Please input your name:" << std::endl;
  40.         std::cin >> name;
  41.         std::cout << "Please input your password:" << std::endl;
  42.         std::cin >> pwd;
  43.         if (type == 1)
  44.         {
  45.                 //student login
  46.                 //todo
  47.         }
  48.         else if (type == 2)
  49.         {
  50.                 //teacher login
  51.                 //todo
  52.         }
  53.         else if (type == 3)
  54.         {
  55.                 //admin login
  56.                 //todo
  57.         }
  58.        
  59.         std::cout << "Validation failed !" << std::endl;
  60.         system("pause");
  61.         system("cls");
  62.         return;
  63. }
  64. int main() {
  65.         int select = 0;//存放用户的选择
  66.         while (true)
  67.         {
  68.                 std::cout << "======================  Welcome to the Laboratory Management System  =====================" << std::endl;
  69.                 std::cout << "\t\t -------------------------------\n";
  70.                 std::cout << "\t\t|          1.Student            |\n";
  71.                 std::cout << "\t\t|          2.Teacher            |\n";
  72.                 std::cout << "\t\t|          3.Admin              |\n";
  73.                 std::cout << "\t\t|          0.Exit               |\n";
  74.                 std::cout << "\t\t -------------------------------\n";
  75.                 std::cout << std::endl << "Please enter your identity: " << std::endl;
  76.                 std::cin >> select; //接受用户选择
  77.                 switch (select)
  78.                 {
  79.                 case 1:  //学生身份
  80.                         Login(STUDENT_FILE,1);
  81.                         break;
  82.                 case 2:  //老师身份
  83.                         Login(TEACHER_FILE, 2);
  84.                         break;
  85.                 case 3:  //管理员身份
  86.                         Login(ADMIN_FILE, 3);
  87.                         break;
  88.                 case 0:  //退出系统
  89.                         std::cout << "Welcome to you next time!" << std::endl;
  90.                         system("pause");
  91.                         return 0;
  92.                         break;
  93.                 default:
  94.                         std::cout << "You have entered the wrong information, please make a new selection!" << std::endl;
  95.                         system("pause");
  96.                         system("cls");
  97.                         break;
  98.                 }
  99.         }
  100.         system("pause");
  101.         return 0;
  102. }
复制代码
③学生登录实现

在student.txt中添加几条记录信息进行测试

修改完善主函数main中的全局登录函数Login
main.cpp
  1. //全局登录函数
  2. //参数一:操作的文件名称
  3. //参数二:身份(1为学生、2为实验室教师、3为管理员)
  4. void Login(std::string fileName,int type)
  5. {
  6.         //创建父类指针,将来通过多态指向子类对象
  7.         Identify* identify_p = NULL;
  8.         //读取文件
  9.         std::ifstream ifs;
  10.         ifs.open(fileName, std::ios::in);//以读的方式打开文件
  11.         //若文件不存在
  12.         if (!ifs.is_open()) //打开失败
  13.         {
  14.                 std::cout << "The file does not exist" << std::endl;
  15.                 ifs.close();
  16.                 return;
  17.         }
  18.         //存放用户将来输入的信息
  19.         int id = 0;//存储学号或职工号
  20.         std::string name;//学生或实验室教师的姓名
  21.         std::string pwd;//学生或实验室教师的密码
  22.         if (type == 1)
  23.         {
  24.                 std::cout << "Please input your student_id:" << std::endl;
  25.                 std::cin >> id;
  26.         }
  27.         else if (type == 2)
  28.         {
  29.                 std::cout << "Please input your teacher_id:" << std::endl;
  30.                 std::cin >> id;
  31.         }
  32.         std::cout << "Please input your name:" << std::endl;
  33.         std::cin >> name;
  34.         std::cout << "Please input your password:" << std::endl;
  35.         std::cin >> pwd;
  36.         if (type == 1)
  37.         {
  38.                 //student login
  39.                 int f_id;//文件读取得到的id
  40.                 std::string f_name;//从文件中获取得到的name
  41.                 std::string f_pwd; //从文件中获取得到的pwd
  42.                 while (ifs >> f_id && ifs >> f_name && ifs >> f_pwd) //文件按行读取,遇到空格存一下,每行数据信息拆成三个
  43.                 {
  44.                         //文件读取得到的信息与用户输入的信息进行对比
  45.                         if (f_id == id && f_name == name && f_pwd == pwd) //学号、姓名、密码均输入成功
  46.                         {
  47.                                 std::cout << "Student verification login is successful!" << std::endl;
  48.                                 system("pause");
  49.                                 system("cls");
  50.                                 identify_p = new Student(id, name, pwd);//父类指针指向子类对象,创建学生实例
  51.                                
  52.                                 //进入学生身份的子菜单 todo
  53.                                 return;
  54.                         }
  55.                 }
  56.         }
  57.         else if (type == 2)
  58.         {
  59.                 //teacher login
  60.         }
  61.         else if (type == 3)
  62.         {
  63.                 //admin login
  64.         }
  65.        
  66.         std::cout << "Validation failed !" << std::endl;
  67.         system("pause");
  68.         system("cls");
  69.         return;
  70. }
复制代码
测试效果:


④实验室西席登录实现

在teacher.txt中添加几条记录信息进行测试

修改完善main.cpp中的全局登录函数Login
main.cpp
  1. //全局登录函数
  2. //参数一:操作的文件名称
  3. //参数二:身份(1为学生、2为实验室教师、3为管理员)
  4. void Login(std::string fileName,int type)
  5. {
  6.         //创建父类指针,将来通过多态指向子类对象
  7.         Identify* identify_p = NULL;
  8.         //读取文件
  9.         std::ifstream ifs;
  10.         ifs.open(fileName, std::ios::in);//以读的方式打开文件
  11.         //若文件不存在
  12.         if (!ifs.is_open()) //打开失败
  13.         {
  14.                 std::cout << "The file does not exist" << std::endl;
  15.                 ifs.close();
  16.                 return;
  17.         }
  18.         //存放用户将来输入的信息
  19.         int id = 0;//存储学号或职工号
  20.         std::string name;//学生或实验室教师的姓名
  21.         std::string pwd;//学生或实验室教师的密码
  22.         if (type == 1)
  23.         {
  24.                 std::cout << "Please input your student_id:" << std::endl;
  25.                 std::cin >> id;
  26.         }
  27.         else if (type == 2)
  28.         {
  29.                 std::cout << "Please input your teacher_id:" << std::endl;
  30.                 std::cin >> id;
  31.         }
  32.         std::cout << "Please input your name:" << std::endl;
  33.         std::cin >> name;
  34.         std::cout << "Please input your password:" << std::endl;
  35.         std::cin >> pwd;
  36.         if (type == 1)
  37.         {
  38.                 //student login
  39.                 int f_id;//文件读取得到的id
  40.                 std::string f_name;//从文件中获取得到的name
  41.                 std::string f_pwd; //从文件中获取得到的pwd
  42.                 while (ifs >> f_id && ifs >> f_name && ifs >> f_pwd) //文件按行读取,遇到空格存一下,每行数据信息拆成三个
  43.                 {
  44.                         //文件读取得到的信息与用户输入的信息进行对比
  45.                         if (f_id == id && f_name == name && f_pwd == pwd) //学号、姓名、密码均输入成功
  46.                         {
  47.                                 std::cout << "Student verification login is successful!" << std::endl;
  48.                                 system("pause");
  49.                                 system("cls");
  50.                                 identify_p = new Student(id, name, pwd);//父类指针指向子类对象,创建学生实例
  51.                                
  52.                                 //进入学生身份的子菜单 todo
  53.                                 return;
  54.                         }
  55.                 }
  56.         }
  57.         else if (type == 2)
  58.         {
  59.                 //teacher login
  60.                 int f_id;//文件读取得到的id
  61.                 std::string f_name;//从文件中获取得到的name
  62.                 std::string f_pwd; //从文件中获取得到的pwd
  63.                 while (ifs >> f_id && ifs >> f_name && ifs >> f_pwd) //文件按行读取,遇到空格存一下,每行数据信息拆成三个
  64.                 {
  65.                         //文件读取得到的信息与用户输入的信息进行对比
  66.                         if (f_id == id && f_name == name && f_pwd == pwd)//教职工编号、姓名、密码输入成功
  67.                         {
  68.                                 std::cout << "Teacher verification login is successful!" << std::endl;
  69.                                 system("pause");
  70.                                 system("cls");
  71.                                 identify_p = new Teacher(id, name, pwd);//父类指针指向子类对象,创建教职工实例
  72.                                 //进入实验室教师身份的子菜单 todo
  73.                                 return;
  74.                         }
  75.                 }
  76.         }
  77.         else if (type == 3)
  78.         {
  79.                 //admin login
  80.         }
  81.        
  82.         std::cout << "Validation failed !" << std::endl;
  83.         system("pause");
  84.         system("cls");
  85.         return;
  86. }
复制代码
测试效果:


⑤管理员登录实现

在admin.txt中添加几条记录信息进行测试

修改完善main.cpp中的全局登录函数Login
main.cpp
  1. //全局登录函数
  2. //参数一:操作的文件名称
  3. //参数二:身份(1为学生、2为实验室教师、3为管理员)
  4. void Login(std::string fileName,int type)
  5. {
  6.         //创建父类指针,将来通过多态指向子类对象
  7.         Identify* identify_p = NULL;
  8.         //读取文件
  9.         std::ifstream ifs;
  10.         ifs.open(fileName, std::ios::in);//以读的方式打开文件
  11.         //若文件不存在
  12.         if (!ifs.is_open()) //打开失败
  13.         {
  14.                 std::cout << "The file does not exist" << std::endl;
  15.                 ifs.close();
  16.                 return;
  17.         }
  18.         //存放用户将来输入的信息
  19.         int id = 0;//存储学号或职工号
  20.         std::string name;//学生或实验室教师的姓名
  21.         std::string pwd;//学生或实验室教师的密码
  22.         if (type == 1)
  23.         {
  24.                 std::cout << "Please input your student_id:" << std::endl;
  25.                 std::cin >> id;
  26.         }
  27.         else if (type == 2)
  28.         {
  29.                 std::cout << "Please input your teacher_id:" << std::endl;
  30.                 std::cin >> id;
  31.         }
  32.         std::cout << "Please input your name:" << std::endl;
  33.         std::cin >> name;
  34.         std::cout << "Please input your password:" << std::endl;
  35.         std::cin >> pwd;
  36.         if (type == 1)
  37.         {
  38.                 //student login
  39.                 int f_id;//文件读取得到的id
  40.                 std::string f_name;//从文件中获取得到的name
  41.                 std::string f_pwd; //从文件中获取得到的pwd
  42.                 while (ifs >> f_id && ifs >> f_name && ifs >> f_pwd) //文件按行读取,遇到空格存一下,每行数据信息拆成三个
  43.                 {
  44.                         //文件读取得到的信息与用户输入的信息进行对比
  45.                         if (f_id == id && f_name == name && f_pwd == pwd) //学号、姓名、密码均输入成功
  46.                         {
  47.                                 std::cout << "Student verification login is successful!" << std::endl;
  48.                                 system("pause");
  49.                                 system("cls");
  50.                                 identify_p = new Student(id, name, pwd);//父类指针指向子类对象,创建学生实例
  51.                                
  52.                                 //进入学生身份的子菜单 todo
  53.                                 return;
  54.                         }
  55.                 }
  56.         }
  57.         else if (type == 2)
  58.         {
  59.                 //teacher login
  60.                 int f_id;//文件读取得到的id
  61.                 std::string f_name;//从文件中获取得到的name
  62.                 std::string f_pwd; //从文件中获取得到的pwd
  63.                 while (ifs >> f_id && ifs >> f_name && ifs >> f_pwd) //文件按行读取,遇到空格存一下,每行数据信息拆成三个
  64.                 {
  65.                         //文件读取得到的信息与用户输入的信息进行对比
  66.                         if (f_id == id && f_name == name && f_pwd == pwd)//教职工编号、姓名、密码输入成功
  67.                         {
  68.                                 std::cout << "Teacher verification login is successful!" << std::endl;
  69.                                 system("pause");
  70.                                 system("cls");
  71.                                 identify_p = new Teacher(id, name, pwd);//父类指针指向子类对象,创建教职工实例
  72.                                 //进入实验室教师身份的子菜单 todo
  73.                                 return;
  74.                         }
  75.                 }
  76.         }
  77.         else if (type == 3)
  78.         {
  79.                 //admin login
  80.                 std::string f_name;//从文件中获取得到的name
  81.                 std::string f_pwd;//从文件中获取得到的pwd
  82.                 while (ifs >> f_name && ifs >> f_pwd) //文件按行读取,遇到空格存一下,每行数据信息拆成三个
  83.                 {
  84.                         //文件读取得到的信息与用户输入的信息进行对比
  85.                         if (f_name == name && f_pwd == pwd) //管理员登录只需要姓名和密码
  86.                         {
  87.                                 std::cout << "Admin verification login is successful!" << std::endl;
  88.                                 system("pause");
  89.                                 system("cls");
  90.                                 identify_p = new Admin(name, pwd);//父类指针指向子类对象,创建管理员实例
  91.                                 //进入管理员身份的子菜单 todo
  92.                                 return;
  93.                         }
  94.                 }
  95.         }
  96.        
  97.         std::cout << "Validation failed !" << std::endl;
  98.         system("pause");
  99.         system("cls");
  100.         return;
  101. }
复制代码
测试效果:


5,管理员模块

实验室西席和学生的账号信息是由管理员进行创建的,故起首得搞定管理员
管理员功能:登录和注销
起首,有参构造记录一下管理员信息、
其次,进入到管理员身份对应的子菜单页面中,管理员的功能包罗添加账号、查看账号、查看实验室、清空预约、注销登录
末了,在main.cpp中添加全局函数adminMenu()进行体现管理员菜单,根据用户选择的功能进行对应的函数实现
①登录和注销功能——AdminMenu()

Admin.h头文件定义的函数不必要改变
Admin.cpp实现头文件中所定义的一些函数,函数selectMenu()体现功能菜单的实现
Admin.cpp
  1. #include "Admin.h"
  2. Admin::Admin() {}
  3. Admin::Admin(std::string name, std::string pwd)
  4. {
  5.         //初始化管理员信息
  6.         Admin::setName(name);
  7.         Admin::setPwd(pwd);
  8. }
  9. void Admin::selectMenu()//功能菜单实现
  10. {
  11.         std::cout << "Welcome admin: " << this->getName() << " login!" << std::endl;
  12.         std::cout << "\t\t --------------------------------\n";
  13.         std::cout << "\t\t|          1.addUser             |\n";
  14.         std::cout << "\t\t|          2.showUser            |\n";
  15.         std::cout << "\t\t|          3.addLabInfo          |\n";
  16.         std::cout << "\t\t|          4.showLabInfo         |\n";
  17.         std::cout << "\t\t|          5.cleanRecord         |\n";
  18.         std::cout << "\t\t|          0 or OtherKey.logout  |\n";
  19.         std::cout << "\t\t --------------------------------\n";
  20.         std::cout << "Please select your action: " << std::endl;
  21. }
  22. void Admin::addUser() {}//添加用户功能实现
  23. void Admin::showUser() {}//查看用户功能实现
  24. void Admin::showLabInfo() {}//查看实验室信息功能实现
  25. void Admin::cleanRecord() {}//清空预约记录功能实现
复制代码
定义全局函数AdminMenu()用于体现管理员身份菜单界面
在Login()登录全局函数中进行调用即可

main.cpp
  1. //管理员身份菜单界面
  2. void AdminMenu(Identify*& identify_p) //传入父类的指针
  3. {
  4.         //进入菜单界面
  5.         while (true)
  6.         {
  7.                 //调用管理员子菜单
  8.                 identify_p->selectMenu();//这是父类指针,只能调用公共的接口纯虚函数
  9.                 //想要调用子类特有的接口,需要把父类指针转回去,将父类的指针转为子类的指针
  10.                 Admin* admin = (Admin*)identify_p;
  11.                 int userselect = 0;//用户的选择
  12.                 std::cin >> userselect;//接收用户的选择
  13.                 if (userselect == 1) //添加账号
  14.                 {
  15.                         std::cout << "Your select is add an account" << std::endl;
  16.                         admin->addUser();
  17.                 }
  18.                 else if (userselect == 2)//查看账号
  19.                 {
  20.                         std::cout << "Your select is view all account" << std::endl;
  21.                         admin->showUser();
  22.                 }
  23.                 else if (userselect == 3) //添加实验室信息
  24.                 {
  25.                         std::cout << "Your select is add lab information" << std::endl;
  26.                         admin->addLabInfo();
  27.                 }
  28.                 else if (userselect == 4) //显示实验室信息
  29.                 {
  30.                         std::cout << "Your select is show lab information" << std::endl;
  31.                         admin->showLabInfo();
  32.                 }
  33.                 else if (userselect == 5) //清空预约
  34.                 {
  35.                         std::cout << "Your select is clear all appointment" << std::endl;
  36.                         admin->cleanRecord();
  37.                 }
  38.                 else//选成其他选项,默认代表注销登录
  39.                 {
  40.                         delete admin;//销毁堆区创建的对象
  41.                         std::cout << "The logout is successful" << std::endl;
  42.                         system("pause");
  43.                         system("cls");
  44.                         return;
  45.                 }
  46.         }
  47. }
  48. //全局登录函数
  49. //参数一:操作的文件名称
  50. //参数二:身份(1为学生、2为实验室教师、3为管理员)
  51. void Login(std::string fileName,int type)
  52. {
  53.         //创建父类指针,将来通过多态指向子类对象
  54.         Identify* identify_p = NULL;
  55.         //读取文件
  56.         std::ifstream ifs;
  57.         ifs.open(fileName, std::ios::in);//以读的方式打开文件
  58.         //若文件不存在
  59.         if (!ifs.is_open()) //打开失败
  60.         {
  61.                 std::cout << "The file does not exist" << std::endl;
  62.                 ifs.close();
  63.                 return;
  64.         }
  65.         //存放用户将来输入的信息
  66.         int id = 0;//存储学号或职工号
  67.         std::string name;//学生或实验室教师的姓名
  68.         std::string pwd;//学生或实验室教师的密码
  69.         if (type == 1)
  70.         {
  71.                 std::cout << "Please input your student_id:" << std::endl;
  72.                 std::cin >> id;
  73.         }
  74.         else if (type == 2)
  75.         {
  76.                 std::cout << "Please input your teacher_id:" << std::endl;
  77.                 std::cin >> id;
  78.         }
  79.         std::cout << "Please input your name:" << std::endl;
  80.         std::cin >> name;
  81.         std::cout << "Please input your password:" << std::endl;
  82.         std::cin >> pwd;
  83.         if (type == 1)
  84.         {
  85.                 //student login
  86.                 int f_id;//文件读取得到的id
  87.                 std::string f_name;//从文件中获取得到的name
  88.                 std::string f_pwd; //从文件中获取得到的pwd
  89.                 while (ifs >> f_id && ifs >> f_name && ifs >> f_pwd) //文件按行读取,遇到空格存一下,每行数据信息拆成三个
  90.                 {
  91.                         //文件读取得到的信息与用户输入的信息进行对比
  92.                         if (f_id == id && f_name == name && f_pwd == pwd) //学号、姓名、密码均输入成功
  93.                         {
  94.                                 std::cout << "Student verification login is successful!" << std::endl;
  95.                                 system("pause");
  96.                                 system("cls");
  97.                                 identify_p = new Student(id, name, pwd);//父类指针指向子类对象,创建学生实例
  98.                                
  99.                                 //进入学生身份的子菜单 todo
  100.                                 return;
  101.                         }
  102.                 }
  103.         }
  104.         else if (type == 2)
  105.         {
  106.                 //teacher login
  107.                 int f_id;//文件读取得到的id
  108.                 std::string f_name;//从文件中获取得到的name
  109.                 std::string f_pwd; //从文件中获取得到的pwd
  110.                 while (ifs >> f_id && ifs >> f_name && ifs >> f_pwd) //文件按行读取,遇到空格存一下,每行数据信息拆成三个
  111.                 {
  112.                         //文件读取得到的信息与用户输入的信息进行对比
  113.                         if (f_id == id && f_name == name && f_pwd == pwd)//教职工编号、姓名、密码输入成功
  114.                         {
  115.                                 std::cout << "Teacher verification login is successful!" << std::endl;
  116.                                 system("pause");
  117.                                 system("cls");
  118.                                 identify_p = new Teacher(id, name, pwd);//父类指针指向子类对象,创建教职工实例
  119.                                 //进入实验室教师身份的子菜单 todo
  120.                                 return;
  121.                         }
  122.                 }
  123.         }
  124.         else if (type == 3)
  125.         {
  126.                 //admin login
  127.                 std::string f_name;//从文件中获取得到的name
  128.                 std::string f_pwd;//从文件中获取得到的pwd
  129.                 while (ifs >> f_name && ifs >> f_pwd) //文件按行读取,遇到空格存一下,每行数据信息拆成三个
  130.                 {
  131.                         //文件读取得到的信息与用户输入的信息进行对比
  132.                         if (f_name == name && f_pwd == pwd) //管理员登录只需要姓名和密码
  133.                         {
  134.                                 std::cout << "Admin verification login is successful!" << std::endl;
  135.                                 system("pause");
  136.                                 system("cls");
  137.                                 identify_p = new Admin(name, pwd);//父类指针指向子类对象,创建管理员实例
  138.                                 //进入管理员身份的子菜单
  139.                                 AdminMenu(identify_p);
  140.                                 return;
  141.                         }
  142.                 }
  143.         }
  144.        
  145.         std::cout << "Validation failed !" << std::endl;
  146.         system("pause");
  147.         system("cls");
  148.         return;
  149. }
复制代码
测试效果:

②添加用户功能——addUser()

为学生和实验室西席添加账号,要求添加账号的时候学号和职工号不能够重复
对接口addUser()进行实现
Admin.cpp
  1. void Admin::addUser()添加用户功能实现
  2. {
  3.         std::string fileType;//根据用户的选择进行操作不同的文件
  4.         std::string tip;//提示是学号还是职工号
  5.         std::string repeat_tip;//重复提示信息
  6.         std::ofstream ofs;//文件操作对象
  7.         int userselect = 0;//获取用户的选择
  8.        
  9.         while (true)
  10.         {
  11.                 //提示用户
  12.                 std::cout << "Please enter the type of account you want to add (1.student or 2.teacher):" << std::endl;
  13.                 std::cout << "1、student" << std::endl;
  14.                 std::cout << "2、teacher" << std::endl;
  15.                 std::cout << "0、exit" << std::endl;
  16.                 std::cin >> userselect;
  17.                 if (userselect == 1)//添加学生
  18.                 {
  19.                         fileType = STUDENT_FILE;
  20.                         tip = "please input studentID: ";
  21.                         repeat_tip = "studentID already exists, please input again: ";
  22.                         break;
  23.                 }
  24.                 else if (userselect == 2)//添加老师
  25.                 {
  26.                         fileType = TEACHER_FILE;
  27.                         tip = "please input teacherID: ";
  28.                         repeat_tip = "teacherID already exists, please input again: ";
  29.                         break;
  30.                 }
  31.                 else if(userselect == 0)
  32.                 {
  33.                         system("cls");//清屏
  34.                         return;
  35.                 }
  36.                 else
  37.                 {
  38.                         std::cout << "Invalid input, please try again." << std::endl;
  39.                 }
  40.         }
  41.                 ofs.open(fileType, std::ios::app);//以追加的方式写入文件
  42.                 if (!ofs.is_open())//文件打开失败
  43.                 {
  44.                         std::cout << "Failed to open file: " << fileType << std::endl;
  45.                         return;
  46.                 }
  47.                 int id;//学号或职工号
  48.                 std::string name;//姓名
  49.                 std::string pwd;//密码
  50.                 std::cout << tip << std::endl;//提示输入学号或职工号
  51.                 while (true)
  52.                 {
  53.                         std::cin >> id;
  54.                         bool repeat = this->checkRepeate(id, userselect);//检查是否有重复的用户
  55.                         if (repeat)//有重复的用户
  56.                         {
  57.                                 std::cout << repeat_tip << std::endl;
  58.                         }
  59.                         else//没有重复的用户
  60.                         {
  61.                                 break;
  62.                         }
  63.                 }
  64.                
  65.                 std::cout << "please input name: " << std::endl;
  66.                 std::cin >> name;//输入姓名
  67.                 std::cout << "please input password: " << std::endl;
  68.                 std::cin >> pwd;//输入密码
  69.                
  70.                
  71.                 //写入文件
  72.                 ofs << id << " " << name << " " << pwd << " " << std::endl;
  73.                 ofs.close();
  74.                 std::cout << "Add user successfully!" << std::endl;
  75.                 system("pause");//暂停程序
  76.                 system("cls");//清屏
  77. }
复制代码
测试效果:


运行完之后,teacher信息内里多了个账号

同样,student信息内里也会多一个账号

去重操作

添加新账号的时候,要制止学号\职工号的重复
解决思路:
起首,在Admin.h中,把学生和实验室西席账号文件信息全部读到各自的容器中,两个容器(std::vector v_Studentvector v_Teacher)分别存放实验室西席和学生全部账号信息
然后,在Admin.h中,添加一个成员函数void init_Stu_Tea_Vector()用于对学生和实验室西席信息的容器(v_Studentv_Teacher)进行初始化也就是将对应的文本文件数据对应读取到这两个容器中
再次,在Admin.cpp中,对成员函数void init_Stu_Tea_Vector()进行实现,并在构造函数中对两个容器初始化(this->initVector())
其次,在Admin.h中,添加成员函数bool checkRepeat(int id, int type); id表现职工号\学号、type表现类型,1为学生,2为实验室西席用于检查两个容器(v_Studentv_Teacher)中是否已存在管理员当前注册时输入的学号\职工号(id)信息,存在为true,不存在为false
末了,每次添加完新用户之后,必要重新初始化容器(即将新的账号文件全部重新读取到容器中),并在每次调用添加用户的函数(void Admin::addUser)的末了进行初始化(this->init_Stu_Tea_Vector())即可
Admin.h
  1.         void init_Stu_Tea_Vector();//初始化容器,用于将对应的文本文件信息读取到对应的容器中
  2.         std::vector<Student> v_Student;//学生容器
  3.         std::vector<Teacher> v_Teacher;//教师容器
  4.        
  5.         bool checkRepeate(int id, int type);//检查学号\职工号是否重复
复制代码
Admin.cpp
  1. #include "Admin.h"
  2. Admin::Admin() {}
  3. Admin::Admin(std::string name, std::string pwd)
  4. {
  5.         //初始化管理员信息
  6.         Admin::setName(name);
  7.         Admin::setPwd(pwd);
  8.         //初始化学生和老师的容器,获取文件中所有老师和学生的信息
  9.         this->init_Stu_Tea_Vector();
  10. }
  11. void Admin::selectMenu()//功能菜单实现
  12. {
  13.         std::cout << "Welcome admin: " << this->getName() << " login!" << std::endl;
  14.         std::cout << "\t\t --------------------------------\n";
  15.         std::cout << "\t\t|          1.addUser             |\n";
  16.         std::cout << "\t\t|          2.showUser            |\n";
  17.         std::cout << "\t\t|          3.showLabInfo         |\n";
  18.         std::cout << "\t\t|          4.cleanRecord         |\n";
  19.         std::cout << "\t\t|          0.logout              |\n";
  20.         std::cout << "\t\t --------------------------------\n";
  21.         std::cout << "Please select your action: " << std::endl;
  22. }
  23. void Admin::addUser()添加用户功能实现
  24. {
  25.         std::string fileType;//根据用户的选择进行操作不同的文件
  26.         std::string tip;//提示是学号还是职工号
  27.         std::string repeat_tip;//重复提示信息
  28.         std::ofstream ofs;//文件操作对象
  29.         int userselect = 0;//获取用户的选择
  30.        
  31.         while (true)
  32.         {
  33.                 //提示用户
  34.                 std::cout << "Please enter the type of account you want to add (1.student or 2.teacher):" << std::endl;
  35.                 std::cout << "1、student" << std::endl;
  36.                 std::cout << "2、teacher" << std::endl;
  37.                 std::cout << "0、exit" << std::endl;
  38.                 std::cin >> userselect;
  39.                 if (userselect == 1)//添加学生
  40.                 {
  41.                         fileType = STUDENT_FILE;
  42.                         tip = "please input studentID: ";
  43.                         repeat_tip = "studentID already exists, please input again: ";
  44.                         break;
  45.                 }
  46.                 else if (userselect == 2)//添加老师
  47.                 {
  48.                         fileType = TEACHER_FILE;
  49.                         tip = "please input teacherID: ";
  50.                         repeat_tip = "teacherID already exists, please input again: ";
  51.                         break;
  52.                 }
  53.                 else if(userselect == 0)
  54.                 {
  55.                         system("cls");//清屏
  56.                         return;
  57.                 }
  58.                 else
  59.                 {
  60.                         std::cout << "Invalid input, please try again." << std::endl;
  61.                 }
  62.         }
  63.                 ofs.open(fileType, std::ios::app);//以追加的方式写入文件
  64.                 if (!ofs.is_open())//文件打开失败
  65.                 {
  66.                         std::cout << "Failed to open file: " << fileType << std::endl;
  67.                         return;
  68.                 }
  69.                 int id;//学号或职工号
  70.                 std::string name;//姓名
  71.                 std::string pwd;//密码
  72.                 std::cout << tip << std::endl;//提示输入学号或职工号
  73.                 while (true)
  74.                 {
  75.                         std::cin >> id;
  76.                         bool repeat = this->checkRepeate(id, userselect);//检查是否有重复的用户
  77.                         if (repeat)//有重复的用户
  78.                         {
  79.                                 std::cout << repeat_tip << std::endl;
  80.                         }
  81.                         else//没有重复的用户
  82.                         {
  83.                                 break;
  84.                         }
  85.                 }
  86.                
  87.                 std::cout << "please input name: " << std::endl;
  88.                 std::cin >> name;//输入姓名
  89.                 std::cout << "please input password: " << std::endl;
  90.                 std::cin >> pwd;//输入密码
  91.                
  92.                
  93.                 //写入文件
  94.                 ofs << id << " " << name << " " << pwd << " " << std::endl;
  95.                 ofs.close();
  96.                 std::cout << "Add user successfully!" << std::endl;
  97.                 system("pause");//暂停程序
  98.                 system("cls");//清屏
  99.                 this->init_Stu_Tea_Vector();//刷新容器,重新读取文件中的用户信息
  100. }
  101. void Admin::init_Stu_Tea_Vector()//初始化学生和老师的容器,用于存放从文件中读取的所有用户信息
  102. {
  103.         //对容器进行初始化,清空用于存放对应用户信息的容器
  104.         this->v_Student.clear();
  105.         this->v_Teacher.clear();
  106.         std::ifstream ifs;//文件读取操作对象
  107.        
  108.         //读取学生信息
  109.         ifs.open(STUDENT_FILE, std::ios::in);//以读取的方式打开学生文件
  110.         if (!ifs.is_open())//文件打开失败
  111.         {
  112.                 std::cout << "Failed to open file: " << STUDENT_FILE << std::endl;
  113.                 return;
  114.         }
  115.         Student student;
  116.         int student_id;
  117.         std::string student_name;
  118.         std::string student_pwd;
  119.         while (ifs >> student_id && ifs >> student_name && ifs >> student_pwd)
  120.         {
  121.                 student.setId(student_id);
  122.                 student.setName(student_name);
  123.                 student.setPwd(student_pwd);
  124.                 v_Student.push_back(student);
  125.         }
  126.         std::cout << "Read student information successfully!" << std::endl;
  127.         std::cout<<"Student size: "<<v_Student.size()<<std::endl;
  128.         ifs.close();
  129.         //读取老师信息
  130.         ifs.open(TEACHER_FILE, std::ios::in);//以读取的方式打开老师文件
  131.         if (!ifs.is_open())//文件打开失败
  132.         {
  133.                 std::cout << "Failed to open file: " << TEACHER_FILE << std::endl;
  134.                 return;
  135.         }
  136.         Teacher teacher;
  137.         int teacher_id;
  138.         std::string teacher_name;
  139.         std::string teacher_pwd;
  140.         while (ifs >> teacher_id && ifs >> teacher_name && ifs >> teacher_pwd)
  141.         {
  142.                 teacher.setId(teacher_id);
  143.                 teacher.setName(teacher_name);
  144.                 teacher.setPwd(teacher_pwd);
  145.                 v_Teacher.push_back(teacher);
  146.         }
  147.         std::cout << "Read teacher information successfully!" << std::endl;
  148.         std::cout << "Teacher size: " << v_Teacher.size() << std::endl;
  149.         ifs.close();
  150. }
  151. bool Admin::checkRepeate(int id,int fileType)//检查是否有重复的用户
  152. {
  153.         if (fileType == 1)//学生
  154.         {
  155.                 for (int i = 0; i < v_Student.size(); i++)
  156.                 {
  157.                         if (v_Student[i].getId() == id)
  158.                         {
  159.                                 return true;
  160.                         }
  161.                 }
  162.         }
  163.         else if (fileType == 2)//老师
  164.         {
  165.                 for (int i = 0; i < v_Teacher.size(); i++)
  166.                 {
  167.                         if (v_Teacher[i].getId() == id)
  168.                         {
  169.                                 return true;
  170.                         }
  171.                 }
  172.         }
  173.         return false;
  174. }
  175. void Admin::showUser() {}//查看用户功能实现
  176. void Admin::showLabInfo() {}//查看实验室信息功能实现
  177. void Admin::cleanRecord() {}//清空预约记录功能实现
复制代码
③体现账号功能——showUser()

体现账号功能函数(showUser())的实现
接收用户的选择,根据用户的差别选择进行遍历添加用户功能中所定义的分别存放实验室西席和学生全部账号信息的两个容器(std::vector v_Studentvector v_Teacher),输出即可
admin.cpp
  1. void Admin::showUser() //查看用户功能实现
  2. {
  3.         while (true)
  4.         {
  5.                 std::cout << "Please enter the type of account you want to show (1.student or 2.teacher):" << std::endl;
  6.                 std::cout << "1、student" << std::endl;
  7.                 std::cout << "2、teacher" << std::endl;
  8.                 std::cout << "0、exit" << std::endl;
  9.                 int userselect = 0;
  10.                 std::cin >> userselect;
  11.                 if (userselect == 1)//查看所有学生的信息
  12.                 {
  13.                         std::cout << "Student information:" << std::endl;
  14.                         std::for_each(v_Student.begin(), v_Student.end(), printStudent);
  15.                         system("pause");//暂停程序
  16.                         system("cls");//清屏
  17.                         break;
  18.                 }
  19.                 else if (userselect == 2)//查看所有老师的信息
  20.                 {
  21.                         std::cout << "Teacher information:" << std::endl;
  22.                         std::for_each(v_Teacher.begin(), v_Teacher.end(), printTeacher);
  23.                         system("pause");//暂停程序
  24.                         system("cls");//清屏
  25.                         break;
  26.                 }
  27.                 else if (userselect == 0)//退出
  28.                 {
  29.                         system("cls");//清屏
  30.                         break;
  31.                 }
  32.                 else
  33.                 {
  34.                         std::cout << "Invalid input, please try again." << std::endl;
  35.                         system("pause");//暂停程序
  36.                         system("cls");//清屏
  37.                 }
  38.         }
  39. }
复制代码
④添加实验室信息功能——addLabInfo()

起首,在Admin.h中,把实验室信息全部读到容器(std::vector v_Lab)中
然后,在Admin.h中,添加一个成员函数void init_Lab_Vector()用于对实验室信息的容器(std::vector v_Lab)进行初始化,也就是将实验室文本文件数据对应读取到这个容器中
再次,在Admin.cpp中,对成员函数void init_Lab_Vector()进行实现,并在构造函数中对两个容器初始化(this->init_Lab_Vector())
其次,在Admin.h中,添加成员函数bool checkRepeat(int id, int type); id表现职工号\学号\实验室编号、type表现类型,1为学生,2为实验室西席,3为实验室用于检查容器(std::vector v_Lab)中是否已存在管理员当前注册时输入的实验室编号信息,存在为true,不存在为false
末了,每次添加完新实验室之后,必要重新初始化容器(即将新的实验室信息文件全部重新读取到容器中),并在每次调用添加实验室的函数(void Admin::addLabInfo)的末了进行初始化(this->init_Lab_Vector())即可
创建一个实验室类
Laboratory.h
  1. #pragma once
  2. #include <iostream>
  3. //实验室类
  4. class Laboratory
  5. {
  6. public:
  7.         Laboratory() {};
  8.         Laboratory(int id, std::string name, int capacity):lab_id_(id), lab_name_(name), lab_max_capacity_(capacity) {}
  9.         void set_lab_id(int id) { this->lab_id_ = id; }
  10.         void set_lab_name(std::string name) { this->lab_name_ = name; }
  11.         void set_lab_max_capacity(int capacity) { this->lab_max_capacity_ = capacity; }
  12.         int get_lab_id() const { return this->lab_id_; }
  13.         std::string get_lab_name() const { return this->lab_name_; }
  14.         int get_lab_max_capacity() const { return this->lab_max_capacity_; }
  15.                
  16. private:
  17.         int lab_id_; //实验室编号
  18.         std::string lab_name_; //实验室名称
  19.         int lab_max_capacity_; //实验室容量
  20. };
复制代码
Admin.h
  1.         void init_Lab_Vector();//初始化实验室容器,用于将实验室信息读取到容器中
  2.         std::vector<Laboratory> v_Lab;//实验室容器
复制代码
Admin.cpp
  1. void Admin::addLabInfo() //添加实验室信息功能实现
  2. {
  3.         int lab_id;
  4.         std::string lab_name;
  5.         int lab_max_capacity;
  6.         std::ofstream ofs;//文件操作对象
  7.         ofs.open(LABORATORY_FILE, std::ios::app);//以追加的方式写入文件
  8.         if (!ofs.is_open())//文件打开失败
  9.         {
  10.                 std::cout << "Failed to open file: " << LABORATORY_FILE << std::endl;
  11.                 return;
  12.         }
  13.         while (true)
  14.         {
  15.                 std::cout << "Please input laboratory ID: " << std::endl;
  16.                 std::cin >> lab_id;
  17.                 bool repeat = this->checkRepeate(lab_id, 3);//检查是否有重复的实验室
  18.                 if (repeat)
  19.                 {
  20.                         std::cout << "Laboratory ID already exists, please input again: " << std::endl;
  21.                 }
  22.                 else
  23.                 {
  24.                         break;
  25.                 }
  26.         }
  27.         std::cout << "Please input laboratory name: " << std::endl;
  28.         std::cin >> lab_name;
  29.         std::cout << "Please input laboratory max capacity: " << std::endl;
  30.         std::cin >> lab_max_capacity;
  31.         ofs << lab_id << " " << lab_name << " " << lab_max_capacity << " " << std::endl;
  32.         ofs.close();
  33.         std::cout << "Add laboratory information successfully!" << std::endl;
  34.         this->init_Lab_Vector();
  35.         system("pause");//暂停程序
  36.         system("cls");//清屏
  37. }
复制代码
⑤体实际验室信息功能——showLabInfo()

对实验室容器(std::vector<Laboratory> v_Lab)进行遍历输出一下即可
Admin.cpp
  1. void printLab(Laboratory& lab)
  2. {
  3.         std::cout << "Laboratory ID: " << lab.get_lab_id() << ", Name: " << lab.get_lab_name() << ", Max capacity: " << lab.get_lab_max_capacity() << std::endl;
  4. }
  5. void Admin::showLabInfo() //查看实验室信息功能实现
  6. {
  7.         std::cout << "Laboratory information:" << std::endl;
  8.         std::for_each(v_Lab.begin(), v_Lab.end(), printLab);
  9.         /* 和for_each的用法一样,二选一即可
  10.         for (std::vector<Laboratory>::iterator it = v_Lab.begin(); it != v_Lab.end(); it++)
  11.         {
  12.                 std::cout << "Laboratory ID: " << it->get_lab_id() << ", Name: " << it->get_lab_name() << ", Max capacity: " << it->get_lab_max_capacity() << std::endl;
  13.         }
  14.         */
  15.         system("pause");//暂停程序
  16.         system("cls");//清屏
  17. }
复制代码
⑥清空预约记录功能——cleanRecord()

trunc方式读取打开文件即可,trunc方式可以将文件清空重新追加
用户输入Y/y表现确认删除,输入N/n表现去掉操作
Admin.cpp
  1. void Admin::cleanRecord() //清空预约记录功能实现
  2. {
  3.         while (true)
  4.         {
  5.                 std::cout << "Are you sure to clean all the record? (Y/N)" << std::endl;
  6.                 std::ofstream ofs;
  7.                 char choice;
  8.                 std::cin >> choice;
  9.                 if (choice == 'Y' || choice == 'y')
  10.                 {
  11.                         ofs.open(ORDER_FILE, std::ios::trunc);
  12.                         if (!ofs.is_open())
  13.                         {
  14.                                 std::cout << "Failed to open file: " << ORDER_FILE << std::endl;
  15.                                 break;
  16.                         }
  17.                         ofs.close();
  18.                         std::cout << "Clean record successfully!" << std::endl;
  19.                         system("pause");//暂停程序
  20.                         system("cls");//清屏
  21.                         break;
  22.                 }
  23.                 else if (choice == 'N' || choice == 'n')
  24.                 {
  25.                         std::cout << "Cancel operation!" << std::endl;
  26.                         system("pause");//暂停程序
  27.                         system("cls");//清屏
  28.                         break;
  29.                 }
  30.                 else
  31.                 {
  32.                         std::cout << "Invalid input, please try again." << std::endl;
  33.                         system("pause");//暂停程序       
  34.                 }
  35.         }
  36. }
复制代码
6,学生模块

①登录和注销功能——studentMenu()

在Student类的有参构造函数中进行学生信息的初始化操作
Student.cpp
  1. Student::Student(int id,std::string name,std::string pwd)
  2. {
  3.         this->setId(id);
  4.         this->setName(name);
  5.         this->setPwd(pwd);
  6. }
复制代码
在main.cpp中定义全局函数studentMenu(),学生登录成功之后进入页面,进行相应的功能的选择
在main.cpp中的用户登录函数Login()中,用户登录成功进入到学生功能页面(调用StudentMenu()函数即可)
main.cpp
  1. //学生身份菜单界面
  2. void StudentMenu(Identify*& identify_p) //传入父类的指针
  3. {
  4.         //进入菜单界面
  5.         while(true)
  6.         {
  7.                 //调用管理员子菜单
  8.                 identify_p->selectMenu();//这是父类指针,只能调用公共的接口纯虚函数
  9.                 //想要调用子类特有的接口,需要把父类指针转回去,将父类的指针转为子类的指针
  10.                 Student* student = (Student*)identify_p;
  11.                 int userselect = 0;//用户的选择
  12.                 std::cin >> userselect;//接收用户的选择
  13.                 if (userselect == 1) //申请实验室预约
  14.                 {
  15.                         std::cout << "Your select is apply for lab appointment" << std::endl;
  16.                         student->applyOrder();
  17.                 }
  18.                 else if (userselect == 2) //查看自身预约信息
  19.                 {
  20.                         std::cout << "Your select is view your appointment information" << std::endl;
  21.                         student->showMyOrder();
  22.                 }
  23.                 else if (userselect == 3) //查看所有的实验室预约信息
  24.                 {
  25.                         std::cout << "Your select is view all appointment information" << std::endl;
  26.                         student->showAllOrder();
  27.                 }
  28.                 else if (userselect == 4) //取消实验室预约
  29.                 {
  30.                         std::cout << "Your select is cancel lab appointment" << std::endl;
  31.                         student->cancelOrder();
  32.                 }
  33.                 else //选成其他选项,默认代表注销登录
  34.                 {
  35.                         delete student;//销毁堆区创建的对象
  36.                         std::cout << "The logout is successful" << std::endl;
  37.                         system("pause");
  38.                         system("cls");
  39.                         return;
  40.                 }
  41.         }
  42. }
复制代码
定义全局函数StudentMenu()用于体现学生身份菜单界面
在Login()登录全局函数中进行调用即可

②申请实验室预约功能——applyOrder()

学生申请实验室预约必要的流程:选择日期(周一至周五)、选择时间段(上午、下午)、选择实验室、生成预约记录
在选择实验室的时候,必要让学生知道都有哪些实验室可以预约
在Student.h中添加实验室容器std::vector<Laboratory> v_Lab,并且在有参构造中读取实验室信息文件并放到实验室容器v_Lab中,就可以申请实验室预约了
Student.h
  1.         std::vector<Laboratory> v_Lab;//学生所申请的实验室
复制代码
Student.cpp
  1. #include "Student.h"
  2. Student::Student() {}
  3. Student::Student(int id,std::string name,std::string pwd)
  4. {
  5.         this->setId(id);
  6.         this->setName(name);
  7.         this->setPwd(pwd);
  8.         std::ifstream ifs;
  9.         ifs.open(LABORATORY_FILE, std::ios::in);
  10.         if (!ifs.is_open())
  11.         {
  12.                 std::cout << "Error: can not open file " << LABORATORY_FILE << std::endl;
  13.                 return;
  14.         }
  15.        
  16.         Laboratory lab;
  17.         int lab_id;
  18.         std::string lab_name;
  19.         int lab_max_capacity;
  20.         while (ifs >> lab_id && ifs >> lab_name && ifs >> lab_max_capacity)
  21.         {
  22.                 lab.set_lab_id(lab_id);
  23.                 lab.set_lab_name(lab_name);
  24.                 lab.set_lab_max_capacity(lab_max_capacity);
  25.                 this->v_Lab.push_back(lab);
  26.         }
  27.         ifs.close();
  28. }
  29. void Student::selectMenu()//功能菜单实现
  30. {
  31.         std::cout << "Welcome student: " << this->getName() << "login!" << std::endl;
  32.         std::cout << "\t\t ----------------------------------\n";
  33.         std::cout << "\t\t|          1.applyOrder           |\n";
  34.         std::cout << "\t\t|          2.showMyOrder          |\n";
  35.         std::cout << "\t\t|          3.showAllOrder         |\n";
  36.         std::cout << "\t\t|          4.cancelOrder          |\n";
  37.         std::cout << "\t\t|          0 or OtherKey.logout   |\n";
  38.         std::cout << "\t\t ----------------------------------\n";
  39.         std::cout << "Please select your action: " << std::endl;
  40. }
  41. void Student::applyOrder() //预约申请实现
  42. {
  43.         int day;
  44.         int time_slot;
  45.         int lab_id;
  46.         while (true)
  47.         {
  48.                 std::cout << "The lab is open Monday to Friday!" << std::endl;
  49.                 std::cout << "Please enter the time to request an appointment:" << std::endl;
  50.                 std::cout << "1, Monday" << std::endl;
  51.                 std::cout << "2, Tuesday" << std::endl;
  52.                 std::cout << "3, Wednesday" << std::endl;
  53.                 std::cout << "4, Thursday" << std::endl;
  54.                 std::cout << "5, Friday" << std::endl;
  55.                
  56.                 std::cin >> day;
  57.                 if (day < 1 || day>5)
  58.                 {
  59.                         std::cout << "Invalid input!" << std::endl;
  60.                         std::cout << "-------------------" << std::endl;
  61.                 }
  62.                 else
  63.                 {
  64.                         break;
  65.                 }
  66.         }
  67.         while (true)
  68.         {
  69.                 std::cout << "Please enter the time slot for requesting an appointment:" << std::endl;
  70.                 std::cout << "1, Morning 8:00-12:00" << std::endl;
  71.                 std::cout << "2, Afternoon 13:00-17:00" << std::endl;
  72.                 std::cout << "3, Evening 18:00-22:00" << std::endl;
  73.                
  74.                 std::cin >> time_slot;
  75.                 if (time_slot < 1 || time_slot>3)
  76.                 {
  77.                         std::cout << "Invalid input!" << std::endl;
  78.                         std::cout << "-------------------" << std::endl;
  79.                 }
  80.                 else
  81.                 {
  82.                         break;
  83.                 }
  84.         }
  85.         while (true)
  86.         {
  87.                 std::cout << "Please select a laboratory:" << std::endl;
  88.                 for (int i = 0; i < this->v_Lab.size(); i++)
  89.                 {
  90.                         std::cout << i + 1 << ". " << this->v_Lab[i].get_lab_name() << " (Capacity: " << this->v_Lab[i].get_lab_max_capacity() << ")" << std::endl;
  91.                 }
  92.                
  93.                 std::cin >> lab_id;
  94.                 if (lab_id < 1 || lab_id>this->v_Lab.size())
  95.                 {
  96.                         std::cout << "Invalid input!" << std::endl;
  97.                         std::cout<<"-------------------"<<std::endl;
  98.                 }
  99.                 else
  100.                 {
  101.                         break;
  102.                 }
  103.         }
  104.         std::cout<<"The appointment application is successful! Wait for the lab teacher to review it."<<std::endl;
  105.         std::ofstream ofs;
  106.         ofs.open(ORDER_FILE, std::ios::app);
  107.         if (!ofs.is_open())
  108.         {
  109.                 std::cout << "Error: can not open file " << ORDER_FILE << std::endl;
  110.                 return;
  111.         }
  112.         ofs << "day:" << day << " ";
  113.         ofs<<"time_slot:" << time_slot << " " ;
  114.         ofs<<"student_id:" << this->getId() << " " ;
  115.         ofs<<"student_name:"<<this->getName()<<" " ;
  116.         ofs<<"lab_id:"<<lab_id<<" " ;
  117.         ofs<<"lab_name:"<<this->v_Lab[lab_id-1].get_lab_name()<<" " ;
  118.         ofs<<"status:"<<1<<std::endl;//1表示审核中,2表示已通过,3表示已拒绝,4表示已取消
  119.         ofs.close();
  120.        
  121.         system("pause");
  122.         system("cls");
  123. }
  124. void Student::showMyOrder() {}//显示我的预约
  125. void Student::showAllOrder() {}//显示所有预约
  126. void Student::cancelOrder() {}//取消预约
复制代码
③体现我的预约功能——showMyOrder()

起首创建实验室预约文件LabAppointments.h和LabAppointments.cpp
实验室预约文件类LabAppointments中含有更新预约记录成员函数updateAppointments(),记录全部实验室预约记录的容器map<int, std::map<std::string, std::string>> v_Appointments,统计实验室预约记录条数appointments_size_
LabAppointments.h
  1. #pragma once
  2. #include <map>
  3. #include <iostream>
  4. #include <fstream>
  5. #include "globalFile.h"
  6. #include <map>
  7. class LabAppointments
  8. {
  9. public:
  10.         LabAppointments();
  11.         void updateAppointments();//更新预约记录信息
  12.         std::map<int,std::map<std::string, std::string>> v_Appointments;//记录所有预约记录信息,key为预约记录的总条数,value为对应预约记录信息
  13. private:
  14.         int appointments_size_;//预约记录的总条数
  15. };
复制代码
LabAppointments.cpp
  1. #include "LabAppointments.h"
  2. LabAppointments::LabAppointments()
  3. {
  4.         std::ifstream ifs;
  5.         ifs.open(ORDER_FILE,std::ios::in);
  6.         if(!ifs.is_open())
  7.         {
  8.                 std::cout << "Error: Failed to open file " << ORDER_FILE << std::endl;
  9.                 return;
  10.         }
  11.         //为了解析方便,这里都使用string类型存储
  12.         std::string day;//1-5 represent Monday to Friday
  13.         std::string time_slot;//1-3 represent 8:00-12:00,13:00-17:00,18:00-22:00
  14.         std::string student_id;//预约实验室申请的学生编号
  15.         std::string student_name;//预约实验室申请的学生姓名
  16.         std::string lab_id;//实验室编号
  17.         std::string lab_name;//实验室名称
  18.         std::string status;//预约申请的状态,1表示待审核状态
  19.        
  20.         this->appointments_size_ = 0;//预约申请的数量
  21.         while (ifs >> day && ifs >> time_slot && ifs >> student_id && ifs >> student_name && ifs >> lab_id && ifs >> lab_name && ifs >> status)
  22.         {
  23.                 /*
  24.                 std::cout << day << std::endl;
  25.                 std::cout << time_slot << std::endl;
  26.                 std::cout << student_id << std::endl;
  27.                 std::cout << student_name << std::endl;
  28.                 std::cout << lab_id << std::endl;
  29.                 std::cout << lab_name << std::endl;
  30.                 std::cout << status << std::endl;
  31.                 std::cout << std::endl;
  32.                 */
  33.                 /*解析内容如下:
  34.                 day:1
  35.                 time_slot:1
  36.                 student_id:1
  37.                 student_name:977
  38.                 lab_id:4
  39.                 lab_name:QT
  40.                 status:1
  41.                 */
  42.                 std::string key;
  43.                 std::string value;
  44.                 std::map<std::string, std::string> m;//存放key和value
  45.                
  46.                 //day:1
  47.                 int pos = day.find(':');//pos=3
  48.                 if (pos != -1) //找到冒号了
  49.                 {
  50.                         key = day.substr(0, pos);//day
  51.                         value = day.substr(pos + 1, day.size() - pos - 1);//1
  52.                         m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
  53.                 }
  54.                
  55.                 //time_slot:1
  56.                 pos = time_slot.find(':');
  57.                 if (pos != -1) //找到冒号了
  58.                 {
  59.                         key = time_slot.substr(0, pos);//time_slot
  60.                         value = time_slot.substr(pos + 1, time_slot.size() - pos - 1);
  61.                         m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
  62.                 }
  63.                 //student_id:1
  64.                 pos = student_id.find(':');
  65.                 if (pos != -1) //找到冒号了
  66.                 {
  67.                         key = student_id.substr(0, pos);//student_id
  68.                         value = student_id.substr(pos + 1, student_id.size() - pos - 1);
  69.                         m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
  70.                 }
  71.                 //student_name:977
  72.                 pos = student_name.find(':');
  73.                 if (pos != -1) //找到冒号了
  74.                 {
  75.                         key = student_name.substr(0, pos);//student_name
  76.                         value = student_name.substr(pos + 1, student_name.size() - pos - 1);//1
  77.                         m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
  78.                 }
  79.                 //lab_id:4
  80.                 pos = lab_id.find(':');
  81.                 if (pos != -1) //找到冒号了
  82.                 {
  83.                         key = lab_id.substr(0, pos);//lab_id
  84.                         value = lab_id.substr(pos + 1, lab_id.size() - pos - 1);//1
  85.                         m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
  86.                 }
  87.                 //lab_name:QT
  88.                 pos = lab_name.find(':');
  89.                 if (pos != -1) //找到冒号了
  90.                 {
  91.                         key = lab_name.substr(0, pos);//lab_name
  92.                         value = lab_name.substr(pos + 1, lab_name.size() - pos - 1);//1
  93.                         m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
  94.                 }
  95.                 //status:1
  96.                 pos = status.find(':');
  97.                 if (pos != -1) //找到冒号了
  98.                 {
  99.                         key = status.substr(0, pos);
  100.                         value = status.substr(pos + 1, status.size() - pos - 1);//1
  101.                         m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
  102.                 }
  103.                 //将map中的内容存入Appointment对象中
  104.                 this->v_Appointments.insert(std::make_pair(this->appointments_size_, m));
  105.                 this->appointments_size_++;
  106.         }
  107.         ifs.close();
  108.         //测试
  109.         /*
  110.         for (std::map<int, std::map<std::string, std::string>>::iterator it = this->v_Appointments.begin(); it != this->v_Appointments.end(); it++)
  111.         {
  112.                 std::cout << "Number of laboratory reservation records: " << it->first << ", Value is: " << std::endl;
  113.                 for (std::map<std::string, std::string>::iterator it2 = it->second.begin(); it2 != it->second.end(); it2++)
  114.                 {
  115.                         std::cout << it2->first << ":" << it2->second << " ";
  116.                 }
  117.                 std::cout << std::endl;
  118.         }
  119.         */
  120. }
  121. void LabAppointments::updateAppointments() //实验室预约申请状态更新
  122. {
  123.         if (this->appointments_size_ == 0) //一条实验室预约记录都没有
  124.         {
  125.                 return;
  126.         }
  127.         std::ofstream ofs;//写入到文件中
  128.         ofs.open(ORDER_FILE, std::ios::out | std::ios::trunc);//对实验室预约记录文件进行重写覆盖写入
  129.         if (!ofs.is_open())
  130.         {
  131.                 std::cout << "Error: Failed to open file " << ORDER_FILE << std::endl;
  132.                 return;
  133.         }
  134.         for (int i=0;i<this->appointments_size_;i++)
  135.         {        //v_Appointments[i]第i条实验室预约记录,是一个map
  136.                 //v_Appointments[i]["day"]第i条实验室预约记录的日期,通过这个map的key("day")拿到对应的value
  137.                 ofs << "day:" << this->v_Appointments[i]["day"] << " ";
  138.                 ofs << "time_slot:" << this->v_Appointments[i]["time_slot"] << " ";
  139.                 ofs << "student_id:" << this->v_Appointments[i]["student_id"] << " ";
  140.                 ofs << "student_name:" << this->v_Appointments[i]["student_name"] << " ";
  141.                 ofs << "lab_id:" << this->v_Appointments[i]["lab_id"] << " ";
  142.                 ofs << "lab_name:" << this->v_Appointments[i]["lab_name"] << " ";
  143.                 ofs << "status:" << this->v_Appointments[i]["status"] << std::endl;
  144.         }
  145.         ofs.close();
  146. }
复制代码
接下来开始实现体现我的预约操作
实验室预约记录order.txt中有三条实验室预约记录

先创建一个LabAppointments对象,看下它有几条预约记录(appointments_size_),若为0,则没有实验室申请预约记录;若有记录,则遍历全部的实验室预约记录,然后以学号(student_id_)为判断记录进行体现即可
体现内容:哪一天、时间段、实验室编号、实验室名称、状态,不必要体现学号和学生姓名
Student.cpp
  1. void Student::showMyOrder() //显示我的预约
  2. {
  3.         int number{0};//实验室申请记录条数
  4.         LabAppointments appointments;//预约信息
  5.         if (appointments.getAppointments() == 0)//如果没有预约信息
  6.         {
  7.                 std::cout << "No LabAppointments Appointment!" << std::endl;
  8.                 system("pause");
  9.                 system("cls");
  10.                 return;
  11.         }
  12.        
  13.         for (int i = 0; i < appointments.getAppointments(); i++)
  14.         {
  15.                 //this->getId()                                                   是一个int类型
  16.                 //appointments.v_Appointments[i]["student_id"]                    是一个string类型
  17.                 //appointments.v_Appointments[i]["student_id"].c_str()            是一个char*类型,是C语言的字符串;.c_str()将string 转 const char*
  18.                 //atoi(appointments.v_Appointments[i]["student_id"].c_str())      是一个int类型,是C语言的整数;      atoi()将const char* 转 int
  19.                 //所以可以比较两个string类型的值
  20.                 if (std::atoi(appointments.v_Appointments[i]["student_id"].c_str()) == this->getId())//如果是自己的预约
  21.                 {
  22.                         number++;
  23.                         std::cout << "Your appointment is the day: " << appointments.v_Appointments[i]["day"];
  24.                         std::cout << "; time slot is: " << (appointments.v_Appointments[i]["time_slot"] == "1"?"Morning":(appointments.v_Appointments[i]["time_slot"] == "2")?"Afternoon":(appointments.v_Appointments[i]["time_slot"] == "3")?"Evening":"Your Appointment Time Slot Is Error.");
  25.                         std::cout << "; lab_id is: " << appointments.v_Appointments[i]["lab_id"];
  26.                         std::cout << "; lab_name is: " << appointments.v_Appointments[i]["lab_name"];
  27.                         std::string status = "; statis: ";//1表示审核中,2表示已通过,3表示已拒绝,4表示已取消
  28.                         if (appointments.v_Appointments[i]["status"] == "1")
  29.                         {
  30.                                 status += "Pending Review";
  31.                         }
  32.                         else if (appointments.v_Appointments[i]["status"] == "2")
  33.                         {
  34.                                 status += "Approved";
  35.                         }
  36.                         else if (appointments.v_Appointments[i]["status"] == "3")
  37.                         {
  38.                                 status += "Rejected";
  39.                         }
  40.                         else if (appointments.v_Appointments[i]["status"] == "4")
  41.                         {
  42.                                 status += "Canceled";
  43.                         }
  44.                         else
  45.                         {
  46.                                 status += "Your Appointment Status Is Error.";
  47.                         }
  48.                         std::cout << " " << status << std::endl;
  49.                 }
  50.         }
  51.         std::cout << "Total " << number << " LabAppointments Appointment!" << std::endl;
  52.         system("pause");
  53.         system("cls");
  54. }
复制代码
④查看全部预约功能——showAllOrder()

和体现我的预约功能雷同,体现内容:哪一天、时间段、学号、学生姓名、实验室编号、实验室名称、预约状态
比体现我的预约功能多了体现学号、学生姓名,优化一点,为每条实验室申请预约记录加个序号
Student.cpp
  1. void Student::showAllOrder() //显示所有预约
  2. {
  3.         LabAppointments appointments;
  4.         if (appointments.getAppointments() == 0)
  5.         {
  6.                 std::cout << "No LabAppointments Appointment!" << std::endl;
  7.                 system("pause");
  8.                 system("cls");
  9.                 return;
  10.         }
  11.         for (int i = 0; i < appointments.getAppointments(); i++)
  12.         {
  13.                 std::cout << i + 1 << ".";
  14.                 std::cout << "The day: " << appointments.v_Appointments[i]["day"];
  15.                 std::cout << "; time slot is: " << (appointments.v_Appointments[i]["time_slot"] == "1"?"Morning":(appointments.v_Appointments[i]["time_slot"] == "2")?"Afternoon":(appointments.v_Appointments[i]["time_slot"] == "3")?"Evening":"Your Appointment Time Slot Is Error.");
  16.                 std::cout << "; student_id is: " << appointments.v_Appointments[i]["student_id"];
  17.                 std::cout << "; student_name is: " << appointments.v_Appointments[i]["student_name"];
  18.                 std::cout << "; lab_id is: " << appointments.v_Appointments[i]["lab_id"];
  19.                 std::cout << "; lab_name is: " << appointments.v_Appointments[i]["lab_name"];
  20.                 std::string status = "; statis: ";//1表示审核中,2表示已通过,3表示已拒绝,4表示已取消
  21.                 if (appointments.v_Appointments[i]["status"] == "1")
  22.                 {
  23.                         status += "Pending Review";
  24.                 }
  25.                 else if (appointments.v_Appointments[i]["status"] == "2")
  26.                 {
  27.                         status += "Approved";
  28.                 }
  29.                 else if (appointments.v_Appointments[i]["status"] == "3")
  30.                 {
  31.                         status += "Rejected";
  32.                 }
  33.                 else if (appointments.v_Appointments[i]["status"] == "4")
  34.                 {
  35.                         status += "Canceled";
  36.                 }
  37.                 else
  38.                 {
  39.                         status += "Your Appointment Status Is Error.";
  40.                 }
  41.                 std::cout << " " << status << std::endl;
  42.         }
  43.         system("pause");
  44.         system("cls");
  45. }
复制代码
⑤取消预约功能——cancelOrder()

只有在自己的学号并且预约成功或稽核中才可以取消预约
1表现稽核中,2表现已通过,3表现已拒绝,4表现已取消
Student.cpp
  1. void Student::cancelOrder() //取消预约
  2. {
  3.         LabAppointments appointments;
  4.         if (appointments.getAppointments() == 0)
  5.         {
  6.                 std::cout << "No LabAppointments Appointment!" << std::endl;
  7.                 system("pause");
  8.                 system("cls");
  9.                 return;
  10.         }
  11.         std::cout << "Records that are under review or have a successful appointment can be canceled, so please enter the canceled record:" << std::endl;
  12.         std::vector<int> v_index;
  13.         int index = 1;
  14.         for (int i = 0; i < appointments.getAppointments(); i++)
  15.         {
  16.                 if (atoi(appointments.v_Appointments[i]["student_id"].c_str()) == this->getId())//是自己的预约
  17.                 {
  18.                         if (appointments.v_Appointments[i]["status"] == "1" || appointments.v_Appointments[i]["status"] == "2")//审核中或已通过的预约可以取消
  19.                         {
  20.                                 v_index.push_back(i);
  21.                                 std::cout << index << ". ";
  22.                                 std::cout <<" The day: " << appointments.v_Appointments[i]["day"];
  23.                                 std::cout << "; time slot is: " << (appointments.v_Appointments[i]["time_slot"] == "1"?"Morning":(appointments.v_Appointments[i]["time_slot"] == "2")?"Afternoon":(appointments.v_Appointments[i]["time_slot"] == "3")?"Evening":"Your Appointment Time Slot Is Error.");
  24.                                 std::cout << "; lab_id is: " << appointments.v_Appointments[i]["lab_id"];
  25.                                 std::cout << "; lab_name is: " << appointments.v_Appointments[i]["lab_name"];
  26.                                 std::string status = "; statis: ";//1表示审核中,2表示已通过,3表示已拒绝,4表示已取消
  27.                                 if (appointments.v_Appointments[i]["status"] == "1")//审核中
  28.                                 {
  29.                                         status += "Pending Review";
  30.                                 }
  31.                                 else if (appointments.v_Appointments[i]["status"] == "2")//已通过
  32.                                 {
  33.                                         status += "Approved";
  34.                                 }
  35.                                 std::cout << " " << status << std::endl;
  36.                                 index++;
  37.                         }
  38.                 }
  39.         }
  40.         std::cout << "Please enter the index of the canceled record, or enter 0 to return:" << std::endl;
  41.         int select = 0;
  42.         while (true)
  43.         {
  44.                 std::cin >> select;
  45.                 if (select >= 0 && select <= v_index.size())
  46.                 {
  47.                         if (select == 0) //输入0表示取消
  48.                         {
  49.                                 break;
  50.                         }
  51.                         else
  52.                         {
  53.                                 int i = v_index[select - 1];
  54.                                 appointments.v_Appointments[i]["status"] = "4";//将状态设置为已取消
  55.                                 appointments.updateAppointments();
  56.                                 std::cout << "The appointment has been canceled successfully!" << std::endl;
  57.                                 break;
  58.                         }
  59.                 }
  60.                 std::cout << "You have made a mistake, please re-enter." << std::endl;
  61.         }
  62.         system("pause");
  63.         system("cls");
  64. }
复制代码
7,西席模块

①登录和注销功能——TeacherMenu()

在Teacher类的有参构造函数中进行学生信息的初始化操作
Teacher.cpp
  1. Teacher::Teacher(int teacher_id,std::string name,std::string pwd)
  2. {
  3.     //初始化实验室教师职工号、姓名、密码
  4.     this->setId(teacher_id);
  5.     this->setName(name);
  6.     this->setPwd(pwd);
  7. }
复制代码
在main.cpp中定义全局函数teacherMenu(),实验室西席登录成功之后进入页面,进行相应的功能的选择
在main.cpp中的用户登录函数Login()中,用户登录成功进入到实验室西席功能页面(调用TeacherMenu()函数即可)
main.cpp
  1. void TeacherMenu(Identify*& identify_p) //传入父类的指针
  2. {
  3.         //进入菜单界面
  4.         while (true)
  5.         {
  6.                 //调用管理员子菜单
  7.                 identify_p->selectMenu(); //这是父类指针,只能调用公共的接口纯虚函数
  8.                 Teacher* teacher = (Teacher*)identify_p; //将父类指针转为子类指针
  9.                 int userselect = 0; //用户的选择
  10.                 std::cin >> userselect; //接收用户的选择
  11.                 if (userselect == 1)
  12.                 {
  13.                         std::cout << "Your select is view all appointment information" << std::endl;
  14.                         teacher->showAllOrder();
  15.                 }
  16.                 else if (userselect == 2)
  17.                 {
  18.                         std::cout << "Your select is valid appointment information" << std::endl;
  19.                         teacher->validOrder();
  20.                 }
  21.                 else//选成其他选项,默认代表注销登录
  22.                 {
  23.                         delete teacher; //销毁堆区创建的对象
  24.                         std::cout << "The logout is successful" << std::endl;
  25.                         system("pause");
  26.                         system("cls");
  27.                         return;
  28.                 }
  29.         }
  30. }
复制代码
定义全局函数TeacherMenu()用于体现学生身份菜单界面
在Login()登录全局函数中进行调用即可

②查看全部预约功能——showAllOrder()

与学生身份的查看全部预约功能相似,用于体现全部预约记录
Teacher.cpp
  1. void Teacher::showAllOrder()//显示所有预约
  2. {
  3.         LabAppointments appointments;
  4.         if (appointments.getAppointments() == 0)
  5.         {
  6.                 std::cout << "No LabAppointments Appointment!" << std::endl;
  7.                 system("pause");
  8.                 system("cls");
  9.                 return;
  10.         }
  11.         for (int i = 0; i < appointments.getAppointments(); i++)
  12.         {
  13.                 std::cout << i + 1 << ".";
  14.                 std::cout << "The day: " << appointments.v_Appointments[i]["day"];
  15.                 std::cout << "; time slot is: " << (appointments.v_Appointments[i]["time_slot"] == "1" ? "Morning" : (appointments.v_Appointments[i]["time_slot"] == "2") ? "Afternoon" : (appointments.v_Appointments[i]["time_slot"] == "3") ? "Evening" : "Your Appointment Time Slot Is Error.");
  16.                 std::cout << "; student_id is: " << appointments.v_Appointments[i]["student_id"];
  17.                 std::cout << "; student_name is: " << appointments.v_Appointments[i]["student_name"];
  18.                 std::cout << "; lab_id is: " << appointments.v_Appointments[i]["lab_id"];
  19.                 std::cout << "; lab_name is: " << appointments.v_Appointments[i]["lab_name"];
  20.                 std::string status = "; status: ";//1表示审核中,2表示已通过,3表示已拒绝,4表示已取消
  21.                 if (appointments.v_Appointments[i]["status"] == "1")
  22.                 {
  23.                         status += "Pending Review";
  24.                 }
  25.                 else if (appointments.v_Appointments[i]["status"] == "2")
  26.                 {
  27.                         status += "Approved";
  28.                 }
  29.                 else if (appointments.v_Appointments[i]["status"] == "3")
  30.                 {
  31.                         status += "Rejected";
  32.                 }
  33.                 else if (appointments.v_Appointments[i]["status"] == "4")
  34.                 {
  35.                         status += "Canceled";
  36.                 }
  37.                 else
  38.                 {
  39.                         status += "Your Appointment Status Is Error.";
  40.                 }
  41.                 std::cout << " " << status << std::endl;
  42.         }
  43.         system("pause");
  44.         system("cls");
  45. }
复制代码
③稽核预约功能——validOrder()

获取实验室预约记录order.txt中全部数据,筛选status为待稽核状态的进行体现
实验室老师对这些待稽核状态的实验室预约记录进行稽核,稽核完成之后重新更新状态即可
Teacher.cpp
  1. void Teacher::validOrder() //审核预约
  2. {
  3.         LabAppointments appointments;
  4.         if (appointments.getAppointments() == 0)
  5.         {
  6.                 std::cout << "No LabAppointments Appointment!" << std::endl;
  7.                 system("pause");
  8.                 system("cls");
  9.                 return;
  10.         }
  11.         std::cout << "The records of laboratory appointments to be reviewed are as follows:" << std::endl;
  12.         std::vector<int> v_valid_order;
  13.         int index = 1;
  14.         for (int i = 0; i < appointments.getAppointments(); i++)
  15.         {
  16.                 if (appointments.v_Appointments[i]["status"] == "1")//1表示审核中
  17.                 {
  18.                         v_valid_order.push_back(i);
  19.                         std::cout << index++ << ".";
  20.                         std::cout << "The day: " << appointments.v_Appointments[i]["day"];
  21.                         std::cout << "; time slot is: " << (appointments.v_Appointments[i]["time_slot"] == "1" ? "Morning" : (appointments.v_Appointments[i]["time_slot"] == "2") ? "Afternoon" : (appointments.v_Appointments[i]["time_slot"] == "3") ? "Evening" : "Your Appointment Time Slot Is Error.");
  22.                         std::cout << "; student_id is: " << appointments.v_Appointments[i]["student_id"];
  23.                         std::cout << "; student_name is: " << appointments.v_Appointments[i]["student_name"];
  24.                         std::cout << "; lab_id is: " << appointments.v_Appointments[i]["lab_id"];
  25.                         std::cout << "; lab_name is: " << appointments.v_Appointments[i]["lab_name"];
  26.                        
  27.                         std::string status = "; status: Pending Revie";//1表示审核中,2表示已通过,3表示已拒绝,4表示已取消
  28.                         std::cout << status << std::endl;
  29.                 }
  30.         }
  31.         //输入审核的实验室预约序号,0表示返回,不想审核了
  32.         std::cout << "Please enter the lab appointment record you want to review, 0 means return" << std::endl;
  33.         int select = 0;//实验室教师选择要审核的预约序号
  34.         int ret = 0;//接收实验室教师给的审核结果,1表示审核通过,2表示审核拒绝
  35.         while (true)
  36.         {
  37.                 std::cin >> select;
  38.                 if (select >= 0 && select <= v_valid_order.size())
  39.                 {
  40.                         if (select == 0)//选择0,表示不想审核了,退出循环即可
  41.                         {
  42.                                 break;
  43.                         }
  44.                         else
  45.                         {
  46.                                 //1表示审核中,2表示已通过,3表示已拒绝,4表示已取消
  47.                                 std::cout << "Please enter the result of the review" << std::endl;
  48.                                 std::cout << "1. Approved" << std::endl;
  49.                                 std::cout << "2. Rejected" << std::endl;
  50.                                 std::cin >> ret;
  51.                                 if (ret == 1)
  52.                                 {
  53.                                         appointments.v_Appointments[v_valid_order[select - 1]]["status"] = "2";
  54.                                 }
  55.                                 else if (ret == 2)
  56.                                 {
  57.                                         appointments.v_Appointments[v_valid_order[select - 1]]["status"] = "3";
  58.                                 }
  59.                                 else
  60.                                 {
  61.                                         std::cout << "Your input is error!" << std::endl;
  62.                                         continue;
  63.                                 }
  64.                                 appointments.updateAppointments();//更新预约信息到文件中
  65.                                 std::cout << "The review result has been saved!" << std::endl;
  66.                                 break;
  67.                         }
  68.                 }
  69.                 std::cout << "You have made a mistake, please re-enter." << std::endl;
  70.         }
  71.         system("pause");
  72.         system("cls");
  73. }
复制代码
四、完整代码

0,项目结构


项目同级目次下创建五个文本充当数据库

1,globalFile.h

宏定义,确定五个设置文件
  1. #pragma once
  2. //管理员文件
  3. #define ADMIN_FILE     "admin.txt"
  4. //学生文件
  5. #define STUDENT_FILE   "student.txt"
  6. //教师文件
  7. #define TEACHER_FILE   "teacher.txt"
  8. //实验室信息文件
  9. #define LABORATORY_FILE  "laboratoryRoom.txt"
  10. //订单文件
  11. #define ORDER_FILE     "order.txt"
复制代码
2,Identify.h

管理员、学生、老师的基类
  1. #pragma once
  2. #include <iostream>
  3. class Identify
  4. {
  5. public:
  6.         virtual void selectMenu() = 0;//纯虚函数,不同用户登录成功之后显示的菜单均不同
  7.        
  8.         std::string getName()
  9.         {
  10.                 return this->name_;
  11.         }
  12.         std::string getPwd()
  13.         {
  14.                 return this->pwd_;
  15.         }
  16.         void setName(std::string name)
  17.         {
  18.                 this->name_ = name;
  19.         }
  20.         void setPwd(std::string pwd)
  21.         {
  22.                 this->pwd_ = pwd;
  23.         }
  24. private:
  25.         std::string name_;//姓名
  26.         std::string pwd_;//密码
  27. };
复制代码
3,Laboratory.h

实验室类
  1. #pragma once
  2. #include <iostream>
  3. //实验室类
  4. class Laboratory
  5. {
  6. public:
  7.         Laboratory() {};
  8.         Laboratory(int id, std::string name, int capacity):lab_id_(id), lab_name_(name), lab_max_capacity_(capacity) {}
  9.         void set_lab_id(int id) { this->lab_id_ = id; }
  10.         void set_lab_name(std::string name) { this->lab_name_ = name; }
  11.         void set_lab_max_capacity(int capacity) { this->lab_max_capacity_ = capacity; }
  12.         int get_lab_id() const { return this->lab_id_; }
  13.         std::string get_lab_name() const { return this->lab_name_; }
  14.         int get_lab_max_capacity() const { return this->lab_max_capacity_; }
  15.                
  16. private:
  17.         int lab_id_; //实验室编号
  18.         std::string lab_name_; //实验室名称
  19.         int lab_max_capacity_; //实验室容量
  20. };
复制代码
4,Admin.h

  1. #pragma once#include <iostream>#include <fstream>#include "globalFile.h"#include "Identify.h"#include <vector>#include "Student.h"#include "Teacher.h"#include <algorithm>#include "Laboratory.h"class Admin:public Identify{public:        Admin();        Admin(std::string name,std::string pwd);        virtual void Identify::selectMenu() override;//功能菜单实现        void addUser();//添加用户        void showUser();//查看用户        void addLabInfo();//添加实验室信息        void showLabInfo();//查看实验室信息        void cleanRecord();//清空预约记录        void init_Stu_Tea_Vector();//初始化容器,用于将对应的文本文件信息读取到对应的容器中        std::vector<Student> v_Student;//学生容器        std::vector<Teacher> v_Teacher;//西席容器        bool checkRepeate(int id, int type);//检查学号\职工号\实验室编号是否重复        void init_Lab_Vector();//初始化实验室容器,用于将实验室信息读取到容器中
  2.         std::vector<Laboratory> v_Lab;//实验室容器
  3. };
复制代码
5,Admin.cpp

  1. #include "Admin.h"Admin::Admin() {}Admin::Admin(std::string name, std::string pwd) {        //初始化管理员信息        Admin::setName(name);        Admin::setPwd(pwd);        //初始化学生和老师的容器,获取文件中全部老师和学生的信息        this->init_Stu_Tea_Vector();        //初始化实验室的容器,获取文件中全部实验室的信息        this->init_Lab_Vector();}void Admin::selectMenu()//功能菜单实现{        std::cout << "Welcome admin: " << this->getName() << " login!" << std::endl;        std::cout << "\t\t --------------------------------\n";        std::cout << "\t\t|          1.addUser             |\n";        std::cout << "\t\t|          2.showUser            |\n";        std::cout << "\t\t|          3.addLabInfo          |\n";        std::cout << "\t\t|          4.showLabInfo         |\n";        std::cout << "\t\t|          5.cleanRecord         |\n";        std::cout << "\t\t|          0 or OtherKey.logout  |\n";        std::cout << "\t\t --------------------------------\n";        std::cout << "Please select your action: " << std::endl;}void Admin::addUser()添加用户功能实现 {        std::string fileType;//根据用户的选择进行操作差别的文件        std::string tip;//提示是学号还是职工号        std::string repeat_tip;//重复提示信息        std::ofstream ofs;//文件操作对象        int userselect = 0;//获取用户的选择                while (true)        {                //提示用户                std::cout << "Please enter the type of account you want to add (1.student or 2.teacher):" << std::endl;                std::cout << "1、student" << std::endl;                std::cout << "2、teacher" << std::endl;                std::cout << "0、exit" << std::endl;                std::cin >> userselect;                if (userselect == 1)//添加学生                {                        fileType = STUDENT_FILE;                        tip = "please input studentID: ";                        repeat_tip = "studentID already exists, please input again: ";                        break;                }                else if (userselect == 2)//添加老师                {                        fileType = TEACHER_FILE;                        tip = "please input teacherID: ";                        repeat_tip = "teacherID already exists, please input again: ";                        break;                }                else if(userselect == 0)                {                        system("cls");//清屏                        return;                }                else                 {                        std::cout << "Invalid input, please try again." << std::endl;                }        }        ofs.open(fileType, std::ios::app);//以追加的方式写入文件        if (!ofs.is_open())//文件打开失败        {                std::cout << "Failed to open file: " << fileType << std::endl;                return;        }        int id;//学号或职工号        std::string name;//姓名        std::string pwd;//暗码        std::cout << tip << std::endl;//提示输入学号或职工号        while (true)         {                std::cin >> id;                bool repeat = this->checkRepeate(id, userselect);//检查是否有重复的用户                if (repeat)//有重复的用户                {                        std::cout << repeat_tip << std::endl;                }                else//没有重复的用户                {                        break;                }        }                        std::cout << "please input name: " << std::endl;        std::cin >> name;//输入姓名        std::cout << "please input password: " << std::endl;        std::cin >> pwd;//输入暗码                                        //写入文件        ofs << id << " " << name << " " << pwd << " " << std::endl;        ofs.close();        std::cout << "Add user successfully!" << std::endl;        this->init_Stu_Tea_Vector();//刷新容器,重新读取文件中的用户信息                        system("pause");//暂停步伐        system("cls");//清屏}void Admin::init_Stu_Tea_Vector()//初始化学生和老师的容器,用于存放从文件中读取的全部用户信息{        //对容器进行初始化,清空用于存放对应用户信息的容器        this->v_Student.clear();        this->v_Teacher.clear();        std::ifstream ifs;//文件读取操作对象                //读取学生信息        ifs.open(STUDENT_FILE, std::ios::in);//以读取的方式打开学生文件        if (!ifs.is_open())//文件打开失败        {                std::cout << "Failed to open file: " << STUDENT_FILE << std::endl;                return;        }        Student student;        int student_id;        std::string student_name;        std::string student_pwd;        while (ifs >> student_id && ifs >> student_name && ifs >> student_pwd)        {                student.setId(student_id);                student.setName(student_name);                student.setPwd(student_pwd);                v_Student.push_back(student);        }        //std::cout << "Read student information successfully!" << std::endl;        //std::cout<<"Student size: "<<v_Student.size()<<std::endl;        ifs.close();        //读取老师信息        ifs.open(TEACHER_FILE, std::ios::in);//以读取的方式打开老师文件        if (!ifs.is_open())//文件打开失败        {                std::cout << "Failed to open file: " << TEACHER_FILE << std::endl;                return;        }        Teacher teacher;        int teacher_id;        std::string teacher_name;        std::string teacher_pwd;        while (ifs >> teacher_id && ifs >> teacher_name && ifs >> teacher_pwd)        {                teacher.setId(teacher_id);                teacher.setName(teacher_name);                teacher.setPwd(teacher_pwd);                v_Teacher.push_back(teacher);        }        //std::cout << "Read teacher information successfully!" << std::endl;        //std::cout << "Teacher size: " << v_Teacher.size() << std::endl;        ifs.close();}bool Admin::checkRepeate(int id,int fileType)//检查是否有重复的用户{        if (fileType == 1)//学生        {                for (int i = 0; i < v_Student.size(); i++)                {                        if (v_Student[i].getId() == id)                        {                                return true;                        }                }        }        else if (fileType == 2)//老师        {                for (int i = 0; i < v_Teacher.size(); i++)                {                        if (v_Teacher[i].getId() == id)                        {                                return true;                        }                }        }        else if (fileType == 3) //实验室        {                for (int i = 0; i < v_Lab.size(); i++)                {                        if (v_Lab[i].get_lab_id() == id)                        {                                return true;                        }                }        }        return false;}void printStudent(Student& student){        std::cout << "Student ID: " << student.getId() << ", Name: "<<student.getName() << ", Password: " << student.getPwd() << std::endl;}void printTeacher(Teacher& teacher){        std::cout << "Teacher ID: " << teacher.getId() << ", Name: " << teacher.getName() << ", Password: " << teacher.getPwd() << std::endl;}void Admin::showUser() //查看用户功能实现{        while (true)        {                std::cout << "Please enter the type of account you want to show (1.student or 2.teacher):" << std::endl;                std::cout << "1、student" << std::endl;                std::cout << "2、teacher" << std::endl;                std::cout << "0、exit" << std::endl;                int userselect = 0;                std::cin >> userselect;                if (userselect == 1)//查看全部学生的信息                {                        std::cout << "Student information:" << std::endl;                        std::for_each(v_Student.begin(), v_Student.end(), printStudent);                        /* 和for_each的用法一样,二选一即可                        for(std::vector<Student>::iterator it=v_Student.begin();it!=v_Student.end();it++)                        {                                std::cout<<"Student ID: "<<it->getId()<<", Name: "<<it->getName()<<", Password: "<<it->getPwd()<<std::endl;                        }                        */                        system("pause");//暂停步伐                        system("cls");//清屏                        break;                }                else if (userselect == 2)//查看全部老师的信息                {                        std::cout << "Teacher information:" << std::endl;                        std::for_each(v_Teacher.begin(), v_Teacher.end(), printTeacher);                        /* 和for_each的用法一样,二选一即可                        for (std::vector<Teacher>::iterator it = v_Teacher.begin(); it != v_Teacher.end(); it++)                        {                                std::cout << "Teacher ID: " << it->getId() << ", Name: " << it->getName() << ", Password: " << it->getPwd() << std::endl;                        }                        */                        system("pause");//暂停步伐                        system("cls");//清屏                        break;                }                else if (userselect == 0)//退出                {                        system("cls");//清屏                        break;                }                else                {                        std::cout << "Invalid input, please try again." << std::endl;                        system("pause");//暂停步伐                        system("cls");//清屏                }        }}void Admin::addLabInfo() //添加实验室信息功能实现
  2. {
  3.         int lab_id;
  4.         std::string lab_name;
  5.         int lab_max_capacity;
  6.         std::ofstream ofs;//文件操作对象
  7.         ofs.open(LABORATORY_FILE, std::ios::app);//以追加的方式写入文件
  8.         if (!ofs.is_open())//文件打开失败
  9.         {
  10.                 std::cout << "Failed to open file: " << LABORATORY_FILE << std::endl;
  11.                 return;
  12.         }
  13.         while (true)
  14.         {
  15.                 std::cout << "Please input laboratory ID: " << std::endl;
  16.                 std::cin >> lab_id;
  17.                 bool repeat = this->checkRepeate(lab_id, 3);//检查是否有重复的实验室
  18.                 if (repeat)
  19.                 {
  20.                         std::cout << "Laboratory ID already exists, please input again: " << std::endl;
  21.                 }
  22.                 else
  23.                 {
  24.                         break;
  25.                 }
  26.         }
  27.         std::cout << "Please input laboratory name: " << std::endl;
  28.         std::cin >> lab_name;
  29.         std::cout << "Please input laboratory max capacity: " << std::endl;
  30.         std::cin >> lab_max_capacity;
  31.         ofs << lab_id << " " << lab_name << " " << lab_max_capacity << " " << std::endl;
  32.         ofs.close();
  33.         std::cout << "Add laboratory information successfully!" << std::endl;
  34.         this->init_Lab_Vector();
  35.         system("pause");//暂停程序
  36.         system("cls");//清屏
  37. }
  38. void Admin::init_Lab_Vector() //初始化实验室信息功能实现{        //对容器进行初始化,清空用于存放实验室信息的容器        this->v_Lab.clear();        //读取实验室信息        std::ifstream ifs;        ifs.open(LABORATORY_FILE, std::ios::in);//以读取的方式打开实验室信息文件        if (!ifs.is_open())         {                std::cout << "Failed to open file: " << LABORATORY_FILE << std::endl;                return;        }        Laboratory lab;        int lab_id;        std::string lab_name;        int lab_max_capacity;        while (ifs>>lab_id&&ifs>>lab_name&&ifs>>lab_max_capacity)        {                lab.set_lab_id(lab_id);                lab.set_lab_name(lab_name);                lab.set_lab_max_capacity(lab_max_capacity);                this->v_Lab.push_back(lab);        }        ifs.close();        //std::cout << "Read laboratory information successfully!" << std::endl;        //std::cout << "Laboratory size: " << this->v_Lab.size() << std::endl;        }void printLab(Laboratory& lab)
  39. {
  40.         std::cout << "Laboratory ID: " << lab.get_lab_id() << ", Name: " << lab.get_lab_name() << ", Max capacity: " << lab.get_lab_max_capacity() << std::endl;
  41. }
  42. void Admin::showLabInfo() //查看实验室信息功能实现
  43. {
  44.         std::cout << "Laboratory information:" << std::endl;
  45.         std::for_each(v_Lab.begin(), v_Lab.end(), printLab);
  46.         /* 和for_each的用法一样,二选一即可
  47.         for (std::vector<Laboratory>::iterator it = v_Lab.begin(); it != v_Lab.end(); it++)
  48.         {
  49.                 std::cout << "Laboratory ID: " << it->get_lab_id() << ", Name: " << it->get_lab_name() << ", Max capacity: " << it->get_lab_max_capacity() << std::endl;
  50.         }
  51.         */
  52.         system("pause");//暂停程序
  53.         system("cls");//清屏
  54. }
  55. void Admin::cleanRecord() //清空预约记录功能实现{        while (true)         {                std::cout << "Are you sure to clean all the record? (Y/N)" << std::endl;                std::ofstream ofs;                char choice;                std::cin >> choice;                if (choice == 'Y' || choice == 'y')                {                        ofs.open(ORDER_FILE, std::ios::trunc);                        if (!ofs.is_open())                        {                                std::cout << "Failed to open file: " << ORDER_FILE << std::endl;                                break;                        }                        ofs.close();                        std::cout << "Clean record successfully!" << std::endl;                        system("pause");//暂停步伐                        system("cls");//清屏                        break;                }                else if (choice == 'N' || choice == 'n')                {                        std::cout << "Cancel operation!" << std::endl;                        system("pause");//暂停步伐                        system("cls");//清屏                        break;                }                else                {                        std::cout << "Invalid input, please try again." << std::endl;                        system("pause");//暂停步伐                                        }        }}
复制代码
6,LabAppointments.h

实验室预约申请类,存放学生的预约申请实验室记录
  1. #pragma once
  2. #include <map>
  3. #include <iostream>
  4. #include <fstream>
  5. #include "globalFile.h"
  6. #include <map>
  7. class LabAppointments
  8. {
  9. public:
  10.         LabAppointments();
  11.         void updateAppointments();//更新预约记录信息
  12.         std::map<int,std::map<std::string, std::string>> v_Appointments;//记录所有预约记录信息,key为预约记录的总条数,value为对应预约记录信息
  13.         int getAppointments() //获取预约记录信息
  14.         {
  15.                 return this->appointments_size_;
  16.         }
  17.         void setAppointments(int size) //设置预约记录信息
  18.         {
  19.                 this->appointments_size_ = size;
  20.         }
  21. private:
  22.         int appointments_size_;//预约记录的总条数
  23. };
复制代码
7,LabAppointments.cpp

  1. #include "LabAppointments.h"
  2. LabAppointments::LabAppointments()
  3. {
  4.         std::ifstream ifs;
  5.         ifs.open(ORDER_FILE,std::ios::in);
  6.         if(!ifs.is_open())
  7.         {
  8.                 std::cout << "Error: Failed to open file " << ORDER_FILE << std::endl;
  9.                 return;
  10.         }
  11.         //为了解析方便,这里都使用string类型存储
  12.         std::string day;//1-5 represent Monday to Friday
  13.         std::string time_slot;//1-3 represent 8:00-12:00,13:00-17:00,18:00-22:00
  14.         std::string student_id;//预约实验室申请的学生编号
  15.         std::string student_name;//预约实验室申请的学生姓名
  16.         std::string lab_id;//实验室编号
  17.         std::string lab_name;//实验室名称
  18.         std::string status;//预约申请的状态,1表示待审核状态
  19.        
  20.         this->appointments_size_ = 0;//预约申请的数量
  21.         while (ifs >> day && ifs >> time_slot && ifs >> student_id && ifs >> student_name && ifs >> lab_id && ifs >> lab_name && ifs >> status)
  22.         {
  23.                 /*
  24.                 std::cout << day << std::endl;
  25.                 std::cout << time_slot << std::endl;
  26.                 std::cout << student_id << std::endl;
  27.                 std::cout << student_name << std::endl;
  28.                 std::cout << lab_id << std::endl;
  29.                 std::cout << lab_name << std::endl;
  30.                 std::cout << status << std::endl;
  31.                 std::cout << std::endl;
  32.                 */
  33.                 /*解析内容如下:
  34.                 day:1
  35.                 time_slot:1
  36.                 student_id:1
  37.                 student_name:977
  38.                 lab_id:4
  39.                 lab_name:QT
  40.                 status:1
  41.                 */
  42.                 std::string key;
  43.                 std::string value;
  44.                 std::map<std::string, std::string> m;//存放key和value
  45.                
  46.                 //day:1
  47.                 int pos = day.find(':');//pos=3
  48.                 if (pos != -1) //找到冒号了
  49.                 {
  50.                         key = day.substr(0, pos);//day
  51.                         value = day.substr(pos + 1, day.size() - pos - 1);//1
  52.                         m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
  53.                 }
  54.                
  55.                 //time_slot:1
  56.                 pos = time_slot.find(':');
  57.                 if (pos != -1) //找到冒号了
  58.                 {
  59.                         key = time_slot.substr(0, pos);//time_slot
  60.                         value = time_slot.substr(pos + 1, time_slot.size() - pos - 1);
  61.                         m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
  62.                 }
  63.                 //student_id:1
  64.                 pos = student_id.find(':');
  65.                 if (pos != -1) //找到冒号了
  66.                 {
  67.                         key = student_id.substr(0, pos);//student_id
  68.                         value = student_id.substr(pos + 1, student_id.size() - pos - 1);
  69.                         m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
  70.                 }
  71.                 //student_name:977
  72.                 pos = student_name.find(':');
  73.                 if (pos != -1) //找到冒号了
  74.                 {
  75.                         key = student_name.substr(0, pos);//student_name
  76.                         value = student_name.substr(pos + 1, student_name.size() - pos - 1);//1
  77.                         m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
  78.                 }
  79.                 //lab_id:4
  80.                 pos = lab_id.find(':');
  81.                 if (pos != -1) //找到冒号了
  82.                 {
  83.                         key = lab_id.substr(0, pos);//lab_id
  84.                         value = lab_id.substr(pos + 1, lab_id.size() - pos - 1);//1
  85.                         m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
  86.                 }
  87.                 //lab_name:QT
  88.                 pos = lab_name.find(':');
  89.                 if (pos != -1) //找到冒号了
  90.                 {
  91.                         key = lab_name.substr(0, pos);//lab_name
  92.                         value = lab_name.substr(pos + 1, lab_name.size() - pos - 1);//1
  93.                         m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
  94.                 }
  95.                 //status:1
  96.                 pos = status.find(':');
  97.                 if (pos != -1) //找到冒号了
  98.                 {
  99.                         key = status.substr(0, pos);
  100.                         value = status.substr(pos + 1, status.size() - pos - 1);//1
  101.                         m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
  102.                 }
  103.                 //将map中的内容存入Appointment对象中
  104.                 this->v_Appointments.insert(std::make_pair(this->appointments_size_, m));
  105.                 this->appointments_size_++;
  106.         }
  107.         ifs.close();
  108.         //测试
  109.         /*
  110.         for (std::map<int, std::map<std::string, std::string>>::iterator it = this->v_Appointments.begin(); it != this->v_Appointments.end(); it++)
  111.         {
  112.                 std::cout << "Number of laboratory reservation records: " << it->first << ", Value is: " << std::endl;
  113.                 for (std::map<std::string, std::string>::iterator it2 = it->second.begin(); it2 != it->second.end(); it2++)
  114.                 {
  115.                         std::cout << it2->first << ":" << it2->second << " ";
  116.                 }
  117.                 std::cout << std::endl;
  118.         }
  119.         */
  120. }
  121. void LabAppointments::updateAppointments() //实验室预约申请状态更新
  122. {
  123.         if (this->appointments_size_ == 0) //一条实验室预约记录都没有
  124.         {
  125.                 return;
  126.         }
  127.         std::ofstream ofs;//写入到文件中
  128.         ofs.open(ORDER_FILE, std::ios::out | std::ios::trunc);//对实验室预约记录文件进行重写覆盖写入
  129.         if (!ofs.is_open())
  130.         {
  131.                 std::cout << "Error: Failed to open file " << ORDER_FILE << std::endl;
  132.                 return;
  133.         }
  134.         for (int i=0;i<this->appointments_size_;i++)
  135.         {        //v_Appointments[i]第i条实验室预约记录,是一个map
  136.                 //v_Appointments[i]["day"]第i条实验室预约记录的日期,通过这个map的key("day")拿到对应的value
  137.                 ofs << "day:" << this->v_Appointments[i]["day"] << " ";
  138.                 ofs << "time_slot:" << this->v_Appointments[i]["time_slot"] << " ";
  139.                 ofs << "student_id:" << this->v_Appointments[i]["student_id"] << " ";
  140.                 ofs << "student_name:" << this->v_Appointments[i]["student_name"] << " ";
  141.                 ofs << "lab_id:" << this->v_Appointments[i]["lab_id"] << " ";
  142.                 ofs << "lab_name:" << this->v_Appointments[i]["lab_name"] << " ";
  143.                 ofs << "status:" << this->v_Appointments[i]["status"] << std::endl;
  144.         }
  145.         ofs.close();
  146. }
复制代码
8,Student.h

  1. #pragma once#include "Identify.h"#include <fstream>#include "globalFile.h"#include "Laboratory.h"#include <vector>#include "LabAppointments.h"class Student:public Identify{public:        Student();        Student(int id,std::string name,std::string pwd);//学号、姓名、暗码        virtual void Identify::selectMenu() override;//重写基类中的虚函数        void applyOrder();//申请预约功能        void showMyOrder();//体现我的预约功能        void showAllOrder();//查看全部预约功能        void cancelOrder();//取消预约功能        int getId()         {                return this->student_id_;        }        void setId(int id)         {                this->student_id_ = id;        }        std::vector<Laboratory> v_Lab;//学生所申请的实验室
  2. private:        int student_id_;};
复制代码
9,Student.cpp

  1. #include "Student.h"Student::Student() {}Student::Student(int id,std::string name,std::string pwd) {        this->setId(id);        this->setName(name);        this->setPwd(pwd);        std::ifstream ifs;//读取实验室信息        ifs.open(LABORATORY_FILE, std::ios::in);        if (!ifs.is_open())        {                std::cout << "Error: can not open file " << LABORATORY_FILE << std::endl;                return;        }                Laboratory lab;        int lab_id;        std::string lab_name;        int lab_max_capacity;        while (ifs >> lab_id && ifs >> lab_name && ifs >> lab_max_capacity)        {                lab.set_lab_id(lab_id);                lab.set_lab_name(lab_name);                lab.set_lab_max_capacity(lab_max_capacity);                this->v_Lab.push_back(lab);        }        ifs.close();}void Student::selectMenu()//功能菜单实现{        std::cout << "Welcome student: " << this->getName() << "login!" << std::endl;        std::cout << "\t\t ----------------------------------\n";        std::cout << "\t\t|          1.applyOrder           |\n";        std::cout << "\t\t|          2.showMyOrder          |\n";        std::cout << "\t\t|          3.showAllOrder         |\n";        std::cout << "\t\t|          4.cancelOrder          |\n";        std::cout << "\t\t|          0 or OtherKey.logout   |\n";        std::cout << "\t\t ----------------------------------\n";        std::cout << "Please select your action: " << std::endl;}void Student::applyOrder() //预约申请实现{        int day;        int time_slot;        int lab_id;        while (true)         {                std::cout << "The lab is open Monday to Friday!" << std::endl;                std::cout << "Please enter the time to request an appointment:" << std::endl;                std::cout << "1, Monday" << std::endl;                std::cout << "2, Tuesday" << std::endl;                std::cout << "3, Wednesday" << std::endl;                std::cout << "4, Thursday" << std::endl;                std::cout << "5, Friday" << std::endl;                                std::cin >> day;                if (day < 1 || day>5)                {                        std::cout << "Day is invalid input!" << std::endl;                        std::cout << "-------------------" << std::endl;                }                else                {                        break;                }        }        while (true)        {                std::cout << "Please enter the time slot for requesting an appointment:" << std::endl;                std::cout << "1, Morning 8:00-12:00" << std::endl;                std::cout << "2, Afternoon 13:00-17:00" << std::endl;                std::cout << "3, Evening 18:00-22:00" << std::endl;                                std::cin >> time_slot;                if (time_slot < 1 || time_slot>3)                {                        std::cout << "Time slot is invalid input!" << std::endl;                        std::cout << "-------------------" << std::endl;                }                else                {                        break;                }        }        while (true)         {                std::cout << "Please select a laboratory:" << std::endl;                for (int i = 0; i < this->v_Lab.size(); i++)                {                        std::cout << i + 1 << ". " << this->v_Lab[i].get_lab_name() << " (Capacity: " << this->v_Lab[i].get_lab_max_capacity() << ")" << std::endl;                }                                std::cin >> lab_id;                if (lab_id < 1 || lab_id>this->v_Lab.size())                {                        std::cout << "Laboratory id is invalid input!" << std::endl;                        std::cout<<"-------------------"<<std::endl;                }                else                {                        break;                }        }        std::cout<<"The appointment application is successful! Wait for the lab teacher to review it."<<std::endl;        std::ofstream ofs;//写入预约信息        ofs.open(ORDER_FILE, std::ios::app);        if (!ofs.is_open())        {                std::cout << "Error: can not open file " << ORDER_FILE << std::endl;                return;        }        ofs << "day:" << day << " ";        ofs<<"time_slot:" << time_slot << " " ;        ofs<<"student_id:" << this->getId() << " " ;        ofs<<"student_name:"<<this->getName()<<" " ;        ofs<<"lab_id:"<<lab_id<<" " ;        ofs<<"lab_name:"<<this->v_Lab[lab_id-1].get_lab_name()<<" " ;        ofs<<"status:"<<1<<std::endl;//1表现稽核中,2表现已通过,3表现已拒绝,4表现已取消        ofs.close();                system("pause");        system("cls");}void Student::showMyOrder() //显示我的预约
  2. {
  3.         int number{0};//实验室申请记录条数
  4.         LabAppointments appointments;//预约信息
  5.         if (appointments.getAppointments() == 0)//如果没有预约信息
  6.         {
  7.                 std::cout << "No LabAppointments Appointment!" << std::endl;
  8.                 system("pause");
  9.                 system("cls");
  10.                 return;
  11.         }
  12.        
  13.         for (int i = 0; i < appointments.getAppointments(); i++)
  14.         {
  15.                 //this->getId()                                                   是一个int类型
  16.                 //appointments.v_Appointments[i]["student_id"]                    是一个string类型
  17.                 //appointments.v_Appointments[i]["student_id"].c_str()            是一个char*类型,是C语言的字符串;.c_str()将string 转 const char*
  18.                 //atoi(appointments.v_Appointments[i]["student_id"].c_str())      是一个int类型,是C语言的整数;      atoi()将const char* 转 int
  19.                 //所以可以比较两个string类型的值
  20.                 if (std::atoi(appointments.v_Appointments[i]["student_id"].c_str()) == this->getId())//如果是自己的预约
  21.                 {
  22.                         number++;
  23.                         std::cout << "Your appointment is the day: " << appointments.v_Appointments[i]["day"];
  24.                         std::cout << "; time slot is: " << (appointments.v_Appointments[i]["time_slot"] == "1"?"Morning":(appointments.v_Appointments[i]["time_slot"] == "2")?"Afternoon":(appointments.v_Appointments[i]["time_slot"] == "3")?"Evening":"Your Appointment Time Slot Is Error.");
  25.                         std::cout << "; lab_id is: " << appointments.v_Appointments[i]["lab_id"];
  26.                         std::cout << "; lab_name is: " << appointments.v_Appointments[i]["lab_name"];
  27.                         std::string status = "; statis: ";//1表示审核中,2表示已通过,3表示已拒绝,4表示已取消
  28.                         if (appointments.v_Appointments[i]["status"] == "1")
  29.                         {
  30.                                 status += "Pending Review";
  31.                         }
  32.                         else if (appointments.v_Appointments[i]["status"] == "2")
  33.                         {
  34.                                 status += "Approved";
  35.                         }
  36.                         else if (appointments.v_Appointments[i]["status"] == "3")
  37.                         {
  38.                                 status += "Rejected";
  39.                         }
  40.                         else if (appointments.v_Appointments[i]["status"] == "4")
  41.                         {
  42.                                 status += "Canceled";
  43.                         }
  44.                         else
  45.                         {
  46.                                 status += "Your Appointment Status Is Error.";
  47.                         }
  48.                         std::cout << " " << status << std::endl;
  49.                 }
  50.         }
  51.         std::cout << "Total " << number << " LabAppointments Appointment!" << std::endl;
  52.         system("pause");
  53.         system("cls");
  54. }
  55. void Student::showAllOrder() //显示所有预约
  56. {
  57.         LabAppointments appointments;
  58.         if (appointments.getAppointments() == 0)
  59.         {
  60.                 std::cout << "No LabAppointments Appointment!" << std::endl;
  61.                 system("pause");
  62.                 system("cls");
  63.                 return;
  64.         }
  65.         for (int i = 0; i < appointments.getAppointments(); i++)
  66.         {
  67.                 std::cout << i + 1 << ".";
  68.                 std::cout << "The day: " << appointments.v_Appointments[i]["day"];
  69.                 std::cout << "; time slot is: " << (appointments.v_Appointments[i]["time_slot"] == "1"?"Morning":(appointments.v_Appointments[i]["time_slot"] == "2")?"Afternoon":(appointments.v_Appointments[i]["time_slot"] == "3")?"Evening":"Your Appointment Time Slot Is Error.");
  70.                 std::cout << "; student_id is: " << appointments.v_Appointments[i]["student_id"];
  71.                 std::cout << "; student_name is: " << appointments.v_Appointments[i]["student_name"];
  72.                 std::cout << "; lab_id is: " << appointments.v_Appointments[i]["lab_id"];
  73.                 std::cout << "; lab_name is: " << appointments.v_Appointments[i]["lab_name"];
  74.                 std::string status = "; statis: ";//1表示审核中,2表示已通过,3表示已拒绝,4表示已取消
  75.                 if (appointments.v_Appointments[i]["status"] == "1")
  76.                 {
  77.                         status += "Pending Review";
  78.                 }
  79.                 else if (appointments.v_Appointments[i]["status"] == "2")
  80.                 {
  81.                         status += "Approved";
  82.                 }
  83.                 else if (appointments.v_Appointments[i]["status"] == "3")
  84.                 {
  85.                         status += "Rejected";
  86.                 }
  87.                 else if (appointments.v_Appointments[i]["status"] == "4")
  88.                 {
  89.                         status += "Canceled";
  90.                 }
  91.                 else
  92.                 {
  93.                         status += "Your Appointment Status Is Error.";
  94.                 }
  95.                 std::cout << " " << status << std::endl;
  96.         }
  97.         system("pause");
  98.         system("cls");
  99. }
  100. void Student::cancelOrder() //取消预约
  101. {
  102.         LabAppointments appointments;
  103.         if (appointments.getAppointments() == 0)
  104.         {
  105.                 std::cout << "No LabAppointments Appointment!" << std::endl;
  106.                 system("pause");
  107.                 system("cls");
  108.                 return;
  109.         }
  110.         std::cout << "Records that are under review or have a successful appointment can be canceled, so please enter the canceled record:" << std::endl;
  111.         std::vector<int> v_index;
  112.         int index = 1;
  113.         for (int i = 0; i < appointments.getAppointments(); i++)
  114.         {
  115.                 if (atoi(appointments.v_Appointments[i]["student_id"].c_str()) == this->getId())//是自己的预约
  116.                 {
  117.                         if (appointments.v_Appointments[i]["status"] == "1" || appointments.v_Appointments[i]["status"] == "2")//审核中或已通过的预约可以取消
  118.                         {
  119.                                 v_index.push_back(i);
  120.                                 std::cout << index << ". ";
  121.                                 std::cout <<" The day: " << appointments.v_Appointments[i]["day"];
  122.                                 std::cout << "; time slot is: " << (appointments.v_Appointments[i]["time_slot"] == "1"?"Morning":(appointments.v_Appointments[i]["time_slot"] == "2")?"Afternoon":(appointments.v_Appointments[i]["time_slot"] == "3")?"Evening":"Your Appointment Time Slot Is Error.");
  123.                                 std::cout << "; lab_id is: " << appointments.v_Appointments[i]["lab_id"];
  124.                                 std::cout << "; lab_name is: " << appointments.v_Appointments[i]["lab_name"];
  125.                                 std::string status = "; statis: ";//1表示审核中,2表示已通过,3表示已拒绝,4表示已取消
  126.                                 if (appointments.v_Appointments[i]["status"] == "1")//审核中
  127.                                 {
  128.                                         status += "Pending Review";
  129.                                 }
  130.                                 else if (appointments.v_Appointments[i]["status"] == "2")//已通过
  131.                                 {
  132.                                         status += "Approved";
  133.                                 }
  134.                                 std::cout << " " << status << std::endl;
  135.                                 index++;
  136.                         }
  137.                 }
  138.         }
  139.         std::cout << "Please enter the index of the canceled record, or enter 0 to return:" << std::endl;
  140.         int select = 0;
  141.         while (true)
  142.         {
  143.                 std::cin >> select;
  144.                 if (select >= 0 && select <= v_index.size())
  145.                 {
  146.                         if (select == 0) //输入0表示取消
  147.                         {
  148.                                 break;
  149.                         }
  150.                         else
  151.                         {
  152.                                 int i = v_index[select - 1];
  153.                                 appointments.v_Appointments[i]["status"] = "4";//将状态设置为已取消
  154.                                 appointments.updateAppointments();
  155.                                 std::cout << "The appointment has been canceled successfully!" << std::endl;
  156.                                 break;
  157.                         }
  158.                 }
  159.                 std::cout << "You have made a mistake, please re-enter." << std::endl;
  160.         }
  161.         system("pause");
  162.         system("cls");
  163. }
复制代码
10,Teacher.h

  1. #pragma once
  2. #include "Identify.h"
  3. #include "LabAppointments.h"
  4. #include <vector>
  5. class Teacher:public Identify
  6. {
  7. public:
  8.         Teacher();
  9.         Teacher(int teacher_id,std::string name,std::string pwd);
  10.         virtual void Identify::selectMenu() override;//功能菜单实现
  11.         void showAllOrder();//显示所有预约
  12.         void validOrder();//审核预约
  13.         int getId()
  14.         {
  15.                 return this->teacher_id_;
  16.         }
  17.         void setId(int id)
  18.         {
  19.                 this->teacher_id_ = id;
  20.         }
  21. private:
  22.         int teacher_id_;//教职工编号
  23. };
复制代码
11,Teacher.cpp

  1. #include "Teacher.h"Teacher::Teacher() {}Teacher::Teacher(int teacher_id,std::string name,std::string pwd)
  2. {
  3.     //初始化实验室教师职工号、姓名、密码
  4.     this->setId(teacher_id);
  5.     this->setName(name);
  6.     this->setPwd(pwd);
  7. }
  8. void Teacher::selectMenu() //功能菜单实现{        std::cout << "Weclome teacher: " << this->getName() << "login!" << std::endl;        std::cout << "\t\t ---------------------------------\n";        std::cout << "\t\t|          1.showAllOrder         |\n";        std::cout << "\t\t|          2.validOrder           |\n";        std::cout << "\t\t|          0 or OtherKey.logout   |\n";        std::cout << "\t\t ---------------------------------\n";        std::cout << "Please select your action: " << std::endl;}void Teacher::showAllOrder()//显示所有预约
  9. {
  10.         LabAppointments appointments;
  11.         if (appointments.getAppointments() == 0)
  12.         {
  13.                 std::cout << "No LabAppointments Appointment!" << std::endl;
  14.                 system("pause");
  15.                 system("cls");
  16.                 return;
  17.         }
  18.         for (int i = 0; i < appointments.getAppointments(); i++)
  19.         {
  20.                 std::cout << i + 1 << ".";
  21.                 std::cout << "The day: " << appointments.v_Appointments[i]["day"];
  22.                 std::cout << "; time slot is: " << (appointments.v_Appointments[i]["time_slot"] == "1" ? "Morning" : (appointments.v_Appointments[i]["time_slot"] == "2") ? "Afternoon" : (appointments.v_Appointments[i]["time_slot"] == "3") ? "Evening" : "Your Appointment Time Slot Is Error.");
  23.                 std::cout << "; student_id is: " << appointments.v_Appointments[i]["student_id"];
  24.                 std::cout << "; student_name is: " << appointments.v_Appointments[i]["student_name"];
  25.                 std::cout << "; lab_id is: " << appointments.v_Appointments[i]["lab_id"];
  26.                 std::cout << "; lab_name is: " << appointments.v_Appointments[i]["lab_name"];
  27.                 std::string status = "; status: ";//1表示审核中,2表示已通过,3表示已拒绝,4表示已取消
  28.                 if (appointments.v_Appointments[i]["status"] == "1")
  29.                 {
  30.                         status += "Pending Review";
  31.                 }
  32.                 else if (appointments.v_Appointments[i]["status"] == "2")
  33.                 {
  34.                         status += "Approved";
  35.                 }
  36.                 else if (appointments.v_Appointments[i]["status"] == "3")
  37.                 {
  38.                         status += "Rejected";
  39.                 }
  40.                 else if (appointments.v_Appointments[i]["status"] == "4")
  41.                 {
  42.                         status += "Canceled";
  43.                 }
  44.                 else
  45.                 {
  46.                         status += "Your Appointment Status Is Error.";
  47.                 }
  48.                 std::cout << " " << status << std::endl;
  49.         }
  50.         system("pause");
  51.         system("cls");
  52. }
  53. void Teacher::validOrder() //审核预约
  54. {
  55.         LabAppointments appointments;
  56.         if (appointments.getAppointments() == 0)
  57.         {
  58.                 std::cout << "No LabAppointments Appointment!" << std::endl;
  59.                 system("pause");
  60.                 system("cls");
  61.                 return;
  62.         }
  63.         std::cout << "The records of laboratory appointments to be reviewed are as follows:" << std::endl;
  64.         std::vector<int> v_valid_order;
  65.         int index = 1;
  66.         for (int i = 0; i < appointments.getAppointments(); i++)
  67.         {
  68.                 if (appointments.v_Appointments[i]["status"] == "1")//1表示审核中
  69.                 {
  70.                         v_valid_order.push_back(i);
  71.                         std::cout << index++ << ".";
  72.                         std::cout << "The day: " << appointments.v_Appointments[i]["day"];
  73.                         std::cout << "; time slot is: " << (appointments.v_Appointments[i]["time_slot"] == "1" ? "Morning" : (appointments.v_Appointments[i]["time_slot"] == "2") ? "Afternoon" : (appointments.v_Appointments[i]["time_slot"] == "3") ? "Evening" : "Your Appointment Time Slot Is Error.");
  74.                         std::cout << "; student_id is: " << appointments.v_Appointments[i]["student_id"];
  75.                         std::cout << "; student_name is: " << appointments.v_Appointments[i]["student_name"];
  76.                         std::cout << "; lab_id is: " << appointments.v_Appointments[i]["lab_id"];
  77.                         std::cout << "; lab_name is: " << appointments.v_Appointments[i]["lab_name"];
  78.                        
  79.                         std::string status = "; status: Pending Revie";//1表示审核中,2表示已通过,3表示已拒绝,4表示已取消
  80.                         std::cout << status << std::endl;
  81.                 }
  82.         }
  83.         //输入审核的实验室预约序号,0表示返回,不想审核了
  84.         std::cout << "Please enter the lab appointment record you want to review, 0 means return" << std::endl;
  85.         int select = 0;//实验室教师选择要审核的预约序号
  86.         int ret = 0;//接收实验室教师给的审核结果,1表示审核通过,2表示审核拒绝
  87.         while (true)
  88.         {
  89.                 std::cin >> select;
  90.                 if (select >= 0 && select <= v_valid_order.size())
  91.                 {
  92.                         if (select == 0)//选择0,表示不想审核了,退出循环即可
  93.                         {
  94.                                 break;
  95.                         }
  96.                         else
  97.                         {
  98.                                 //1表示审核中,2表示已通过,3表示已拒绝,4表示已取消
  99.                                 std::cout << "Please enter the result of the review" << std::endl;
  100.                                 std::cout << "1. Approved" << std::endl;
  101.                                 std::cout << "2. Rejected" << std::endl;
  102.                                 std::cin >> ret;
  103.                                 if (ret == 1)
  104.                                 {
  105.                                         appointments.v_Appointments[v_valid_order[select - 1]]["status"] = "2";
  106.                                 }
  107.                                 else if (ret == 2)
  108.                                 {
  109.                                         appointments.v_Appointments[v_valid_order[select - 1]]["status"] = "3";
  110.                                 }
  111.                                 else
  112.                                 {
  113.                                         std::cout << "Your input is error!" << std::endl;
  114.                                         continue;
  115.                                 }
  116.                                 appointments.updateAppointments();//更新预约信息到文件中
  117.                                 std::cout << "The review result has been saved!" << std::endl;
  118.                                 break;
  119.                         }
  120.                 }
  121.                 std::cout << "You have made a mistake, please re-enter." << std::endl;
  122.         }
  123.         system("pause");
  124.         system("cls");
  125. }
复制代码
12,main.cpp

  1. #include <iostream>
  2. #include "Identify.h"
  3. #include <fstream>
  4. #include "globalFile.h"
  5. #include "Student.h"
  6. #include "Teacher.h"
  7. #include "Admin.h"
  8. //管理员身份菜单界面
  9. void AdminMenu(Identify*& identify_p) //传入父类的指针
  10. {
  11.         //进入菜单界面
  12.         while (true)
  13.         {
  14.                 //调用管理员子菜单
  15.                 identify_p->selectMenu();//这是父类指针,只能调用公共的接口纯虚函数
  16.                 //想要调用子类特有的接口,需要把父类指针转回去,将父类的指针转为子类的指针
  17.                 Admin* admin = (Admin*)identify_p;
  18.                 int userselect = 0;//用户的选择
  19.                 std::cin >> userselect;//接收用户的选择
  20.                 if (userselect == 1) //添加账号
  21.                 {
  22.                         //std::cout << "Your select is add an account" << std::endl;
  23.                         admin->addUser();
  24.                 }
  25.                 else if (userselect == 2)//查看账号
  26.                 {
  27.                         //std::cout << "Your select is view all account" << std::endl;
  28.                         admin->showUser();
  29.                 }
  30.                 else if (userselect == 3) //添加实验室信息
  31.                 {
  32.                         //std::cout << "Your select is add lab information" << std::endl;
  33.                         admin->addLabInfo();
  34.                 }
  35.                 else if (userselect == 4) //显示实验室信息
  36.                 {
  37.                         //std::cout << "Your select is show lab information" << std::endl;
  38.                         admin->showLabInfo();
  39.                 }
  40.                 else if (userselect == 5) //清空预约
  41.                 {
  42.                         //std::cout << "Your select is clear all appointment" << std::endl;
  43.                         admin->cleanRecord();
  44.                 }
  45.                 else//选成其他选项,默认代表注销登录
  46.                 {
  47.                         delete admin;//销毁堆区创建的对象
  48.                         std::cout << "The logout is successful" << std::endl;
  49.                         system("pause");
  50.                         system("cls");
  51.                         return;
  52.                 }
  53.         }
  54. }
  55. //学生身份菜单界面
  56. void StudentMenu(Identify*& identify_p) //传入父类的指针
  57. {
  58.         //进入菜单界面
  59.         while(true)
  60.         {
  61.                 //调用管理员子菜单
  62.                 identify_p->selectMenu();//这是父类指针,只能调用公共的接口纯虚函数
  63.                 //想要调用子类特有的接口,需要把父类指针转回去,将父类的指针转为子类的指针
  64.                 Student* student = (Student*)identify_p;
  65.                 int userselect = 0;//用户的选择
  66.                 std::cin >> userselect;//接收用户的选择
  67.                 if (userselect == 1) //申请实验室预约
  68.                 {
  69.                         //std::cout << "Your select is apply for lab appointment" << std::endl;
  70.                         student->applyOrder();
  71.                 }
  72.                 else if (userselect == 2) //查看自身预约信息
  73.                 {
  74.                         //std::cout << "Your select is view your appointment information" << std::endl;
  75.                         student->showMyOrder();
  76.                 }
  77.                 else if (userselect == 3) //查看所有的实验室预约信息
  78.                 {
  79.                         //std::cout << "Your select is view all appointment information" << std::endl;
  80.                         student->showAllOrder();
  81.                 }
  82.                 else if (userselect == 4) //取消实验室预约
  83.                 {
  84.                         //std::cout << "Your select is cancel lab appointment" << std::endl;
  85.                         student->cancelOrder();
  86.                 }
  87.                 else //选成其他选项,默认代表注销登录
  88.                 {
  89.                         delete student;//销毁堆区创建的对象
  90.                         std::cout << "The logout is successful" << std::endl;
  91.                         system("pause");
  92.                         system("cls");
  93.                         return;
  94.                 }
  95.         }
  96. }
  97. //实验室教师身份菜单页面
  98. void TeacherMenu(Identify*& identify_p) //传入父类的指针
  99. {
  100.         //进入菜单界面
  101.         while (true)
  102.         {
  103.                 //调用管理员子菜单
  104.                 identify_p->selectMenu(); //这是父类指针,只能调用公共的接口纯虚函数
  105.                 Teacher* teacher = (Teacher*)identify_p; //将父类指针转为子类指针
  106.                 int userselect = 0; //用户的选择
  107.                 std::cin >> userselect; //接收用户的选择
  108.                 if (userselect == 1)
  109.                 {
  110.                         //std::cout << "Your select is view all appointment information" << std::endl;
  111.                         teacher->showAllOrder();
  112.                 }
  113.                 else if (userselect == 2)
  114.                 {
  115.                         //std::cout << "Your select is valid appointment information" << std::endl;
  116.                         teacher->validOrder();
  117.                 }
  118.                 else//选成其他选项,默认代表注销登录
  119.                 {
  120.                         delete teacher; //销毁堆区创建的对象
  121.                         std::cout << "The logout is successful" << std::endl;
  122.                         system("pause");
  123.                         system("cls");
  124.                         return;
  125.                 }
  126.         }
  127. }
  128. //全局登录函数
  129. //参数一:操作的文件名称
  130. //参数二:身份(1为学生、2为实验室教师、3为管理员)
  131. void Login(std::string fileName,int type)
  132. {
  133.         //创建父类指针,将来通过多态指向子类对象
  134.         Identify* identify_p = NULL;
  135.         //读取文件
  136.         std::ifstream ifs;
  137.         ifs.open(fileName, std::ios::in);//以读的方式打开文件
  138.         //若文件不存在
  139.         if (!ifs.is_open()) //打开失败
  140.         {
  141.                 std::cout << "The file does not exist" << std::endl;
  142.                 ifs.close();
  143.                 return;
  144.         }
  145.         //存放用户将来输入的信息
  146.         int id = 0;//存储学号或职工号
  147.         std::string name;//学生或实验室教师的姓名
  148.         std::string pwd;//学生或实验室教师的密码
  149.         if (type == 1)
  150.         {
  151.                 std::cout << "Please input your student_id:" << std::endl;
  152.                 std::cin >> id;
  153.         }
  154.         else if (type == 2)
  155.         {
  156.                 std::cout << "Please input your teacher_id:" << std::endl;
  157.                 std::cin >> id;
  158.         }
  159.         std::cout << "Please input your name:" << std::endl;
  160.         std::cin >> name;
  161.         std::cout << "Please input your password:" << std::endl;
  162.         std::cin >> pwd;
  163.         if (type == 1)
  164.         {
  165.                 //student login
  166.                 int f_id;//文件读取得到的id
  167.                 std::string f_name;//从文件中获取得到的name
  168.                 std::string f_pwd; //从文件中获取得到的pwd
  169.                 while (ifs >> f_id && ifs >> f_name && ifs >> f_pwd) //文件按行读取,遇到空格存一下,每行数据信息拆成三个
  170.                 {
  171.                         //文件读取得到的信息与用户输入的信息进行对比
  172.                         if (f_id == id && f_name == name && f_pwd == pwd) //学号、姓名、密码均输入成功
  173.                         {
  174.                                 std::cout << "Student verification login is successful!" << std::endl;
  175.                                 system("pause");
  176.                                 system("cls");
  177.                                 identify_p = new Student(id, name, pwd);//父类指针指向子类对象,创建学生实例
  178.                                
  179.                                 //进入学生身份的子菜单
  180.                                 StudentMenu(identify_p);
  181.                                 return;
  182.                         }
  183.                 }
  184.         }
  185.         else if (type == 2)
  186.         {
  187.                 //teacher login
  188.                 int f_id;//文件读取得到的id
  189.                 std::string f_name;//从文件中获取得到的name
  190.                 std::string f_pwd; //从文件中获取得到的pwd
  191.                 while (ifs >> f_id && ifs >> f_name && ifs >> f_pwd) //文件按行读取,遇到空格存一下,每行数据信息拆成三个
  192.                 {
  193.                         //文件读取得到的信息与用户输入的信息进行对比
  194.                         if (f_id == id && f_name == name && f_pwd == pwd)//教职工编号、姓名、密码输入成功
  195.                         {
  196.                                 std::cout << "Teacher verification login is successful!" << std::endl;
  197.                                 system("pause");
  198.                                 system("cls");
  199.                                 identify_p = new Teacher(id, name, pwd);//父类指针指向子类对象,创建教职工实例
  200.                                 //进入实验室教师身份的子菜单
  201.                                 TeacherMenu(identify_p);
  202.                                 return;
  203.                         }
  204.                 }
  205.         }
  206.         else if (type == 3)
  207.         {
  208.                 //admin login
  209.                 std::string f_name;//从文件中获取得到的name
  210.                 std::string f_pwd;//从文件中获取得到的pwd
  211.                 while (ifs >> f_name && ifs >> f_pwd) //文件按行读取,遇到空格存一下,每行数据信息拆成三个
  212.                 {
  213.                         //文件读取得到的信息与用户输入的信息进行对比
  214.                         if (f_name == name && f_pwd == pwd) //管理员登录只需要姓名和密码
  215.                         {
  216.                                 std::cout << "Admin verification login is successful!" << std::endl;
  217.                                 system("pause");
  218.                                 system("cls");       
  219.                                 identify_p = new Admin(name, pwd);//父类指针指向子类对象,创建管理员实例
  220.                                 //进入管理员身份的子菜单
  221.                                 AdminMenu(identify_p);
  222.                                 return;
  223.                         }
  224.                 }
  225.         }
  226.        
  227.         std::cout << "Validation failed !" << std::endl;
  228.         system("pause");
  229.         system("cls");
  230.         return;
  231. }
  232. int main() {
  233.         int select = 0;//存放用户的选择
  234.         while (true)
  235.         {
  236.                 std::cout << "======================  Welcome to the Laboratory Management System  =====================" << std::endl;
  237.                 std::cout << "\t\t -------------------------------\n";
  238.                 std::cout << "\t\t|          1.Student            |\n";
  239.                 std::cout << "\t\t|          2.Teacher            |\n";
  240.                 std::cout << "\t\t|          3.Admin              |\n";
  241.                 std::cout << "\t\t|          0.Exit               |\n";
  242.                 std::cout << "\t\t -------------------------------\n";
  243.                 std::cout << std::endl << "Please enter your identity: " << std::endl;
  244.                 std::cin >> select; //接受用户选择
  245.                 switch (select)
  246.                 {
  247.                 case 1:  //学生身份
  248.                         Login(STUDENT_FILE,1);
  249.                         break;
  250.                 case 2:  //老师身份
  251.                         Login(TEACHER_FILE, 2);
  252.                         break;
  253.                 case 3:  //管理员身份
  254.                         Login(ADMIN_FILE, 3);
  255.                         break;
  256.                 case 0:  //退出系统
  257.                         std::cout << "Welcome to you next time!" << std::endl;
  258.                         system("pause");
  259.                         return 0;
  260.                         break;
  261.                 default:
  262.                         std::cout << "You have entered the wrong information, please make a new selection!" << std::endl;
  263.                         system("pause");
  264.                         system("cls");
  265.                         break;
  266.                 }
  267.         }
  268.         system("pause");
  269.         return 0;
  270. }
复制代码
五、项目下载链接

实验室预约综合管理系统

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。

本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有账号?立即注册

x
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

玛卡巴卡的卡巴卡玛

论坛元老
这个人很懒什么都没写!
快速回复 返回顶部 返回列表