機房預約系統
1、機房預約系統需求
1.1 系統簡介
- 學校現有幾個規格不同的機房,由於使用時經常出現"撞車"現象,現開發一套機房預約系統,解決這一問題。
1.2 身份簡介
分別有三種身份使用該程序
- 學生代表:申請使用機房
- 教師:審核學生的預約申請
- 管理員:給學生、教師創建賬號
1.3 機房簡介
機房總共有3間
- 1號機房 --- 最大容量20人
- 2號機房 --- 最多容量50人
- 3號機房 --- 最多容量100人
1.4 申請簡介
- 申請的訂單每周由管理員負責清空。
- 學生可以預約未來一周內的機房使用,預約的日期為周一至周五,預約時需要選擇預約時段(上午、下午)
- 教師來審核預約,依據實際情況審核預約通過或者不通過
1.5 系統具體需求
- 首先進入登錄界面,可選登錄身份有:
- 學生代表
- 老師
- 管理員
- 退出
- 每個身份都需要進行驗證后,進入子菜單
- 學生需要輸入 :學號、姓名、登錄密碼
- 老師需要輸入:職工號、姓名、登錄密碼
- 管理員需要輸入:管理員姓名、登錄密碼
- 學生具體功能
- 申請預約 --- 預約機房
- 查看自身的預約 --- 查看自己的預約狀態
- 查看所有預約 --- 查看全部預約信息以及預約狀態
- 取消預約 --- 取消自身的預約,預約成功或審核中的預約均可取消
- 注銷登錄 --- 退出登錄
- 教師具體功能
- 查看所有預約 --- 查看全部預約信息以及預約狀態
- 審核預約 --- 對學生的預約進行審核
- 注銷登錄 --- 退出登錄
- 管理員具體功能
- 添加賬號 --- 添加學生或教師的賬號,需要檢測學生編號或教師職工號是否重復
- 查看賬號 --- 可以選擇查看學生或教師的全部信息
- 查看機房 --- 查看所有機房的信息
- 清空預約 --- 清空所有預約記錄
- 注銷登錄 --- 退出登錄
2、創建項目
創建項目步驟如下:
- 創建新項目
- 添加文件
2.1 創建項目
- 打開vs2017后,點擊創建新項目,創建新的C++項目
如圖:
- 填寫項目名稱以及選取項目路徑,點擊確定生成項目
2.2 添加文件
- 右鍵源文件,進行添加文件操作
- 填寫文件名稱,點擊添加
- 生成文件成功,效果如下圖
3、創建主菜單
功能描述:
- 設計主菜單,與用戶進行交互
3.1 菜單實現
- 在主函數main中添加菜單提示,代碼如下:
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;
}
運行效果如圖:
3.2 搭建接口
- 接受用戶的選擇,搭建接口
- 在main中添加代碼
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會重新回到界面,輸入其他提示輸入有誤,清屏后重新選擇
效果如圖:
至此,界面搭建完畢
4、 退出功能實現
4.1 退出功能實現
在main函數分支 0 選項中,添加退出程序的代碼:
cout << "歡迎下一次使用"<<endl;
system("pause");
return 0;
4.2 測試退出功能
運行程序,效果如圖:
至此,退出程序功能實現
5、 創建身份類
5.1 身份的基類
- 在整個系統中,有三種身份,分別為:學生代表、老師以及管理員
- 三種身份有其共性也有其特性,因此我們可以將三種身份抽象出一個身份基類identity
- 在頭文件下創建Identity.h文件
Identity.h中添加如下代碼:
#pragma once
#include<iostream>
using namespace std;
//身份抽象類
class Identity
{
public:
//操作菜單
virtual void operMenu() = 0;
string m_Name; //用戶名
string m_Pwd; //密碼
};
效果如圖:
5.2 學生類
5.2.1 功能分析
-
學生類主要功能是可以通過類中成員函數,實現預約實驗室操作
-
學生類中主要功能有:
- 顯示學生操作的菜單界面
- 申請預約
- 查看自身預約
- 查看所有預約
- 取消預約
5.2.2 類的創建
- 在頭文件以及源文件下創建 student.h 和 student.cpp文件
student.h中添加如下代碼:
#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中添加如下代碼:
#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()
{
}
5.3 老師類
5.3.1 功能分析
-
教師類主要功能是查看學生的預約,並進行審核
-
教師類中主要功能有:
-
顯示教師操作的菜單界面
-
查看所有預約
-
審核預約
-
5.3.2 類的創建
- 在頭文件以及源文件下創建 teacher.h 和 teacher.cpp文件
teacher.h中添加如下代碼:
#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中添加如下代碼:
#include"teacher.h"
//默認構造
Teacher::Teacher()
{
}
//有參構造 (職工編號,姓名,密碼)
Teacher::Teacher(int empId, string name, string pwd)
{
}
//菜單界面
void Teacher::operMenu()
{
}
//查看所有預約
void Teacher::showAllOrder()
{
}
//審核預約
void Teacher::validOrder()
{
}
5.4 管理員類
5.4.1 功能分析
-
管理員類主要功能是對學生和老師賬戶進行管理,查看機房信息以及清空預約記錄
-
管理員類中主要功能有:
-
顯示管理員操作的菜單界面
-
添加賬號
-
查看賬號
-
查看機房信息
-
清空預約記錄
-
5.4.2 類的創建
- 在頭文件以及源文件下創建 manager.h 和 manager.cpp文件
manager.h中添加如下代碼:
#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中添加如下代碼:
#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()
{
}
至此,所有身份類創建完畢,效果如圖:
6、 登錄模塊
6.1 全局文件添加
功能描述:
- 不同的身份可能會用到不同的文件操作,我們可以將所有的文件名定義到一個全局的文件中
- 在頭文件中添加 globalFile.h 文件
- 並添加如下代碼:
#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"
並且在同級目錄下,創建這幾個文件
6.2 登錄函數封裝
功能描述:
- 根據用戶的選擇,進入不同的身份登錄
在預約系統的.cpp文件中添加全局函數 void LoginIn(string fileName, int type)
參數:
- fileName --- 操作的文件名
- type --- 登錄的身份 (1代表學生、2代表老師、3代表管理員)
LoginIn中添加如下代碼:
#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函數的不同分支中,填入不同的登錄接口
6.3 學生登錄實現
在student.txt文件中添加兩條學生信息,用於測試
添加信息:
1 張三 123
2 李四 123456
其中:
- 第一列 代表 學號
- 第二列 代表 學生姓名
- 第三列 代表 密碼
效果圖:
在Login函數的學生分支中加入如下代碼,驗證學生身份
//學生登錄驗證
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;
}
}
添加代碼效果圖
測試:
6.4 教師登錄實現
在teacher.txt文件中添加一條老師信息,用於測試
添加信息:
1 老王 123
其中:
- 第一列 代表 教師職工編號
- 第二列 代表 教師姓名
- 第三列 代表 密碼
效果圖:
在Login函數的教師分支中加入如下代碼,驗證教師身份
//教師登錄驗證
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;
}
}
添加代碼效果圖
測試:
6.5 管理員登錄實現
在admin.txt文件中添加一條管理員信息,由於我們只有一條管理員,因此本案例中沒有添加管理員的功能
添加信息:
admin 123
其中:admin
代表管理員用戶名,123
代表管理員密碼
效果圖:
在Login函數的管理員分支中加入如下代碼,驗證管理員身份
//管理員登錄驗證
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;
}
}
添加效果如圖:
測試效果如圖:
至此,所有身份的登錄功能全部實現!
7、 管理員模塊
7.1 管理員登錄和注銷
7.1.1 構造函數
- 在Manager類的構造函數中,初始化管理員信息,代碼如下:
//有參構造
Manager::Manager(string name, string pwd)
{
this->m_Name = name;
this->m_Pwd = pwd;
}
7.1.2 管理員子菜單
- 在機房預約系統.cpp中,當用戶登錄的是管理員,添加管理員菜單接口
- 將不同的分支提供出來
- 添加賬號
- 查看賬號
- 查看機房
- 清空預約
- 注銷登錄
- 實現注銷功能
添加全局函數 void managerMenu(Identity * &manager)
,代碼如下:
//管理員菜單
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;
}
}
}
7.1.3 菜單功能實現
- 在實現成員函數
void Manager::operMenu()
代碼如下:
//選擇菜單
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;
}
7.1.4 接口對接
- 管理員成功登錄后,調用管理員子菜單界面
- 在管理員登錄驗證分支中,添加代碼:
//進入管理員子菜單
managerMenu(person);
添加效果如:
測試對接,效果如圖:
登錄成功
注銷登錄:
至此,管理員身份可以成功登錄以及注銷
7.2 添加賬號
功能描述:
- 給學生或教師添加新的賬號
功能要求:
- 添加時學生學號不能重復、教師職工號不能重復
7.2.1 添加功能實現
在Manager的addPerson成員函數中,實現添加新賬號功能,代碼如下:
//添加賬號
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();
}
測試添加學生:
成功在學生文件中添加了一條信息
測試添加教師:
成功在教師文件中添加了一條信息
7.2.2 去重操作
功能描述:添加新賬號時,如果是重復的學生編號,或是重復的教師職工編號,提示有誤
7.2.2.1 讀取信息
- 要去除重復的賬號,首先要先將學生和教師的賬號信息獲取到程序中,方可檢測
- 在manager.h中,添加兩個容器,用於存放學生和教師的信息
- 添加一個新的成員函數
void initVector()
初始化容器
//初始化容器
void initVector();
//學生容器
vector<Student> vStu;
//教師容器
vector<Teacher> vTea;
添加位置如圖:
在Manager的有參構造函數中,獲取目前的學生和教師信息
代碼如下:
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();
}
在有參構造函數中,調用初始化容器函數
//有參構造
Manager::Manager(string name, string pwd)
{
this->m_Name = name;
this->m_Pwd = pwd;
//初始化容器
this->initVector();
}
測試,運行代碼可以看到測試代碼獲取當前學生和教師數量
7.2.2.2 去重函數封裝
在manager.h文件中添加成員函數 bool checkRepeat(int id, int type);
//檢測重復 參數:(傳入id,傳入類型) 返回值:(true 代表有重復,false代表沒有重復)
bool checkRepeat(int id, int type);
在manager.cpp文件中實現成員函數 bool checkRepeat(int id, int type);
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;
}
7.2.2.3 添加去重操作
在添加學生編號或者教師職工號時,檢測是否有重復,代碼如下:
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;
}
}
代碼位置如圖:
檢測效果:
7.2.2.4 bug解決
bug描述:
- 雖然可以檢測重復的賬號,但是剛添加的賬號由於沒有更新到容器中,因此不會做檢測
- 導致剛加入的賬號的學生號或者職工編號,再次添加時依然可以重復
解決方案:
- 在每次添加新賬號時,重新初始化容器
在添加完畢后,加入代碼:
//初始化容器
this->initVector();
位置如圖:
再次測試,剛加入的賬號不會重復添加了!
7.3 顯示賬號
功能描述:顯示學生信息或教師信息
7.3.1 顯示功能實現
在Manager的showPerson成員函數中,實現顯示賬號功能,代碼如下:
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");
}
7.3.2 測試
測試查看學生效果
測試查看教師效果
至此,顯示賬號功能實現完畢
7.4 查看機房
7.4.1 添加機房信息
案例需求中,機房一共有三個,其中1號機房容量20台機器,2號50台,3號100台
我們可以將信息錄入到computerRoom.txt中
7.4.2 機房類創建
在頭文件下,創建新的文件 computerRoom.h
並添加如下代碼:
#pragma once
#include<iostream>
using namespace std;
//機房類
class ComputerRoom
{
public:
int m_ComId; //機房id號
int m_MaxNum; //機房最大容量
};
7.4.3 初始化機房信息
在Manager管理員類下,添加機房的容器,用於保存機房信息
//機房容器
vector<ComputerRoom> vCom;
在Manager有參構造函數中,追加如下代碼,初始化機房信息
//獲取機房信息
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();
位置如圖:
因為機房信息目前版本不會有所改動,如果后期有修改功能,最好封裝到一個函數中,方便維護
7.4.4 顯示機房信息
在Manager類的showComputer成員函數中添加如下代碼:
//查看機房信息
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");
}
測試顯示機房信息功能:
7.5 清空預約
功能描述:
清空生成的order.txt
預約文件
7.5.1 清空功能實現
在Manager的cleanFile成員函數中添加如下代碼:
//清空預約記錄
void Manager::cleanFile()
{
ofstream ofs(ORDER_FILE, ios::trunc);
ofs.close();
cout << "清空成功!" << endl;
system("pause");
system("cls");
}
測試清空,可以隨意寫入一些信息在order.txt中,然后調用cleanFile清空文件接口,查看是否清空干凈
8、 學生模塊
8.1 學生登錄和注銷
8.1.1 構造函數
- 在Student類的構造函數中,初始化學生信息,代碼如下:
//有參構造(學號、姓名、密碼)
Student::Student(int id, string name, string pwd)
{
//初始化屬性
this->m_Id = id;
this->m_Name = name;
this->m_Pwd = pwd;
}
8.1.2 管理員子菜單
- 在機房預約系統.cpp中,當用戶登錄的是學生,添加學生菜單接口
- 將不同的分支提供出來
- 申請預約
- 查看我的預約
- 查看所有預約
- 取消預約
- 注銷登錄
- 實現注銷功能
添加全局函數 void studentMenu(Identity * &manager)
代碼如下:
//學生菜單
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;
}
}
}
8.1.3 菜單功能實現
- 在實現成員函數
void Student::operMenu()
代碼如下:
//菜單界面
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;
}
8.1.4 接口對接
- 學生成功登錄后,調用學生的子菜單界面
- 在學生登錄分支中,添加代碼:
//進入學生子菜單
studentMenu(person);
添加效果如圖:
測試對接,效果如圖:
登錄驗證通過:
學生子菜單:
注銷登錄:
8.2 申請預約
8.2.1 獲取機房信息
- 在申請預約時,學生可以看到機房的信息,因此我們需要讓學生獲取到機房的信息
在student.h中添加新的成員函數如下:
//機房容器
vector<ComputerRoom> vCom;
在學生的有參構造函數中追加如下代碼:
//獲取機房信息
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();
追加位置如圖:
至此,vCom容器中保存了所有機房的信息
8.2.2 預約功能實現
在student.cpp中實現成員函數 void Student::applyOrder()
//申請預約
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");
}
運行程序,測試代碼:
在order.txt文件中生成如下內容:
8.3 顯示預約
8.3.1 創建預約類
功能描述:顯示預約記錄時,需要從文件中獲取到所有記錄,用來顯示,創建預約的類來管理記錄以及更新
在頭文件以及源文件下分別創建orderFile.h 和 orderFile.cpp文件
orderFile.h中添加如下代碼:
#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;
};
構造函數中獲取所有信息,並存放在容器中,添加如下代碼:
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代碼如下:
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();
}
8.3.2 顯示自身預約
首先我們先添加幾條預約記錄,可以用程序添加或者直接修改order.txt文件
order.txt文件內容如下: 比如我們有三名同學分別產生了3條預約記錄
在Student類的void Student::showMyOrder()
成員函數中,添加如下代碼
//查看我的預約
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");
}
測試效果如圖:
8.3.3 顯示所有預約
在Student類的void Student::showAllOrder()
成員函數中,添加如下代碼
//查看所有預約
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");
}
測試效果如圖:
8.4 取消預約
在Student類的void Student::cancelOrder()
成員函數中,添加如下代碼
//取消預約
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 ++ << "、 ";//后置++,先輸出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");
}
測試取消預約:
再次查看個人預約記錄:
查看所有預約
查看order.txt預約文件
至此,學生模塊功能全部實現
9、 教師模塊
9.1 教師登錄和注銷
9.1.1 構造函數
- 在Teacher類的構造函數中,初始化教師信息,代碼如下:
//有參構造 (職工編號,姓名,密碼)
Teacher::Teacher(int empId, string name, string pwd)
{
//初始化屬性
this->m_EmpId = empId;
this->m_Name = name;
this->m_Pwd = pwd;
}
9.1.2 教師子菜單
- 在機房預約系統.cpp中,當用戶登錄的是教師,添加教師菜單接口
- 將不同的分支提供出來
- 查看所有預約
- 審核預約
- 注銷登錄
- 實現注銷功能
添加全局函數 void TeacherMenu(Person * &manager)
代碼如下:
//教師菜單
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;
}
}
}
9.1.3 菜單功能實現
- 在實現成員函數
void Teacher::operMenu()
代碼如下:
//教師菜單界面
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;
}
9.1.4 接口對接
- 教師成功登錄后,調用教師的子菜單界面
- 在教師登錄分支中,添加代碼:
//進入教師子菜單
TeacherMenu(person);
添加效果如圖:
測試對接,效果如圖:
登錄驗證通過:
教師子菜單:
注銷登錄:
9.2 查看所有預約
9.2.1 所有預約功能實現
該功能與學生身份的查看所有預約功能相似,用於顯示所有預約記錄
在Teacher.cpp中實現成員函數 void Teacher::showAllOrder()
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");
}
9.2.2 測試功能
運行測試教師身份的查看所有預約功能
測試效果如圖:
9.3 審核預約
9.3.1 審核功能實現
功能描述:教師審核學生的預約,依據實際情況審核預約
在Teacher.cpp中實現成員函數 void Teacher::validOrder()
代碼如下:
//審核預約
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");
}
9.3.2 測試審核預約
測試 - 審核通過
審核通過情況
測試-審核未通過
審核未通過情況:
學生身份下查看記錄:
審核預約成功!
至此本案例制作完畢! ^_^
代碼目錄結構:
代碼目錄結構如下:
具體文件如下:
各文件中具體代碼:
computerRoom.h
#pragma once
#include <iostream>
using namespace std;
class ComputerRoom
{
public:
int m_ComId; //機房id號
int m_MaxNum; //機房最大容量
};
globalFile.h
#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"
identify.h
#pragma once
#include <iostream>
#include <string>
#include <fstream>
#include "globalFile.h"
using namespace std;
//身份抽象類
class Identify
{
public:
//操作菜單
virtual void operMenu() = 0;
string m_Name;//用戶名
string m_Pwd;//密碼
};
manager.h
#pragma once
#include <iostream>
using namespace std;
#include "identify.h"
#include "student.h"
#include "teacher.h"
#include <vector>
#include "computerRoom.h"
//管理員類
class Manager :public Identify
{
public:
//默認構造
Manager();
//有參構造 管理員姓名,密碼
Manager(string name, string pwd);
//選擇菜單
virtual void operMenu();
//添加賬號
void addPerson();
//查看賬號
void showPerson();
//查看機房信息
void showComputer();
//清空預約記錄
void cleanFile();
//初始化容器
void initVector();
//檢測重復
bool checkRepeat(int id, int type);
//學生容器
vector<Student> vStu;
//教師容器
vector<Teacher> vTea;
//機房容器
vector<ComputerRoom> vCR;
};
orderFile.h
#pragma once
#include <iostream>
using namespace std;
#include <map>
#include "globalFile.h"
#include <string>
class OrderFile
{
public:
//構造函數
OrderFile();
//更新預約記錄
void updateOrder();
//記錄的容器 key-預約的數量 value—預約的記錄
map<int, map<string, string>> m_OrderData;
//預約記錄條數
int m_Size;
};
student.h
#pragma once
#include <iostream>
using namespace std;
#include "identify.h"
#include "computerRoom.h"
#include <vector>
#include "orderFile.h"
//學生類
class Student :public Identify
{
public:
//默認構造
Student();
//有參構造(學號,姓名,密碼)
Student(int id, string name, string pwd);
//菜單界面
virtual void operMenu();
//申請預約
void applyOrder();
//查看我的預約
void showMyOrder();
//查看所有預約
void showAllOrder();
//取消預約
void cancelOrder();
//學號
int m_Id;
//獲取機房信息
vector<ComputerRoom> vCR;
};
teacher.h
#pragma once
#define _CRT_SECURE_NO_WARNINGS
using namespace std;
#include "identify.h"
class Teacher :public Identify
{
public:
//默認構造
Teacher();
//有參構造(職工編號,姓名,密碼)
Teacher(int empId, string name, string pwd);
//顯示教師操作的界面菜單
virtual void operMenu();
//查看所有預約
void showAllOrder();
//審核預約
void validOrder();
//教師編號
int m_EmpId;
};
manager.cpp
#include "manager.h"
#include "student.h"
#include "teacher.h"
#include <algorithm>
#include "computerRoom.h"
//默認構造
Manager::Manager()
{
}
//有參構造 管理員姓名,密碼
Manager::Manager(string name, string pwd)
{
//初始化管理員信息
this->m_Name = name;
this->m_Pwd = pwd;
//初始化容器
this->initVector();
}
//選擇菜單
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;
}
//添加賬號
void Manager::addPerson()
{
cout << "請輸入添加賬號的類型" << endl;
cout << "1、添加學生" << endl;
cout << "2、添加老師" << endl;
string fileName;//文件名稱
string tip;//提示的id類型 學號或者職工號
ofstream ofs;//文件操作對象 寫方式
string errorTip;//檢測重復錯誤提示
int select;//選擇
cin >> select;//接收用戶的選項
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, select);//查看是否有重復
if (ret)//有重復
{
cout << errorTip << endl;
}
else
{
break;
}
}
cout << "請輸入姓名:" << endl;
cin >> name;
cout << "請輸入密碼:" << endl;
cin >> pwd;
ofs << id << " " << name << " " << pwd <<" "<< endl;
cout << "添加成功!" << endl;
system("pause");
system("cls");
ofs.close();
//初始化容器 在添加完數據之后立刻更新容器中的內容
this->initVector();
}
//打印學生信息
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");
}
//查看機房信息
void Manager::showComputer()
{
cout << "機房信息如下:" << endl;
for (vector<ComputerRoom>::iterator it = vCR.begin(); it != vCR.end(); it++)
{
cout << "機房編號:" << it->m_ComId << " 機房最大容量:" << it->m_MaxNum << endl;
}
system("pause");
system("cls");
}
//清空預約記錄
void Manager::cleanFile()
{
ofstream ofs;
//用覆蓋的方式打開文件,如源文件有數據則清空,創建全新文件
ofs.open(ORDER_FILE, ios::trunc);
ofs.close();
cout << "清空成功!" << endl;
system("pause");
system("cls");
}
//初始化容器
void Manager::initVector()
{
//讀取學生文件中的信息
ifstream ifs;
ifs.open(STUDENT_FILE, ios::in);
if (!ifs.is_open())
{
cout << "文件讀取失敗!" << endl;
return;
}
//清空容器
vStu.clear();
vTea.clear();
vCR.clear();
Student s;//創建對象,將文件中的記錄讀入到對象的屬性中
while (ifs >> s.m_Id && ifs >> s.m_Name && ifs >> s.m_Pwd)
{
vStu.push_back(s);//讀取到vector容器中
}
cout << "當前學生的數量為:" << vStu.size() << endl;
ifs.close();
//讀取教師文件中的信息
ifs.open(TEACHER_FILE, ios::in);
if (!ifs.is_open())
{
cout << "文件讀取失敗!" << endl;
return;
}
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();
//讀取機房文件中的信息
ifs.open(COMPUTER_FILE, ios::in);
ComputerRoom c;
while (ifs >> c.m_ComId && ifs >> c.m_MaxNum)
{
vCR.push_back(c);
}
cout << "當前機房數量為: " << vCR.size() << endl;
ifs.close();
}
//檢測重復
bool Manager::checkRepeat(int id, int type)
{
if (type == 1)//檢測學生
{
for (vector<Student>::iterator it = vStu.begin(); it != vStu.end(); it++)
{
if (it->m_Id == id)
{
return true;
}
}
}
else//檢測職工
{
for (vector<Teacher>::iterator it = vTea.begin(); it != vTea.end(); it++)
{
if (it->m_EmpId == id)
{
return true;
}
}
}
return false;
}
orderFile.cpp
#include "orderFile.h"
#include <fstream>
//構造函數
OrderFile::OrderFile()
{
ifstream ifs;
ifs.open(ORDER_FILE, ios::in);
string data; //日期
string interval; //時間段
string stuId; //學生編號
string stuName; //學生姓名
string roomId; //機房編號
string status; //預約狀態
this->m_Size = 0;//預約記錄個數
while (ifs>>data && ifs>>interval && ifs>>stuId && ifs>>stuName && ifs>>roomId && ifs>>status)
{
//測試代碼
/*
cout << data << endl;
cout << interval << endl;
cout << stuId << endl;
cout << stuName << endl;
cout << roomId << endl;
cout << status << endl;
*/
string key;
string value;
map<string, string> m;//創建map子容器
//記錄讀取的第一個數據中“:”的位置,對字符串進行分割
int pos = data.find(":");
if (pos != -1)
{
key = data.substr(0, pos);
value = data.substr(pos + 1, data.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();
}
//更新預約記錄
void OrderFile::updateOrder()
{
if (this->m_Size == 0)//預約記錄為0
{
return;
}
ofstream ofs(ORDER_FILE, ios::out | ios::trunc);
//將文件內容全部銷毀,然后將更新過的數據寫入文件中
//因為不知道哪些數據需要更新,所以將全部數據都進行寫入操作
for (int i = 0; i < this->m_Size; i++)
{
ofs << "data:" << this->m_OrderData[i]["data"] << " ";
ofs << "interveal:" << this->m_OrderData[i]["interveal"] << " ";
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();
}
student.cpp
#include "student.h"
//默認構造
Student::Student()
{
}
//有參構造 (姓名,學號,密碼)
Student::Student(int id, string name, string pwd)
{
//屬性初始化
this->m_Id = id;
this->m_Name = name;
this->m_Pwd = pwd;
//初始化的時候讓學生獲取機房信息
ifstream ifs;
ifs.open(COMPUTER_FILE, ios::in);
ComputerRoom c;
while (ifs>>c.m_ComId && ifs>>c.m_MaxNum)
{
//將讀取的信息放入到容器中
vCR.push_back(c);
}
ifs.close();
}
//菜單界面
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;
}
//申請預約
void Student::applyOrder()
{
cout << "機房開放時間為周一至周五!" << endl;
cout << "請輸入申請預約的時間:" << endl;
cout << "1、周一" << endl;
cout << "2、周二" << endl;
cout << "3、周三" << endl;
cout << "4、周四" << endl;
cout << "5、周五" << endl;
int data = 0;//日期 周一—周五
int interveal = 0;//時段 上午/下午
int room = 0;//機房 1 2 3
while (true)//輸入日期
{
cin >> data;
if (data >= 1 && data <= 5)
{
break;
}
cout << "輸入有誤,請重新輸入!" << endl;
}
cout << "請輸入申請預約的時間段:" << endl;
cout << "1、上午" << endl;
cout << "2、下午" << endl;
while (true)//輸入時間段 上午/下午
{
cin >> interveal;
if (interveal >= 1 && interveal <= 2)
{
break;
}
cout << "輸入有誤,請重新輸入!" << endl;
}
cout << "請選擇機房:" << endl;
cout << "1號機房容量:" << vCR[0].m_MaxNum << endl;
cout << "2號機房容量:" << vCR[1].m_MaxNum << endl;
cout << "3號機房容量:" << vCR[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 << "data:" << data << " ";//日期
ofs << "interveal:" << interveal << " ";//時段
ofs << "stuID:" << this->m_Id << " ";//學生id
ofs << "stuName:" << this->m_Name << " ";//學生姓名
ofs << "roomID:" << room << " ";//學生預約地機房號
ofs << "status:" << 1 << endl;//預約狀態
ofs.close();
system("pause");
system("cls");
}
//查看我的預約
void Student::showMyOrder()
{
OrderFile of;//創建對象的同時已經調用了構造函數
//預約記錄為0時
if (of.m_Size == 0)
{
cout << "無預約記錄" << endl;
system("pause");
system("cls");
return;
}
//輸出自身預約信息
for (int i = 0; i < of.m_Size; i++)
{
//string轉int
//string利用.c_str()轉const char*
//atoi(const char*)轉為int類型
if (atoi(of.m_OrderData[i]["stuID"].c_str()) == this->m_Id)
{
cout << "預約日期: 周" << of.m_OrderData[i]["data"];
cout << " 時段:" << (of.m_OrderData[i]["interveal"] == "1" ? "上午" : "下午");
cout << " 機房:" << of.m_OrderData[i]["roomID"];
string status = " 狀態: "; //1-審核中 2-以預約 -1-預約失敗 0-取消預約
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");
}
//查看所有預約
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]["data"];
cout << " 時段:" << (of.m_OrderData[i]["interveal"] == "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");
}
//取消預約
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);//將修改的預約記錄下標記錄下來,方便修改狀態status
cout << index++ << "、 ";//后置++,先輸出index本身的值,然后再加一
cout << "預約日期: 周" << of.m_OrderData[i]["data"];
cout << " 時段:" << (of.m_OrderData[i]["interveal"] == "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");
}
teacher.cpp
#include "teacher.h"
#include "orderFile.h"
#include <vector>
//默認構造
Teacher::Teacher()
{
}
//有參構造(職工編號,姓名,密碼)
Teacher::Teacher(int empId, string name, string pwd)
{
//初始化屬性
this->m_EmpId = empId;
this->m_Name = name;
this->m_Pwd = pwd;
}
//顯示教師操作的界面菜單
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;
}
//查看所有預約
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]["data"];
cout << " 時段:" << (of.m_OrderData[i]["interveal"] == "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");
}
//審核預約
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]["data"];
cout << " 時段:" << (of.m_OrderData[i]["interveal"] == "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");
}
機房預約系統.cpp
#include <iostream>
using namespace std;
#include <string>
#include<fstream>
#include "identify.h"
#include "globalFile.h"
#include "student.h"
#include "teacher.h"
#include "manager.h"
//管理員菜單
void ManagerMenu(Identify * &manager)//指針引用,等價於c中的二級指針
{
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 StudentMenu(Identify * &student)//指針引用,等價於c中的二級指針
{
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 TeacherMenu(Identify * &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;
}
}
}
//登錄功能 參數1 操作文件名 參數2 操作身份類型
void LoginIn(string fileName,int type)
{
//創建父類指針,指向子類對象
Identify *person = NULL;
//讀文件
ifstream ifs;
ifs.open(fileName, ios::in);
//判斷文件是否存在
if (!ifs.is_open())
{
cout << "文件不存在!" << endl;
ifs.close();
return;
}
//准備接收用戶的信息
int id = 0;//id
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)
{
//學生登錄驗證
int fId;//從文件中讀取的id
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);
//進入學生身份的子菜單
StudentMenu(person);
return;
}
}
}
else if (type == 2)
{
//教師登錄驗證
int fId;//從文件中讀取的id
string fName;//從文件中讀取的姓名
string fpwd;//從文件中讀取的密碼
while (ifs >> fId && ifs >> fName && ifs >> fpwd)//讀取文件中每一行的內容
{
if (fId == id && fName == name && fpwd == pwd)
{
/*cout << fId << endl;
cout << fName << endl;
cout << fpwd << endl;*/
cout << "教師登錄成功!" << endl;
system("pause");
system("cls");
//創建子類對象
person = new Teacher(id, name, pwd);
//進入教師身份的子菜單
TeacherMenu(person);
return;
}
}
}
else if (type == 3)
{
//管理員登錄驗證
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);
//進入管理員菜單
ManagerMenu(person);
return;
}
}
}
cout << "驗證登錄失敗 !" << endl;
system("pause");
system("cls");
return;
}
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://學生身份
LoginIn(STUDENT_FILE, 1);
break;
case 2://老師身份
LoginIn(TEACHER_FILE, 2);
break;
case 3://管理員身份
LoginIn(ADMIN_FILE, 3);
break;
case 0://退出系統
cout << "歡迎下次使用!" << endl;
system("pause");
return 0;
break;
default:
cout << "輸入有誤,請重寫選擇!" << endl;
system("pause");
system("cls");
break;
}
}
system("pause");
return 0;
}