机房预约系统
系统需求
1.身份简介
- 学生代表:申请使用机房
- 教师:审核学生的预约申请
- 管理员:给学生 和 老师 添加账号
2.机房简介
- 1号机房——最多容纳30人
- 2号机房——最多容纳50人
- 3号机房——最多容纳100人
3.申请简介
- 管理员每周要清空申请记录
- 学生可以预约未来一周内的机房使用,预约的时间为周一到周五,预约时需要选择预约的时间段(上午还是下午)
- 教师来审核学生的预约申请,根据实际情况审核 通过 还是 不通过
4.系统具体需求
进入登录页面,可选身份有:
- 学生代表
- 老师
- 管理员
每一个身份都需要验证后,进入子菜单
- 学生需要输入:学号、姓名、登录密码
- 老师需要输入:职工号、姓名、登录密码
- 管理员需要输入:管理员姓名、登录密码
学生功能:
- 申请预约——预约机房
- 查看自身的预约记录——查看全部的预约信息以及预约状态
- 取消预约——取消自身的预约(只有状态为预约成功 或 审核中 的预约才能取消)
教师功能:
- 查看所有预约——查看全部预约信息以及预约的状态
- 审核预约——对学生的预约申请进行审核
- 注销退出——退出登录
管理员功能:
- 添加账号——添加学生或教师的账号,需要检测学生编号或教师职工号是否重复
- 查看账号——可以选择查看学生 或 教师的全部信息
- 查看机房——查看所有机房信息
功能代码
1.实现菜单
cout << "============================= 欢迎来到机房预约系统 ================================" << endl;
cout << "请输入您的身份" << endl;
cout << "\t\t-------------------------------------------\n";
cout << "\t\t| |\n";
cout << "\t\t| 1.学生代表 |\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 2.老 师 |\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 3.管理员 |\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 0. 退 出 |\n";
cout << "\t\t| |\n";
cout << "\t\t-------------------------------------------\n";
cout << "请输入您的选择:" << endl;
2. 退出功能
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
3. 创建身份类
身份类的基类
identity.h
#pragma once
#include<iostream>
using namespace std;
//抽象身份基类
class Identity {
public:
virtual void operMenu() = 0;
//用户名
string m_username;
//密码
string m_password;
};
学生类的创建
student.h
#pragma once
#include<iostream>
#include<vector>
#include<fstream>
#include<cstdlib>
using namespace std;
#include"identity.h"
#include"globalFile.h"
#include"computerRoom.h"
#include "orderFile.h"
class Student :public Identity {
public:
Student();
Student(int id, string username, string password);
virtual void operMenu();
//申请预约
void applyOrder();
//查看预约
void showOrder();
//查看所有预约
void showAllOrder();
//取消预约
void cancelOrder();
//学号
int m_id;
//保存机房信息
vector<ComputerRoom> vCom;
};
老师类的创建
#pragma once
#include<iostream>
using namespace std;
#include"identity.h"
#include"orderFile.h"
class Teacher : public Identity {
public:
Teacher();
Teacher(int empId,string username,string password);
virtual void operMenu();
//查看所有预约
void showAllOrder();
//审核预约
void checkOrder();
//职工编号
int m_empId;
};
管理员类的创建
#pragma once
#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
using namespace std;
#include"identity.h"
#include "globalFile.h"
#include "teacher.h"
#include "student.h"
#include "computerRoom.h"
class Admin :public Identity {
public:
Admin();
Admin(string username, string password);
virtual void operMenu();
//添加账号
void addPerson();
//查看账号
void showPerson();
//查看机房
void showComputer();
//清空记录
void clearRecord();
//初始化容器
void initVector();
//检测id 是否重复
bool checkRepeat(int id, int type);
//存放学生学号
vector<Student> vStu;
//存放教师职工号
vector<Teacher> vTea;
//存放机房信息
vector<ComputerRoom> vCom;
};
4. 登录功能
全局文件添加
globalFIle.h
#pragma once
//学生文件
#define STUDENT_FILE "student.txt"
//教师文件
#define TEACHER_FILE "teacher.txt"
//管理员文件
#define ADMIN_FILE "admin.txt"
//机房信息文件
#define COMPUTER_FILE "computerRoom.txt"
//订单文件
#define ORDER_FILE "order.txt"
登陆函数
//登录
void login(string fileName, int type) {
Identity* person = nullptr;
ifstream ifs(fileName, ios::in);
if (!ifs.is_open()) {
cout << "文件不存在" << endl;
ifs.close();
return;
}
//接收用户信息
int id;
string username;
string password;
//学生登录
if (type == 1) {
cout << "请输入您的学号:" << endl;
cin >> id;
}
//老师登录
else if(type == 2) {
cout << "请输入您的职工编号:" << endl;
cin >> id;
}
cout << "请输入您的用户名:" << endl;
cin >> username;
cout << "请输入您的密码:" << endl;
cin >> password;
if (type == 1) {
//学生验证
int fId;
string fName;
string fPwd;
while (ifs >> fId && ifs >> fName && ifs >> fPwd) {
if (fId == id && fName == username && fPwd == password) {
cout << "学生验证成功!" << endl;
system("pause");
system("cls");
person = new Student(id, username, password);
ifs.close();
//进入学生子菜单
studentMenu(person);
return;
}
}
}
else if (type == 2) {
//老师验证
int fId;
string fName;
string fPwd;
while (ifs >> fId && ifs >> fName && ifs >> fPwd) {
if (fId == id && fName == username && fPwd == password) {
cout << "老师验证成功!" << endl;
system("pause");
system("cls");
person = new Teacher(id, username, password);
ifs.close();
//进入老师子菜单
teacherMenu(person);
return;
}
}
}
else if (type == 3) {
//管理员验证
string fName;
string fPwd;
while ( ifs >> fName && ifs >> fPwd) {
if (fName == username && fPwd == password) {
cout << "管理员验证成功!" << endl;
system("pause");
system("cls");
person = new Admin(username, password);
ifs.close();
//进入管理员子菜单
adminMenu(person);
return;
}
}
}
cout << "验证登录失败!!!" << endl;
system("pause");
system("cls");
}
5. 管理员模块
构造函数
Admin::Admin(string username, string password) {
this->m_username = username;
this->m_password = password;
this->initVector();
//初始化机房信息
ifstream ifs(COMPUTER_FILE, ios::in);
ComputerRoom com;
while (ifs >> com.m_comId && ifs >> com.m_maxSize) {
vCom.push_back(com);
}
ifs.close();
cout << "机房的数量为: " << vCom.size() << endl;
}
//初始化容器
void Admin::initVector() {
//清空操作
this->vStu.clear();
this->vTea.clear();
ifstream ifs(STUDENT_FILE, ios::in);
if (!ifs.is_open()) {
cout << "学生文件打开失败" << endl;
ifs.close();
return;
}
Student student;
while (ifs >> student.m_id && ifs >> student.m_username && ifs >> student.m_password) {
vStu.push_back(student);
}
cout << "学生账号的数量为:" << vStu.size() << endl;
ifs.close();
ifs.open(TEACHER_FILE, ios::in);
if (!ifs.is_open()) {
cout << "学生文件打开失败" << endl;
ifs.close();
return;
}
Teacher t;
while (ifs >> t.m_empId && ifs >> t.m_username && ifs >> t.m_password) {
vTea.push_back(t);
}
cout << "老师账号的数量为:" << vTea.size() << endl;
ifs.close();
}
管理员子菜单
void Admin::operMenu() {
cout << "欢迎管理员:" << this->m_username << "登录" << endl;
cout << "\t\t ----------------------------------------\n";
cout << "\t\t| |\n";
cout << "\t\t| 1.添加账号 |\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 2.查看账号 |\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 3.查看机房信息 |\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 4.清空记录 |\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 0.注销退出 |\n";
cout << "\t\t| |\n";
cout << "\t\t ----------------------------------------\n";
cout << "请输入您的选择:" << endl;
}
添加账号
//添加账号
void Admin::addPerson() {
cout << "请输入添加账号的类型:" << endl;
cout << "1、添加学生" << endl;
cout << "2、添加老师" << endl;
string fileName;
//错误提示
string errorTip;
int choice;
cin >> choice;
if (choice == 1) {
fileName = STUDENT_FILE;
errorTip = "学号重复,请重新输入";
cout << "请输入您的学号:" << endl;
}
else if (choice == 2) {
fileName = TEACHER_FILE;
errorTip = "职工号重复,请重新输入";
cout << "请输入您的职工编号:" << endl;
}
string username;
string password;
int id;
while (true) {
cin >> id;
bool ok = checkRepeat(id, choice);
if (ok) {
cout << errorTip << endl;
}
else break;
}
cout << "请输入用户名:" << endl;
cin >> username;
cout << "请输入密码:" << endl;
cin >> password;
ofstream ofs(fileName, ios::out | ios::app);
//写入文件
ofs << id << " " << username << " " << password << endl;
cout << "创建账号成功!" << endl;
system("pause");
system("cls");
ofs.close();
//每次添加一个人 都要重新更新到容器中
this->initVector();
}
//检测id 是否重复
bool Admin::checkRepeat(int id,int type) {
if (type == 1) {
//检测学生id 是否重复
for (auto it = vStu.begin(); it != vStu.end(); it++) {
if (id == it->m_id) return true;
}
}
else {
//检测老师职工号 是否重复
for (auto it = vTea.begin(); it != vTea.end(); it++) {
if (id == it->m_empId) return true;
}
}
return false;
}
显示账号
//查看账号
void Admin::showPerson() {
cout << "请输入您要查看的内容" << endl;
cout << "1、查看所有学生信息" << endl;
cout << "2、查看所有老师信息" << endl;
int choice = 0;
cin >> choice;
if (choice == 1) {
cout << "所有学生信息如下:" << endl;
for_each(vStu.begin(), vStu.end(), printStudent);
}
else if (choice == 2) {
cout << "所有老师信息如下:" << endl;
for_each(vTea.begin(), vTea.end(), printTeacher);
}
system("pause");
system("cls");
}
查看机房
//查看机房
void Admin::showComputer() {
cout << "机房信息如下:" << endl;
for_each(vCom.begin(), vCom.end(), [](auto& com) {
cout << "机房编号: " << com.m_comId << " 机房最大容量: " << com.m_maxSize << endl;
});
system("pause");
system("cls");
}
清空预约
//清空预约
void Admin::clearRecord() {
ifstream ifs(ORDER_FILE, ios::trunc);
ifs.close();
cout << "清空预约成功!" << endl;
system("pause");
system("cls");
}
6.学生模块
构造函数
Student::Student(int id, string username, string password) {
this->m_id = id;
this->m_username = username;
this->m_password = password;
//将机房信息读入vCom 容器中
ifstream ifs(COMPUTER_FILE, ios::in);
ComputerRoom com;
while (ifs >> com.m_comId && ifs >> com.m_maxSize) {
vCom.push_back(com);
}
ifs.close();
}
申请预约功能
//申请预约
void Student::applyOrder() {
cout << "机房可以开放的时间为周一至周五" << endl;
cout << "请输入您具体要预约的哪一天:" << endl;
cout << "1、周一" << endl;
cout << "2、周二" << endl;
cout << "3、周三" << endl;
cout << "4、周四" << endl;
cout << "5、周五" << endl;
int d;
int interval;
int roomId;
while (true) {
cin >> d;
if (d >= 1 && d <= 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;
for (int i = 0; i < vCom.size(); i++) {
cout << i + 1 << "、第 " << vCom[i].m_comId << " 号机房的最大容量为:" << vCom[i].m_maxSize << endl;
}
while (true) {
cin >> roomId;
if (roomId >= 1 && roomId <= 3) break;
cout << "输入有误,请重新输入!" << endl;
}
cout << "预约成功,预约审核中!" << endl;
ofstream ofs(ORDER_FILE, ios::app);
ofs << "date:" << d << " ";
ofs << "interval:" << interval << " ";
ofs << "stuId:" << this->m_id << " ";
ofs << "stuName:" << this->m_username << " ";
ofs << "roomId:" << roomId << " ";
//1 代表预约审核中
ofs << "status:" << 1 << endl;
ofs.close();
system("pause");
system("cls");
}
显示预约
orderFIle.h
#pragma once
#include<iostream>
#include<fstream>
#include<map>
#include<vector>
using namespace std;
#include "globalFile.h"
class OrderFile {
public:
OrderFile();
//更新预约记录
void updateOrderRecord();
//记录预约的条数
int m_size;
//记录预约的信息
map<int, map<string, string>> m_orderData;
};
orderFile.cpp
#include"orderFile.h"
using namespace std;
void process(vector<vector<string>> &v,map<int,map<string,string>> &orderData,int &m_size) {
for (auto& e : v) {
map<string, string> ma;
for (auto& s : e) {
int pos = s.find(":");
if (pos == -1) continue;
string key = s.substr(0, pos);
string value = s.substr(pos + 1);
ma.insert(make_pair(key, value));
}
orderData.insert(make_pair(m_size,ma));
m_size++;
}
}
OrderFile::OrderFile() {
this->m_size = 0;
vector<vector<string>> v;
ifstream ifs(ORDER_FILE, ios::in);
string date;
string interval;
string stuId;
string stuName;
string roomId;
string status;
while (ifs >> date && ifs >> interval && ifs >> stuId && ifs >> stuName && ifs >> roomId && ifs >> status) {
vector<string> temp;
temp.push_back(date);
temp.push_back(interval);
temp.push_back(stuId);
temp.push_back(stuName);
temp.push_back(roomId);
temp.push_back(status);
v.push_back(temp);
}
//process 函数处理预约记录 加入到 m_orderData 中
process(v, this->m_orderData, this->m_size);
ifs.close();
//打印测试
/*for (auto it = this->m_orderData.begin(); it != this->m_orderData.end(); it++) {
cout << "第 " << it->first + 1 << " 条记录:" << endl;
for (auto mit : this->m_orderData[it->first]) {
cout <<mit.first << " : " << mit.second << " ";
}
cout << endl;
}
system("pause");
system("cls");*/
}
//更新预约记录
void OrderFile::updateOrderRecord() {
if (this->m_size == 0) {
cout << "预约记录为空" << endl;
return;
}
ofstream ofs(ORDER_FILE, ios::out | ios::trunc);
for (int i = 0; i < this->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();
}
//查看预约
void Student::showOrder() {
OrderFile of;
if (of.m_size == 0) {
cout << "预约记录为空!" << endl;
system("pause");
system("cls");
return;
}
for (int i = 0; i < of.m_size; i++) {
if (this->m_id == atoi(of.m_orderData[i]["stuId"].c_str())) {
cout << "时间: 周" << of.m_orderData[i]["date"];
cout << " 时间段: " << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
cout << " 机房编号: " << of.m_orderData[i]["roomId"];
string status = "状态: ";
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 if (of.m_orderData[i]["status"] == "0") {
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;
}
int index = 1;
vector<int> v;
for (int i = 0; i < of.m_size; i++) {
if (this->m_id == atoi(of.m_orderData[i]["stuId"].c_str())) {
//只能取消 预约成功 和 审核中的 预约
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 = "状态: ";
if (of.m_orderData[i]["status"] == "1") {
status += "审核中";
}
else if (of.m_orderData[i]["status"] == "2") {
status += "预约成功";
}
cout << " " << status << endl;
}
}
}
cout << "请输入要取消的预约,输入0返回" << endl;
int choice = 0;
cin >> choice;
while (true) {
if (choice >= 0 && choice <= v.size()) {
//返回
if (choice == 0) {
break;
}
else {
of.m_orderData[v[choice - 1]]["status"] = "0";
//更新预约
of.updateOrderRecord();
cout << "已经取消预约" << endl;
break;
}
}
cout << "输入有误,请重新输入!" << endl;
}
system("pause");
system("cls");
}
7. 教师模块
教师子菜单
void Teacher::operMenu() {
cout << "欢迎老师:" << this->m_username << "登录" << endl;
cout << "\t\t ----------------------------------------\n";
cout << "\t\t| |\n";
cout << "\t\t| 1.查看所有预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 2.审核预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 0.注销退出 |\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t ----------------------------------------\n";
cout << "请输入您的选择:" << endl;
}
审核预约
//审核预约
void Teacher::checkOrder() {
OrderFile of;
if (of.m_size == 0) {
cout << "预约记录为空!" << endl;
system("pause");
system("cls");
return;
}
int index = 1;
vector<int> v;
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 = "状态: 审核中";
cout << " " << status << endl;
}
}
cout << "请输入要审核的预约,输入0返回" << endl;
int choice = 0;
cin >> choice;
while (true) {
if (choice >= 0 && choice <= v.size()) {
//返回
if (choice == 0) {
break;
}
else {
cout << "请输入审核的结果:" << endl;
cout << "1、通过" << endl;
cout << "2、不通过" << endl;
int ret = 1;
cin >> ret;
if (ret == 1) {
//状态修改为审核通过
of.m_orderData[v[choice - 1]]["status"] = "2";
}
else {
//状态修改为审核失败
of.m_orderData[v[choice - 1]]["status"] = "-1";
}
cout << "审核完毕!" << endl;
//更新预约记录
of.updateOrderRecord();
break;
}
}
cout << "输入有误,请重新输入!" << endl;
}
system("pause");
system("cls");
}
完整代码
admin.h
#pragma once
#include<iostream>
#include<fstream>
#include<vector>
#include<algorithm>
using namespace std;
#include"identity.h"
#include "globalFile.h"
#include "teacher.h"
#include "student.h"
#include "computerRoom.h"
class Admin :public Identity {
public:
Admin();
Admin(string username, string password);
virtual void operMenu();
//添加账号
void addPerson();
//查看账号
void showPerson();
//查看机房
void showComputer();
//清空记录
void clearRecord();
//初始化容器
void initVector();
//检测id 是否重复
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;//机房编号
int m_maxSize;//机房最大容量
};
globalFile.h
#pragma once
//学生文件
#define STUDENT_FILE "student.txt"
//教师文件
#define TEACHER_FILE "teacher.txt"
//管理员文件
#define ADMIN_FILE "admin.txt"
//机房信息文件
#define COMPUTER_FILE "computerRoom.txt"
//订单文件
#define ORDER_FILE "order.txt"
identity.h
#pragma once
#include<iostream>
using namespace std;
//抽象身份基类
class Identity {
public:
virtual void operMenu() = 0;
//用户名
string m_username;
//密码
string m_password;
};
orderFile.h
#pragma once
#include<iostream>
#include<fstream>
#include<map>
#include<vector>
using namespace std;
#include "globalFile.h"
class OrderFile {
public:
OrderFile();
//更新预约记录
void updateOrderRecord();
//记录预约的条数
int m_size;
//记录预约的信息
map<int, map<string, string>> m_orderData;
};
student.h
#pragma once
#include<iostream>
#include<vector>
#include<fstream>
#include<cstdlib>
using namespace std;
#include"identity.h"
#include"globalFile.h"
#include"computerRoom.h"
#include "orderFile.h"
class Student :public Identity {
public:
Student();
Student(int id, string username, string password);
virtual void operMenu();
//申请预约
void applyOrder();
//查看预约
void showOrder();
//查看所有预约
void showAllOrder();
//取消预约
void cancelOrder();
//学号
int m_id;
//保存机房信息
vector<ComputerRoom> vCom;
};
teacher.h
#pragma once
#include<iostream>
using namespace std;
#include"identity.h"
#include"orderFile.h"
class Teacher : public Identity {
public:
Teacher();
Teacher(int empId,string username,string password);
virtual void operMenu();
//查看所有预约
void showAllOrder();
//审核预约
void checkOrder();
//职工编号
int m_empId;
};
admin.cpp
#include"admin.h"
using namespace std;
Admin::Admin() {
}
Admin::Admin(string username, string password) {
this->m_username = username;
this->m_password = password;
this->initVector();
//初始化机房信息
ifstream ifs(COMPUTER_FILE, ios::in);
ComputerRoom com;
while (ifs >> com.m_comId && ifs >> com.m_maxSize) {
vCom.push_back(com);
}
ifs.close();
cout << "机房的数量为: " << vCom.size() << endl;
}
void Admin::operMenu() {
cout << "欢迎管理员:" << this->m_username << "登录" << endl;
cout << "\t\t ----------------------------------------\n";
cout << "\t\t| |\n";
cout << "\t\t| 1.添加账号 |\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 2.查看账号 |\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 3.查看机房信息 |\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 4.清空记录 |\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 0.注销退出 |\n";
cout << "\t\t| |\n";
cout << "\t\t ----------------------------------------\n";
cout << "请输入您的选择:" << endl;
}
//添加账号
void Admin::addPerson() {
cout << "请输入添加账号的类型:" << endl;
cout << "1、添加学生" << endl;
cout << "2、添加老师" << endl;
string fileName;
//错误提示
string errorTip;
int choice;
cin >> choice;
if (choice == 1) {
fileName = STUDENT_FILE;
errorTip = "学号重复,请重新输入";
cout << "请输入您的学号:" << endl;
}
else if (choice == 2) {
fileName = TEACHER_FILE;
errorTip = "职工号重复,请重新输入";
cout << "请输入您的职工编号:" << endl;
}
string username;
string password;
int id;
while (true) {
cin >> id;
bool ok = checkRepeat(id, choice);
if (ok) {
cout << errorTip << endl;
}
else break;
}
cout << "请输入用户名:" << endl;
cin >> username;
cout << "请输入密码:" << endl;
cin >> password;
ofstream ofs(fileName, ios::out | ios::app);
//写入文件
ofs << id << " " << username << " " << password << endl;
cout << "创建账号成功!" << endl;
system("pause");
system("cls");
ofs.close();
//每次添加一个人 都要重新更新到容器中
this->initVector();
}
void printStudent(Student& s) {
cout << "学号: " << s.m_id << " 姓名: " << s.m_username << " 密码: " << s.m_password << endl;
}
void printTeacher(Teacher& t) {
cout << "学号: " << t.m_empId << " 姓名: " << t.m_username << " 密码: " << t.m_password << endl;
}
//查看账号
void Admin::showPerson() {
cout << "请输入您要查看的内容" << endl;
cout << "1、查看所有学生信息" << endl;
cout << "2、查看所有老师信息" << endl;
int choice = 0;
cin >> choice;
if (choice == 1) {
cout << "所有学生信息如下:" << endl;
for_each(vStu.begin(), vStu.end(), printStudent);
}
else if (choice == 2) {
cout << "所有老师信息如下:" << endl;
for_each(vTea.begin(), vTea.end(), printTeacher);
}
system("pause");
system("cls");
}
//查看机房
void Admin::showComputer() {
cout << "机房信息如下:" << endl;
for_each(vCom.begin(), vCom.end(), [](auto& com) {
cout << "机房编号: " << com.m_comId << " 机房最大容量: " << com.m_maxSize << endl;
});
system("pause");
system("cls");
}
//清空预约
void Admin::clearRecord() {
ifstream ifs(ORDER_FILE, ios::trunc);
ifs.close();
cout << "清空预约成功!" << endl;
system("pause");
system("cls");
}
//初始化容器
void Admin::initVector() {
//清空操作
this->vStu.clear();
this->vTea.clear();
ifstream ifs(STUDENT_FILE, ios::in);
if (!ifs.is_open()) {
cout << "学生文件打开失败" << endl;
ifs.close();
return;
}
Student student;
while (ifs >> student.m_id && ifs >> student.m_username && ifs >> student.m_password) {
vStu.push_back(student);
}
cout << "学生账号的数量为:" << vStu.size() << endl;
ifs.close();
ifs.open(TEACHER_FILE, ios::in);
if (!ifs.is_open()) {
cout << "学生文件打开失败" << endl;
ifs.close();
return;
}
Teacher t;
while (ifs >> t.m_empId && ifs >> t.m_username && ifs >> t.m_password) {
vTea.push_back(t);
}
cout << "老师账号的数量为:" << vTea.size() << endl;
ifs.close();
}
//检测id 是否重复
bool Admin::checkRepeat(int id,int type) {
if (type == 1) {
//检测学生id 是否重复
for (auto it = vStu.begin(); it != vStu.end(); it++) {
if (id == it->m_id) return true;
}
}
else {
//检测老师职工号 是否重复
for (auto it = vTea.begin(); it != vTea.end(); it++) {
if (id == it->m_empId) return true;
}
}
return false;
}
orderFile.cpp
#include"orderFile.h"
using namespace std;
void process(vector<vector<string>> &v,map<int,map<string,string>> &orderData,int &m_size) {
for (auto& e : v) {
map<string, string> ma;
for (auto& s : e) {
int pos = s.find(":");
if (pos == -1) continue;
string key = s.substr(0, pos);
string value = s.substr(pos + 1);
ma.insert(make_pair(key, value));
}
orderData.insert(make_pair(m_size,ma));
m_size++;
}
}
OrderFile::OrderFile() {
this->m_size = 0;
vector<vector<string>> v;
ifstream ifs(ORDER_FILE, ios::in);
string date;
string interval;
string stuId;
string stuName;
string roomId;
string status;
while (ifs >> date && ifs >> interval && ifs >> stuId && ifs >> stuName && ifs >> roomId && ifs >> status) {
vector<string> temp;
temp.push_back(date);
temp.push_back(interval);
temp.push_back(stuId);
temp.push_back(stuName);
temp.push_back(roomId);
temp.push_back(status);
v.push_back(temp);
}
//process 函数处理预约记录 加入到 m_orderData 中
process(v, this->m_orderData, this->m_size);
ifs.close();
//打印测试
/*for (auto it = this->m_orderData.begin(); it != this->m_orderData.end(); it++) {
cout << "第 " << it->first + 1 << " 条记录:" << endl;
for (auto mit : this->m_orderData[it->first]) {
cout <<mit.first << " : " << mit.second << " ";
}
cout << endl;
}
system("pause");
system("cls");*/
}
//更新预约记录
void OrderFile::updateOrderRecord() {
if (this->m_size == 0) {
cout << "预约记录为空" << endl;
return;
}
ofstream ofs(ORDER_FILE, ios::out | ios::trunc);
for (int i = 0; i < this->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();
}
student.cpp
#include<iostream>
using namespace std;
#include"student.h"
Student::Student() {
}
Student::Student(int id, string username, string password) {
this->m_id = id;
this->m_username = username;
this->m_password = password;
//将机房信息读入vCom 容器中
ifstream ifs(COMPUTER_FILE, ios::in);
ComputerRoom com;
while (ifs >> com.m_comId && ifs >> com.m_maxSize) {
vCom.push_back(com);
}
ifs.close();
}
void Student::operMenu() {
cout << "欢迎学生代表:" << this->m_username << "登录" << endl;
cout << "\t\t ----------------------------------------\n";
cout << "\t\t| |\n";
cout << "\t\t| 1.申请预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 2.查看我的预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 3.查看所有预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 4.取消预约 |\n";
cout << "\t\t| |\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 d;
int interval;
int roomId;
while (true) {
cin >> d;
if (d >= 1 && d <= 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;
for (int i = 0; i < vCom.size(); i++) {
cout << i + 1 << "、第 " << vCom[i].m_comId << " 号机房的最大容量为:" << vCom[i].m_maxSize << endl;
}
while (true) {
cin >> roomId;
if (roomId >= 1 && roomId <= 3) break;
cout << "输入有误,请重新输入!" << endl;
}
cout << "预约成功,预约审核中!" << endl;
ofstream ofs(ORDER_FILE, ios::app);
ofs << "date:" << d << " ";
ofs << "interval:" << interval << " ";
ofs << "stuId:" << this->m_id << " ";
ofs << "stuName:" << this->m_username << " ";
ofs << "roomId:" << roomId << " ";
//1 代表预约审核中
ofs << "status:" << 1 << endl;
ofs.close();
system("pause");
system("cls");
}
//查看预约
void Student::showOrder() {
OrderFile of;
if (of.m_size == 0) {
cout << "预约记录为空!" << endl;
system("pause");
system("cls");
return;
}
for (int i = 0; i < of.m_size; i++) {
if (this->m_id == atoi(of.m_orderData[i]["stuId"].c_str())) {
cout << "时间: 周" << of.m_orderData[i]["date"];
cout << " 时间段: " << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
cout << " 机房编号: " << of.m_orderData[i]["roomId"];
string status = "状态: ";
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 if (of.m_orderData[i]["status"] == "0") {
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]["date"];
cout << " 时间段: " << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
cout << " 机房编号: " << of.m_orderData[i]["roomId"];
cout << " 学号: " << of.m_orderData[i]["stuId"];
cout << " 姓名: " << of.m_orderData[i]["stuName"];
string status = "状态: ";
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 if (of.m_orderData[i]["status"] == "0") {
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;
}
int index = 1;
vector<int> v;
for (int i = 0; i < of.m_size; i++) {
if (this->m_id == atoi(of.m_orderData[i]["stuId"].c_str())) {
//只能取消 预约成功 和 审核中的 预约
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 = "状态: ";
if (of.m_orderData[i]["status"] == "1") {
status += "审核中";
}
else if (of.m_orderData[i]["status"] == "2") {
status += "预约成功";
}
cout << " " << status << endl;
}
}
}
cout << "请输入要取消的预约,输入0返回" << endl;
int choice = 0;
cin >> choice;
while (true) {
if (choice >= 0 && choice <= v.size()) {
//返回
if (choice == 0) {
break;
}
else {
of.m_orderData[v[choice - 1]]["status"] = "0";
//更新预约
of.updateOrderRecord();
cout << "已经取消预约" << endl;
break;
}
}
cout << "输入有误,请重新输入!" << endl;
}
system("pause");
system("cls");
}
teacher.cpp
#include"teacher.h"
using namespace std;
Teacher::Teacher() {
}
Teacher::Teacher(int empId, string username, string password) {
this->m_empId = empId;
this->m_username = username;
this->m_password = password;
}
void Teacher::operMenu() {
cout << "欢迎老师:" << this->m_username << "登录" << endl;
cout << "\t\t ----------------------------------------\n";
cout << "\t\t| |\n";
cout << "\t\t| 1.查看所有预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 2.审核预约 |\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 0.注销退出 |\n";
cout << "\t\t| |\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]["date"];
cout << " 时间段: " << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
cout << " 机房编号: " << of.m_orderData[i]["roomId"];
cout << " 学号: " << of.m_orderData[i]["stuId"];
cout << " 姓名: " << of.m_orderData[i]["stuName"];
string status = "状态: ";
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 if (of.m_orderData[i]["status"] == "0") {
status += "取消预约";
}
cout << " " << status << endl;
}
system("pause");
system("cls");
}
//审核预约
void Teacher::checkOrder() {
OrderFile of;
if (of.m_size == 0) {
cout << "预约记录为空!" << endl;
system("pause");
system("cls");
return;
}
int index = 1;
vector<int> v;
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 = "状态: 审核中";
cout << " " << status << endl;
}
}
cout << "请输入要审核的预约,输入0返回" << endl;
int choice = 0;
cin >> choice;
while (true) {
if (choice >= 0 && choice <= v.size()) {
//返回
if (choice == 0) {
break;
}
else {
cout << "请输入审核的结果:" << endl;
cout << "1、通过" << endl;
cout << "2、不通过" << endl;
int ret = 1;
cin >> ret;
if (ret == 1) {
//状态修改为审核通过
of.m_orderData[v[choice - 1]]["status"] = "2";
}
else {
//状态修改为审核失败
of.m_orderData[v[choice - 1]]["status"] = "-1";
}
cout << "审核完毕!" << endl;
//更新预约记录
of.updateOrderRecord();
break;
}
}
cout << "输入有误,请重新输入!" << endl;
}
system("pause");
system("cls");
}
机房预约系统.cpp
#include<iostream>
#include<fstream>
using namespace std;
#include"identity.h"
#include "student.h"
#include "teacher.h"
#include "admin.h"
#include"globalFile.h"
//老师子菜单
void teacherMenu(Identity*& teacher) {
while (true) {
//管理员子菜单页面
teacher->operMenu();
//将基类指针 转为 子类指针 来调用子类特有的函数
Teacher* tea = (Teacher*)teacher;
int choice;
cin >> choice;
if (choice == 1) {
//查看所有预约
tea->showAllOrder();
}
else if (choice == 2) {
//审核预约
tea->checkOrder();
}
else {
//注销退出
delete teacher;
cout << "注销退出成功" << endl;
system("pause");
system("cls");
break;
}
}
}
//学生子菜单
void studentMenu(Identity*& student) {
while (true) {
//管理员子菜单页面
student->operMenu();
//将基类指针 转为 子类指针 来调用子类特有的函数
Student* stu = (Student*)student;
int choice;
cin >> choice;
if (choice == 1) {
//申请预约
stu->applyOrder();
}
else if (choice == 2) {
//查看我的预约
stu->showOrder();
}
else if (choice == 3) {
//查看所有预约
stu->showAllOrder();
}
else if (choice == 4) {
//取消预约
stu->cancelOrder();
}
else {
//注销退出
delete student;
cout << "注销退出成功" << endl;
system("pause");
system("cls");
break;
}
}
}
//管理员子菜单
void adminMenu(Identity*& admin) {
while (true) {
//管理员子菜单页面
admin->operMenu();
//将基类指针 转为 子类指针 来调用子类特有的函数
Admin* ad = (Admin*)admin;
int choice;
cin >> choice;
if (choice == 1) {
//添加账号
//cout << "添加账号" << endl;
ad->addPerson();
}
else if (choice == 2) {
//查看账号
//cout << "查看账号" << endl;
ad->showPerson();
}
else if (choice == 3) {
//查看机房信息
//cout << "查看机房信息" << endl;
ad->showComputer();
}
else if (choice == 4) {
//清空记录
//cout << "清空记录" << endl;
ad->clearRecord();
}
else {
//注销退出
delete admin;
cout << "注销退出成功" << endl;
system("pause");
system("cls");
return;
}
}
}
//登录
void login(string fileName, int type) {
Identity* person = nullptr;
ifstream ifs(fileName, ios::in);
if (!ifs.is_open()) {
cout << "文件不存在" << endl;
ifs.close();
return;
}
//接收用户信息
int id;
string username;
string password;
//学生登录
if (type == 1) {
cout << "请输入您的学号:" << endl;
cin >> id;
}
//老师登录
else if(type == 2) {
cout << "请输入您的职工编号:" << endl;
cin >> id;
}
cout << "请输入您的用户名:" << endl;
cin >> username;
cout << "请输入您的密码:" << endl;
cin >> password;
if (type == 1) {
//学生验证
int fId;
string fName;
string fPwd;
while (ifs >> fId && ifs >> fName && ifs >> fPwd) {
if (fId == id && fName == username && fPwd == password) {
cout << "学生验证成功!" << endl;
system("pause");
system("cls");
person = new Student(id, username, password);
ifs.close();
//进入学生子菜单
studentMenu(person);
return;
}
}
}
else if (type == 2) {
//老师验证
int fId;
string fName;
string fPwd;
while (ifs >> fId && ifs >> fName && ifs >> fPwd) {
if (fId == id && fName == username && fPwd == password) {
cout << "老师验证成功!" << endl;
system("pause");
system("cls");
person = new Teacher(id, username, password);
ifs.close();
//进入老师子菜单
teacherMenu(person);
return;
}
}
}
else if (type == 3) {
//管理员验证
string fName;
string fPwd;
while ( ifs >> fName && ifs >> fPwd) {
if (fName == username && fPwd == password) {
cout << "管理员验证成功!" << endl;
system("pause");
system("cls");
person = new Admin(username, password);
ifs.close();
//进入管理员子菜单
adminMenu(person);
return;
}
}
}
cout << "验证登录失败!!!" << endl;
system("pause");
system("cls");
}
int main() {
while (true) {
cout << "============================= 欢迎来到机房预约系统 ================================" << endl;
cout << "请输入您的身份" << endl;
cout << "\t\t-------------------------------------------\n";
cout << "\t\t| |\n";
cout << "\t\t| 1.学生代表 |\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 2.老 师 |\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 3.管理员 |\n";
cout << "\t\t| |\n";
cout << "\t\t| |\n";
cout << "\t\t| 0. 退 出 |\n";
cout << "\t\t| |\n";
cout << "\t\t-------------------------------------------\n";
cout << "请输入您的选择:" << endl;
int choice = 0;
cin >> choice;
switch (choice) {
case 1: //学生
login(STUDENT_FILE, 1);
break;
case 2: //老师
login(TEACHER_FILE, 2);
break;
case 3: //管理员
login(ADMIN_FILE, 3);
break;
case 0: //退出
cout << "欢迎下次使用" << endl;
system("pause");
return 0;
default:
cout << "输入有误,请重新输入" << endl;
system("pause");
system("cls");
break;
}
}
system("pause");
return 0;
}