C++自学笔记(42 机房预约系统)
42 机房预约系统
本次记录机房预约系统,还请各位大佬批评指正!
机房预约系统需求:
分别有三个身份使用该程序:
- 学生:申请使用机房
- 老师:审核学生的预约申请
- 管理员:给学生和老师创建账号
机房总共有3间:
- 1号机房 --- 最大容量20人
- 2号机房 --- 最大容量50人
- 3号机房 --- 最大容量100人
申请简介:
- 申请的纪录每周由管理员负责清空
- 学生可以预约未来一周的机房使用,预约日期为周一到周五,预约时段分为上午和下午
- 教师来审核预约,依据实际情况审核预约通过或者不通过
系统需求:
首先进入登录界面,可以选择的身份有:
- 学生
- 老师
- 管理员
- 退出
每个身份都需要进行验证后,进入子菜单:
- 学生需要输入:学号、姓名、登录密码
- 老师需要输入:职工号、姓名、登录密码
- 管理员需要输入:管理员姓名、登录密码
学生具体功能:
- 申请预约 --- 预约机房
- 查看自身的预约 --- 查看自己对的预约状态
- 查看所有预约 --- 查看全部预约信息及预约状态
- 取消预约 --- 取消自身的预约,预约成功或审核中的均可取消
- 注销登录 --- 退出登录
老师具体功能:
- 查看所有预约 --- 查看全部预约信息以及预约状态
- 审核预约 --- 对学生的预约进行审核
- 注销登录 --- 退出登录
管理员具体功能:
- 添加账号 --- 添加学生或老师的账号,需要检测学生或老师编号是否重复
- 查看账号 --- 可以选择查看学生或老师的全部信息
- 查看机房 --- 查看所有机房信息(后期可以添加或修改机房信息)
- 清空预约 --- 清空所有预约纪录
- 注销登录 --- 退出登录
预约状态:
- 审核中
- 预约成功
- 预约失败
- 取消的预约
机房信息:
- 机房编号
- 最大容量
头文件 Admin.h
#pragma once
#include<iostream>
using namespace std;
#include"Identity.h"
#include"Student.h"
#include"Teacher.h"
#include<vector>
#include"ComputerRoom.h"
class Admin:public Identity
{
public:
//构造函数
Admin();
//有参构造 参数:姓名、密码
Admin(string a_name, string a_password);
//菜单界面
virtual void Menu();
//登录函数
virtual void LoginIn(string FileName, int type);
//添加账号
void AddAccount();
//查看账号
void ShowAccount();
//查看机房
void ShowComputerRoom();
//清空预约
void ClearOrder();
//注销登录
//初始化容器
void InitVector();
//初始化机房信息
void InitComputer();
//打印机房信息
void PrintComputerRoom();
//检测重复
bool CheckRepeat(int id, int type);
//学生容器
vector<Student> vStu;
//老师容器
vector<Teacher> vTea;
//机房信息容器
vector<ComputerRoom> vCom;
};
头文件 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_Flie "Admin.txt"
//老师文件
#define Teacher_File "Teacher.txt"
//学生文件
#define Student_File "Student.txt"
//机房信息文件
#define Computer_File "Computer.txt"
//预约文件
#define Order_File "Order.txt"
头文件 Identity.h
#pragma once //防止头文件重复包含
#include<iostream>
using namespace std;
//身份的抽象基类
/*
在整个系统中,有三种身份,分别是学生、老师和管理员
三种身份有其共性,也有其特性,因此我们可以将三种身份抽象出一个身份基类Identity
*/
class Identity
{
public:
//操作菜单,使用多态的技术
virtual void Menu() = 0; //纯虚函数,子类复用该函数
//登录函数
/*
功能描述:
根据用户的选择,进入不同的身份登录
参数:
- filename --- 操作的文件名
- type --- 登录的身份(1代表学生、2代表老师、3代表管理员)
*/
virtual void LoginIn(string FileName, int type) = 0;
string m_Name; //用户名
string m_Password; //密码
};
头文件 OrderFile.h
#pragma once
#include<iostream>
using namespace std;
#include"GlobalFile.h"
#include<fstream>
#include<map>
#include<string>
/*
功能描述:
显示预约纪录时,需要从文件中获取到所有纪录,用来显示
创建预约的类来管理纪录以及更新纪录
*/
class OrderFile
{
public:
//构造函数
OrderFile();
//更新预约纪录
void UpdateOrder();
//纪录预约条数
int m_Size;
//纪录所有预约信息
//key为纪录的条数 value为具体键值对信息
map<int, map<string, string>>mOrder;
};
头文件 Student.h
#pragma once
#include<iostream>
using namespace std;
#include"Identity.h"
#include"ComputerRoom.h"
#include<vector>
#include"OrderFile.h"
/*
学生类功能分析:
- 学生类主要功能是可以通过类中成员函数,实现预约实验室操作
学生类中主要功能有:
- 显示学生操作的菜单界面
- 申请预约
- 查看自身预约
- 查看所有预约
- 取消预约
*/
class Student:public Identity //继承身份抽象类
{
public:
//构造函数
Student();
//有参构造 参数:学号、姓名、密码
Student(int s_id, string s_name, string s_password);
//初始化机房信息
void InitComputer();
//菜单界面
virtual void Menu();
//登录函数
virtual void LoginIn(string FileName, int type);
//申请预约
void ApplyOrder();
//查看我的预约
void ShowMyOrder();
//查看所有预约
void ShowAllOrder();
//取消预约
void CancelOrder();
//注销登录
//学生学号
int m_SId;
//机房信息容器
vector<ComputerRoom> vCom;
};
头文件 Teacher.h
#pragma once
#include<iostream>
using namespace std;
#include"Identity.h"
#include"OrderFile.h"
#include<vector>
/*
老师类功能分析:
老师类主要功能四查看学生预约,并进行审核
老是类主要功能有:
- 显示老师操作的界面菜单
- 查看所有预约
- 审核预约
*/
class Teacher:public Identity
{
public:
//构造函数
Teacher();
//有参构造 参数:学号、姓名、密码
Teacher(int t_id, string t_name, string t_password);
//菜单界面
virtual void Menu();
//登录函数
void LoginIn(string FileName, int type);
//查看所有预约
void ShowAllOrder();
//审核预约
void ValidOrder();
//注销登录
//老师编号
int m_TId;
};
源文件 Admin.cpp
#include<iostream>
using namespace std;
#include"Identity.h"
#include"Admin.h"
#include"GlobalFile.h"
#include<fstream>
#include<vector>
#include<algorithm>
//构造函数
Admin::Admin()
{
}
//有参构造 参数:姓名、密码
Admin::Admin(string a_name, string a_password)
{
//初始化管理员信息
this->m_Name = a_name;
this->m_Password = a_password;
//初始化容器,获取文件中所有学生和老师的信息
this->InitVector();
//初始化机房信息
this->InitComputer();
}
//菜单界面
void Admin::Menu()
{
cout << "欢迎管理员"<<this->m_Name<<"登录!\n";
cout << "\t\t--------------------------------\n";
cout << "\t\t| 1.添加账号 |\n";
cout << "\t\t| 2.查看账号 |\n";
cout << "\t\t| 3.查看机房 |\n";
cout << "\t\t| 4.清空预约 |\n";
cout << "\t\t| 0.注销登录 |\n";
cout << "\t\t--------------------------------\n\n";
cout << "Enter your identity or exit:\n";
}
//登录函数
void Admin::LoginIn(string FileName, int type)
{
}
//添加账号
void Admin::AddAccount()
{
/*
给学生或者老师添加新的账号
添加时学生和老师的ID不能重复
*/
cout << "请输入要添加的账号的类型:\n";
cout << "1、添加学生" << endl;
cout << "2、添加老师" << endl;
string filename;
string tip;
string Errortip; //重复错误提示
ofstream ofs;
int select;
int goon;
cin >> select;
while (true)
{
if (select == 1)
{
//添加学生
filename = Student_File;
tip = "请输入学号:";
Errortip = "学号重复,请重新输入:";
//break;
}
else if (select == 2)
{
//添加老师
filename = Teacher_File;
tip = "请输入老师编号:";
Errortip = "职工号重复,请重新输入:";
//break;
}
else
{
cout << "输入有误,请重新输入!\n";
system("pause");
system("cls");
break;
}
//利用追加的方式写文件
ofs.open(filename, ios::out | ios::app);
int id; //学号/职工号
string name; //姓名
string password; //密码
cout << tip << endl;
//检测重复
while (true)
{
cin >> id;
bool ret = CheckRepeat(id, select);
if (ret == true) //有重复
{
cout << Errortip << endl;
}
else
{
break;
}
}
cout << "请输入姓名:\n";
cin >> name;
cout << "请输入密码:\n";
cin >> password;
//
ofs << id << " " << name << " " << password << " " << endl;
cout << "添加成功!" << endl;
ofs.close();
cout << "是否继续添加?" << endl;
cout << "1、继续添加" << endl;
cout << "2、退出" << endl;
cin >> goon;
if (goon == 1)
{
cout << "请输入要添加的账号的类型:\n";
cout << "1、添加学生" << endl;
cout << "2、添加老师" << endl;
cin >> select;
}
else
{
system("cls");
return;
}
}
}
//初始化容器
void Admin::InitVector()
{
//确保容器是清空的状态
vStu.clear();
vTea.clear();
//读取学生信息
ifstream ifs(Student_File, ios::in);
if (!ifs.is_open())
{
cout << "文件读取失败!\n";
return;
}
Student s;
while (ifs >> s.m_SId && ifs >> s.m_Name && ifs >> s.m_Password)
{
vStu.push_back(s);
}
//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_TId && ifs >> t.m_Name && ifs >> t.m_Password)
{
vTea.push_back(t);
}
//cout << "当前老师的数量为:" << vTea.size() << endl;
ifs.close();
}
//检测重复
//参数 Id和人员类型(老师或者学生)
//true代表有重复 false代表没有重复
bool Admin::CheckRepeat(int id, int type)
{
this->InitVector();
if (type == 1)
{
for (vector<Student>::iterator it = vStu.begin(); it != vStu.end(); it++)
{
if ((*it).m_SId == id)
{
return true;
}
}
}
else
{
for (vector<Teacher>::iterator it = vTea.begin(); it != vTea.end(); it++)
{
if (it->m_TId == id)
{
return true;
}
}
}
return false;
}
//打印学生信息
void PrintStudent(Student& s)
{
cout << "学号:" << s.m_SId << "\t" << "姓名:" << s.m_Name
<< "\t" << "密码:" << s.m_Password << endl;
}
//打印老师信息
void PrintTeacher(Teacher& t)
{
cout << "职工号:" << t.m_TId << "\t" << "姓名:" << t.m_Name
<< "\t" << "密码:" << t.m_Password << endl;
}
//查看账号
void Admin::ShowAccount()
{
this->InitVector();
cout << "请选择查看信息的内容:\n";
cout << "1、查看所有学生信息\n";
cout << "2、查看所有老师信息\n";
int select;
cin >> select;
while (true)
{
if (select == 1)
{
cout << "所有学生信息如下:\n";
for_each(vStu.begin(), vStu.end(), PrintStudent);
system("pause");
system("cls");
break;
}
else if (select == 2)
{
cout << "所有老师信息如下:\n";
for_each(vTea.begin(), vTea.end(), PrintTeacher);
system("pause");
system("cls");
break;
}
else
{
cout << "输入有误,请重新输入!\n";
cin >> select;
}
}
}
//初始化机房信息
void Admin::InitComputer()
{
//确保容器是清空的状态
vCom.clear();
//读取机房信息
ifstream ifs(Computer_File, ios::in);
if (!ifs.is_open())
{
cout << "文件读取失败!\n";
return;
}
ComputerRoom c;
while (ifs >> c.m_ComId && ifs >> c.m_MaxNum)
{
vCom.push_back(c);
}
//cout << "当前机房的数量为:" << vCom.size() << endl;
ifs.close();
}
//打印机房信息
void PrintComputer(ComputerRoom& c)
{
cout << "机房ID:" << c.m_ComId << "\t" << "机房最大容量:" << c.m_MaxNum
<< "\t" << endl;
}
void Admin::PrintComputerRoom()
{
for (vector<ComputerRoom>::iterator it = vCom.begin(); it != vCom.end(); it++)
{
cout << "机房ID:" << it->m_ComId << "\t" << "机房最大容量:" << it->m_MaxNum << endl;
}
}
//查看机房
void Admin::ShowComputerRoom()
{
this->InitComputer();
cout << "机房信息如下:\n";
//for_each(vCom.begin(), vCom.end(), PrintComputer);
PrintComputerRoom();
system("pause");
system("cls");
}
//清空预约
void Admin::ClearOrder()
{
int select;
cout << "确认清空预约?" << endl;
cout << "1、确认" << endl;
cout << "2、取消" << endl;
cin >> select;
if (select == 1)
{
ofstream ofs(Order_File, ios::trunc);
ofs.close();
cout << "清空成功!" << endl;
system("pause");
system("cls");
}
else
{
system("pause");
system("cls");
}
}
源文件 OrderFile.cpp
#include<iostream>
using namespace std;
#include"OrderFile.h"
//构造函数
OrderFile::OrderFile()
{
ifstream ifs(Order_File, ios::in);
string date; //日期
string interval; //时间段
string StudentID; //学号
string StudentName; //姓名
string roomID; //机房号
string status; //预约状态
this->m_Size = 0;
while (ifs >> date && ifs >> interval && ifs >> StudentID
&& ifs >> StudentName && ifs >> roomID && ifs >> status)
{
//测试代码
/*cout << date << endl << interval << endl << StudentID << endl
<< StudentName << endl << roomID << endl << status << endl;*/
//date:1
string key;
string value;
map<string, string>m;
//截取日期
int pos = date.find(":"); // 4
if (pos != -1)
{
key = date.substr(0, pos);
//size = 6 ; pos = 4 ; size - pos -1 = 1
value = date.substr(pos + 1, date.size() - pos - 1);
//cout << "key = " << key << "\t" << "value = " << value << endl;
m.insert(make_pair(key, value));
}
//截取时间段
pos = interval.find(":");
if (pos != -1)
{
key = interval.substr(0, pos);
//size = 6 ; pos = 4 ; size - pos -1 = 1
value = interval.substr(pos + 1, interval.size() - pos - 1);
//cout << "key = " << key << "\t" << "value = " << value << endl;
m.insert(make_pair(key, value));
}
//截取学号
pos = StudentID.find(":");
if (pos != -1)
{
key = StudentID.substr(0, pos);
//size = 6 ; pos = 4 ; size - pos -1 = 1
value = StudentID.substr(pos + 1, StudentID.size() - pos - 1);
//cout << "key = " << key << "\t" << "value = " << value << endl;
m.insert(make_pair(key, value));
}
//截取姓名
pos = StudentName.find(":");
if (pos != -1)
{
key = StudentName.substr(0, pos);
//size = 6 ; pos = 4 ; size - pos -1 = 1
value = StudentName.substr(pos + 1, StudentName.size() - pos - 1);
//cout << "key = " << key << "\t" << "value = " << value << endl;
m.insert(make_pair(key, value));
}
//截取房间号
pos = roomID.find(":");
if (pos != -1)
{
key = roomID.substr(0, pos);
//size = 6 ; pos = 4 ; size - pos -1 = 1
value = roomID.substr(pos + 1, roomID.size() - pos - 1);
//cout << "key = " << key << "\t" << "value = " << value << endl;
m.insert(make_pair(key, value));
}
//截取预约状态
pos = status.find(":");
if (pos != -1)
{
key = status.substr(0, pos);
//size = 6 ; pos = 4 ; size - pos -1 = 1
value = status.substr(pos + 1, status.size() - pos - 1);
//cout << "key = " << key << "\t" << "value = " << value << endl;
m.insert(make_pair(key, value));
}
//将小map容器放入大的map容器中
this->mOrder.insert(make_pair(this->m_Size, m));
this->m_Size++;
this->UpdateOrder();
测试大map容器
//for (map<int, map<string, string>>::iterator it1 = this->mOrder.begin();
// it1 != this->mOrder.end(); it1++)
//{
// cout << "条数为: " << it1->first << " ; value = " << endl;
// for (map<string, string>::iterator it2 = (*it1).second.begin(); it2 != it1->second.end(); it2++)
// {
// cout << "key = " << it2->first << " ; value = " << it2->second << endl;
// }
//}
}
ifs.close();
}
//更新预约纪录
void OrderFile::UpdateOrder()
{
if (this->m_Size == 0)
{
return;
}
ofstream ofs(Order_File, ios::out | ios::trunc);
for (int i = 0; i < this->m_Size; i++)
{
ofs << "date:" << this->mOrder[i]["date"] << " ";
ofs << "interval:" << this->mOrder[i]["interval"] << " ";
ofs << "StudentID:" << this->mOrder[i]["StudentID"] << " ";
ofs << "StudentName:" << this->mOrder[i]["StudentName"] << " ";
ofs << "roomID:" << this->mOrder[i]["roomID"] << " ";
ofs << "status:" << this->mOrder[i]["status"] << endl;
}
ofs.close();
}
源文件 Student.cpp
#include<iostream>
using namespace std;
#include"Student.h"
#include"Identity.h"
#include"GlobalFile.h"
#include<fstream>
#include<vector>
//构造函数
Student::Student()
{
}
//有参构造 参数:学号、姓名、密码
Student::Student(int s_id, string s_name, string s_password)
{
//初始化属性
this->m_SId = s_id;
this->m_Name = s_name;
this->m_Password = s_password;
//初始化机房信息
this->InitComputer();
}
//初始化机房信息
void Student::InitComputer()
{
//确保容器为空
vCom.clear();
ifstream ifs(Computer_File, ios::in); //没有智能提示,一般是没有包含头文件
if (!ifs.is_open())
{
cout << "文件读取失败!" << endl;
return;
}
ComputerRoom c;
while (ifs >> c.m_ComId && ifs >> c.m_MaxNum)
{
//将读取的信息放入容器
vCom.push_back(c);
}
ifs.close();
}
//菜单界面
void Student::Menu()
{
cout << "欢迎学生" << this->m_Name << "登录!\n";
cout << "\t\t------------------------------------\n";
cout << "\t\t| 1.申 请 预 约 |\n";
cout << "\t\t| 2.查看我的预约 |\n";
cout << "\t\t| 3.查看所有预约 |\n";
cout << "\t\t| 4.取 消 预 约 |\n";
cout << "\t\t| 0.注 销 登 录 |\n";
cout << "\t\t------------------------------------\n\n";
cout << "Enter your identity or exit:\n";
}
//登录函数
void Student::LoginIn(string FileName, int type)
{
}
//申请预约
void Student::ApplyOrder()
{
//this->InitComputer();
cout << "机房开放时间为周一到周五!\n";
cout << "请输入要预约的时间:\n";
cout << "1、周一\n";
cout << "2、周二\n";
cout << "3、周三\n";
cout << "4、周四\n";
cout << "5、周五\n";
int date = 0; //周几
int interval = 0; //时间段
int room = 0; //机房号
while (true)
{
cin >> date;
if (date <= 5 && date >= 1)
{
break;
}
cout << "输入有误,请重新输入: " << endl;
}
cout << "请输入要预约的时间段:\n";
cout << "1、上午\n";
cout << "2、下午\n";
while (true)
{
cin >> interval;
if (interval <= 2 && interval >= 1)
{
break;
}
cout << "输入有误,请重新输入: " << endl;
}
cout << "请选择机房:\n";
/*cout << vCom[0].m_ComId << "号机房的容量为:" << vCom[0].m_MaxNum << endl;
cout << vCom[1].m_ComId << "号机房的容量为:" << vCom[1].m_MaxNum << endl;
cout << vCom[2].m_ComId << "号机房的容量为:" << vCom[2].m_MaxNum << endl;*/
for (int i = 0; i < vCom.size(); i++)
{
cout << vCom[i].m_ComId << " 号机房的最大容量为:" << vCom[i].m_MaxNum << endl;
}
while(true)
{
cin >> room;
if (room >= 1 && room <= vCom.size())
{
break;
}
cout << "输入有误,请重新输入:\n";
}
cout << "预约成功!预约审核中!\n";
ofstream ofs(Order_File, ios::app);
ofs << "date:" << date << " ";
ofs << "interval:" << interval << " ";
ofs << "StudentID:" << this->m_SId << " ";
ofs << "StudentName:" << this->m_Name << " ";
ofs << "roomID:" << room << " ";
ofs << "status:" << 1 << endl;
ofs.close();
system("pause");
system("cls");
}
//查看我的预约
void Student::ShowMyOrder()
{
OrderFile of;
//of.UpdateOrder();
if (of.m_Size == 0)
{
cout << "没有预约纪录!\n";
system("pause");
system("cls");
return;
}
int num = 0; //自己的预约数量
for (int i = 0; i < of.m_Size; i++)
{
if (this->m_SId == atoi(of.mOrder[i]["StudentID"].c_str())) //找到自己的预约
{
num++;
}
}
if (num == 0)
{
cout << "没有自己的预约纪录!\n";
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 (this->m_SId == atoi(of.mOrder[i]["StudentID"].c_str())) //找到自己的预约
{
cout << i + 1 << "、" << "预约日期: 周" << of.mOrder[i]["date"] << "\t"
<< "时间段:" << (of.mOrder[i]["interval"] == "1" ? "上午" : "下午") << "\t"
<< "机房号:" << of.mOrder[i]["roomID"] << "\t";
string status = "预约状态: "; //-1 预约失败 ; 0 取消预约 ; 1 审核中 ; 2 预约成功
if (of.mOrder[i]["status"] == "1")
{
status += "审核中!";
}
else if (of.mOrder[i]["status"] == "2")
{
status += "预约成功!";
}
else if (of.mOrder[i]["status"] == "-1")
{
status += "预约失败,审核未通过!";
}
else
{
status += "预约已取消!";
}
cout << status << endl;
}
}
system("pause");
system("cls");
}
//查看所有预约
void Student::ShowAllOrder()
{
OrderFile of;
//of.UpdateOrder();
if (of.m_Size == 0)
{
cout << "没有预约纪录!\n";
system("pause");
system("cls");
return;
}
for (int i = 0; i < of.m_Size; i++)
{
cout << i + 1 << "、" << "预约日期: 周" << of.mOrder[i]["date"] << "\t"
<< "时间段:" << (of.mOrder[i]["interval"] == "1" ? "上午":"下午") << "\t"
<< "学号:" << of.mOrder[i]["StudentID"] << "\t"
<< "姓名:" << of.mOrder[i]["StudentName"] << "\t"
<< "机房号:" << of.mOrder[i]["roomID"] << "\t";
string status = "预约状态:"; //-1 预约失败 ; 0 取消预约 ; 1 审核中 ; 2 预约成功
if (of.mOrder[i]["status"] == "1")
{
status += "审核中!";
}
else if (of.mOrder[i]["status"] == "2")
{
status += "预约成功!";
}
else if (of.mOrder[i]["status"] == "-1")
{
status += "预约失败,审核未通过!";
}
else
{
status += "预约已取消!";
}
cout << status << endl;
}
system("pause");
system("cls");
}
//取消预约
void Student::CancelOrder()
{
OrderFile of;
if (of.m_Size == 0)
{
cout << "没有预约纪录!\n";
system("pause");
system("cls");
return;
}
int num = 0;
for (int i = 0; i < of.m_Size; i++)
{
if (this->m_SId == atoi(of.mOrder[i]["StudentID"].c_str()))
{
if (of.mOrder[i]["status"] == "1" || of.mOrder[i]["status"] == "2")
{
num++;
}
}
}
if (num == 0)
{
cout << "没有可以取消的预约!\n";
system("pause");
system("cls");
return;
}
cout << "审核中或者预约成功可以取消预约!请输入要取消的预约:\n";
vector<int>v; //存放在大容器中的下标编号
int index = 1;
for (int i = 0; i < of.m_Size; i++)
{
//先判断是自己的学号
if (this->m_SId == atoi(of.mOrder[i]["StudentID"].c_str()))
{
if (of.mOrder[i]["status"] == "1" || of.mOrder[i]["status"] == "2")
{
v.push_back(i);
cout << index++ << "、 " << "预约日期: 周" << of.mOrder[i]["date"] << "\t"
<< "时间段:" << (of.mOrder[i]["interval"] == "1" ? "上午" : "下午") << "\t"
<< "机房号:" << of.mOrder[i]["roomID"] << "\t";
string status = "审核状态:";
if (of.mOrder[i]["status"] == "1")
{
status += "审核中!";
}
else if (of.mOrder[i]["status"] == "2")
{
status += "预约成功!";
}
cout << status << endl;
}
}
}
cout << "请输入要取消的纪录(0返回):\n";
int select = 0;
while (true)
{
cin >> select;
if (select >= 0 && select <= v.size())
{
if (select == 0)
{
break;
}
else
{
of.mOrder[v[select - 1]]["status"] = "0";
of.UpdateOrder();
cout << "预约已取消!\n";
break;
}
}
cout << "输入有误,请重新输入:\n";
}
system("pause");
system("cls");
}
源文件 Teacher.cpp
#include<iostream>
using namespace std;
#include"Teacher.h"
#include"Identity.h"
//构造函数
Teacher::Teacher()
{
}
//有参构造 参数:学号、姓名、密码
Teacher::Teacher(int t_id, string t_name, string t_password)
{
this->m_TId = t_id;
this->m_Name = t_name;
this->m_Password = t_password;
}
//菜单界面
void Teacher::Menu()
{
cout << "欢迎老师" << this->m_Name << "登录!\n";
cout << "\t\t------------------------------------\n";
cout << "\t\t| 1.查看所有预约 |\n";
cout << "\t\t| 2.审 核 预 约 |\n";
cout << "\t\t| 0.注 销 登 录 |\n";
cout << "\t\t------------------------------------\n\n";
cout << "Enter your identity or exit:\n";
}
//登录函数
void Teacher::LoginIn(string FileName, int type)
{
}
//查看所有预约
void Teacher::ShowAllOrder()
{
OrderFile of;
//of.UpdateOrder();
if (of.m_Size == 0)
{
cout << "没有预约纪录!\n";
system("pause");
system("cls");
return;
}
for (int i = 0; i < of.m_Size; i++)
{
cout << i + 1 << "、" << "预约日期: 周" << of.mOrder[i]["date"] << "\t"
<< "时间段:" << (of.mOrder[i]["interval"] == "1" ? "上午":"下午") << "\t"
<< "学号:" << of.mOrder[i]["StudentID"] << "\t"
<< "姓名:" << of.mOrder[i]["StudentName"] << "\t"
<< "机房号:" << of.mOrder[i]["roomID"] << "\t";
string status = "预约状态:"; //-1 预约失败 ; 0 取消预约 ; 1 审核中 ; 2 预约成功
if (of.mOrder[i]["status"] == "1")
{
status += "审核中!";
}
else if (of.mOrder[i]["status"] == "2")
{
status += "预约成功!";
}
else if (of.mOrder[i]["status"] == "-1")
{
status += "预约失败,审核未通过!";
}
else
{
status += "预约已取消!";
}
cout << status << endl;
}
system("pause");
system("cls");
}
//审核预约
void Teacher::ValidOrder()
{
OrderFile of;
if (of.m_Size == 0)
{
cout << "没有预约纪录!\n";
system("pause");
system("cls");
return;
}
vector<int>v; //存放待审核预约的下标编号
int index = 0;
int num = 0; //判断是否还有待审核的预约
for (int i = 0; i < of.m_Size; i++)
{
if (of.mOrder[i]["status"] == "1")
{
num++;
}
}
if (num == 0)
{
cout << "没有待审核的预约!\n";
system("pause");
system("cls");
return;
}
cout << "待审核的预约如下:\n";
for (int i = 0; i < of.m_Size; i++)
{
if (of.mOrder[i]["status"] == "1")
{
v.push_back(i);
cout << ++index << "、" << "预约日期:周" << of.mOrder[i]["date"] << "\t"
<< "时间段:" << (of.mOrder[i]["interval"] == "1" ? "上午" : "下午")
<< "学号:" << of.mOrder[i]["StudentID"] << "\t"
<< "姓名:" << of.mOrder[i]["StudentName"] << "\t"
<< "机房号:" << of.mOrder[i]["room"] << "\t";
string status = "审核状态:";
if (of.mOrder[i]["status"] == "1")
{
status += "审核中!";
}
cout << status << endl;
}
}
int select = 0; //接收用于选择的预约纪录
int ret = 0; //接收预约结果纪录
cout << "请输入要审核的预约(0返回):\n";
while (true)
{
cin >> select;
if (select >= 0 && select <= v.size())
{
if (select == 0)
{
break;
}
else
{
cout << "请输入审核结果:\n";
cout << "1、通过\n";
cout << "2、不通过\n";
cin >> ret;
if (ret == 1) //通过
{
of.mOrder[v[select - 1]]["status"] = "2";
}
else //不通过
{
of.mOrder[v[select - 1]]["status"] = "-1";
}
of.UpdateOrder();
cout << "审核完毕!\n";
break;
}
}
cout << "输入有误,请重新输入:\n";
}
system("pause");
system("cls");
}
源文件 机房预约系统.cpp
#include<iostream>
#include <stdlib.h>
#include<string>
#include<vector>
#include<algorithm>
using namespace std;
#include"Identity.h"
#include<fstream> //ifstream
#include"GlobalFile.h"
#include"Student.h"
#include"Teacher.h"
#include"Admin.h"
//主菜单
void MainMenu();
//进入管理员子菜单界面
void AdminMenu(Identity*& manager);
//进入老师子菜单界面
void TeacherMenu(Identity*& manager);
//进入学生子菜单界面
void StudentMenu(Identity*& manager);
//登录函数
void LoginIn(string FileName, int type);
int main()
{
int choice;
while (true)
{
system("cls");
//主菜单
MainMenu();
cout << "Enter your identity or exit:\n";
cin >> choice;
switch (choice)
{
case 1: //学生身份
LoginIn(Student_File, 1);
break;
case 2: //老师身份
LoginIn(Teacher_File, 2);
break;
case 3: //管理员身份
LoginIn(Admin_Flie, 3);
break;
case 0: //退出系统
cout << "欢迎下次使用!\n";
system("pause");
exit(0);
break;
default:
cout << "输入有误,请重新输入!\n";
system("Pause");
system("cls");
break;
}
}
system("pause");
return 0;
}
//主菜单
void MainMenu()
{
cout << "==================== 欢迎使用机房预约系统 ====================\n\n";
cout << "\t\t--------------------------------\n";
cout << "\t\t| 1.学 生 |\n";
cout << "\t\t| 2.老 师 |\n";
cout << "\t\t| 3.管 理 员 |\n";
cout << "\t\t| 0.退 出 |\n";
cout << "\t\t--------------------------------\n\n";
}
//登录函数
// filename-- - 操作的文件名
// type-- - 登录的身份(1代表学生、2代表老师、3代表管理员)
void LoginIn(string FileName, int type)
{
//父类指针,用于指向子类对象
Identity* person = NULL;
//读文件
/*ifstream ifs;
ifs.open(FileName, ios::in);*/
ifstream ifs(FileName, ios::in);
//判断文件是否存在
if (!ifs.is_open())
{
cout << "文件不存在!\n";
ifs.close();
return;
}
//准备接收用户信息
int Id = 0;
string Name;
string Password;
//判断身份
if (type == 1)
{
cout << "Enter your student ID:";
cin >> Id;
}
else if (type == 2)
{
cout << "Enter your Teacher ID: ";
cin >> Id;
}
cout << "Enter your name: ";
cin >> Name;
cout << "Enter your password: ";
cin >> Password;
//验证身份
if (type == 1)
{
//学生身份验证
//txt文件中添加学生信息
//第一列 学号;第二列 学生姓名;第三列 密码
int fId; //从文件中读取的id
string fName; //从文件中读取的姓名
string fPassword; //从文件中读取的密码
while (ifs >> fId && ifs >> fName && ifs >> fPassword)
{
//cout << fId << endl << fName << endl << fPassword << endl;
//与用户输入的信息做对比
if (fId == Id && fName == Name && fPassword == Password)
{
cout << "学生验证登录成功!\n";
system("pause");
system("cls");
person = new Student(Id, Name, Password);
//进入学生身份的子菜单
StudentMenu(person);
return;
}
}
}
else if (type == 2)
{
//老师身份验证
//txt文件中添加老师信息
//第一列 职工号;第二列 老师姓名;第三列 密码
int fId; //从文件中读取的id
string fName; //从文件中读取的姓名
string fPassword; //从文件中读取的密码
while (ifs >> fId && ifs >> fName && ifs >> fPassword)
{
//cout << fId << endl << fName << endl << fPassword << endl;
//与用户输入的信息做对比
if (fId == Id && fName == Name && fPassword == Password)
{
cout << "老师验证登录成功!\n";
system("pause");
system("cls");
person = new Teacher(Id, Name, Password);
//进入老师身份的子菜单
TeacherMenu(person);
return;
}
}
}
else if (type == 3)
{
//管理员身份验证
//txt文件中添加管理员信息
//第一列 管理员姓名;第二列 密码
string fName; //从文件中读取的姓名
string fPassword; //从文件中读取的密码
while (ifs >> fName && ifs >> fPassword)
{
//cout << fId << endl << fName << endl << fPassword << endl;
//与用户输入的信息做对比
if (fName == Name && fPassword == Password)
{
cout << "管理员验证登录成功!\n";
system("pause");
system("cls");
person = new Admin(Name, Password);
//进入管理员身份的子菜单
AdminMenu(person);
return;
}
}
}
cout << "验证登录失败!" << endl;
system("pause");
system("cls");
return;
}
//进入管理员子菜单界面
void AdminMenu(Identity*& manager)
{
while (true)
{
//调用管理员子菜单
manager->Menu();
//将父类指针转为子类指针,调用子类其他接口
Admin* man = (Admin*)manager;
//接收用户选项
int Select;
cin >> Select;
if (Select == 1)
{
//添加账号
//cout << "添加账号" << endl;
man->AddAccount();
}
else if (Select == 2)
{
//查看账号
//cout << "查看账号" << endl;
man->ShowAccount();
}
else if (Select == 3)
{
//查看机房
//cout << "查看机房" << endl;
man->ShowComputerRoom();
}
else if (Select == 4)
{
//清空预约
//cout << "清空预约" << endl;
man->ClearOrder();
}
else
{
//注销登录
delete manager; //销毁堆区的对象
cout << "注销成功!\n";
system("pause");
system("cls");
return;
}
}
}
//进入老师子菜单界面
void TeacherMenu(Identity*& manager)
{
while (true)
{
//调用老师子菜单
manager->Menu();
int select;
cin >> select;
//将父类指针转为子类指针,调用子类其他接口
Teacher* man = (Teacher*)manager;
switch (select)
{
case 1: //查看所有预约
//cout << "查看所有预约" << endl;
man->ShowAllOrder();
break;
case 2: //审核预约
//cout << "审核预约\n";
man->ValidOrder();
break;
case 0: //注销登录
delete manager;
cout << "注销成功!\n";
system("pause");
system("cls");
return;
default:
cout << "输入有误,请重新输入!\n";
system("Pause");
system("cls");
break;
}
}
}
//进入学生子菜单界面
void StudentMenu(Identity*& manager)
{
while (true)
{
//进入学生子界面
manager->Menu();
//将父类指针强转为子类指针,可以访问子类中其他函数
Student* man = (Student*)manager;
int select;
cin >> select;
switch (select)
{
case 1: //申请预约
//cout << "申请预约\n";
man->ApplyOrder();
break;
case 2: //查看我的预约
//cout << "查看我的预约\n";
man->ShowMyOrder();
break;
case 3: //查看所有预约
//cout << "查看所有预约\n";
man->ShowAllOrder();
break;
case 4: //取消预约
//cout << "取消预约\n";
man->CancelOrder();
break;
case 0: //注销登录
delete manager;
cout << "注销成功!\n";
system("Pause");
system("cls");
return;
default:
cout << "输入有误,请重新输入!\n";
system("Pause");
system("cls");
break;
}
}
}