机房预约系统需求

系统简介

学校现有几个规格不同的机房,由于使用时经常出现"撞车"现象,现开发一套机房预约系统,解决这一问题。

1548682783885

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.

1548682783885

身份简介

分别有三种身份使用该程序

  • 学生代表:申请使用机房
  • 教师:审核学生的预约申请
  • 管理员:给学生、教师创建账号

机房简介

机房总共有3间

  • 1号机房 — 最大容量20人
  • 2号机房 — 最多容量50人
  • 3号机房 — 最多容量100人

申请简介

  • 申请的订单每周由管理员负责清空。
  • 学生可以预约未来一周内的机房使用,预约的日期为周一至周五,预约时需要选择预约时段(上午、下午)
  • 教师来审核预约,依据实际情况审核预约通过或者不通过

系统具体需求

  • 首先进入登录界面,可选登录身份有:
    • 学生代表
    • 教师
    • 管理员
    • 退出
  • 每个身份都需要进行验证后,进入子菜单
    • 学生需要输入 :学号、姓名、登录密码
    • 教师需要输入:职工号、姓名、登录密码
    • 管理员需要输入:管理员姓名、登录密码
  • 学生具体功能
    • 申请预约 — 预约机房
    • 查看自身的预约 — 查看自己的预约状态
    • 查看所有预约 — 查看全部预约信息以及预约状态
    • 取消预约 — 取消自身的预约,预约成功或审核中的预约均可取消
    • 注销登录 — 退出登录
  • 教师具体功能
    • 查看所有预约 — 查看全部预约信息以及预约状态
    • 审核预约 — 对学生的预约进行审核
    • 注销登录 — 退出登录
  • 管理员具体功能
    • 添加账号 — 添加学生或教师的账号,需要检测学生编号或教师职工号是否重复
    • 查看账号 — 可以选择查看学生或教师的全部信息
    • 查看机房 — 查看所有机房的信息
    • 清空预约 — 清空所有预约记录
    • 注销登录 — 退出登录

1548682206670

创建项目

创建项目步骤如下:

  • 创建新项目
  • 添加文件

创建项目

  • 打开vs2017后,点击创建新项目,创建新的C++项目

如图:

1548682413343

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

1548682522544

添加文件

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

1548682597721

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

1548682679989

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

1548682733912

创建主菜单

功能描述:

  • 设计主菜单,与用户进行交互

菜单实现

  • 在主函数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

1548682206670

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:

1548682413343

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

1548682522544

Adding Files

  • Right-click Source Files and add a file

1548682597721

  • Fill in the file name and click Add

1548682679989

  • The file is generated successfully, as shown below

1548682733912

Creating the Main Menu

Function description:

  • Design the main menu to interact with the user
  • Add the menu prompt in the main function main as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int main() {

cout << "====================== 欢迎来到传智播客机房预约系统 ====================="
<< endl;
cout << endl << "请输入您的身份" << endl;
cout << "\t\t -------------------------------\n";
cout << "\t\t| |\n";
cout << "\t\t| 1.学生代表 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 2.老 师 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 3.管 理 员 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 0.退 出 |\n";
cout << "\t\t| |\n";
cout << "\t\t -------------------------------\n";
cout << "输入您的选择: ";

system("pause");

return 0;
}

运行效果如图:

1548557945611

搭建接口

  • 接受用户的选择,搭建接口
  • 在main中添加代码

The running effect is shown below:

1548557945611

Building the Interface

  • Accept the user’s choice and build the interface
  • Add code in main
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
int main() {

int select = 0;

while (true)
{

cout << "====================== 欢迎来到传智播客机房预约系统 =====================" << endl;
cout << endl << "请输入您的身份" << endl;
cout << "\t\t -------------------------------\n";
cout << "\t\t| |\n";
cout << "\t\t| 1.学生代表 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 2.老 师 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 3.管 理 员 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 0.退 出 |\n";
cout << "\t\t| |\n";
cout << "\t\t -------------------------------\n";
cout << "输入您的选择: ";

cin >> select; //接受用户选择

switch (select)
{
case 1: //学生身份
break;
case 2: //老师身份
break;
case 3: //管理员身份
break;
case 0: //退出系统
break;
default:
cout << "输入有误,请重新选择!" << endl;
system("pause");
system("cls");
break;
}

}
system("pause");
return 0;
}

测试,输入0、1、2、3会重新回到界面,输入其他提示输入有误,清屏后重新选择

效果如图:

1548558694230

至此,界面搭建完毕

退出功能实现

退出功能实现

在main函数分支 0 选项中,添加退出程序的代码:

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:

1548558694230

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
2
3
cout << "欢迎下一次使用"<<endl;
system("pause");
return 0;

1548558992754

测试退出功能

运行程序,效果如图:

1548559026436

至此,退出程序功能实现

创建身份类

身份的基类

  • 在整个系统中,有三种身份,分别为:学生代表、老师以及管理员
  • 三种身份有其共性也有其特性,因此我们可以将三种身份抽象出一个身份基类identity
  • 在头文件下创建Identity.h文件

Identity.h中添加如下代码:

1548558992754

Testing the Exit Function

Run the program; the effect is shown below:

1548559026436

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#pragma once
#include<iostream>
using namespace std;

//身份抽象类
class Identity
{
public:

//操作菜单
virtual void operMenu() = 0;

string m_Name; //用户名
string m_Pwd; //密码
};

效果如图:

1548573329273

学生类

功能分析

  • 学生类主要功能是可以通过类中成员函数,实现预约实验室操作

  • 学生类中主要功能有:

    • 显示学生操作的菜单界面
    • 申请预约
    • 查看自身预约
    • 查看所有预约
    • 取消预约

类的创建

  • 在头文件以及源文件下创建 student.h 和 student.cpp文件

student.h中添加如下代码:

The effect is shown below:

1548573329273

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#pragma once
#include<iostream>
using namespace std;
#include "identity.h"

//学生类
class Student :public Identity
{
public:
//默认构造
Student();

//有参构造(学号、姓名、密码)
Student(int id, string name, string pwd);

//菜单界面
virtual void operMenu();

//申请预约
void applyOrder();

//查看我的预约
void showMyOrder();

//查看所有预约
void showAllOrder();

//取消预约
void cancelOrder();

//学生学号
int m_Id;

};

student.cpp中添加如下代码:

Add the following code to student.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#include "student.h"

//默认构造
Student::Student()
{
}

//有参构造(学号、姓名、密码)
Student::Student(int id, string name, string pwd)
{
}

//菜单界面
void Student::operMenu()
{
}

//申请预约
void Student::applyOrder()
{

}

//查看我的预约
void Student::showMyOrder()
{

}

//查看所有预约
void Student::showAllOrder()
{

}

//取消预约
void Student::cancelOrder()
{

}

老师类

功能分析

  • 教师类主要功能是查看学生的预约,并进行审核

  • 教师类中主要功能有:

    • 显示教师操作的菜单界面

    • 查看所有预约

    • 审核预约

类的创建

  • 在头文件以及源文件下创建 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#pragma once
#define _CRT_SECURE_NO_WARNINGS
#include<iostream>
using namespace std;
#include "identity.h"

class Teacher :public Identity
{
public:

//默认构造
Teacher();

//有参构造 (职工编号,姓名,密码)
Teacher(int empId, string name, string pwd);

//菜单界面
virtual void operMenu();

//查看所有预约
void showAllOrder();

//审核预约
void validOrder();

int m_EmpId; //教师编号

};
  • teacher.cpp中添加如下代码:
  • Add the following code to teacher.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
#include"teacher.h"

//默认构造
Teacher::Teacher()
{
}

//有参构造 (职工编号,姓名,密码)
Teacher::Teacher(int empId, string name, string pwd)
{
}

//菜单界面
void Teacher::operMenu()
{
}

//查看所有预约
void Teacher::showAllOrder()
{
}

//审核预约
void Teacher::validOrder()
{
}

管理员类

功能分析

  • 管理员类主要功能是对学生和老师账户进行管理,查看机房信息以及清空预约记录

  • 管理员类中主要功能有:

    • 显示管理员操作的菜单界面

    • 添加账号

    • 查看账号

    • 查看机房信息

    • 清空预约记录

类的创建

  • 在头文件以及源文件下创建 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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#pragma once
#include<iostream>
using namespace std;
#include "identity.h"

class Manager :public Identity
{
public:

//默认构造
Manager();

//有参构造 管理员姓名,密码
Manager(string name, string pwd);

//选择菜单
virtual void operMenu();

//添加账号
void addPerson();

//查看账号
void showPerson();

//查看机房信息
void showComputer();

//清空预约记录
void cleanFile();

};
  • manager.cpp中添加如下代码:
  • Add the following code to manager.cpp:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
#include "manager.h"

//默认构造
Manager::Manager()
{
}

//有参构造
Manager::Manager(string name, string pwd)
{
}

//选择菜单
void Manager::operMenu()
{
}

//添加账号
void Manager::addPerson()
{
}

//查看账号
void Manager::showPerson()
{
}

//查看机房信息
void Manager::showComputer()
{
}

//清空预约记录
void Manager::cleanFile()
{
}

至此,所有身份类创建完毕,效果如图:

1548574390768

登录模块

全局文件添加

功能描述:

  • 不同的身份可能会用到不同的文件操作,我们可以将所有的文件名定义到一个全局的文件中
  • 在头文件中添加 globalFile.h 文件
  • 并添加如下代码:

At this point, all identity classes are created; the effect is shown below:

1548574390768

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
2
3
4
5
6
7
8
9
10
11
12
#pragma once 

//管理员文件
#define ADMIN_FILE "admin.txt"
//学生文件
#define STUDENT_FILE "student.txt"
//教师文件
#define TEACHER_FILE "teacher.txt"
//机房信息文件
#define COMPUTER_FILE "computerRoom.txt"
//订单文件
#define ORDER_FILE "order.txt"

并且在同级目录下,创建这几个文件

1548575650130

登录函数封装

功能描述:

  • 根据用户的选择,进入不同的身份登录

在预约系统的.cpp文件中添加全局函数 void LoginIn(string fileName, int type)

参数:

  • fileName — 操作的文件名
  • type — 登录的身份 (1代表学生、2代表老师、3代表管理员)

LoginIn中添加如下代码:

Also create these files in the same directory

1548575650130

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
#include "globalFile.h"
#include "identity.h"
#include <fstream>
#include <string>


//登录功能
void LoginIn(string fileName, int type)
{

Identity * person = NULL;

ifstream ifs;
ifs.open(fileName, ios::in);

//文件不存在情况
if (!ifs.is_open())
{
cout << "文件不存在" << endl;
ifs.close();
return;
}

int id = 0;
string name;
string pwd;

if (type == 1) //学生登录
{
cout << "请输入你的学号" << endl;
cin >> id;
}
else if (type == 2) //教师登录
{
cout << "请输入你的职工号" << endl;
cin >> id;
}

cout << "请输入用户名:" << endl;
cin >> name;

cout << "请输入密码: " << endl;
cin >> pwd;


if (type == 1)
{
//学生登录验证
}
else if (type == 2)
{
//教师登录验证
}
else if(type == 3)
{
//管理员登录验证
}

cout << "验证登录失败!" << endl;

system("pause");
system("cls");
return;
}
  • 在main函数的不同分支中,填入不同的登录接口

1548575945985

学生登录实现

在student.txt文件中添加两条学生信息,用于测试

添加信息:

  • Fill in different login interfaces in different branches of the main function

1548575945985

Student Login Implementation

Add two student records to the student.txt file for testing

Add the information:

1
2
1 张三 123
2 李四 123456

其中:

  • 第一列 代表 学号
  • 第二列 代表 学生姓名
  • 第三列 代表 密码

效果图:

1548583693555

在Login函数的学生分支中加入如下代码,验证学生身份

Where:

  • The first column represents the student ID
  • The second column represents the student name
  • The third column represents the password

Effect screenshot:

1548583693555

Add the following code to the student branch of the Login function to verify the student identity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//学生登录验证
int fId;
string fName;
string fPwd;
while (ifs >> fId && ifs >> fName && ifs >> fPwd)
{
if (id == fId && name == fName && pwd == fPwd)
{
cout << "学生验证登录成功!" << endl;
system("pause");
system("cls");
person = new Student(id, name, pwd);

return;
}
}

添加代码效果图

1548583915819

测试:

1548583950828

教师登录实现

在teacher.txt文件中添加一条老师信息,用于测试

添加信息:

Effect screenshot of the added code

1548583915819

Test:

1548583950828

Teacher Login Implementation

Add one teacher record to the teacher.txt file for testing

Add the information:

1
1 老王 123

其中:

  • 第一列 代表 教师职工编号
  • 第二列 代表 教师姓名
  • 第三列 代表 密码

效果图:

1548584030522

在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:

1548584030522

Add the following code to the teacher branch of the Login function to verify the teacher identity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//教师登录验证
int fId;
string fName;
string fPwd;
while (ifs >> fId && ifs >> fName && ifs >> fPwd)
{
if (id == fId && name == fName && pwd == fPwd)
{
cout << "教师验证登录成功!" << endl;
system("pause");
system("cls");
person = new Teacher(id, name, pwd);
return;
}
}

添加代码效果图

1548584158339

测试:

1548584177003

管理员登录实现

在admin.txt文件中添加一条管理员信息,由于我们只有一条管理员,因此本案例中没有添加管理员的功能

添加信息:

Effect screenshot of the added code

1548584158339

Test:

1548584177003

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代表管理员密码

效果图:

1548577855190

在Login函数的管理员分支中加入如下代码,验证管理员身份

Where: admin represents the administrator username, and 123 represents the administrator password

Effect screenshot:

1548577855190

Add the following code to the administrator branch of the Login function to verify the administrator identity

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
//管理员登录验证
string fName;
string fPwd;
while (ifs >> fName && ifs >> fPwd)
{
if (name == fName && pwd == fPwd)
{
cout << "验证登录成功!" << endl;
//登录成功后,按任意键进入管理员界面
system("pause");
system("cls");
//创建管理员对象
person = new Manager(name,pwd);
return;
}
}

添加效果如图:

1548588322712

测试效果如图:

1548583245072

至此,所有身份的登录功能全部实现!

管理员模块

管理员登录和注销

构造函数

  • 在Manager类的构造函数中,初始化管理员信息,代码如下:

The effect of adding is shown below:

1548588322712

The test effect is shown below:

1548583245072

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
2
3
4
5
6
//有参构造
Manager::Manager(string name, string pwd)
{
this->m_Name = name;
this->m_Pwd = pwd;
}

管理员子菜单

  • 在机房预约系统.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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//管理员菜单
void managerMenu(Identity * &manager)
{
while (true)
{
//管理员菜单
manager->operMenu();

Manager* man = (Manager*)manager;
int select = 0;

cin >> select;

if (select == 1) //添加账号
{
cout << "添加账号" << endl;
man->addPerson();
}
else if (select == 2) //查看账号
{
cout << "查看账号" << endl;
man->showPerson();
}
else if (select == 3) //查看机房
{
cout << "查看机房" << endl;
man->showComputer();
}
else if (select == 4) //清空预约
{
cout << "清空预约" << endl;
man->cleanFile();
}
else
{
delete manager;
cout << "注销成功" << endl;
system("pause");
system("cls");
return;
}
}
}

菜单功能实现

  • 在实现成员函数void Manager::operMenu() 代码如下:
  • Implement the member function void Manager::operMenu() as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//选择菜单
void Manager::operMenu()
{
cout << "欢迎管理员:"<<this->m_Name << "登录!" << endl;
cout << "\t\t ---------------------------------\n";
cout << "\t\t| |\n";
cout << "\t\t| 1.添加账号 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 2.查看账号 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 3.查看机房 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 4.清空预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 0.注销登录 |\n";
cout << "\t\t| |\n";
cout << "\t\t ---------------------------------\n";
cout << "请选择您的操作: " << endl;
}

接口对接

  • 管理员成功登录后,调用管理员子菜单界面
  • 在管理员登录验证分支中,添加代码:

Interface Integration

  • After the administrator logs in successfully, call the administrator submenu interface
  • Add code in the administrator login verification branch:
1
2
//进入管理员子菜单
managerMenu(person);

添加效果如:

1548589297779

测试对接,效果如图:

1548589344206

登录成功

1548589328710

注销登录:

1548589416576

至此,管理员身份可以成功登录以及注销

添加账号

功能描述:

  • 给学生或教师添加新的账号

功能要求:

  • 添加时学生学号不能重复、教师职工号不能重复

添加功能实现

在Manager的addPerson成员函数中,实现添加新账号功能,代码如下:

The effect of adding is shown below:

1548589297779

Test the integration; the effect is shown below:

1548589344206

Login successful

1548589328710

Log out:

1548589416576

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
//添加账号  
void Manager::addPerson()
{

cout << "请输入添加账号的类型" << endl;
cout << "1、添加学生" << endl;
cout << "2、添加老师" << endl;

string fileName;
string tip;
ofstream ofs;

int select = 0;
cin >> select;

if (select == 1)
{
fileName = STUDENT_FILE;
tip = "请输入学号: ";
}
else
{
fileName = TEACHER_FILE;
tip = "请输入职工编号:";
}

ofs.open(fileName, ios::out | ios::app);
int id;
string name;
string pwd;
cout <<tip << endl;
cin >> id;

cout << "请输入姓名: " << endl;
cin >> name;

cout << "请输入密码: " << endl;
cin >> pwd;

ofs << id << " " << name << " " << pwd << " " << endl;
cout << "添加成功" << endl;

system("pause");
system("cls");

ofs.close();
}

测试添加学生:

1548641024216

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

1548641141027

测试添加教师:

1548641195834

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

1548641237513

去重操作

功能描述:添加新账号时,如果是重复的学生编号,或是重复的教师职工编号,提示有误

读取信息
  • 要去除重复的账号,首先要先将学生和教师的账号信息获取到程序中,方可检测
  • 在manager.h中,添加两个容器,用于存放学生和教师的信息
  • 添加一个新的成员函数 void initVector() 初始化容器

Test adding a student:

1548641024216

A record was successfully added to the student file

1548641141027

Test adding a teacher:

1548641195834

A record was successfully added to the teacher file

1548641237513

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
2
3
4
5
6
7
8
//初始化容器
void initVector();

//学生容器
vector<Student> vStu;

//教师容器
vector<Teacher> vTea;

添加位置如图:

1548644354561

在Manager的有参构造函数中,获取目前的学生和教师信息

代码如下:

The addition position is shown below:

1548644354561

In the parameterized constructor of Manager, obtain the current student and teacher information

The code is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
void Manager::initVector()
{
//读取学生文件中信息
ifstream ifs;
ifs.open(STUDENT_FILE, ios::in);
if (!ifs.is_open())
{
cout << "文件读取失败" << endl;
return;
}

vStu.clear();
vTea.clear();

Student s;
while (ifs >> s.m_Id && ifs >> s.m_Name && ifs >> s.m_Pwd)
{
vStu.push_back(s);
}
cout << "当前学生数量为: " << vStu.size() << endl;
ifs.close(); //学生初始化

//读取老师文件信息
ifs.open(TEACHER_FILE, ios::in);

Teacher t;
while (ifs >> t.m_EmpId && ifs >> t.m_Name && ifs >> t.m_Pwd)
{
vTea.push_back(t);
}
cout << "当前教师数量为: " << vTea.size() << endl;

ifs.close();
}

在有参构造函数中,调用初始化容器函数

In the parameterized constructor, call the function that initializes the containers

1
2
3
4
5
6
7
8
9
//有参构造
Manager::Manager(string name, string pwd)
{
this->m_Name = name;
this->m_Pwd = pwd;

//初始化容器
this->initVector();
}

测试,运行代码可以看到测试代码获取当前学生和教师数量

1548642488049

去重函数封装

在manager.h文件中添加成员函数bool checkRepeat(int id, int type);

Test: running the code shows that the test code obtains the current number of students and teachers

1548642488049

Encapsulating the Deduplication Function

Add the member function bool checkRepeat(int id, int type); in the manager.h file

1
2
//检测重复 参数:(传入id,传入类型) 返回值:(true 代表有重复,false代表没有重复)
bool checkRepeat(int id, int type);

在manager.cpp文件中实现成员函数 bool checkRepeat(int id, int type);

In the manager.cpp file, implement the member function bool checkRepeat(int id, int type);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
bool Manager::checkRepeat(int id, int type)
{
if (type == 1)
{
for (vector<Student>::iterator it = vStu.begin(); it != vStu.end(); it++)
{
if (id == it->m_Id)
{
return true;
}
}
}
else
{
for (vector<Teacher>::iterator it = vTea.begin(); it != vTea.end(); it++)
{
if (id == it->m_EmpId)
{
return true;
}
}
}
return false;
}
添加去重操作

在添加学生编号或者教师职工号时,检测是否有重复,代码如下:

Adding the Deduplication Operation

When adding a student ID or teacher employee ID, detect whether it is duplicated; the code is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
string errorTip; //重复错误提示

if (select == 1)
{
fileName = STUDENT_FILE;
tip = "请输入学号: ";
errorTip = "学号重复,请重新输入";
}
else
{
fileName = TEACHER_FILE;
tip = "请输入职工编号:";
errorTip = "职工号重复,请重新输入";
}
ofs.open(fileName, ios::out | ios::app);
int id;
string name;
string pwd;
cout <<tip << endl;

while (true)
{
cin >> id;

bool ret = this->checkRepeat(id, 1);

if (ret) //有重复
{
cout << errorTip << endl;
}
else
{
break;
}
}

代码位置如图:

1548643909979

检测效果:

1548644151974

bug解决

bug描述:

  • 虽然可以检测重复的账号,但是刚添加的账号由于没有更新到容器中,因此不会做检测
  • 导致刚加入的账号的学生号或者职工编号,再次添加时依然可以重复

解决方案:

  • 在每次添加新账号时,重新初始化容器

在添加完毕后,加入代码:

The code position is shown below:

1548643909979

Detection effect:

1548644151974

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
2
//初始化容器
this->initVector();

位置如图:

1548644779578

再次测试,刚加入的账号不会重复添加了!

显示账号

功能描述:显示学生信息或教师信息

显示功能实现

在Manager的showPerson成员函数中,实现显示账号功能,代码如下:

The position is shown below:

1548644779578

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
void printStudent(Student & s)
{
cout << "学号: " << s.m_Id << " 姓名: " << s.m_Name << " 密码:" << s.m_Pwd << endl;
}
void printTeacher(Teacher & t)
{
cout << "职工号: " << t.m_EmpId << " 姓名: " << t.m_Name << " 密码:" << t.m_Pwd << endl;
}

void Manager::showPerson()
{
cout << "请选择查看内容:" << endl;
cout << "1、查看所有学生" << endl;
cout << "2、查看所有老师" << endl;

int select = 0;

cin >> select;

if (select == 1)
{
cout << "所有学生信息如下: " << endl;
for_each(vStu.begin(), vStu.end(), printStudent);
}
else
{
cout << "所有老师信息如下: " << endl;
for_each(vTea.begin(), vTea.end(), printTeacher);
}
system("pause");
system("cls");
}

测试

测试查看学生效果

1548646791248

测试查看教师效果

1548646833665

至此,显示账号功能实现完毕

查看机房

添加机房信息

案例需求中,机房一共有三个,其中1号机房容量20台机器,2号50台,3号100台

我们可以将信息录入到computerRoom.txt中

1548647538570

机房类创建

在头文件下,创建新的文件 computerRoom.h

并添加如下代码:

Test

Test viewing the student effect

1548646791248

Test viewing the teacher effect

1548646833665

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

1548647538570

Creating the Computer Room Class

Create a new file computerRoom.h under the header files

And add the following code:

1
2
3
4
5
6
7
8
9
10
11
12
#pragma once
#include<iostream>
using namespace std;
//机房类
class ComputerRoom
{
public:

int m_ComId; //机房id号

int m_MaxNum; //机房最大容量
};

初始化机房信息

在Manager管理员类下,添加机房的容器,用于保存机房信息

Initializing Computer Room Information

Under the Manager administrator class, add a container for computer rooms to store computer room information

1
2
//机房容器
vector<ComputerRoom> vCom;

在Manager有参构造函数中,追加如下代码,初始化机房信息

In the parameterized constructor of Manager, append the following code to initialize the computer room information

1
2
3
4
5
6
7
8
9
10
11
12
13
//获取机房信息
ifstream ifs;

ifs.open(COMPUTER_FILE, ios::in);

ComputerRoom c;
while (ifs >> c.m_ComId && ifs >> c.m_MaxNum)
{
vCom.push_back(c);
}
cout << "当前机房数量为: " << vCom.size() << endl;

ifs.close();

位置如图:

1548647976462

因为机房信息目前版本不会有所改动,如果后期有修改功能,最好封装到一个函数中,方便维护

显示机房信息

在Manager类的showComputer成员函数中添加如下代码:

The position is shown below:

1548647976462

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
2
3
4
5
6
7
8
9
10
11
//查看机房信息
void Manager::showComputer()
{
cout << "机房信息如下: " << endl;
for (vector<ComputerRoom>::iterator it = vCom.begin(); it != vCom.end(); it++)
{
cout << "机房编号: " << it->m_ComId << " 机房最大容量: " << it->m_MaxNum << endl;
}
system("pause");
system("cls");
}

测试显示机房信息功能:

1548648276929

清空预约

功能描述:

清空生成的order.txt预约文件

清空功能实现

在Manager的cleanFile成员函数中添加如下代码:

Test the computer room information display function:

1548648276929

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
2
3
4
5
6
7
8
9
10
//清空预约记录
void Manager::cleanFile()
{
ofstream ofs(ORDER_FILE, ios::trunc);
ofs.close();

cout << "清空成功!" << endl;
system("pause");
system("cls");
}

测试清空,可以随意写入一些信息在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
2
3
4
5
6
7
8
//有参构造(学号、姓名、密码)
Student::Student(int id, string name, string pwd)
{
//初始化属性
this->m_Id = id;
this->m_Name = name;
this->m_Pwd = pwd;
}

管理员子菜单

  • 在机房预约系统.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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
//学生菜单
void studentMenu(Identity * &student)
{
while (true)
{
//学生菜单
student->operMenu();

Student* stu = (Student*)student;
int select = 0;

cin >> select;

if (select == 1) //申请预约
{
stu->applyOrder();
}
else if (select == 2) //查看自身预约
{
stu->showMyOrder();
}
else if (select == 3) //查看所有预约
{
stu->showAllOrder();
}
else if (select == 4) //取消预约
{
stu->cancelOrder();
}
else
{
delete student;
cout << "注销成功" << endl;
system("pause");
system("cls");
return;
}
}
}

菜单功能实现

  • 在实现成员函数void Student::operMenu() 代码如下:
  • Implement the member function void Student::operMenu() as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
//菜单界面
void Student::operMenu()
{
cout << "欢迎学生代表:" << this->m_Name << "登录!" << endl;
cout << "\t\t ----------------------------------\n";
cout << "\t\t| |\n";
cout << "\t\t| 1.申请预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 2.查看我的预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 3.查看所有预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 4.取消预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 0.注销登录 |\n";
cout << "\t\t| |\n";
cout << "\t\t ----------------------------------\n";
cout << "请选择您的操作: " << endl;
}

接口对接

  • 学生成功登录后,调用学生的子菜单界面
  • 在学生登录分支中,添加代码:

Interface Integration

  • After the student logs in successfully, call the student submenu interface
  • Add code in the student login branch:
1
2
//进入学生子菜单
studentMenu(person);

添加效果如图:

1548659552298

测试对接,效果如图:

登录验证通过:

1548659590221

学生子菜单:

1548659670651

注销登录:

1548659682402

申请预约

获取机房信息

  • 在申请预约时,学生可以看到机房的信息,因此我们需要让学生获取到机房的信息

在student.h中添加新的成员函数如下:

The effect of adding is shown below:

1548659552298

Test the integration; the effect is shown below:

Login verification passed:

1548659590221

Student submenu:

1548659670651

Log out:

1548659682402

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
2
//机房容器
vector<ComputerRoom> vCom;

在学生的有参构造函数中追加如下代码:

Append the following code in the parameterized constructor of the student:

1
2
3
4
5
6
7
8
9
10
11
//获取机房信息
ifstream ifs;
ifs.open(COMPUTER_FILE, ios::in);

ComputerRoom c;
while (ifs >> c.m_ComId && ifs >> c.m_MaxNum)
{
vCom.push_back(c);
}

ifs.close();

追加位置如图:

1548661562158

至此,vCom容器中保存了所有机房的信息

预约功能实现

在student.cpp中实现成员函数 void Student::applyOrder()

The appended position is shown below:

1548661562158

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//申请预约
void Student::applyOrder()
{
cout << "机房开放时间为周一至周五!" << endl;
cout << "请输入申请预约的时间:" << endl;
cout << "1、周一" << endl;
cout << "2、周二" << endl;
cout << "3、周三" << endl;
cout << "4、周四" << endl;
cout << "5、周五" << endl;
int date = 0;
int interval = 0;
int room = 0;

while (true)
{
cin >> date;
if (date >= 1 && date <= 5)
{
break;
}
cout << "输入有误,请重新输入" << endl;
}


cout << "请输入申请预约的时间段:" << endl;
cout << "1、上午" << endl;
cout << "2、下午" << endl;

while (true)
{
cin >> interval;
if (interval >= 1 && interval <= 2)
{
break;
}
cout << "输入有误,请重新输入" << endl;
}

cout << "请选择机房:" << endl;
cout << "1号机房容量:" << vCom[0].m_MaxNum << endl;
cout << "2号机房容量:" << vCom[1].m_MaxNum << endl;
cout << "3号机房容量:" << vCom[2].m_MaxNum << endl;

while (true)
{
cin >> room;
if (room >= 1 && room <= 3)
{
break;
}
cout << "输入有误,请重新输入" << endl;
}

cout << "预约成功!审核中" << endl;

ofstream ofs(ORDER_FILE, ios::app);
ofs << "date:" << date << " ";
ofs << "interval:" << interval << " ";
ofs << "stuId:" << this->m_Id << " ";
ofs << "stuName:" << this->m_Name << " ";
ofs << "roomId:" << room << " ";
ofs << "status:" << 1 << endl;

ofs.close();

system("pause");
system("cls");
}

运行程序,测试代码:

1548728936052

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

1548662281801

显示预约

创建预约类

功能描述:显示预约记录时,需要从文件中获取到所有记录,用来显示,创建预约的类来管理记录以及更新

在头文件以及源文件下分别创建orderFile.horderFile.cpp文件

orderFile.h中添加如下代码:

Run the program and test the code:

1548728936052

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

1548662281801

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#pragma once
#include<iostream>
using namespace std;
#include <map>
#include "globalFile.h"

class OrderFile
{
public:

//构造函数
OrderFile();

//更新预约记录
void updateOrder();

//记录的容器 key --- 记录的条数 value --- 具体记录的键值对信息
map<int, map<string, string>> m_orderData;

//预约记录条数
int m_Size;
};

构造函数中获取所有信息,并存放在容器中,添加如下代码:

In the constructor, obtain all information and store it in a container; add the following code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
OrderFile::OrderFile()
{
ifstream ifs;
ifs.open(ORDER_FILE, ios::in);

string date; //日期
string interval; //时间段
string stuId; //学生编号
string stuName; //学生姓名
string roomId; //机房编号
string status; //预约状态


this->m_Size = 0; //预约记录个数

while (ifs >> date && ifs >> interval && ifs >> stuId && ifs >> stuName && ifs >> roomId && ifs >> status)
{
//测试代码
/*
cout << date << endl;
cout << interval << endl;
cout << stuId << endl;
cout << stuName << endl;
cout << roomId << endl;
cout << status << endl;
*/

string key;
string value;
map<string, string> m;

int pos = date.find(":");
if (pos != -1)
{
key = date.substr(0, pos);
value = date.substr(pos + 1, date.size() - pos -1);
m.insert(make_pair(key, value));
}

pos = interval.find(":");
if (pos != -1)
{
key = interval.substr(0, pos);
value = interval.substr(pos + 1, interval.size() - pos -1 );
m.insert(make_pair(key, value));
}

pos = stuId.find(":");
if (pos != -1)
{
key = stuId.substr(0, pos);
value = stuId.substr(pos + 1, stuId.size() - pos -1 );
m.insert(make_pair(key, value));
}

pos = stuName.find(":");
if (pos != -1)
{
key = stuName.substr(0, pos);
value = stuName.substr(pos + 1, stuName.size() - pos -1);
m.insert(make_pair(key, value));
}

pos = roomId.find(":");
if (pos != -1)
{
key = roomId.substr(0, pos);
value = roomId.substr(pos + 1, roomId.size() - pos -1 );
m.insert(make_pair(key, value));
}

pos = status.find(":");
if (pos != -1)
{
key = status.substr(0, pos);
value = status.substr(pos + 1, status.size() - pos -1);
m.insert(make_pair(key, value));
}


this->m_orderData.insert(make_pair(this->m_Size, m));
this->m_Size++;
}

//测试代码
//for (map<int, map<string, string>>::iterator it = m_orderData.begin(); it != m_orderData.end();it++)
//{
// cout << "key = " << it->first << " value = " << endl;
// for (map<string, string>::iterator mit = it->second.begin(); mit != it->second.end(); mit++)
// {
// cout << mit->first << " " << mit->second << " ";
// }
// cout << endl;
//}

ifs.close();
}

更新预约记录的成员函数updateOrder代码如下:

The updateOrder member function for updating reservation records is as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
void OrderFile::updateOrder()
{
if (this->m_Size == 0)
{
return;
}

ofstream ofs(ORDER_FILE, ios::out | ios::trunc);
for (int i = 0; i < m_Size;i++)
{
ofs << "date:" << this->m_orderData[i]["date"] << " ";
ofs << "interval:" << this->m_orderData[i]["interval"] << " ";
ofs << "stuId:" << this->m_orderData[i]["stuId"] << " ";
ofs << "stuName:" << this->m_orderData[i]["stuName"] << " ";
ofs << "roomId:" << this->m_orderData[i]["roomId"] << " ";
ofs << "status:" << this->m_orderData[i]["status"] << endl;
}
ofs.close();
}

显示自身预约

首先我们先添加几条预约记录,可以用程序添加或者直接修改order.txt文件

order.txt文件内容如下: 比如我们有三名同学分别产生了3条预约记录

1548667534747

在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

1548667534747

Add the following code in the void Student::showMyOrder() member function of the Student class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
//查看我的预约
void Student::showMyOrder()
{
OrderFile of;
if (of.m_Size == 0)
{
cout << "无预约记录" << endl;
system("pause");
system("cls");
return;
}
for (int i = 0; i < of.m_Size; i++)
{
if (atoi(of.m_orderData[i]["stuId"].c_str()) == this->m_Id)
{
cout << "预约日期: 周" << of.m_orderData[i]["date"];
cout << " 时段:" << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
cout << " 机房:" << of.m_orderData[i]["roomId"];
string status = " 状态: "; // 0 取消的预约 1 审核中 2 已预约 -1 预约失败
if (of.m_orderData[i]["status"] == "1")
{
status += "审核中";
}
else if (of.m_orderData[i]["status"] == "2")
{
status += "预约成功";
}
else if (of.m_orderData[i]["status"] == "-1")
{
status += "审核未通过,预约失败";
}
else
{
status += "预约已取消";
}
cout << status << endl;

}
}

system("pause");
system("cls");
}

测试效果如图:

1548667252474

显示所有预约

在Student类的void Student::showAllOrder()成员函数中,添加如下代码

The test effect is shown below:

1548667252474

Viewing All Reservations

Add the following code in the void Student::showAllOrder() member function of the Student class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
//查看所有预约
void Student::showAllOrder()
{
OrderFile of;
if (of.m_Size == 0)
{
cout << "无预约记录" << endl;
system("pause");
system("cls");
return;
}

for (int i = 0; i < of.m_Size; i++)
{
cout << i + 1 << "、 ";

cout << "预约日期: 周" << of.m_orderData[i]["date"];
cout << " 时段:" << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
cout << " 学号:" << of.m_orderData[i]["stuId"];
cout << " 姓名:" << of.m_orderData[i]["stuName"];
cout << " 机房:" << of.m_orderData[i]["roomId"];
string status = " 状态: "; // 0 取消的预约 1 审核中 2 已预约 -1 预约失败
if (of.m_orderData[i]["status"] == "1")
{
status += "审核中";
}
else if (of.m_orderData[i]["status"] == "2")
{
status += "预约成功";
}
else if (of.m_orderData[i]["status"] == "-1")
{
status += "审核未通过,预约失败";
}
else
{
status += "预约已取消";
}
cout << status << endl;
}

system("pause");
system("cls");
}

测试效果如图:

1548667591734

取消预约

在Student类的void Student::cancelOrder()成员函数中,添加如下代码

The test effect is shown below:

1548667591734

Canceling a Reservation

Add the following code in the void Student::cancelOrder() member function of the Student class

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
//取消预约
void Student::cancelOrder()
{
OrderFile of;
if (of.m_Size == 0)
{
cout << "无预约记录" << endl;
system("pause");
system("cls");
return;
}
cout << "审核中或预约成功的记录可以取消,请输入取消的记录" << endl;

vector<int>v;
int index = 1;
for (int i = 0; i < of.m_Size; i++)
{
if (atoi(of.m_orderData[i]["stuId"].c_str()) == this->m_Id)
{
if (of.m_orderData[i]["status"] == "1" || of.m_orderData[i]["status"] == "2")
{
v.push_back(i);
cout << index ++ << "、 ";
cout << "预约日期: 周" << of.m_orderData[i]["date"];
cout << " 时段:" << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
cout << " 机房:" << of.m_orderData[i]["roomId"];
string status = " 状态: "; // 0 取消的预约 1 审核中 2 已预约 -1 预约失败
if (of.m_orderData[i]["status"] == "1")
{
status += "审核中";
}
else if (of.m_orderData[i]["status"] == "2")
{
status += "预约成功";
}
cout << status << endl;

}
}
}

cout << "请输入取消的记录,0代表返回" << endl;
int select = 0;
while (true)
{
cin >> select;
if (select >= 0 && select <= v.size())
{
if (select == 0)
{
break;
}
else
{
// cout << "记录所在位置: " << v[select - 1] << endl;
of.m_orderData[v[select - 1]]["status"] = "0";
of.updateOrder();
cout << "已取消预约" << endl;
break;
}

}
cout << "输入有误,请重新输入" << endl;
}

system("pause");
system("cls");
}

测试取消预约:

1548669551036

再次查看个人预约记录:

1548669728023

查看所有预约

1548669753496

查看order.txt预约文件

1548669798037

至此,学生模块功能全部实现

教师模块

教师登录和注销

构造函数

  • 在Teacher类的构造函数中,初始化教师信息,代码如下:

Test canceling a reservation:

1548669551036

View my reservation records again:

1548669728023

View all reservations

1548669753496

View the order.txt reservation file

1548669798037

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
2
3
4
5
6
7
8
//有参构造 (职工编号,姓名,密码)
Teacher::Teacher(int empId, string name, string pwd)
{
//初始化属性
this->m_EmpId = empId;
this->m_Name = name;
this->m_Pwd = pwd;
}

教师子菜单

  • 在机房预约系统.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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
//教师菜单
void TeacherMenu(Identity * &teacher)
{
while (true)
{
//教师菜单
teacher->operMenu();

Teacher* tea = (Teacher*)teacher;
int select = 0;

cin >> select;

if (select == 1)
{
//查看所有预约
tea->showAllOrder();
}
else if (select == 2)
{
//审核预约
tea->validOrder();
}
else
{
delete teacher;
cout << "注销成功" << endl;
system("pause");
system("cls");
return;
}

}
}

菜单功能实现

  • 在实现成员函数void Teacher::operMenu() 代码如下:
  • Implement the member function void Teacher::operMenu() as follows:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
//教师菜单界面
void Teacher::operMenu()
{
cout << "欢迎教师:" << this->m_Name << "登录!" << endl;
cout << "\t\t ----------------------------------\n";
cout << "\t\t| |\n";
cout << "\t\t| 1.查看所有预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 2.审核预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 0.注销登录 |\n";
cout << "\t\t| |\n";
cout << "\t\t ----------------------------------\n";
cout << "请选择您的操作: " << endl;
}

接口对接

  • 教师成功登录后,调用教师的子菜单界面
  • 在教师登录分支中,添加代码:

Interface Integration

  • After the teacher logs in successfully, call the teacher submenu interface
  • Add code in the teacher login branch:
1
2
//进入教师子菜单
TeacherMenu(person);

添加效果如图:

1548670866708

测试对接,效果如图:

登录验证通过:

1548670949885

教师子菜单:

1548670958602

注销登录:

1548670966988

查看所有预约

所有预约功能实现

该功能与学生身份的查看所有预约功能相似,用于显示所有预约记录

在Teacher.cpp中实现成员函数 void Teacher::showAllOrder()

The effect of adding is shown below:

1548670866708

Test the integration; the effect is shown below:

Login verification passed:

1548670949885

Teacher submenu:

1548670958602

Log out:

1548670966988

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
void Teacher::showAllOrder()
{
OrderFile of;
if (of.m_Size == 0)
{
cout << "无预约记录" << endl;
system("pause");
system("cls");
return;
}
for (int i = 0; i < of.m_Size; i++)
{
cout << i + 1 << "、 ";

cout << "预约日期: 周" << of.m_orderData[i]["date"];
cout << " 时段:" << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
cout << " 学号:" << of.m_orderData[i]["stuId"];
cout << " 姓名:" << of.m_orderData[i]["stuName"];
cout << " 机房:" << of.m_orderData[i]["roomId"];
string status = " 状态: "; // 0 取消的预约 1 审核中 2 已预约 -1 预约失败
if (of.m_orderData[i]["status"] == "1")
{
status += "审核中";
}
else if (of.m_orderData[i]["status"] == "2")
{
status += "预约成功";
}
else if (of.m_orderData[i]["status"] == "-1")
{
status += "审核未通过,预约失败";
}
else
{
status += "预约已取消";
}
cout << status << endl;
}

system("pause");
system("cls");
}

测试功能

运行测试教师身份的查看所有预约功能

测试效果如图:

1548676922678

审核预约

审核功能实现

功能描述:教师审核学生的预约,依据实际情况审核预约

在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:

1548676922678

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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
//审核预约
void Teacher::validOrder()
{
OrderFile of;
if (of.m_Size == 0)
{
cout << "无预约记录" << endl;
system("pause");
system("cls");
return;
}
cout << "待审核的预约记录如下:" << endl;

vector<int>v;
int index = 0;
for (int i = 0; i < of.m_Size; i++)
{
if (of.m_orderData[i]["status"] == "1")
{
v.push_back(i);
cout << ++index << "、 ";
cout << "预约日期: 周" << of.m_orderData[i]["date"];
cout << " 时段:" << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
cout << " 机房:" << of.m_orderData[i]["roomId"];
string status = " 状态: "; // 0取消的预约 1 审核中 2 已预约 -1 预约失败
if (of.m_orderData[i]["status"] == "1")
{
status += "审核中";
}
cout << status << endl;
}
}
cout << "请输入审核的预约记录,0代表返回" << endl;
int select = 0;
int ret = 0;
while (true)
{
cin >> select;
if (select >= 0 && select <= v.size())
{
if (select == 0)
{
break;
}
else
{
cout << "请输入审核结果" << endl;
cout << "1、通过" << endl;
cout << "2、不通过" << endl;
cin >> ret;

if (ret == 1)
{
of.m_orderData[v[select - 1]]["status"] = "2";
}
else
{
of.m_orderData[v[select - 1]]["status"] = "-1";
}
of.updateOrder();
cout << "审核完毕!" << endl;
break;
}
}
cout << "输入有误,请重新输入" << endl;
}

system("pause");
system("cls");
}

测试审核预约

测试 - 审核通过

1548677286679

审核通过情况

1548677383681

测试-审核未通过

1548677402705

审核未通过情况:

1548677632792

学生身份下查看记录:

1548677798815

审核预约成功!

至此本案例制作完毕! ^_^

我的实现

头文件

身份抽象类----Identity.h:

Testing Reservation Review

Test - review passed

1548677286679

Review passed situation

1548677383681

Test - review not passed

1548677402705

Review not passed situation:

1548677632792

View the records under the student identity:

1548677798815

Reservation review succeeded!

At this point, this case is complete! ^_^

1
2
3
4
5
6
7
8
9
10
11
#pragma once
#include <string>
#include <iostream>
using namespace std;

class Identity {
public:
virtual void showMenu() = 0;
string m_Name;
string m_pwd;
};

管理员类----manager.h:

Administrator class----manager.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#pragma once
#include <string>
#include "identity.h"
#include "fileManage.h"
using namespace std;

class Manager:public Identity {
public:
Manager();
Manager(string name, string pwd, FileManage &fm);
~Manager();
virtual void showMenu(); // 显示菜单
void addUser(); // 添加账号
void showUserInfo(); // 查看账号
void showCompRoomInfo(); // 查看机房
void cleanOrder(); // 清空预约
void signOut(); // 注销登录
public:
FileManage fm;
int select;
};

学生类----student.h:

Student class----student.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
#pragma once

#include "identity.h"
#include "fileManage.h"
#include <string>
#include <iostream>
using namespace std;

class Student :public Identity {
public:
string m_ID; // 学号
FileManage fm;
public:
Student();
Student(string id, string name, string pwd,FileManage& fm);
~Student();
virtual void showMenu();


// 申请预约
void applyOrder();

// 查看自身预约
void showMyOrder();

// 查看所有预约
void showAllOrder();

// 取消预约
void undoOrder();

// 注销登录
void signOut();
};

教师类----teacher.h:

Teacher class----teacher.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#pragma once
#include "identity.h"
#include "fileManage.h"
#include <string>

class Teacher :public Identity {
public:
string m_ID; // 职工号
FileManage fm;
public:
Teacher();
~Teacher();
Teacher(string id, string name, string pwd,FileManage &fm);

virtual void showMenu();

void showAllOrder(); // 查看所有预约
void auditOrder(); // 审核预约
void signOut(); // 注销登录

};

文件管理类----fileManage.h:

File management class----fileManage.h:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include <vector>
#include <map>
#include "filename.h"
#include <algorithm>
#include <functional>
using namespace std;

class FileManage {
public:
FileManage();
~FileManage();
bool signIn(int type, string id, string name, string pwd);
bool signIn(int type, string name, string pwd);
bool fileEmptyOrNotExist(string filename);
void init_vStu();
void init_vTea();
void init_vAd();
void init_vCompRoom();
void init_vOrder();
bool checkID(int type,string id);
void save_vStu();
void save_vTea();
void save_vOrder();
void showStuInfo();
void showTeaInfo();
void showAllOrder();
void showCompRoomInfo();
void cleanOrder();
public:
vector<vector<string>> vStu; // 学生容器
vector<vector<string>> vTea; // 教师容器
vector<vector<string>> vAd; // 管理员容器
vector<vector<int>> vCompRoom; // 机房信息容器
vector<map<string,string>> vOrder; // 预约记录容器
};

文件名----filename.h:

File names----filename.h:

1
2
3
4
5
6
7
8
#pragma once

#define ADMIN "admin.txt"
#define TEACHER "teacher.txt"
#define STUDENT "student.txt"
#define COMPUTER "computerroom.txt"
#define ORDER "order.txt"

源文件

主函数文件----机房预约管理系统.cpp:

Source Files

Main function file----机房预约管理系统.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <iostream>
#include "filename.h"
#include <string>
#include "identity.h"
#include "manager.h"
#include "student.h"
#include "teacher.h"
#include "fileManage.h"
using namespace std;

// 显示主页面菜单
void showMenu();
void signIn(int type);


int main() {


int select;

while (true) {
showMenu();
cout << "请输入您的选择:" << endl;
cin >> select;
switch (select) {
case 1:
signIn(1);
break;
case 2:
signIn(2);
break;
case 3:
signIn(3);
break;
case 0:
cout << "感谢您的使用!再见!" << endl;
system("pause");
exit(0);
break;
default:
cout << "输入有误!" << endl;
system("pause");
system("cls");
break;
}

}



system("pause");
return 0;
}


void showMenu() {
cout << "===================== 欢迎使用机房预约系统 =====================" << endl<<endl;
cout << "请输入您的身份" << endl;
cout << "\t\t ---------------------------------" << endl
<< "\t\t| |" << endl
<< "\t\t| 1. 学生代表 |" << endl
<< "\t\t| |" << endl
<< "\t\t| 2. 老 师 |" << endl
<< "\t\t| |" << endl
<< "\t\t| 3. 管 理 员 |" << endl
<< "\t\t| |" << endl
<< "\t\t| 0. 退 出 |" << endl
<< "\t\t| |" << endl
<< "\t\t ---------------------------------" << endl
<< endl;

}

void signIn(int type) {
int select;
string id;
string name;
string pwd;
FileManage fm;
if (type == 1) {
cout << "请输入您的学号:" << endl;
cin >> id;
cout << "请输入用户名:" << endl;
cin >> name;
cout << "请输入您的密码:" << endl;
cin >> pwd;
if (fm.signIn(1, id, name, pwd)) {
cout << "登录成功!" << endl;
system("pause");
system("cls");
Identity* usr = new Student(id,name, pwd, fm);

usr->showMenu();
}
else {
cout << "登录验证失败!" << endl;
system("pause");
system("cls");
}
}
else if (type == 2) {
cout << "请输入您的工号:" << endl;
cin >> id;
cout << "请输入用户名:" << endl;
cin >> name;
cout << "请输入您的密码:" << endl;
cin >> pwd;
if (fm.signIn(2, id, name, pwd)) {
cout << "登录成功!" << endl;
system("pause");
system("cls");
Identity* usr = new Teacher(id, name, pwd, fm);

usr->showMenu();
}
else {
cout << "登录验证失败!" << endl;
system("pause");
system("cls");
}
}
else if (type == 3) {
cout << "请输入用户名:" << endl;
cin >> name;
cout << "请输入您的密码:" << endl;
cin >> pwd;
if (fm.signIn(3,name, pwd)) {
cout << "登录成功!" << endl;
system("pause");
system("cls");
Identity* usr = new Manager(name, pwd,fm);

usr->showMenu();
}
else {
cout << "登录验证失败!" << endl;
system("pause");
system("cls");
}
}

}

管理员类----manager.cpp:

Administrator class----manager.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include "manager.h"

Manager::Manager() {

}
Manager::Manager(string name, string pwd, FileManage &fm) {
this->m_Name = name;
this->m_pwd = pwd;
this->fm = fm;
}
Manager::~Manager() {

}

// 显示菜单
void Manager::showMenu() {
while (true) {
cout << "欢迎管理员:" << this->m_Name << " 登录!" << endl;
cout << "\t\t --------------------------------\n";
cout << "\t\t| |\n";
cout << "\t\t| 1.添加账号 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 2.查看账号 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 3.查看机房 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 4.清空预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 0.注销登录 |\n";
cout << "\t\t| |\n";
cout << "\t\t --------------------------------\n";
cout << "请选择您的操作: " << endl;

cin >> this->select;
switch (this->select) {
case 1:
this->addUser();
break;
case 2:
this->showUserInfo();
break;
case 3:
this->showCompRoomInfo();
break;
case 4:
this->cleanOrder();
break;
case 0:
this->signOut();
return;
break;
default:
system("cls");
break;
}
}

}

// 添加账号
void Manager::addUser() {
cout << "请输入添加账号的类型:" << endl;
cout << "1. 添加学生" << endl;
cout << "2. 添加老师" << endl;
int type;
cin >> type;
string id, name, pwd;
string tip = (type == 1) ? "请输入学生学号:" : "请输入教师职工号:";
cout << tip << endl;
cin >> id;
if (fm.checkID(type, id)) {
cout << "请输入姓名:" << endl;
cin >> name;
cout << "请输入密码:" << endl;
cin >> pwd;
vector<string> temp;
temp.push_back(id);
temp.push_back(name);
temp.push_back(pwd);
type == 1 ? fm.vStu.push_back(temp) : fm.vTea.push_back(temp);
type == 1 ? fm.save_vStu() : fm.save_vTea();
cout << "添加成功!" << endl;
}
else {
cout << "输入的 " << (type == 1 ? "学号" : "职工号") << " 已经存在!" << endl;
}
system("pause");
system("cls");
return;

}

// 查看账号
void Manager::showUserInfo() {
cout << "请选择查看账号类型:" << endl;
cout << "1. 查看学生账号" << endl;
cout << "2. 查看教师账号" << endl;
int type;
cin >> type;
if (type == 1) {
fm.showStuInfo();
}
else {
fm.showTeaInfo();
}
system("pause");
system("cls");
}

// 查看机房
void Manager::showCompRoomInfo() {
fm.showCompRoomInfo();
system("pause");
system("cls");
}

// 清空预约
void Manager::cleanOrder() {
fm.cleanOrder();
cout << "清空成功!" << endl;
system("pause");
system("cls");
}

// 注销登录
void Manager::signOut() {
cout << "感谢您的使用!" << endl;
system("pause");
system("cls");
}

学生类----student.cpp:

Student class----student.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#include "student.h"

Student::Student() {

}
Student::Student(string id, string name, string pwd, FileManage& fm) {
this->m_ID = id;
this->m_Name = name;
this->m_pwd = pwd;
this->fm = fm;
}
Student::~Student() {

}
void Student::showMenu() {
int select;
while (true) {
cout << "欢迎学生:" << this->m_Name << " 登录!" << endl;
cout << "\t\t ----------------------------------\n";
cout << "\t\t| |\n";
cout << "\t\t| 1.申请预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 2.查看我的预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 3.查看所有预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 4.取消预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 0.注销登录 |\n";
cout << "\t\t| |\n";
cout << "\t\t ----------------------------------\n";
cout << "请选择您的操作: " << endl;

cin >> select;
switch (select) {
case 1:
this->applyOrder();
break;
case 2:
this->showMyOrder();
break;
case 3:
this->showAllOrder();
break;
case 4:
this->undoOrder();
break;
case 0:
this->signOut();
return;
break;
default:
system("cls");
break;
}
}
}


// 申请预约
void Student::applyOrder() {
string date, time, roomid, status = "1";
cout << "机房开放时间为周一至周五!" << endl;
cout << "请输入申请预约的时间:" << endl
<< "1. 周一" << endl << "2. 周二" << endl << "3. 周三" << endl << "4. 周四" << endl << "5. 周五" << endl;
cin >> date;
cout << "请选择预约的时间段:" << endl << "1. 上午" << endl << "2. 下午" << endl;
cin >> time;
cout << "请选择预约的机房号:" << endl;
fm.showCompRoomInfo();
cin >> roomid;

fm.init_vOrder();
map<string, string> temp;
temp.insert(make_pair("date", date));
temp.insert(make_pair("interval", time));
temp.insert(make_pair("stuId", this->m_ID));
temp.insert(make_pair("stuName", this->m_Name));
temp.insert(make_pair("roomId", roomid));
temp.insert(make_pair("status", status));
fm.vOrder.push_back(temp);
fm.save_vOrder();
temp.clear();

cout << "申请成功!审核中..." << endl;
system("pause");
system("cls");
}

// 查看自身预约
void Student::showMyOrder() {
fm.init_vOrder();
int index = 1;
for (vector<map<string, string>>::iterator it = fm.vOrder.begin(); it != fm.vOrder.end(); it++) {
if (this->m_ID == (*it)["stuId"]) {
string date = "err",status = "err";
if ((*it)["date"] == "1") {
date = "周一";
}
else if ((*it)["date"] == "2") {
date = "周二";
}
else if ((*it)["date"] == "3") {
date = "周三";
}
else if ((*it)["date"] == "4") {
date = "周四";
}
else if((*it)["date"] == "5") {
date = "周五";
}

if ((*it)["status"] == "1") {
status = "审核中";
}
else if ((*it)["status"] == "-1") {
status = "预约失败";
}
else if ((*it)["status"] == "0") {
status = "已取消";
}
else if ((*it)["status"] == "2") {
status = "预约成功";
}

cout << index++ << "、 " << "日期:" << date << "\t时间段:" << ((*it)["interval"] == "1" ? "上午" : "下午")
<< "\t机房号:" << (*it)["roomId"] << "\t状态:" << status << endl;
}
}
if (index == 1) {
cout << "无预约记录!" << endl;
}
system("pause");
system("cls");
}

// 查看所有预约
void Student::showAllOrder() {
fm.showAllOrder();
system("pause");
system("cls");
}

// 取消预约
void Student::undoOrder() {
fm.init_vOrder();
int index = 1;
map<int, vector<map<string, string>>::iterator> temp;
for (vector<map<string, string>>::iterator it = fm.vOrder.begin(); it != fm.vOrder.end(); it++) {
if (this->m_ID == (*it)["stuId"] && ((*it)["status"] == "2" || (*it)["status"] == "1")) {
string date = "err", status = "err";
if ((*it)["date"] == "1") {
date = "周一";
}
else if ((*it)["date"] == "2") {
date = "周二";
}
else if ((*it)["date"] == "3") {
date = "周三";
}
else if ((*it)["date"] == "4") {
date = "周四";
}
else if ((*it)["date"] == "5") {
date = "周五";
}

if ((*it)["status"] == "1") {
status = "审核中";
}
else if ((*it)["status"] == "2") {
status = "预约成功";
}


temp.insert(make_pair(index, it));

cout << index++ << "、 " << "日期:" << date << "\t时间段:" << ((*it)["interval"] == "1" ? "上午" : "下午")
<< "\t机房号:" << (*it)["roomId"] << "\t状态:" << status << endl;


}
}

if (index == 1) {
cout << "无可取消记录!" << endl;

}
else {
int select;
cout << "请选择要取消的记录:" << endl;
cin >> select;
if (0 < select && select < index) {
(*(temp[select]))["status"] = "0";
fm.save_vOrder();
cout << "取消成功!" << endl;
}
else {
cout << "输入有误!" << endl;
}
}


system("pause");
system("cls");
}

// 注销登录
void Student::signOut() {
cout << "感谢您的使用!" << endl;
system("pause");
system("cls");
}

教师类----teacher.cpp:

Teacher class----teacher.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
#include "teacher.h"


Teacher::Teacher() {

}
Teacher::~Teacher() {

}
Teacher::Teacher(string id, string name, string pwd, FileManage& fm) {
this->m_ID = id;
this->m_Name = name;
this->m_pwd = pwd;
this->fm = fm;
}

void Teacher::showMenu() {
while (true) {
cout << "欢迎教师:" << this->m_Name << "登录!" << endl;
cout << "\t\t ----------------------------------\n";
cout << "\t\t| |\n";
cout << "\t\t| 1.查看所有预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 2.审核预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| 0.注销登录 |\n";
cout << "\t\t| |\n";
cout << "\t\t ----------------------------------\n";
cout << "请选择您的操作: " << endl;

int select;
cin >> select;
switch (select) {
case 1:
this->showAllOrder();
break;
case 2:
this->auditOrder();
break;
case 0:
this->signOut();
return;
break;
default:
system("cls");
break;
}
}
}

// 查看所有预约
void Teacher::showAllOrder() {
fm.showAllOrder();
system("pause");
system("cls");
}

// 审核预约
void Teacher::auditOrder() {
fm.init_vOrder();
cout << "需要审核的记录如下:" << endl;
int index = 1;
map<int, vector < map<string, string>>::iterator> temp;
for (vector < map<string, string>>::iterator it = fm.vOrder.begin(); it != fm.vOrder.end(); it++) {
string stuId, stuName, roomId, date = "err", interval;
if ((*it)["status"] == "1") {
if ((*it)["date"] == "1") {
date = "周一";
}
else if ((*it)["date"] == "2") {
date = "周二";
}
else if ((*it)["date"] == "3") {
date = "周三";
}
else if ((*it)["date"] == "4") {
date = "周四";
}
else if ((*it)["date"] == "5") {
date = "周五";
}

temp.insert(make_pair(index, it));

cout << index++ << ". " << "学生学号:" << (*it)["stuId"] << "\t学生姓名:" << (*it)["stuName"]
<< "\t预约日期:" << date << "\t时间段:" << ((*it)["interval"] == "1" ? "上午" : "下午")
<< "\t机房号:" << (*it)["roomId"] << "\t状态:审核中" << endl;


}
}

if (index == 1) {
cout << "记录为空!" << endl;
}
else {
int select;
cout << "请输入审核的记录编号(输入 0 取消):" << endl;
cin >> select;
if (0 < select && select < index) {
cout << "是否通过预约:" << endl
<< "1. 通过" << endl
<< "2. 不通过" << endl;
int flag;
cin >> flag;
if (flag == 1) {
(*(temp[select]))["status"] = "2";
fm.save_vOrder();
}
else if (flag == 2) {
(*(temp[select]))["status"] = "-1";
fm.save_vOrder();
}
else {
cout << "输入有误!" << endl;
}
}
else if (select == 0) {

}
else {
cout << "输入有误!" << endl;
}
}

system("pause");
system("cls");

}

// 注销登录
void Teacher::signOut() {
cout << "感谢您的使用!" << endl;
system("pause");
system("cls");
}

文件管理类----filemanage.cpp:

File management class----filemanage.cpp:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
#include "fileManage.h"

// 构造函数
FileManage::FileManage() {

this->init_vAd();
this->init_vStu();
this->init_vTea();
this->init_vCompRoom();
this->init_vOrder();
}
FileManage::~FileManage() {

}

// 学生教师登录认证
bool FileManage::signIn(int type, string id,string name,string pwd) {
if (type == 1) {
for (vector<vector<string>>::iterator it = this->vStu.begin(); it != this->vStu.end(); it++) {
if ((*it)[0] == id && (*it)[1] == name && (*it)[2] == pwd) {
return true;
}
}
}
else if (type == 2) {
for (vector<vector<string>>::iterator it = this->vTea.begin(); it != this->vTea.end(); it++) {
if ((*it)[0] == id && (*it)[1] == name && (*it)[2] == pwd) {
return true;
}
}
}
return false;
}

// 管理员登录认证
bool FileManage::signIn(int type, string name, string pwd) {
if (type == 3) {
for (vector<vector<string>>::iterator it = this->vAd.begin(); it != this->vAd.end(); it++) {
if ((*it)[0] == name && (*it)[1] == pwd) {
return true;
}
}
}
return false;
}

// 文件存在或者为空判断
bool FileManage::fileEmptyOrNotExist(string filename) {
bool flag = false;
ifstream ifs;
ifs.open(filename, ios::in);
if (!ifs.is_open()) {
flag = true;
}
else {
char ch;
ifs >> ch;
if (ifs.eof()) {
flag = true;
}
}
ifs.close();
return flag;
}

// 初始化容器
void FileManage::init_vStu() {

this->vStu.clear();

if (!this->fileEmptyOrNotExist(STUDENT)) {
ifstream ifs;
ifs.open(STUDENT, ios::in);
if (ifs.is_open()) {
string id;
string name;
string pwd;
vector<string> temp;
while (ifs >> id && ifs >> name && ifs >> pwd)
{
temp.push_back(id);
temp.push_back(name);
temp.push_back(pwd);
this->vStu.push_back(temp);
temp.clear();
}
}
ifs.close();
}
}
void FileManage::init_vTea() {

this->vTea.clear();
if (!this->fileEmptyOrNotExist(TEACHER)) {
ifstream ifs;
ifs.open(TEACHER, ios::in);
if (ifs.is_open()) {
string id;
string name;
string pwd;
vector<string> temp;
while (ifs >> id && ifs >> name && ifs >> pwd)
{
temp.push_back(id);
temp.push_back(name);
temp.push_back(pwd);
this->vTea.push_back(temp);
temp.clear();
}
}
ifs.close();
}

}
void FileManage::init_vAd() {
this->vAd.clear();

if (!this->fileEmptyOrNotExist(ADMIN)) {
ifstream ifs;
ifs.open(ADMIN, ios::in);
if (ifs.is_open()) {
string name;
string pwd;
vector<string> temp;
while (ifs >> name && ifs >> pwd) {
temp.push_back(name);
temp.push_back(pwd);
this->vAd.push_back(temp);
temp.clear();
}
}
ifs.close();
}

}
void FileManage::init_vCompRoom() {
this->vCompRoom.clear();

if (!this->fileEmptyOrNotExist(COMPUTER)) {
ifstream ifs;
ifs.open(COMPUTER, ios::in);
vector<int> temp;
if (ifs.is_open()) {
int id;
int max_Size;
while (ifs >> id && ifs >> max_Size) {
temp.push_back(id);
temp.push_back(max_Size);
this->vCompRoom.push_back(temp);
temp.clear();
}
}
ifs.close();
}

}
void FileManage::init_vOrder() {
this->vOrder.clear();

if (!this->fileEmptyOrNotExist(ORDER)) {
ifstream ifs;
ifs.open(ORDER, ios::in);
string date,time,stuid,name,roomid,status;
map<string, string> temp;
if (ifs.is_open()) {
while (ifs >> date && ifs >> time && ifs >> stuid && ifs >> name && ifs >> roomid && ifs >> status) {
temp.insert(make_pair(date.substr(0, date.find(":")), date.substr(date.find(":") + 1, date.size())));
temp.insert(make_pair(time.substr(0, time.find(":")), time.substr(time.find(":") + 1, time.size())));
temp.insert(make_pair(stuid.substr(0, stuid.find(":")), stuid.substr(stuid.find(":") + 1, stuid.size())));
temp.insert(make_pair(name.substr(0, name.find(":")), name.substr(name.find(":") + 1, name.size())));
temp.insert(make_pair(roomid.substr(0, roomid.find(":")), roomid.substr(roomid.find(":") + 1, roomid.size())));
temp.insert(make_pair(status.substr(0, status.find(":")), status.substr(status.find(":") + 1, status.size())));
this->vOrder.push_back(temp);
temp.clear();
}

}
ifs.close();
}
}

// 保存容器中的变量写入文件
void FileManage::save_vStu() {
ofstream ofs;
ofs.open(STUDENT, ios::out);
if (ofs.is_open()) {
for (vector<vector<string>>::iterator it = this->vStu.begin(); it != this->vStu.end(); it++) {
ofs << (*it)[0] << " " << (*it)[1] << " " << (*it)[2] << endl;
}
}
ofs.close();
}
void FileManage::save_vTea() {
ofstream ofs;
ofs.open(TEACHER, ios::out);
if (ofs.is_open()) {
for (vector<vector<string>>::iterator it = this->vTea.begin(); it != this->vTea.end(); it++) {
ofs << (*it)[0] << " " << (*it)[1] << " " << (*it)[2] << endl;
}
}
ofs.close();
}
void FileManage::save_vOrder() {
ofstream ofs;
ofs.open(ORDER, ios::out);
if (ofs.is_open()) {
for (vector<map<string, string>>::iterator it = this->vOrder.begin(); it != this->vOrder.end(); it++) {
for (map<string, string>::iterator mit = (*it).begin(); mit != (*it).end(); mit++) {
ofs << mit->first << ":" << mit->second << " ";
}
ofs << endl;
}
}
ofs.close();
}

// 检查ID 是否重复
bool FileManage::checkID(int type, string id) {

if (type == 1) {
this->init_vStu();
for (vector<vector<string>>::iterator it = this->vStu.begin(); it != this->vStu.end(); it++) {
if (id == (*it)[0]) {
return false;
}
}
}
else {
this->init_vTea();
for (vector<vector<string>>::iterator it = this->vTea.begin(); it != this->vTea.end(); it++) {
if (id == (*it)[0]) {
return false;
}
}
}
return true;
}

// 显示学生信息
void FileManage::showStuInfo() {
this->init_vStu();
for (vector<vector<string>>::iterator it = this->vStu.begin(); it != this->vStu.end(); it++) {
cout << "学号: " << (*it)[0] << "\t姓名: " << (*it)[1] << "\t密码: " << (*it)[2] << endl;
}
}

// 显示教师信息
void FileManage::showTeaInfo() {
this->init_vTea();
for (vector<vector<string>>::iterator it = this->vTea.begin(); it != this->vTea.end(); it++) {
cout << "职工号: " << (*it)[0] << "\t姓名: " << (*it)[1] << "\t密码: " << (*it)[2] << endl;
}
}

// 显示机房信息
void FileManage::showCompRoomInfo() {
this->init_vCompRoom();
for (vector<vector<int>>::iterator it = this->vCompRoom.begin(); it != this->vCompRoom.end(); it++) {
cout << "机房编号: " << (*it)[0] << "\t机房容量: " << (*it)[1] << endl;
}
}

// 显示所有预约记录信息
void FileManage::showAllOrder() {
this->init_vOrder();
int index = 1;
for (vector<map<string, string>>::iterator it = this->vOrder.begin(); it != this->vOrder.end(); it++) {
string date = "error", status = "error";
if ((*it)["date"] == "1") {
date = "周一";
}
else if ((*it)["date"] == "2") {
date = "周二";
}
else if ((*it)["date"] == "3") {
date = "周三";
}
else if ((*it)["date"] == "4") {
date = "周四";
}
else if ((*it)["date"] == "5") {
date = "周五";
}

if ((*it)["status"] == "1") {
status = "审核中";
}
else if ((*it)["status"] == "-1") {
status = "预约失败";
}
else if ((*it)["status"] == "0") {
status = "已取消";
}
else if ((*it)["status"] == "2") {
status = "预约成功";
}

cout << index++ << "、 " << "学生学号:" << (*it)["stuId"] << "\t学生姓名:" << (*it)["stuName"]
<<"\t日期:" << date << "\t时间段:" << ((*it)["interval"] == "1" ? "上午" : "下午")
<< "\t机房号:" << (*it)["roomId"] << "\t状态:" << status << endl;
}
}

// 清空预约记录
void FileManage::cleanOrder() {
ofstream ofs;
ofs.open(ORDER, ios::trunc);
ofs.close();
this->init_vOrder();
}