项目干货满满,内容有点过多,看起来可能会有点卡。系统提示读完凌驾2小时,发起多篇发布,我以为分篇就不完整了,失去了这个项目标魂魄
一、需求分析
高校实验室预约管理系统包罗三种差别身份:管理员、实验室西席、学生
管理员:给学生和实验室西席创建账号并分发
实验室西席:稽核学生的预约申请
学生:申请利用实验室
高校实验室包罗:超景深实验室(可容纳10人)、大数据实验室(可容纳20人)、物联网实验室(可容纳30人)等,管理员可以添加实验室
学生可以预约将来一周内的实验室,必要选择周一至周五的上午、下午或晚上时间段
实验室西席对学生的预约申请进行稽核,稽核分为通过或不通过
管理员负责每周对学生申请的实验室预约清单进行清空
二、功能详情
1,主页面登录系统选择:学生、实验室西席、管理员、退出
2,学生:输入学号、姓名和登录暗码
实验室西席:输入职工号、姓名和登录暗码
管理员:输入姓名和登录暗码
3,学生功能
申请预约:预约实验室
查看自身的预约:查看自身申请的预约状态
查看全部预约:取消自身的预约,预约成功或稽核中的预约都可以随时取消
退出登录
4,实验室西席功能
查看全部学生预约环境:查看全部的实验室预约信息及预约状态
稽核学生的预约:对学生申请的实验室预约进行稽核
退出登录
5,管理员功能
创建账号:为学生或实验室西席创建账号,必要检测学生学号和西席职工号是否重复
查看账号:查看学生或实验室西席的全部信息
添加实验室:添加实验室的信息,包罗实验室的id、名称和最大容量
查看实验室:查看全部实验室的信息
清空预约:清空全部的预约记录
退出登录
三、项目创建
1,新建C++空项目
2,创建主菜单及退出功能的实现
创建项目入口,即主页面main.cpp
通过switch case分支进行差别身份的登录,
退出功能没啥好说的,用户选择0之后直接return 0;即可
- #include <iostream>
- int main() {
- int select = 0;//存放用户的选择
- while (true)
- {
- std::cout << "====================== Welcome to the Laboratory Management System =====================" << std::endl;
- std::cout << "\t\t -------------------------------\n";
- std::cout << "\t\t| 1.Student |\n";
- std::cout << "\t\t| 2.Teacher |\n";
- std::cout << "\t\t| 3.Admin |\n";
- std::cout << "\t\t| 0.Exit |\n";
- std::cout << "\t\t -------------------------------\n";
- std::cout << std::endl << "Please enter your identity: " << std::endl;
- std::cin >> select; //接受用户选择
- switch (select)
- {
- case 1: //学生身份
- break;
- case 2: //老师身份
- break;
- case 3: //管理员身份
- break;
- case 0: //退出系统
- std::cout << "Welcome to you next time!" << std::endl;
- system("pause");
- return 0;
- break;
- default:
- std::cout << "You have entered the wrong information, please make a new selection!" << std::endl;
- system("pause");
- system("cls");
- break;
- }
- }
- system("pause");
- return 0;
- }
复制代码 3,身份类创建——搭建框架
无论Student、Teacher还是Admin,都有相通的共性(姓名和暗码),抽取出来整合成一个基类
继承这个基类,差别的子类根据自身的特性再新增属性功能即可
①身份类基类——Identify
经过功能详情的分析,三种差别身份的共性是:name和pwd
因为差别身份登录成功必要进入到差别的页面,再基类中通过纯虚函数定义一个接口,子类根据自身环境实现即可
基类只必要声明,不必要实现
身份类基类:Identify.h
- #pragma once
- #include <iostream>
- class Identify
- {
- public:
- virtual void selectMenu() = 0;//纯虚函数,不同用户登录成功之后显示的菜单均不同
-
- std::string getName()
- {
- return this->name_;
- }
- std::string getPwd()
- {
- return this->pwd_;
- }
- void setName(std::string name)
- {
- this->name_ = name;
- }
- void setPwd(std::string pwd)
- {
- this->pwd_ = pwd;
- }
- private:
- std::string name_;//姓名
- std::string pwd_;//密码
- };
复制代码 ②Student类
学生具有的功能:申请预约、体现我的预约、查看全部预约和取消预约
Student.h
- #pragma once
- #include "Identify.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;
- }
- private:
- int student_id_;
- };
复制代码 Student.cpp
- #include "Student.h"
- Student::Student() {}
- Student::Student(int id,std::string name,std::string pwd) {}
- void Student::selectMenu() {}//功能菜单实现
- void Student::applyOrder() {}//预约申请实现
- void Student::showMyOrder() {}//显示我的预约
- void Student::showAllOrder() {}//显示所有预约
- void Student::cancelOrder() {}//取消预约
复制代码 ③Teacher类
西席具有的功能:体现全部预约和稽核预约
Teacher.h
- #pragma once
- #include "Identify.h"
- class Teacher:public Identify
- {
- public:
- Teacher();
- Teacher(int teacher_id,std::string name,std::string pwd);
- virtual void Identify::selectMenu() override;//功能菜单实现
- void showAllOrder();//显示所有预约
- void validOrder();//审核预约
- int getId()
- {
- return this->teaher_id_;
- }
- void setId(int id)
- {
- this->teaher_id_ = id;
- }
- private:
- int teaher_id_;//教职工编号
- };
复制代码 Teacher.cpp
- #include "Teacher.h"
- Teacher::Teacher() {}
- Teacher::Teacher(int teacher_id,std::string name,std::string pwd) {}
- void Teacher::selectMenu() {}//功能菜单实现
- void Teacher::showAllOrder() {}//显示所有预约
- void Teacher::validOrder() {}//审核预约
复制代码 ④Admin类
管理员具有的功能:添加用户(包罗学生和教职工)、查看用户、看实验室信息和清空预约记录
Admin.h
- #pragma once
- #include <iostream>
- #include "Identify.h"
- class Admin:public Identify
- {
- public:
- Admin();
- Admin(std::string name,std::string pwd);
- virtual void Identify::selectMenu() override;//功能菜单实现
- void addUser();//添加用户
- void showUser();//查看用户
- void showLabInfo();//查看实验室信息
- void cleanRecord();//清空预约记录
- };
复制代码 Admin.cpp
- #include "Admin.h"
- Admin::Admin() {}
- Admin::Admin(std::string name, std::string pwd) {}
- void Admin::selectMenu(){}//功能菜单实现
- void Admin::addUser() {}//添加用户功能实现
- void Admin::showUser() {}//查看用户功能实现
- void Admin::showLabInfo() {}//查看实验室信息功能实现
- void Admin::cleanRecord() {}//清空预约记录功能实现
复制代码 4,登录模块
①全局文件
将差别身份的账号暗码等信息存放到文件中,将全部的文件名都定义到全局文件中
通过宏定义进行关联文件,方便后续的操作
globalFile.h
- #pragma once
- //管理员文件
- #define ADMIN_FILE "admin.txt"
- //学生文件
- #define STUDENT_FILE "student.txt"
- //教师文件
- #define TEACHER_FILE "teacher.txt"
- //实验室信息文件
- #define LABORATORY_FILE "laboratoryRoom.txt"
- //订单文件
- #define ORDER_FILE "order.txt"
复制代码 同级路径下创建对应的txt文本文件
②登录函数封装
在main.cpp中添加一个全局函数Login,根据用户的选择进行差别的身份登录操作
void Login(string fileName, int type)
参数一:操作的文件名称
参数二:身份(1为学生、2为实验室西席、3为管理员)
main.cpp
③学生登录实现
在student.txt中添加几条记录信息进行测试

修改完善主函数main中的全局登录函数Login
main.cpp
- //全局登录函数
- //参数一:操作的文件名称
- //参数二:身份(1为学生、2为实验室教师、3为管理员)
- void Login(std::string fileName,int type)
- {
- //创建父类指针,将来通过多态指向子类对象
- Identify* identify_p = NULL;
- //读取文件
- std::ifstream ifs;
- ifs.open(fileName, std::ios::in);//以读的方式打开文件
- //若文件不存在
- if (!ifs.is_open()) //打开失败
- {
- std::cout << "The file does not exist" << std::endl;
- ifs.close();
- return;
- }
- //存放用户将来输入的信息
- int id = 0;//存储学号或职工号
- std::string name;//学生或实验室教师的姓名
- std::string pwd;//学生或实验室教师的密码
- if (type == 1)
- {
- std::cout << "Please input your student_id:" << std::endl;
- std::cin >> id;
- }
- else if (type == 2)
- {
- std::cout << "Please input your teacher_id:" << std::endl;
- std::cin >> id;
- }
- std::cout << "Please input your name:" << std::endl;
- std::cin >> name;
- std::cout << "Please input your password:" << std::endl;
- std::cin >> pwd;
- if (type == 1)
- {
- //student login
- int f_id;//文件读取得到的id
- std::string f_name;//从文件中获取得到的name
- std::string f_pwd; //从文件中获取得到的pwd
- while (ifs >> f_id && ifs >> f_name && ifs >> f_pwd) //文件按行读取,遇到空格存一下,每行数据信息拆成三个
- {
- //文件读取得到的信息与用户输入的信息进行对比
- if (f_id == id && f_name == name && f_pwd == pwd) //学号、姓名、密码均输入成功
- {
- std::cout << "Student verification login is successful!" << std::endl;
- system("pause");
- system("cls");
- identify_p = new Student(id, name, pwd);//父类指针指向子类对象,创建学生实例
-
- //进入学生身份的子菜单 todo
- return;
- }
- }
- }
- else if (type == 2)
- {
- //teacher login
- }
- else if (type == 3)
- {
- //admin login
- }
-
- std::cout << "Validation failed !" << std::endl;
- system("pause");
- system("cls");
- return;
- }
复制代码 测试效果:


④实验室西席登录实现
在teacher.txt中添加几条记录信息进行测试

修改完善main.cpp中的全局登录函数Login
main.cpp
- //全局登录函数
- //参数一:操作的文件名称
- //参数二:身份(1为学生、2为实验室教师、3为管理员)
- void Login(std::string fileName,int type)
- {
- //创建父类指针,将来通过多态指向子类对象
- Identify* identify_p = NULL;
- //读取文件
- std::ifstream ifs;
- ifs.open(fileName, std::ios::in);//以读的方式打开文件
- //若文件不存在
- if (!ifs.is_open()) //打开失败
- {
- std::cout << "The file does not exist" << std::endl;
- ifs.close();
- return;
- }
- //存放用户将来输入的信息
- int id = 0;//存储学号或职工号
- std::string name;//学生或实验室教师的姓名
- std::string pwd;//学生或实验室教师的密码
- if (type == 1)
- {
- std::cout << "Please input your student_id:" << std::endl;
- std::cin >> id;
- }
- else if (type == 2)
- {
- std::cout << "Please input your teacher_id:" << std::endl;
- std::cin >> id;
- }
- std::cout << "Please input your name:" << std::endl;
- std::cin >> name;
- std::cout << "Please input your password:" << std::endl;
- std::cin >> pwd;
- if (type == 1)
- {
- //student login
- int f_id;//文件读取得到的id
- std::string f_name;//从文件中获取得到的name
- std::string f_pwd; //从文件中获取得到的pwd
- while (ifs >> f_id && ifs >> f_name && ifs >> f_pwd) //文件按行读取,遇到空格存一下,每行数据信息拆成三个
- {
- //文件读取得到的信息与用户输入的信息进行对比
- if (f_id == id && f_name == name && f_pwd == pwd) //学号、姓名、密码均输入成功
- {
- std::cout << "Student verification login is successful!" << std::endl;
- system("pause");
- system("cls");
- identify_p = new Student(id, name, pwd);//父类指针指向子类对象,创建学生实例
-
- //进入学生身份的子菜单 todo
- return;
- }
- }
- }
- else if (type == 2)
- {
- //teacher login
- int f_id;//文件读取得到的id
- std::string f_name;//从文件中获取得到的name
- std::string f_pwd; //从文件中获取得到的pwd
- while (ifs >> f_id && ifs >> f_name && ifs >> f_pwd) //文件按行读取,遇到空格存一下,每行数据信息拆成三个
- {
- //文件读取得到的信息与用户输入的信息进行对比
- if (f_id == id && f_name == name && f_pwd == pwd)//教职工编号、姓名、密码输入成功
- {
- std::cout << "Teacher verification login is successful!" << std::endl;
- system("pause");
- system("cls");
- identify_p = new Teacher(id, name, pwd);//父类指针指向子类对象,创建教职工实例
- //进入实验室教师身份的子菜单 todo
- return;
- }
- }
- }
- else if (type == 3)
- {
- //admin login
- }
-
- std::cout << "Validation failed !" << std::endl;
- system("pause");
- system("cls");
- return;
- }
复制代码 测试效果:


⑤管理员登录实现
在admin.txt中添加几条记录信息进行测试
修改完善main.cpp中的全局登录函数Login
main.cpp
测试效果:


5,管理员模块
实验室西席和学生的账号信息是由管理员进行创建的,故起首得搞定管理员
管理员功能:登录和注销
起首,有参构造记录一下管理员信息、
其次,进入到管理员身份对应的子菜单页面中,管理员的功能包罗添加账号、查看账号、查看实验室、清空预约、注销登录
末了,在main.cpp中添加全局函数adminMenu()进行体现管理员菜单,根据用户选择的功能进行对应的函数实现
①登录和注销功能——AdminMenu()
Admin.h头文件定义的函数不必要改变
Admin.cpp实现头文件中所定义的一些函数,函数selectMenu()体现功能菜单的实现
Admin.cpp
- #include "Admin.h"
- Admin::Admin() {}
- Admin::Admin(std::string name, std::string pwd)
- {
- //初始化管理员信息
- Admin::setName(name);
- Admin::setPwd(pwd);
- }
- 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() {}//添加用户功能实现
- void Admin::showUser() {}//查看用户功能实现
- void Admin::showLabInfo() {}//查看实验室信息功能实现
- void Admin::cleanRecord() {}//清空预约记录功能实现
复制代码 定义全局函数AdminMenu()用于体现管理员身份菜单界面
在Login()登录全局函数中进行调用即可
main.cpp
测试效果:

②添加用户功能——addUser()
为学生和实验室西席添加账号,要求添加账号的时候学号和职工号不能够重复
对接口addUser()进行实现
Admin.cpp
- 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;
- system("pause");//暂停程序
- system("cls");//清屏
- }
复制代码 测试效果:


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

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

去重操作
添加新账号的时候,要制止学号\职工号的重复
解决思路:
起首,在Admin.h中,把学生和实验室西席账号文件信息全部读到各自的容器中,两个容器(std::vector v_Student和vector v_Teacher)分别存放实验室西席和学生全部账号信息
然后,在Admin.h中,添加一个成员函数void init_Stu_Tea_Vector()用于对学生和实验室西席信息的容器(v_Student和 v_Teacher)进行初始化也就是将对应的文本文件数据对应读取到这两个容器中
再次,在Admin.cpp中,对成员函数void init_Stu_Tea_Vector()进行实现,并在构造函数中对两个容器初始化(this->initVector())
其次,在Admin.h中,添加成员函数bool checkRepeat(int id, int type); id表现职工号\学号、type表现类型,1为学生,2为实验室西席用于检查两个容器(v_Student和 v_Teacher)中是否已存在管理员当前注册时输入的学号\职工号(id)信息,存在为true,不存在为false
末了,每次添加完新用户之后,必要重新初始化容器(即将新的账号文件全部重新读取到容器中),并在每次调用添加用户的函数(void Admin::addUser)的末了进行初始化(this->init_Stu_Tea_Vector())即可
Admin.h
- void init_Stu_Tea_Vector();//初始化容器,用于将对应的文本文件信息读取到对应的容器中
- std::vector<Student> v_Student;//学生容器
- std::vector<Teacher> v_Teacher;//教师容器
-
- bool checkRepeate(int id, int type);//检查学号\职工号是否重复
复制代码 Admin.cpp
③体现账号功能——showUser()
体现账号功能函数(showUser())的实现
接收用户的选择,根据用户的差别选择进行遍历添加用户功能中所定义的分别存放实验室西席和学生全部账号信息的两个容器(std::vector v_Student和vector v_Teacher),输出即可
admin.cpp
- 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);
- 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);
- 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");//清屏
- }
- }
- }
复制代码 ④添加实验室信息功能——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
- #pragma once
- #include <iostream>
- //实验室类
- class Laboratory
- {
- public:
- Laboratory() {};
- Laboratory(int id, std::string name, int capacity):lab_id_(id), lab_name_(name), lab_max_capacity_(capacity) {}
- void set_lab_id(int id) { this->lab_id_ = id; }
- void set_lab_name(std::string name) { this->lab_name_ = name; }
- void set_lab_max_capacity(int capacity) { this->lab_max_capacity_ = capacity; }
- int get_lab_id() const { return this->lab_id_; }
- std::string get_lab_name() const { return this->lab_name_; }
- int get_lab_max_capacity() const { return this->lab_max_capacity_; }
-
- private:
- int lab_id_; //实验室编号
- std::string lab_name_; //实验室名称
- int lab_max_capacity_; //实验室容量
- };
复制代码 Admin.h
- void init_Lab_Vector();//初始化实验室容器,用于将实验室信息读取到容器中
- std::vector<Laboratory> v_Lab;//实验室容器
复制代码 Admin.cpp
- void Admin::addLabInfo() //添加实验室信息功能实现
- {
- int lab_id;
- std::string lab_name;
- int lab_max_capacity;
- std::ofstream ofs;//文件操作对象
- ofs.open(LABORATORY_FILE, std::ios::app);//以追加的方式写入文件
- if (!ofs.is_open())//文件打开失败
- {
- std::cout << "Failed to open file: " << LABORATORY_FILE << std::endl;
- return;
- }
- while (true)
- {
- std::cout << "Please input laboratory ID: " << std::endl;
- std::cin >> lab_id;
- bool repeat = this->checkRepeate(lab_id, 3);//检查是否有重复的实验室
- if (repeat)
- {
- std::cout << "Laboratory ID already exists, please input again: " << std::endl;
- }
- else
- {
- break;
- }
- }
- std::cout << "Please input laboratory name: " << std::endl;
- std::cin >> lab_name;
- std::cout << "Please input laboratory max capacity: " << std::endl;
- std::cin >> lab_max_capacity;
- ofs << lab_id << " " << lab_name << " " << lab_max_capacity << " " << std::endl;
- ofs.close();
- std::cout << "Add laboratory information successfully!" << std::endl;
- this->init_Lab_Vector();
- system("pause");//暂停程序
- system("cls");//清屏
- }
复制代码 ⑤体实际验室信息功能——showLabInfo()
对实验室容器(std::vector<Laboratory> v_Lab)进行遍历输出一下即可
Admin.cpp
- void printLab(Laboratory& lab)
- {
- std::cout << "Laboratory ID: " << lab.get_lab_id() << ", Name: " << lab.get_lab_name() << ", Max capacity: " << lab.get_lab_max_capacity() << std::endl;
- }
- void Admin::showLabInfo() //查看实验室信息功能实现
- {
- std::cout << "Laboratory information:" << std::endl;
- std::for_each(v_Lab.begin(), v_Lab.end(), printLab);
- /* 和for_each的用法一样,二选一即可
- for (std::vector<Laboratory>::iterator it = v_Lab.begin(); it != v_Lab.end(); it++)
- {
- std::cout << "Laboratory ID: " << it->get_lab_id() << ", Name: " << it->get_lab_name() << ", Max capacity: " << it->get_lab_max_capacity() << std::endl;
- }
- */
- system("pause");//暂停程序
- system("cls");//清屏
- }
复制代码 ⑥清空预约记录功能——cleanRecord()
以trunc方式读取打开文件即可,trunc方式可以将文件清空重新追加
用户输入Y/y表现确认删除,输入N/n表现去掉操作
Admin.cpp
- 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,学生模块
①登录和注销功能——studentMenu()
在Student类的有参构造函数中进行学生信息的初始化操作
Student.cpp
- Student::Student(int id,std::string name,std::string pwd)
- {
- this->setId(id);
- this->setName(name);
- this->setPwd(pwd);
- }
复制代码 在main.cpp中定义全局函数studentMenu(),学生登录成功之后进入页面,进行相应的功能的选择
在main.cpp中的用户登录函数Login()中,用户登录成功进入到学生功能页面(调用StudentMenu()函数即可)
main.cpp
- //学生身份菜单界面
- void StudentMenu(Identify*& identify_p) //传入父类的指针
- {
- //进入菜单界面
- while(true)
- {
- //调用管理员子菜单
- identify_p->selectMenu();//这是父类指针,只能调用公共的接口纯虚函数
- //想要调用子类特有的接口,需要把父类指针转回去,将父类的指针转为子类的指针
- Student* student = (Student*)identify_p;
- int userselect = 0;//用户的选择
- std::cin >> userselect;//接收用户的选择
- if (userselect == 1) //申请实验室预约
- {
- std::cout << "Your select is apply for lab appointment" << std::endl;
- student->applyOrder();
- }
- else if (userselect == 2) //查看自身预约信息
- {
- std::cout << "Your select is view your appointment information" << std::endl;
- student->showMyOrder();
- }
- else if (userselect == 3) //查看所有的实验室预约信息
- {
- std::cout << "Your select is view all appointment information" << std::endl;
- student->showAllOrder();
- }
- else if (userselect == 4) //取消实验室预约
- {
- std::cout << "Your select is cancel lab appointment" << std::endl;
- student->cancelOrder();
- }
- else //选成其他选项,默认代表注销登录
- {
- delete student;//销毁堆区创建的对象
- std::cout << "The logout is successful" << std::endl;
- system("pause");
- system("cls");
- return;
- }
- }
- }
复制代码 定义全局函数StudentMenu()用于体现学生身份菜单界面
在Login()登录全局函数中进行调用即可

②申请实验室预约功能——applyOrder()
学生申请实验室预约必要的流程:选择日期(周一至周五)、选择时间段(上午、下午)、选择实验室、生成预约记录
在选择实验室的时候,必要让学生知道都有哪些实验室可以预约
在Student.h中添加实验室容器std::vector<Laboratory> v_Lab,并且在有参构造中读取实验室信息文件并放到实验室容器v_Lab中,就可以申请实验室预约了
Student.h
- std::vector<Laboratory> v_Lab;//学生所申请的实验室
复制代码 Student.cpp
- #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 << "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 << "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 << "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() {}//显示我的预约
- void Student::showAllOrder() {}//显示所有预约
- void Student::cancelOrder() {}//取消预约
复制代码 ③体现我的预约功能——showMyOrder()
起首创建实验室预约文件LabAppointments.h和LabAppointments.cpp
实验室预约文件类LabAppointments中含有更新预约记录成员函数updateAppointments(),记录全部实验室预约记录的容器map<int, std::map<std::string, std::string>> v_Appointments,统计实验室预约记录条数appointments_size_
LabAppointments.h
- #pragma once
- #include <map>
- #include <iostream>
- #include <fstream>
- #include "globalFile.h"
- #include <map>
- class LabAppointments
- {
- public:
- LabAppointments();
- void updateAppointments();//更新预约记录信息
- std::map<int,std::map<std::string, std::string>> v_Appointments;//记录所有预约记录信息,key为预约记录的总条数,value为对应预约记录信息
- private:
- int appointments_size_;//预约记录的总条数
- };
复制代码 LabAppointments.cpp
- #include "LabAppointments.h"
- LabAppointments::LabAppointments()
- {
- std::ifstream ifs;
- ifs.open(ORDER_FILE,std::ios::in);
- if(!ifs.is_open())
- {
- std::cout << "Error: Failed to open file " << ORDER_FILE << std::endl;
- return;
- }
- //为了解析方便,这里都使用string类型存储
- std::string day;//1-5 represent Monday to Friday
- std::string time_slot;//1-3 represent 8:00-12:00,13:00-17:00,18:00-22:00
- std::string student_id;//预约实验室申请的学生编号
- std::string student_name;//预约实验室申请的学生姓名
- std::string lab_id;//实验室编号
- std::string lab_name;//实验室名称
- std::string status;//预约申请的状态,1表示待审核状态
-
- this->appointments_size_ = 0;//预约申请的数量
- while (ifs >> day && ifs >> time_slot && ifs >> student_id && ifs >> student_name && ifs >> lab_id && ifs >> lab_name && ifs >> status)
- {
- /*
- std::cout << day << std::endl;
- std::cout << time_slot << std::endl;
- std::cout << student_id << std::endl;
- std::cout << student_name << std::endl;
- std::cout << lab_id << std::endl;
- std::cout << lab_name << std::endl;
- std::cout << status << std::endl;
- std::cout << std::endl;
- */
- /*解析内容如下:
- day:1
- time_slot:1
- student_id:1
- student_name:977
- lab_id:4
- lab_name:QT
- status:1
- */
- std::string key;
- std::string value;
- std::map<std::string, std::string> m;//存放key和value
-
- //day:1
- int pos = day.find(':');//pos=3
- if (pos != -1) //找到冒号了
- {
- key = day.substr(0, pos);//day
- value = day.substr(pos + 1, day.size() - pos - 1);//1
- m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
- }
-
- //time_slot:1
- pos = time_slot.find(':');
- if (pos != -1) //找到冒号了
- {
- key = time_slot.substr(0, pos);//time_slot
- value = time_slot.substr(pos + 1, time_slot.size() - pos - 1);
- m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
- }
- //student_id:1
- pos = student_id.find(':');
- if (pos != -1) //找到冒号了
- {
- key = student_id.substr(0, pos);//student_id
- value = student_id.substr(pos + 1, student_id.size() - pos - 1);
- m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
- }
- //student_name:977
- pos = student_name.find(':');
- if (pos != -1) //找到冒号了
- {
- key = student_name.substr(0, pos);//student_name
- value = student_name.substr(pos + 1, student_name.size() - pos - 1);//1
- m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
- }
- //lab_id:4
- pos = lab_id.find(':');
- if (pos != -1) //找到冒号了
- {
- key = lab_id.substr(0, pos);//lab_id
- value = lab_id.substr(pos + 1, lab_id.size() - pos - 1);//1
- m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
- }
- //lab_name:QT
- pos = lab_name.find(':');
- if (pos != -1) //找到冒号了
- {
- key = lab_name.substr(0, pos);//lab_name
- value = lab_name.substr(pos + 1, lab_name.size() - pos - 1);//1
- m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
- }
- //status:1
- pos = status.find(':');
- if (pos != -1) //找到冒号了
- {
- key = status.substr(0, pos);
- value = status.substr(pos + 1, status.size() - pos - 1);//1
- m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
- }
- //将map中的内容存入Appointment对象中
- this->v_Appointments.insert(std::make_pair(this->appointments_size_, m));
- this->appointments_size_++;
- }
- ifs.close();
- //测试
- /*
- for (std::map<int, std::map<std::string, std::string>>::iterator it = this->v_Appointments.begin(); it != this->v_Appointments.end(); it++)
- {
- std::cout << "Number of laboratory reservation records: " << it->first << ", Value is: " << std::endl;
- for (std::map<std::string, std::string>::iterator it2 = it->second.begin(); it2 != it->second.end(); it2++)
- {
- std::cout << it2->first << ":" << it2->second << " ";
- }
- std::cout << std::endl;
- }
- */
- }
- void LabAppointments::updateAppointments() //实验室预约申请状态更新
- {
- if (this->appointments_size_ == 0) //一条实验室预约记录都没有
- {
- return;
- }
- std::ofstream ofs;//写入到文件中
- ofs.open(ORDER_FILE, std::ios::out | std::ios::trunc);//对实验室预约记录文件进行重写覆盖写入
- if (!ofs.is_open())
- {
- std::cout << "Error: Failed to open file " << ORDER_FILE << std::endl;
- return;
- }
- for (int i=0;i<this->appointments_size_;i++)
- { //v_Appointments[i]第i条实验室预约记录,是一个map
- //v_Appointments[i]["day"]第i条实验室预约记录的日期,通过这个map的key("day")拿到对应的value
- ofs << "day:" << this->v_Appointments[i]["day"] << " ";
- ofs << "time_slot:" << this->v_Appointments[i]["time_slot"] << " ";
- ofs << "student_id:" << this->v_Appointments[i]["student_id"] << " ";
- ofs << "student_name:" << this->v_Appointments[i]["student_name"] << " ";
- ofs << "lab_id:" << this->v_Appointments[i]["lab_id"] << " ";
- ofs << "lab_name:" << this->v_Appointments[i]["lab_name"] << " ";
- ofs << "status:" << this->v_Appointments[i]["status"] << std::endl;
- }
- ofs.close();
- }
复制代码 接下来开始实现体现我的预约操作
实验室预约记录order.txt中有三条实验室预约记录
先创建一个LabAppointments对象,看下它有几条预约记录(appointments_size_),若为0,则没有实验室申请预约记录;若有记录,则遍历全部的实验室预约记录,然后以学号(student_id_)为判断记录进行体现即可
体现内容:哪一天、时间段、实验室编号、实验室名称、状态,不必要体现学号和学生姓名
Student.cpp
- void Student::showMyOrder() //显示我的预约
- {
- int number{0};//实验室申请记录条数
- LabAppointments appointments;//预约信息
- if (appointments.getAppointments() == 0)//如果没有预约信息
- {
- std::cout << "No LabAppointments Appointment!" << std::endl;
- system("pause");
- system("cls");
- return;
- }
-
- for (int i = 0; i < appointments.getAppointments(); i++)
- {
- //this->getId() 是一个int类型
- //appointments.v_Appointments[i]["student_id"] 是一个string类型
- //appointments.v_Appointments[i]["student_id"].c_str() 是一个char*类型,是C语言的字符串;.c_str()将string 转 const char*
- //atoi(appointments.v_Appointments[i]["student_id"].c_str()) 是一个int类型,是C语言的整数; atoi()将const char* 转 int
- //所以可以比较两个string类型的值
- if (std::atoi(appointments.v_Appointments[i]["student_id"].c_str()) == this->getId())//如果是自己的预约
- {
- number++;
- std::cout << "Your appointment is the day: " << appointments.v_Appointments[i]["day"];
- 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.");
- std::cout << "; lab_id is: " << appointments.v_Appointments[i]["lab_id"];
- std::cout << "; lab_name is: " << appointments.v_Appointments[i]["lab_name"];
- std::string status = "; statis: ";//1表示审核中,2表示已通过,3表示已拒绝,4表示已取消
- if (appointments.v_Appointments[i]["status"] == "1")
- {
- status += "Pending Review";
- }
- else if (appointments.v_Appointments[i]["status"] == "2")
- {
- status += "Approved";
- }
- else if (appointments.v_Appointments[i]["status"] == "3")
- {
- status += "Rejected";
- }
- else if (appointments.v_Appointments[i]["status"] == "4")
- {
- status += "Canceled";
- }
- else
- {
- status += "Your Appointment Status Is Error.";
- }
- std::cout << " " << status << std::endl;
- }
- }
- std::cout << "Total " << number << " LabAppointments Appointment!" << std::endl;
- system("pause");
- system("cls");
- }
复制代码 ④查看全部预约功能——showAllOrder()
和体现我的预约功能雷同,体现内容:哪一天、时间段、学号、学生姓名、实验室编号、实验室名称、预约状态
比体现我的预约功能多了体现学号、学生姓名,优化一点,为每条实验室申请预约记录加个序号
Student.cpp
- void Student::showAllOrder() //显示所有预约
- {
- LabAppointments appointments;
- if (appointments.getAppointments() == 0)
- {
- std::cout << "No LabAppointments Appointment!" << std::endl;
- system("pause");
- system("cls");
- return;
- }
- for (int i = 0; i < appointments.getAppointments(); i++)
- {
- std::cout << i + 1 << ".";
- std::cout << "The day: " << appointments.v_Appointments[i]["day"];
- 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.");
- std::cout << "; student_id is: " << appointments.v_Appointments[i]["student_id"];
- std::cout << "; student_name is: " << appointments.v_Appointments[i]["student_name"];
- std::cout << "; lab_id is: " << appointments.v_Appointments[i]["lab_id"];
- std::cout << "; lab_name is: " << appointments.v_Appointments[i]["lab_name"];
- std::string status = "; statis: ";//1表示审核中,2表示已通过,3表示已拒绝,4表示已取消
- if (appointments.v_Appointments[i]["status"] == "1")
- {
- status += "Pending Review";
- }
- else if (appointments.v_Appointments[i]["status"] == "2")
- {
- status += "Approved";
- }
- else if (appointments.v_Appointments[i]["status"] == "3")
- {
- status += "Rejected";
- }
- else if (appointments.v_Appointments[i]["status"] == "4")
- {
- status += "Canceled";
- }
- else
- {
- status += "Your Appointment Status Is Error.";
- }
- std::cout << " " << status << std::endl;
- }
- system("pause");
- system("cls");
- }
复制代码 ⑤取消预约功能——cancelOrder()
只有在自己的学号并且预约成功或稽核中才可以取消预约
1表现稽核中,2表现已通过,3表现已拒绝,4表现已取消
Student.cpp
- void Student::cancelOrder() //取消预约
- {
- LabAppointments appointments;
- if (appointments.getAppointments() == 0)
- {
- std::cout << "No LabAppointments Appointment!" << std::endl;
- system("pause");
- system("cls");
- return;
- }
- std::cout << "Records that are under review or have a successful appointment can be canceled, so please enter the canceled record:" << std::endl;
- std::vector<int> v_index;
- int index = 1;
- for (int i = 0; i < appointments.getAppointments(); i++)
- {
- if (atoi(appointments.v_Appointments[i]["student_id"].c_str()) == this->getId())//是自己的预约
- {
- if (appointments.v_Appointments[i]["status"] == "1" || appointments.v_Appointments[i]["status"] == "2")//审核中或已通过的预约可以取消
- {
- v_index.push_back(i);
- std::cout << index << ". ";
- std::cout <<" The day: " << appointments.v_Appointments[i]["day"];
- 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.");
- std::cout << "; lab_id is: " << appointments.v_Appointments[i]["lab_id"];
- std::cout << "; lab_name is: " << appointments.v_Appointments[i]["lab_name"];
- std::string status = "; statis: ";//1表示审核中,2表示已通过,3表示已拒绝,4表示已取消
- if (appointments.v_Appointments[i]["status"] == "1")//审核中
- {
- status += "Pending Review";
- }
- else if (appointments.v_Appointments[i]["status"] == "2")//已通过
- {
- status += "Approved";
- }
- std::cout << " " << status << std::endl;
- index++;
- }
- }
- }
- std::cout << "Please enter the index of the canceled record, or enter 0 to return:" << std::endl;
- int select = 0;
- while (true)
- {
- std::cin >> select;
- if (select >= 0 && select <= v_index.size())
- {
- if (select == 0) //输入0表示取消
- {
- break;
- }
- else
- {
- int i = v_index[select - 1];
- appointments.v_Appointments[i]["status"] = "4";//将状态设置为已取消
- appointments.updateAppointments();
- std::cout << "The appointment has been canceled successfully!" << std::endl;
- break;
- }
- }
- std::cout << "You have made a mistake, please re-enter." << std::endl;
- }
- system("pause");
- system("cls");
- }
复制代码 7,西席模块
①登录和注销功能——TeacherMenu()
在Teacher类的有参构造函数中进行学生信息的初始化操作
Teacher.cpp
- Teacher::Teacher(int teacher_id,std::string name,std::string pwd)
- {
- //初始化实验室教师职工号、姓名、密码
- this->setId(teacher_id);
- this->setName(name);
- this->setPwd(pwd);
- }
复制代码 在main.cpp中定义全局函数teacherMenu(),实验室西席登录成功之后进入页面,进行相应的功能的选择
在main.cpp中的用户登录函数Login()中,用户登录成功进入到实验室西席功能页面(调用TeacherMenu()函数即可)
main.cpp
- void TeacherMenu(Identify*& identify_p) //传入父类的指针
- {
- //进入菜单界面
- while (true)
- {
- //调用管理员子菜单
- identify_p->selectMenu(); //这是父类指针,只能调用公共的接口纯虚函数
- Teacher* teacher = (Teacher*)identify_p; //将父类指针转为子类指针
- int userselect = 0; //用户的选择
- std::cin >> userselect; //接收用户的选择
- if (userselect == 1)
- {
- std::cout << "Your select is view all appointment information" << std::endl;
- teacher->showAllOrder();
- }
- else if (userselect == 2)
- {
- std::cout << "Your select is valid appointment information" << std::endl;
- teacher->validOrder();
- }
- else//选成其他选项,默认代表注销登录
- {
- delete teacher; //销毁堆区创建的对象
- std::cout << "The logout is successful" << std::endl;
- system("pause");
- system("cls");
- return;
- }
- }
- }
复制代码 定义全局函数TeacherMenu()用于体现学生身份菜单界面
在Login()登录全局函数中进行调用即可
②查看全部预约功能——showAllOrder()
与学生身份的查看全部预约功能相似,用于体现全部预约记录
Teacher.cpp
- void Teacher::showAllOrder()//显示所有预约
- {
- LabAppointments appointments;
- if (appointments.getAppointments() == 0)
- {
- std::cout << "No LabAppointments Appointment!" << std::endl;
- system("pause");
- system("cls");
- return;
- }
- for (int i = 0; i < appointments.getAppointments(); i++)
- {
- std::cout << i + 1 << ".";
- std::cout << "The day: " << appointments.v_Appointments[i]["day"];
- 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.");
- std::cout << "; student_id is: " << appointments.v_Appointments[i]["student_id"];
- std::cout << "; student_name is: " << appointments.v_Appointments[i]["student_name"];
- std::cout << "; lab_id is: " << appointments.v_Appointments[i]["lab_id"];
- std::cout << "; lab_name is: " << appointments.v_Appointments[i]["lab_name"];
- std::string status = "; status: ";//1表示审核中,2表示已通过,3表示已拒绝,4表示已取消
- if (appointments.v_Appointments[i]["status"] == "1")
- {
- status += "Pending Review";
- }
- else if (appointments.v_Appointments[i]["status"] == "2")
- {
- status += "Approved";
- }
- else if (appointments.v_Appointments[i]["status"] == "3")
- {
- status += "Rejected";
- }
- else if (appointments.v_Appointments[i]["status"] == "4")
- {
- status += "Canceled";
- }
- else
- {
- status += "Your Appointment Status Is Error.";
- }
- std::cout << " " << status << std::endl;
- }
- system("pause");
- system("cls");
- }
复制代码 ③稽核预约功能——validOrder()
获取实验室预约记录order.txt中全部数据,筛选status为待稽核状态的进行体现
实验室老师对这些待稽核状态的实验室预约记录进行稽核,稽核完成之后重新更新状态即可
Teacher.cpp
- void Teacher::validOrder() //审核预约
- {
- LabAppointments appointments;
- if (appointments.getAppointments() == 0)
- {
- std::cout << "No LabAppointments Appointment!" << std::endl;
- system("pause");
- system("cls");
- return;
- }
- std::cout << "The records of laboratory appointments to be reviewed are as follows:" << std::endl;
- std::vector<int> v_valid_order;
- int index = 1;
- for (int i = 0; i < appointments.getAppointments(); i++)
- {
- if (appointments.v_Appointments[i]["status"] == "1")//1表示审核中
- {
- v_valid_order.push_back(i);
- std::cout << index++ << ".";
- std::cout << "The day: " << appointments.v_Appointments[i]["day"];
- 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.");
- std::cout << "; student_id is: " << appointments.v_Appointments[i]["student_id"];
- std::cout << "; student_name is: " << appointments.v_Appointments[i]["student_name"];
- std::cout << "; lab_id is: " << appointments.v_Appointments[i]["lab_id"];
- std::cout << "; lab_name is: " << appointments.v_Appointments[i]["lab_name"];
-
- std::string status = "; status: Pending Revie";//1表示审核中,2表示已通过,3表示已拒绝,4表示已取消
- std::cout << status << std::endl;
- }
- }
- //输入审核的实验室预约序号,0表示返回,不想审核了
- std::cout << "Please enter the lab appointment record you want to review, 0 means return" << std::endl;
- int select = 0;//实验室教师选择要审核的预约序号
- int ret = 0;//接收实验室教师给的审核结果,1表示审核通过,2表示审核拒绝
- while (true)
- {
- std::cin >> select;
- if (select >= 0 && select <= v_valid_order.size())
- {
- if (select == 0)//选择0,表示不想审核了,退出循环即可
- {
- break;
- }
- else
- {
- //1表示审核中,2表示已通过,3表示已拒绝,4表示已取消
- std::cout << "Please enter the result of the review" << std::endl;
- std::cout << "1. Approved" << std::endl;
- std::cout << "2. Rejected" << std::endl;
- std::cin >> ret;
- if (ret == 1)
- {
- appointments.v_Appointments[v_valid_order[select - 1]]["status"] = "2";
- }
- else if (ret == 2)
- {
- appointments.v_Appointments[v_valid_order[select - 1]]["status"] = "3";
- }
- else
- {
- std::cout << "Your input is error!" << std::endl;
- continue;
- }
- appointments.updateAppointments();//更新预约信息到文件中
- std::cout << "The review result has been saved!" << std::endl;
- break;
- }
- }
- std::cout << "You have made a mistake, please re-enter." << std::endl;
- }
- system("pause");
- system("cls");
- }
复制代码 四、完整代码
0,项目结构
项目同级目次下创建五个文本充当数据库
1,globalFile.h
宏定义,确定五个设置文件
- #pragma once
- //管理员文件
- #define ADMIN_FILE "admin.txt"
- //学生文件
- #define STUDENT_FILE "student.txt"
- //教师文件
- #define TEACHER_FILE "teacher.txt"
- //实验室信息文件
- #define LABORATORY_FILE "laboratoryRoom.txt"
- //订单文件
- #define ORDER_FILE "order.txt"
复制代码 2,Identify.h
管理员、学生、老师的基类
- #pragma once
- #include <iostream>
- class Identify
- {
- public:
- virtual void selectMenu() = 0;//纯虚函数,不同用户登录成功之后显示的菜单均不同
-
- std::string getName()
- {
- return this->name_;
- }
- std::string getPwd()
- {
- return this->pwd_;
- }
- void setName(std::string name)
- {
- this->name_ = name;
- }
- void setPwd(std::string pwd)
- {
- this->pwd_ = pwd;
- }
- private:
- std::string name_;//姓名
- std::string pwd_;//密码
- };
复制代码 3,Laboratory.h
实验室类
- #pragma once
- #include <iostream>
- //实验室类
- class Laboratory
- {
- public:
- Laboratory() {};
- Laboratory(int id, std::string name, int capacity):lab_id_(id), lab_name_(name), lab_max_capacity_(capacity) {}
- void set_lab_id(int id) { this->lab_id_ = id; }
- void set_lab_name(std::string name) { this->lab_name_ = name; }
- void set_lab_max_capacity(int capacity) { this->lab_max_capacity_ = capacity; }
- int get_lab_id() const { return this->lab_id_; }
- std::string get_lab_name() const { return this->lab_name_; }
- int get_lab_max_capacity() const { return this->lab_max_capacity_; }
-
- private:
- int lab_id_; //实验室编号
- std::string lab_name_; //实验室名称
- int lab_max_capacity_; //实验室容量
- };
复制代码 4,Admin.h
- #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();//初始化实验室容器,用于将实验室信息读取到容器中
- std::vector<Laboratory> v_Lab;//实验室容器
- };
复制代码 5,Admin.cpp
- #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() //添加实验室信息功能实现
- {
- int lab_id;
- std::string lab_name;
- int lab_max_capacity;
- std::ofstream ofs;//文件操作对象
- ofs.open(LABORATORY_FILE, std::ios::app);//以追加的方式写入文件
- if (!ofs.is_open())//文件打开失败
- {
- std::cout << "Failed to open file: " << LABORATORY_FILE << std::endl;
- return;
- }
- while (true)
- {
- std::cout << "Please input laboratory ID: " << std::endl;
- std::cin >> lab_id;
- bool repeat = this->checkRepeate(lab_id, 3);//检查是否有重复的实验室
- if (repeat)
- {
- std::cout << "Laboratory ID already exists, please input again: " << std::endl;
- }
- else
- {
- break;
- }
- }
- std::cout << "Please input laboratory name: " << std::endl;
- std::cin >> lab_name;
- std::cout << "Please input laboratory max capacity: " << std::endl;
- std::cin >> lab_max_capacity;
- ofs << lab_id << " " << lab_name << " " << lab_max_capacity << " " << std::endl;
- ofs.close();
- std::cout << "Add laboratory information successfully!" << std::endl;
- this->init_Lab_Vector();
- system("pause");//暂停程序
- system("cls");//清屏
- }
- 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)
- {
- std::cout << "Laboratory ID: " << lab.get_lab_id() << ", Name: " << lab.get_lab_name() << ", Max capacity: " << lab.get_lab_max_capacity() << std::endl;
- }
- void Admin::showLabInfo() //查看实验室信息功能实现
- {
- std::cout << "Laboratory information:" << std::endl;
- std::for_each(v_Lab.begin(), v_Lab.end(), printLab);
- /* 和for_each的用法一样,二选一即可
- for (std::vector<Laboratory>::iterator it = v_Lab.begin(); it != v_Lab.end(); it++)
- {
- std::cout << "Laboratory ID: " << it->get_lab_id() << ", Name: " << it->get_lab_name() << ", Max capacity: " << it->get_lab_max_capacity() << std::endl;
- }
- */
- system("pause");//暂停程序
- system("cls");//清屏
- }
- 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
实验室预约申请类,存放学生的预约申请实验室记录
- #pragma once
- #include <map>
- #include <iostream>
- #include <fstream>
- #include "globalFile.h"
- #include <map>
- class LabAppointments
- {
- public:
- LabAppointments();
- void updateAppointments();//更新预约记录信息
- std::map<int,std::map<std::string, std::string>> v_Appointments;//记录所有预约记录信息,key为预约记录的总条数,value为对应预约记录信息
- int getAppointments() //获取预约记录信息
- {
- return this->appointments_size_;
- }
- void setAppointments(int size) //设置预约记录信息
- {
- this->appointments_size_ = size;
- }
- private:
- int appointments_size_;//预约记录的总条数
- };
复制代码 7,LabAppointments.cpp
- #include "LabAppointments.h"
- LabAppointments::LabAppointments()
- {
- std::ifstream ifs;
- ifs.open(ORDER_FILE,std::ios::in);
- if(!ifs.is_open())
- {
- std::cout << "Error: Failed to open file " << ORDER_FILE << std::endl;
- return;
- }
- //为了解析方便,这里都使用string类型存储
- std::string day;//1-5 represent Monday to Friday
- std::string time_slot;//1-3 represent 8:00-12:00,13:00-17:00,18:00-22:00
- std::string student_id;//预约实验室申请的学生编号
- std::string student_name;//预约实验室申请的学生姓名
- std::string lab_id;//实验室编号
- std::string lab_name;//实验室名称
- std::string status;//预约申请的状态,1表示待审核状态
-
- this->appointments_size_ = 0;//预约申请的数量
- while (ifs >> day && ifs >> time_slot && ifs >> student_id && ifs >> student_name && ifs >> lab_id && ifs >> lab_name && ifs >> status)
- {
- /*
- std::cout << day << std::endl;
- std::cout << time_slot << std::endl;
- std::cout << student_id << std::endl;
- std::cout << student_name << std::endl;
- std::cout << lab_id << std::endl;
- std::cout << lab_name << std::endl;
- std::cout << status << std::endl;
- std::cout << std::endl;
- */
- /*解析内容如下:
- day:1
- time_slot:1
- student_id:1
- student_name:977
- lab_id:4
- lab_name:QT
- status:1
- */
- std::string key;
- std::string value;
- std::map<std::string, std::string> m;//存放key和value
-
- //day:1
- int pos = day.find(':');//pos=3
- if (pos != -1) //找到冒号了
- {
- key = day.substr(0, pos);//day
- value = day.substr(pos + 1, day.size() - pos - 1);//1
- m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
- }
-
- //time_slot:1
- pos = time_slot.find(':');
- if (pos != -1) //找到冒号了
- {
- key = time_slot.substr(0, pos);//time_slot
- value = time_slot.substr(pos + 1, time_slot.size() - pos - 1);
- m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
- }
- //student_id:1
- pos = student_id.find(':');
- if (pos != -1) //找到冒号了
- {
- key = student_id.substr(0, pos);//student_id
- value = student_id.substr(pos + 1, student_id.size() - pos - 1);
- m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
- }
- //student_name:977
- pos = student_name.find(':');
- if (pos != -1) //找到冒号了
- {
- key = student_name.substr(0, pos);//student_name
- value = student_name.substr(pos + 1, student_name.size() - pos - 1);//1
- m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
- }
- //lab_id:4
- pos = lab_id.find(':');
- if (pos != -1) //找到冒号了
- {
- key = lab_id.substr(0, pos);//lab_id
- value = lab_id.substr(pos + 1, lab_id.size() - pos - 1);//1
- m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
- }
- //lab_name:QT
- pos = lab_name.find(':');
- if (pos != -1) //找到冒号了
- {
- key = lab_name.substr(0, pos);//lab_name
- value = lab_name.substr(pos + 1, lab_name.size() - pos - 1);//1
- m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
- }
- //status:1
- pos = status.find(':');
- if (pos != -1) //找到冒号了
- {
- key = status.substr(0, pos);
- value = status.substr(pos + 1, status.size() - pos - 1);//1
- m.insert(std::pair<std::string, std::string>(key, value));//插入到map中
- }
- //将map中的内容存入Appointment对象中
- this->v_Appointments.insert(std::make_pair(this->appointments_size_, m));
- this->appointments_size_++;
- }
- ifs.close();
- //测试
- /*
- for (std::map<int, std::map<std::string, std::string>>::iterator it = this->v_Appointments.begin(); it != this->v_Appointments.end(); it++)
- {
- std::cout << "Number of laboratory reservation records: " << it->first << ", Value is: " << std::endl;
- for (std::map<std::string, std::string>::iterator it2 = it->second.begin(); it2 != it->second.end(); it2++)
- {
- std::cout << it2->first << ":" << it2->second << " ";
- }
- std::cout << std::endl;
- }
- */
- }
- void LabAppointments::updateAppointments() //实验室预约申请状态更新
- {
- if (this->appointments_size_ == 0) //一条实验室预约记录都没有
- {
- return;
- }
- std::ofstream ofs;//写入到文件中
- ofs.open(ORDER_FILE, std::ios::out | std::ios::trunc);//对实验室预约记录文件进行重写覆盖写入
- if (!ofs.is_open())
- {
- std::cout << "Error: Failed to open file " << ORDER_FILE << std::endl;
- return;
- }
- for (int i=0;i<this->appointments_size_;i++)
- { //v_Appointments[i]第i条实验室预约记录,是一个map
- //v_Appointments[i]["day"]第i条实验室预约记录的日期,通过这个map的key("day")拿到对应的value
- ofs << "day:" << this->v_Appointments[i]["day"] << " ";
- ofs << "time_slot:" << this->v_Appointments[i]["time_slot"] << " ";
- ofs << "student_id:" << this->v_Appointments[i]["student_id"] << " ";
- ofs << "student_name:" << this->v_Appointments[i]["student_name"] << " ";
- ofs << "lab_id:" << this->v_Appointments[i]["lab_id"] << " ";
- ofs << "lab_name:" << this->v_Appointments[i]["lab_name"] << " ";
- ofs << "status:" << this->v_Appointments[i]["status"] << std::endl;
- }
- ofs.close();
- }
复制代码 8,Student.h
- #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;//学生所申请的实验室
- private: int student_id_;};
复制代码 9,Student.cpp
- #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() //显示我的预约
- {
- int number{0};//实验室申请记录条数
- LabAppointments appointments;//预约信息
- if (appointments.getAppointments() == 0)//如果没有预约信息
- {
- std::cout << "No LabAppointments Appointment!" << std::endl;
- system("pause");
- system("cls");
- return;
- }
-
- for (int i = 0; i < appointments.getAppointments(); i++)
- {
- //this->getId() 是一个int类型
- //appointments.v_Appointments[i]["student_id"] 是一个string类型
- //appointments.v_Appointments[i]["student_id"].c_str() 是一个char*类型,是C语言的字符串;.c_str()将string 转 const char*
- //atoi(appointments.v_Appointments[i]["student_id"].c_str()) 是一个int类型,是C语言的整数; atoi()将const char* 转 int
- //所以可以比较两个string类型的值
- if (std::atoi(appointments.v_Appointments[i]["student_id"].c_str()) == this->getId())//如果是自己的预约
- {
- number++;
- std::cout << "Your appointment is the day: " << appointments.v_Appointments[i]["day"];
- 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.");
- std::cout << "; lab_id is: " << appointments.v_Appointments[i]["lab_id"];
- std::cout << "; lab_name is: " << appointments.v_Appointments[i]["lab_name"];
- std::string status = "; statis: ";//1表示审核中,2表示已通过,3表示已拒绝,4表示已取消
- if (appointments.v_Appointments[i]["status"] == "1")
- {
- status += "Pending Review";
- }
- else if (appointments.v_Appointments[i]["status"] == "2")
- {
- status += "Approved";
- }
- else if (appointments.v_Appointments[i]["status"] == "3")
- {
- status += "Rejected";
- }
- else if (appointments.v_Appointments[i]["status"] == "4")
- {
- status += "Canceled";
- }
- else
- {
- status += "Your Appointment Status Is Error.";
- }
- std::cout << " " << status << std::endl;
- }
- }
- std::cout << "Total " << number << " LabAppointments Appointment!" << std::endl;
- system("pause");
- system("cls");
- }
- void Student::showAllOrder() //显示所有预约
- {
- LabAppointments appointments;
- if (appointments.getAppointments() == 0)
- {
- std::cout << "No LabAppointments Appointment!" << std::endl;
- system("pause");
- system("cls");
- return;
- }
- for (int i = 0; i < appointments.getAppointments(); i++)
- {
- std::cout << i + 1 << ".";
- std::cout << "The day: " << appointments.v_Appointments[i]["day"];
- 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.");
- std::cout << "; student_id is: " << appointments.v_Appointments[i]["student_id"];
- std::cout << "; student_name is: " << appointments.v_Appointments[i]["student_name"];
- std::cout << "; lab_id is: " << appointments.v_Appointments[i]["lab_id"];
- std::cout << "; lab_name is: " << appointments.v_Appointments[i]["lab_name"];
- std::string status = "; statis: ";//1表示审核中,2表示已通过,3表示已拒绝,4表示已取消
- if (appointments.v_Appointments[i]["status"] == "1")
- {
- status += "Pending Review";
- }
- else if (appointments.v_Appointments[i]["status"] == "2")
- {
- status += "Approved";
- }
- else if (appointments.v_Appointments[i]["status"] == "3")
- {
- status += "Rejected";
- }
- else if (appointments.v_Appointments[i]["status"] == "4")
- {
- status += "Canceled";
- }
- else
- {
- status += "Your Appointment Status Is Error.";
- }
- std::cout << " " << status << std::endl;
- }
- system("pause");
- system("cls");
- }
- void Student::cancelOrder() //取消预约
- {
- LabAppointments appointments;
- if (appointments.getAppointments() == 0)
- {
- std::cout << "No LabAppointments Appointment!" << std::endl;
- system("pause");
- system("cls");
- return;
- }
- std::cout << "Records that are under review or have a successful appointment can be canceled, so please enter the canceled record:" << std::endl;
- std::vector<int> v_index;
- int index = 1;
- for (int i = 0; i < appointments.getAppointments(); i++)
- {
- if (atoi(appointments.v_Appointments[i]["student_id"].c_str()) == this->getId())//是自己的预约
- {
- if (appointments.v_Appointments[i]["status"] == "1" || appointments.v_Appointments[i]["status"] == "2")//审核中或已通过的预约可以取消
- {
- v_index.push_back(i);
- std::cout << index << ". ";
- std::cout <<" The day: " << appointments.v_Appointments[i]["day"];
- 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.");
- std::cout << "; lab_id is: " << appointments.v_Appointments[i]["lab_id"];
- std::cout << "; lab_name is: " << appointments.v_Appointments[i]["lab_name"];
- std::string status = "; statis: ";//1表示审核中,2表示已通过,3表示已拒绝,4表示已取消
- if (appointments.v_Appointments[i]["status"] == "1")//审核中
- {
- status += "Pending Review";
- }
- else if (appointments.v_Appointments[i]["status"] == "2")//已通过
- {
- status += "Approved";
- }
- std::cout << " " << status << std::endl;
- index++;
- }
- }
- }
- std::cout << "Please enter the index of the canceled record, or enter 0 to return:" << std::endl;
- int select = 0;
- while (true)
- {
- std::cin >> select;
- if (select >= 0 && select <= v_index.size())
- {
- if (select == 0) //输入0表示取消
- {
- break;
- }
- else
- {
- int i = v_index[select - 1];
- appointments.v_Appointments[i]["status"] = "4";//将状态设置为已取消
- appointments.updateAppointments();
- std::cout << "The appointment has been canceled successfully!" << std::endl;
- break;
- }
- }
- std::cout << "You have made a mistake, please re-enter." << std::endl;
- }
- system("pause");
- system("cls");
- }
复制代码 10,Teacher.h
- #pragma once
- #include "Identify.h"
- #include "LabAppointments.h"
- #include <vector>
- class Teacher:public Identify
- {
- public:
- Teacher();
- Teacher(int teacher_id,std::string name,std::string pwd);
- virtual void Identify::selectMenu() override;//功能菜单实现
- void showAllOrder();//显示所有预约
- void validOrder();//审核预约
- int getId()
- {
- return this->teacher_id_;
- }
- void setId(int id)
- {
- this->teacher_id_ = id;
- }
- private:
- int teacher_id_;//教职工编号
- };
复制代码 11,Teacher.cpp
12,main.cpp
- #include <iostream>
- #include "Identify.h"
- #include <fstream>
- #include "globalFile.h"
- #include "Student.h"
- #include "Teacher.h"
- #include "Admin.h"
- //管理员身份菜单界面
- void AdminMenu(Identify*& identify_p) //传入父类的指针
- {
- //进入菜单界面
- while (true)
- {
- //调用管理员子菜单
- identify_p->selectMenu();//这是父类指针,只能调用公共的接口纯虚函数
- //想要调用子类特有的接口,需要把父类指针转回去,将父类的指针转为子类的指针
- Admin* admin = (Admin*)identify_p;
- int userselect = 0;//用户的选择
- std::cin >> userselect;//接收用户的选择
- if (userselect == 1) //添加账号
- {
- //std::cout << "Your select is add an account" << std::endl;
- admin->addUser();
- }
- else if (userselect == 2)//查看账号
- {
- //std::cout << "Your select is view all account" << std::endl;
- admin->showUser();
- }
- else if (userselect == 3) //添加实验室信息
- {
- //std::cout << "Your select is add lab information" << std::endl;
- admin->addLabInfo();
- }
- else if (userselect == 4) //显示实验室信息
- {
- //std::cout << "Your select is show lab information" << std::endl;
- admin->showLabInfo();
- }
- else if (userselect == 5) //清空预约
- {
- //std::cout << "Your select is clear all appointment" << std::endl;
- admin->cleanRecord();
- }
- else//选成其他选项,默认代表注销登录
- {
- delete admin;//销毁堆区创建的对象
- std::cout << "The logout is successful" << std::endl;
- system("pause");
- system("cls");
- return;
- }
- }
- }
- //学生身份菜单界面
- void StudentMenu(Identify*& identify_p) //传入父类的指针
- {
- //进入菜单界面
- while(true)
- {
- //调用管理员子菜单
- identify_p->selectMenu();//这是父类指针,只能调用公共的接口纯虚函数
- //想要调用子类特有的接口,需要把父类指针转回去,将父类的指针转为子类的指针
- Student* student = (Student*)identify_p;
- int userselect = 0;//用户的选择
- std::cin >> userselect;//接收用户的选择
- if (userselect == 1) //申请实验室预约
- {
- //std::cout << "Your select is apply for lab appointment" << std::endl;
- student->applyOrder();
- }
- else if (userselect == 2) //查看自身预约信息
- {
- //std::cout << "Your select is view your appointment information" << std::endl;
- student->showMyOrder();
- }
- else if (userselect == 3) //查看所有的实验室预约信息
- {
- //std::cout << "Your select is view all appointment information" << std::endl;
- student->showAllOrder();
- }
- else if (userselect == 4) //取消实验室预约
- {
- //std::cout << "Your select is cancel lab appointment" << std::endl;
- student->cancelOrder();
- }
- else //选成其他选项,默认代表注销登录
- {
- delete student;//销毁堆区创建的对象
- std::cout << "The logout is successful" << std::endl;
- system("pause");
- system("cls");
- return;
- }
- }
- }
- //实验室教师身份菜单页面
- void TeacherMenu(Identify*& identify_p) //传入父类的指针
- {
- //进入菜单界面
- while (true)
- {
- //调用管理员子菜单
- identify_p->selectMenu(); //这是父类指针,只能调用公共的接口纯虚函数
- Teacher* teacher = (Teacher*)identify_p; //将父类指针转为子类指针
- int userselect = 0; //用户的选择
- std::cin >> userselect; //接收用户的选择
- if (userselect == 1)
- {
- //std::cout << "Your select is view all appointment information" << std::endl;
- teacher->showAllOrder();
- }
- else if (userselect == 2)
- {
- //std::cout << "Your select is valid appointment information" << std::endl;
- teacher->validOrder();
- }
- else//选成其他选项,默认代表注销登录
- {
- delete teacher; //销毁堆区创建的对象
- std::cout << "The logout is successful" << std::endl;
- system("pause");
- system("cls");
- return;
- }
- }
- }
- //全局登录函数
- //参数一:操作的文件名称
- //参数二:身份(1为学生、2为实验室教师、3为管理员)
- void Login(std::string fileName,int type)
- {
- //创建父类指针,将来通过多态指向子类对象
- Identify* identify_p = NULL;
- //读取文件
- std::ifstream ifs;
- ifs.open(fileName, std::ios::in);//以读的方式打开文件
- //若文件不存在
- if (!ifs.is_open()) //打开失败
- {
- std::cout << "The file does not exist" << std::endl;
- ifs.close();
- return;
- }
- //存放用户将来输入的信息
- int id = 0;//存储学号或职工号
- std::string name;//学生或实验室教师的姓名
- std::string pwd;//学生或实验室教师的密码
- if (type == 1)
- {
- std::cout << "Please input your student_id:" << std::endl;
- std::cin >> id;
- }
- else if (type == 2)
- {
- std::cout << "Please input your teacher_id:" << std::endl;
- std::cin >> id;
- }
- std::cout << "Please input your name:" << std::endl;
- std::cin >> name;
- std::cout << "Please input your password:" << std::endl;
- std::cin >> pwd;
- if (type == 1)
- {
- //student login
- int f_id;//文件读取得到的id
- std::string f_name;//从文件中获取得到的name
- std::string f_pwd; //从文件中获取得到的pwd
- while (ifs >> f_id && ifs >> f_name && ifs >> f_pwd) //文件按行读取,遇到空格存一下,每行数据信息拆成三个
- {
- //文件读取得到的信息与用户输入的信息进行对比
- if (f_id == id && f_name == name && f_pwd == pwd) //学号、姓名、密码均输入成功
- {
- std::cout << "Student verification login is successful!" << std::endl;
- system("pause");
- system("cls");
- identify_p = new Student(id, name, pwd);//父类指针指向子类对象,创建学生实例
-
- //进入学生身份的子菜单
- StudentMenu(identify_p);
- return;
- }
- }
- }
- else if (type == 2)
- {
- //teacher login
- int f_id;//文件读取得到的id
- std::string f_name;//从文件中获取得到的name
- std::string f_pwd; //从文件中获取得到的pwd
- while (ifs >> f_id && ifs >> f_name && ifs >> f_pwd) //文件按行读取,遇到空格存一下,每行数据信息拆成三个
- {
- //文件读取得到的信息与用户输入的信息进行对比
- if (f_id == id && f_name == name && f_pwd == pwd)//教职工编号、姓名、密码输入成功
- {
- std::cout << "Teacher verification login is successful!" << std::endl;
- system("pause");
- system("cls");
- identify_p = new Teacher(id, name, pwd);//父类指针指向子类对象,创建教职工实例
- //进入实验室教师身份的子菜单
- TeacherMenu(identify_p);
- return;
- }
- }
- }
- else if (type == 3)
- {
- //admin login
- std::string f_name;//从文件中获取得到的name
- std::string f_pwd;//从文件中获取得到的pwd
- while (ifs >> f_name && ifs >> f_pwd) //文件按行读取,遇到空格存一下,每行数据信息拆成三个
- {
- //文件读取得到的信息与用户输入的信息进行对比
- if (f_name == name && f_pwd == pwd) //管理员登录只需要姓名和密码
- {
- std::cout << "Admin verification login is successful!" << std::endl;
- system("pause");
- system("cls");
- identify_p = new Admin(name, pwd);//父类指针指向子类对象,创建管理员实例
- //进入管理员身份的子菜单
- AdminMenu(identify_p);
- return;
- }
- }
- }
-
- std::cout << "Validation failed !" << std::endl;
- system("pause");
- system("cls");
- return;
- }
- int main() {
- int select = 0;//存放用户的选择
- while (true)
- {
- std::cout << "====================== Welcome to the Laboratory Management System =====================" << std::endl;
- std::cout << "\t\t -------------------------------\n";
- std::cout << "\t\t| 1.Student |\n";
- std::cout << "\t\t| 2.Teacher |\n";
- std::cout << "\t\t| 3.Admin |\n";
- std::cout << "\t\t| 0.Exit |\n";
- std::cout << "\t\t -------------------------------\n";
- std::cout << std::endl << "Please enter your identity: " << std::endl;
- std::cin >> select; //接受用户选择
- switch (select)
- {
- case 1: //学生身份
- Login(STUDENT_FILE,1);
- break;
- case 2: //老师身份
- Login(TEACHER_FILE, 2);
- break;
- case 3: //管理员身份
- Login(ADMIN_FILE, 3);
- break;
- case 0: //退出系统
- std::cout << "Welcome to you next time!" << std::endl;
- system("pause");
- return 0;
- break;
- default:
- std::cout << "You have entered the wrong information, please make a new selection!" << std::endl;
- system("pause");
- system("cls");
- break;
- }
- }
- system("pause");
- return 0;
- }
复制代码 五、项目下载链接
实验室预约综合管理系统
免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。 |