注意:这篇文章上次更新于1825天前,文章内容可能已经过时。
This article was last updated1825 days ago, the content may be outdated.
Computer Room Reservation System Requirements
System Introduction
The school has several computer rooms of different specifications. Since “collision” often occurs when using them, a computer room reservation system is now developed to solve this problem.

身份简介
分别有三种身份使用该程序
- 学生代表:申请使用机房
- 教师:审核学生的预约申请
- 管理员:给学生、教师创建账号
机房简介
机房总共有3间
- 1号机房 — 最大容量20人
- 2号机房 — 最多容量50人
- 3号机房 — 最多容量100人
申请简介
- 申请的订单每周由管理员负责清空。
- 学生可以预约未来一周内的机房使用,预约的日期为周一至周五,预约时需要选择预约时段(上午、下午)
- 教师来审核预约,依据实际情况审核预约通过或者不通过
系统具体需求
- 首先进入登录界面,可选登录身份有:
- 学生代表
- 教师
- 管理员
- 退出
- 每个身份都需要进行验证后,进入子菜单
- 学生需要输入 :学号、姓名、登录密码
- 教师需要输入:职工号、姓名、登录密码
- 管理员需要输入:管理员姓名、登录密码
- 学生具体功能
- 申请预约 — 预约机房
- 查看自身的预约 — 查看自己的预约状态
- 查看所有预约 — 查看全部预约信息以及预约状态
- 取消预约 — 取消自身的预约,预约成功或审核中的预约均可取消
- 注销登录 — 退出登录
- 教师具体功能
- 查看所有预约 — 查看全部预约信息以及预约状态
- 审核预约 — 对学生的预约进行审核
- 注销登录 — 退出登录
- 管理员具体功能
- 添加账号 — 添加学生或教师的账号,需要检测学生编号或教师职工号是否重复
- 查看账号 — 可以选择查看学生或教师的全部信息
- 查看机房 — 查看所有机房的信息
- 清空预约 — 清空所有预约记录
- 注销登录 — 退出登录

创建项目
创建项目步骤如下:
- 创建新项目
- 添加文件
创建项目
- 打开vs2017后,点击创建新项目,创建新的C++项目
如图:

- 填写项目名称以及选取项目路径,点击确定生成项目

添加文件
- 右键源文件,进行添加文件操作

- 填写文件名称,点击添加

- 生成文件成功,效果如下图

创建主菜单
功能描述:
- 设计主菜单,与用户进行交互
菜单实现
- 在主函数main中添加菜单提示,代码如下:
Identity Introduction
Three identities use this program
- Student representative: applies to use a computer room
- Teacher: reviews students’ reservation applications
- Administrator: creates accounts for students and teachers
Computer Room Introduction
There are 3 computer rooms in total
- Room 1 — maximum capacity 20 people
- Room 2 — maximum capacity 50 people
- Room 3 — maximum capacity 100 people
Application Introduction
- The reservation orders are cleared by the administrator every week.
- Students can reserve the use of a computer room within the next week; the reservation dates are Monday to Friday, and a time slot (morning or afternoon) must be selected when reserving
- Teachers review the reservations and approve or reject them based on the actual situation
Specific System Requirements
- First, enter the login interface, where the selectable login identities are:
- Student representative
- Teacher
- Administrator
- Exit
- Each identity must be verified before entering its submenu
- A student needs to enter: student ID, name, login password
- A teacher needs to enter: employee ID, name, login password
- An administrator needs to enter: administrator name, login password
- Specific functions of the student
- Apply for a reservation — reserve a computer room
- View my reservations — view my reservation status
- View all reservations — view all reservation information and statuses
- Cancel a reservation — cancel my reservation; reservations that are successful or under review can both be canceled
- Log out — exit the login
- Specific functions of the teacher
- View all reservations — view all reservation information and statuses
- Review reservations — review students’ reservations
- Log out — exit the login
- Specific functions of the administrator
- Add an account — add a student or teacher account; it needs to check whether the student ID or teacher employee ID is duplicated
- View accounts — choose to view all information of students or teachers
- View computer rooms — view information of all computer rooms
- Clear reservations — clear all reservation records
- Log out — exit the login

Creating the Project
The steps to create the project are as follows:
- Create a new project
- Add files
Creating the Project
- After opening vs2017, click Create New Project to create a new C++ project
As shown below:

- Fill in the project name and choose the project path, then click OK to generate the project

Adding Files
- Right-click Source Files and add a file

- Fill in the file name and click Add

- The file is generated successfully, as shown below

Creating the Main Menu
Function description:
- Design the main menu to interact with the user
Menu Implementation
- Add the menu prompt in the main function main as follows:
1 | int main() { |
The running effect is shown below:

Building the Interface
- Accept the user’s choice and build the interface
- Add code in main
1 | int main() { |
Test: entering 0, 1, 2, or 3 returns to the interface; entering anything else prompts that the input is invalid, clears the screen, and lets you choose again
The effect is shown below:

At this point, the interface is fully built
Implementing the Exit Function
Implementing the Exit Function
In branch 0 of the main function, add the code to exit the program:
1 | cout << "欢迎下一次使用"<<endl; |

测试退出功能
运行程序,效果如图:

至此,退出程序功能实现
创建身份类
身份的基类
- 在整个系统中,有三种身份,分别为:学生代表、老师以及管理员
- 三种身份有其共性也有其特性,因此我们可以将三种身份抽象出一个身份基类identity
- 在头文件下创建Identity.h文件
Identity.h中添加如下代码:

Testing the Exit Function
Run the program; the effect is shown below:

At this point, the exit function is implemented
Creating Identity Classes
Base Class of Identities
- In the whole system, there are three identities: student representative, teacher, and administrator
- The three identities share commonalities and have their own characteristics, so we can abstract a base class identity from them
- Create the Identity.h file under the header files
Add the following code to Identity.h:
1 |
|
效果如图:

学生类
功能分析
-
学生类主要功能是可以通过类中成员函数,实现预约实验室操作
-
学生类中主要功能有:
- 显示学生操作的菜单界面
- 申请预约
- 查看自身预约
- 查看所有预约
- 取消预约
类的创建
- 在头文件以及源文件下创建 student.h 和 student.cpp文件
student.h中添加如下代码:
The effect is shown below:

Student Class
Functional Analysis
-
The main function of the Student class is to implement computer room reservation operations through its member functions
-
The main functions of the Student class include:
- Displaying the student operation menu interface
- Applying for a reservation
- Viewing my reservations
- Viewing all reservations
- Canceling a reservation
Creating the Class
- Create student.h and student.cpp files under the header files and source files
Add the following code to student.h:
1 |
|
student.cpp中添加如下代码:
Add the following code to student.cpp:
1 |
|
老师类
功能分析
-
教师类主要功能是查看学生的预约,并进行审核
-
教师类中主要功能有:
-
显示教师操作的菜单界面
-
查看所有预约
-
审核预约
-
类的创建
- 在头文件以及源文件下创建 teacher.h 和 teacher.cpp文件
teacher.h中添加如下代码:
Teacher Class
Functional Analysis
-
The main function of the Teacher class is to view students’ reservations and review them
-
The main functions of the Teacher class include:
-
Displaying the teacher operation menu interface
-
Viewing all reservations
-
Reviewing reservations
-
Creating the Class
- Create teacher.h and teacher.cpp files under the header files and source files
Add the following code to teacher.h:
1 |
|
- teacher.cpp中添加如下代码:
- Add the following code to teacher.cpp:
1 |
|
管理员类
功能分析
-
管理员类主要功能是对学生和老师账户进行管理,查看机房信息以及清空预约记录
-
管理员类中主要功能有:
-
显示管理员操作的菜单界面
-
添加账号
-
查看账号
-
查看机房信息
-
清空预约记录
-
类的创建
- 在头文件以及源文件下创建 manager.h 和 manager.cpp文件
manager.h中添加如下代码:
Administrator Class
Functional Analysis
-
The main function of the Administrator class is to manage student and teacher accounts, view computer room information, and clear reservation records
-
The main functions of the Administrator class include:
-
Displaying the administrator operation menu interface
-
Adding accounts
-
Viewing accounts
-
Viewing computer room information
-
Clearing reservation records
-
Creating the Class
- Create manager.h and manager.cpp files under the header files and source files
Add the following code to manager.h:
1 |
|
- manager.cpp中添加如下代码:
- Add the following code to manager.cpp:
1 |
|
至此,所有身份类创建完毕,效果如图:

登录模块
全局文件添加
功能描述:
- 不同的身份可能会用到不同的文件操作,我们可以将所有的文件名定义到一个全局的文件中
- 在头文件中添加 globalFile.h 文件
- 并添加如下代码:
At this point, all identity classes are created; the effect is shown below:

Login Module
Adding the Global File
Function description:
- Different identities may use different file operations, so we can define all file names in a global file
- Add the globalFile.h file under the header files
- And add the following code:
1 |
|
并且在同级目录下,创建这几个文件

登录函数封装
功能描述:
- 根据用户的选择,进入不同的身份登录
在预约系统的.cpp文件中添加全局函数 void LoginIn(string fileName, int type)
参数:
- fileName — 操作的文件名
- type — 登录的身份 (1代表学生、2代表老师、3代表管理员)
LoginIn中添加如下代码:
Also create these files in the same directory

Encapsulating the Login Function
Function description:
- Enter the login of different identities according to the user’s choice
Add the global function void LoginIn(string fileName, int type) in the .cpp file of the reservation system
Parameters:
- fileName — the file name to operate on
- type — the login identity (1 for student, 2 for teacher, 3 for administrator)
Add the following code to LoginIn:
1 |
|
- Fill in different login interfaces in different branches of the main function

Student Login Implementation
Add two student records to the student.txt file for testing
Add the information:
1 | 1 张三 123 |
其中:
- 第一列 代表 学号
- 第二列 代表 学生姓名
- 第三列 代表 密码
效果图:

在Login函数的学生分支中加入如下代码,验证学生身份
Where:
- The first column represents the student ID
- The second column represents the student name
- The third column represents the password
Effect screenshot:

Add the following code to the student branch of the Login function to verify the student identity
1 | //学生登录验证 |
Effect screenshot of the added code

Test:

Teacher Login Implementation
Add one teacher record to the teacher.txt file for testing
Add the information:
1 | 1 老王 123 |
其中:
- 第一列 代表 教师职工编号
- 第二列 代表 教师姓名
- 第三列 代表 密码
效果图:

在Login函数的教师分支中加入如下代码,验证教师身份
Where:
- The first column represents the teacher employee ID
- The second column represents the teacher name
- The third column represents the password
Effect screenshot:

Add the following code to the teacher branch of the Login function to verify the teacher identity
1 | //教师登录验证 |
Effect screenshot of the added code

Test:

Administrator Login Implementation
Add one administrator record to the admin.txt file. Since we only have one administrator, this case does not include the function of adding an administrator
Add the information:
1 | admin 123 |
其中:admin代表管理员用户名,123代表管理员密码
效果图:

在Login函数的管理员分支中加入如下代码,验证管理员身份
Where: admin represents the administrator username, and 123 represents the administrator password
Effect screenshot:

Add the following code to the administrator branch of the Login function to verify the administrator identity
1 | //管理员登录验证 |
The effect of adding is shown below:

The test effect is shown below:

At this point, the login functions for all identities are fully implemented!
Administrator Module
Administrator Login and Logout
Constructor
- In the constructor of the Manager class, initialize the administrator information as follows:
1 | //有参构造 |
管理员子菜单
- 在机房预约系统.cpp中,当用户登录的是管理员,添加管理员菜单接口
- 将不同的分支提供出来
- 添加账号
- 查看账号
- 查看机房
- 清空预约
- 注销登录
- 实现注销功能
添加全局函数 void managerMenu(Identity * &manager),代码如下:
Administrator Submenu
- In 机房预约系统.cpp, when the user logs in as an administrator, add the administrator menu interface
- Provide different branches
- Add an account
- View accounts
- View computer rooms
- Clear reservations
- Log out
- Implement the logout function
Add the global function void managerMenu(Identity * &manager) as follows:
1 | //管理员菜单 |
1 | //选择菜单 |
Interface Integration
- After the administrator logs in successfully, call the administrator submenu interface
- Add code in the administrator login verification branch:
1 | //进入管理员子菜单 |
添加效果如:

测试对接,效果如图:

登录成功

注销登录:

至此,管理员身份可以成功登录以及注销
添加账号
功能描述:
- 给学生或教师添加新的账号
功能要求:
- 添加时学生学号不能重复、教师职工号不能重复
添加功能实现
在Manager的addPerson成员函数中,实现添加新账号功能,代码如下:
The effect of adding is shown below:

Test the integration; the effect is shown below:

Login successful

Log out:

At this point, the administrator identity can log in and log out successfully
Adding Accounts
Function description:
- Add new accounts for students or teachers
Function requirements:
- When adding, student IDs must not be duplicated, and teacher employee IDs must not be duplicated
Adding Function Implementation
In the addPerson member function of Manager, implement the function of adding new accounts as follows:
1 | //添加账号 |
测试添加学生:

成功在学生文件中添加了一条信息

测试添加教师:

成功在教师文件中添加了一条信息

去重操作
功能描述:添加新账号时,如果是重复的学生编号,或是重复的教师职工编号,提示有误
读取信息
- 要去除重复的账号,首先要先将学生和教师的账号信息获取到程序中,方可检测
- 在manager.h中,添加两个容器,用于存放学生和教师的信息
- 添加一个新的成员函数
void initVector()初始化容器
Test adding a student:

A record was successfully added to the student file

Test adding a teacher:

A record was successfully added to the teacher file

Deduplication Operation
Function description: when adding a new account, if the student ID or teacher employee ID is duplicated, prompt an error
Reading Information
- To remove duplicate accounts, first load the student and teacher account information into the program so that they can be detected
- Add two containers in manager.h to store student and teacher information
- Add a new member function
void initVector()to initialize the containers
1 | //初始化容器 |
添加位置如图:

在Manager的有参构造函数中,获取目前的学生和教师信息
代码如下:
The addition position is shown below:

In the parameterized constructor of Manager, obtain the current student and teacher information
The code is as follows:
1 | void Manager::initVector() |
在有参构造函数中,调用初始化容器函数
In the parameterized constructor, call the function that initializes the containers
1 | //有参构造 |
Test: running the code shows that the test code obtains the current number of students and teachers

Encapsulating the Deduplication Function
Add the member function bool checkRepeat(int id, int type); in the manager.h file
1 | //检测重复 参数:(传入id,传入类型) 返回值:(true 代表有重复,false代表没有重复) |
在manager.cpp文件中实现成员函数 bool checkRepeat(int id, int type);
In the manager.cpp file, implement the member function bool checkRepeat(int id, int type);
1 | bool Manager::checkRepeat(int id, int type) |
Adding the Deduplication Operation
When adding a student ID or teacher employee ID, detect whether it is duplicated; the code is as follows:
1 | string errorTip; //重复错误提示 |
代码位置如图:

检测效果:

bug解决
bug描述:
- 虽然可以检测重复的账号,但是刚添加的账号由于没有更新到容器中,因此不会做检测
- 导致刚加入的账号的学生号或者职工编号,再次添加时依然可以重复
解决方案:
- 在每次添加新账号时,重新初始化容器
在添加完毕后,加入代码:
The code position is shown below:

Detection effect:

Bug Fix
Bug description:
- Although duplicate accounts can be detected, newly added accounts are not updated into the container, so they are not detected
- As a result, the student ID or employee ID of a newly added account can still be duplicated when added again
Solution:
- Re-initialize the container every time a new account is added
After adding, add the code:
1 | //初始化容器 |
The position is shown below:

Test again: a newly added account can no longer be added repeatedly!
Viewing Accounts
Function description: display student information or teacher information
Viewing Function Implementation
In the showPerson member function of Manager, implement the account viewing function as follows:
1 | void printStudent(Student & s) |
测试
测试查看学生效果

测试查看教师效果

至此,显示账号功能实现完毕
查看机房
添加机房信息
案例需求中,机房一共有三个,其中1号机房容量20台机器,2号50台,3号100台
我们可以将信息录入到computerRoom.txt中

机房类创建
在头文件下,创建新的文件 computerRoom.h
并添加如下代码:
Test
Test viewing the student effect

Test viewing the teacher effect

At this point, the account viewing function is fully implemented
Viewing Computer Rooms
Adding Computer Room Information
According to the case requirements, there are three computer rooms in total: Room 1 has a capacity of 20 machines, Room 2 has 50, and Room 3 has 100
We can enter the information into computerRoom.txt

Creating the Computer Room Class
Create a new file computerRoom.h under the header files
And add the following code:
1 |
|
Initializing Computer Room Information
Under the Manager administrator class, add a container for computer rooms to store computer room information
1 | //机房容器 |
在Manager有参构造函数中,追加如下代码,初始化机房信息
In the parameterized constructor of Manager, append the following code to initialize the computer room information
1 | //获取机房信息 |
The position is shown below:

Since the computer room information will not change in the current version, if there is a modification function later, it is best to encapsulate it in a function for easy maintenance
Viewing Computer Room Information
Add the following code to the showComputer member function of the Manager class:
1 | //查看机房信息 |
Test the computer room information display function:

Clearing Reservations
Function description:
Clear the generated order.txt reservation file
Clearing Function Implementation
Add the following code to the cleanFile member function of Manager:
1 | //清空预约记录 |
测试清空,可以随意写入一些信息在order.txt中,然后调用cleanFile清空文件接口,查看是否清空干净
学生模块
学生登录和注销
构造函数
- 在Student类的构造函数中,初始化学生信息,代码如下:
Test clearing: you can write some information into order.txt at will, then call the cleanFile file-clearing interface to check whether it is cleared thoroughly
1 | //有参构造(学号、姓名、密码) |
管理员子菜单
- 在机房预约系统.cpp中,当用户登录的是学生,添加学生菜单接口
- 将不同的分支提供出来
- 申请预约
- 查看我的预约
- 查看所有预约
- 取消预约
- 注销登录
- 实现注销功能
添加全局函数 void studentMenu(Identity * &manager) 代码如下:
Constructor
- In the constructor of the Student class, initialize the student information as follows:
Administrator Submenu
- In 机房预约系统.cpp, when the user logs in as a student, add the student menu interface
- Provide different branches
- Apply for a reservation
- View my reservations
- View all reservations
- Cancel a reservation
- Log out
- Implement the logout function
Add the global function void studentMenu(Identity * &manager) as follows:
1 | //学生菜单 |
1 | //菜单界面 |
Interface Integration
- After the student logs in successfully, call the student submenu interface
- Add code in the student login branch:
1 | //进入学生子菜单 |
添加效果如图:

测试对接,效果如图:
登录验证通过:

学生子菜单:

注销登录:

申请预约
获取机房信息
- 在申请预约时,学生可以看到机房的信息,因此我们需要让学生获取到机房的信息
在student.h中添加新的成员函数如下:
The effect of adding is shown below:

Test the integration; the effect is shown below:
Login verification passed:

Student submenu:

Log out:

Applying for a Reservation
Obtaining Computer Room Information
- When applying for a reservation, the student can see the computer room information, so we need to let the student obtain the computer room information
Add a new member function in student.h as follows:
1 | //机房容器 |
在学生的有参构造函数中追加如下代码:
Append the following code in the parameterized constructor of the student:
1 | //获取机房信息 |
The appended position is shown below:

At this point, the vCom container stores all computer room information
Reservation Function Implementation
In student.cpp, implement the member function void Student::applyOrder()
1 | //申请预约 |
运行程序,测试代码:

在order.txt文件中生成如下内容:

显示预约
创建预约类
功能描述:显示预约记录时,需要从文件中获取到所有记录,用来显示,创建预约的类来管理记录以及更新
在头文件以及源文件下分别创建orderFile.h 和 orderFile.cpp文件
orderFile.h中添加如下代码:
Run the program and test the code:

The following content is generated in the order.txt file:

Viewing Reservations
Creating the Reservation Class
Function description: when displaying reservation records, all records need to be obtained from the file for display; create a reservation class to manage and update the records
Create the orderFile.h and orderFile.cpp files under the header files and source files respectively
Add the following code to orderFile.h:
1 |
|
构造函数中获取所有信息,并存放在容器中,添加如下代码:
In the constructor, obtain all information and store it in a container; add the following code:
1 | OrderFile::OrderFile() |
更新预约记录的成员函数updateOrder代码如下:
The updateOrder member function for updating reservation records is as follows:
1 | void OrderFile::updateOrder() |
显示自身预约
首先我们先添加几条预约记录,可以用程序添加或者直接修改order.txt文件
order.txt文件内容如下: 比如我们有三名同学分别产生了3条预约记录

在Student类的void Student::showMyOrder()成员函数中,添加如下代码
Viewing My Reservations
First, let’s add a few reservation records; we can add them with the program or directly modify the order.txt file
The content of the order.txt file is as follows: for example, three students have generated 3 reservation records respectively

Add the following code in the void Student::showMyOrder() member function of the Student class
1 | //查看我的预约 |
The test effect is shown below:

Viewing All Reservations
Add the following code in the void Student::showAllOrder() member function of the Student class
1 | //查看所有预约 |
The test effect is shown below:

Canceling a Reservation
Add the following code in the void Student::cancelOrder() member function of the Student class
1 | //取消预约 |
测试取消预约:

再次查看个人预约记录:

查看所有预约

查看order.txt预约文件

至此,学生模块功能全部实现
教师模块
教师登录和注销
构造函数
- 在Teacher类的构造函数中,初始化教师信息,代码如下:
Test canceling a reservation:

View my reservation records again:

View all reservations

View the order.txt reservation file

At this point, all student module functions are fully implemented
Teacher Module
Teacher Login and Logout
Constructor
- In the constructor of the Teacher class, initialize the teacher information as follows:
1 | //有参构造 (职工编号,姓名,密码) |
教师子菜单
- 在机房预约系统.cpp中,当用户登录的是教师,添加教师菜单接口
- 将不同的分支提供出来
- 查看所有预约
- 审核预约
- 注销登录
- 实现注销功能
添加全局函数 void TeacherMenu(Person * &manager) 代码如下:
Teacher Submenu
- In 机房预约系统.cpp, when the user logs in as a teacher, add the teacher menu interface
- Provide different branches
- View all reservations
- Review reservations
- Log out
- Implement the logout function
Add the global function void TeacherMenu(Person * &manager) as follows:
1 | //教师菜单 |
1 | //教师菜单界面 |
Interface Integration
- After the teacher logs in successfully, call the teacher submenu interface
- Add code in the teacher login branch:
1 | //进入教师子菜单 |
添加效果如图:

测试对接,效果如图:
登录验证通过:

教师子菜单:

注销登录:

查看所有预约
所有预约功能实现
该功能与学生身份的查看所有预约功能相似,用于显示所有预约记录
在Teacher.cpp中实现成员函数 void Teacher::showAllOrder()
The effect of adding is shown below:

Test the integration; the effect is shown below:
Login verification passed:

Teacher submenu:

Log out:

Viewing All Reservations
All Reservations Function Implementation
This function is similar to the view-all-reservations function of the student identity; it is used to display all reservation records
In Teacher.cpp, implement the member function void Teacher::showAllOrder()
1 | void Teacher::showAllOrder() |
测试功能
运行测试教师身份的查看所有预约功能
测试效果如图:

审核预约
审核功能实现
功能描述:教师审核学生的预约,依据实际情况审核预约
在Teacher.cpp中实现成员函数 void Teacher::validOrder()
代码如下:
Test Function
Run to test the view-all-reservations function of the teacher identity
The test effect is shown below:

Reviewing Reservations
Review Function Implementation
Function description: the teacher reviews students’ reservations based on the actual situation
In Teacher.cpp, implement the member function void Teacher::validOrder()
The code is as follows:
1 | //审核预约 |
测试审核预约
测试 - 审核通过

审核通过情况

测试-审核未通过

审核未通过情况:

学生身份下查看记录:

审核预约成功!
至此本案例制作完毕! ^_^
我的实现
头文件
身份抽象类----Identity.h:
Testing Reservation Review
Test - review passed

Review passed situation

Test - review not passed

Review not passed situation:

View the records under the student identity:

Reservation review succeeded!
At this point, this case is complete! ^_^
1 |
|
管理员类----manager.h:
Administrator class----manager.h:
1 |
|
学生类----student.h:
Student class----student.h:
1 |
|
教师类----teacher.h:
Teacher class----teacher.h:
1 |
|
文件管理类----fileManage.h:
File management class----fileManage.h:
1 |
|
文件名----filename.h:
File names----filename.h:
1 |
1 |
|
管理员类----manager.cpp:
Administrator class----manager.cpp:
1 |
|
学生类----student.cpp:
Student class----student.cpp:
1 |
|
教师类----teacher.cpp:
Teacher class----teacher.cpp:
1 |
|
文件管理类----filemanage.cpp:
File management class----filemanage.cpp:
1 |
|


