computerRoom.h
#pragma once #include<iostream> class computerRoom { public: int m_Id; int m_MaxNum; };
globalFile.h
//管理员文件 #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"
Identity.h
#pragma once #include<iostream> #include<string> #include<fstream> using namespace std; class Identity { public: virtual void operMenu() = 0;//子菜单界面 public: string m_name; string m_pwd; };
manager.h
#pragma once #include<vector> #include"computerRoom.h" #include"Identity.h" #include"globalFile.h" #include"teacher.h" #include"student.h" class manager:public Identity { public: manager() = default; manager(string name, string pwd); virtual void operMenu(); void addPerson(); void showPerson(); void showComputer(); //清空预约信息 void cleanFile(); void initVector(); bool checkRepeat(int id, int type); void initComputerRoom(); public: vector<student>vStu; vector<teacher>vTeac; vector<computerRoom>vCom; };
orderFile.h
#pragma once #include<iostream> #include<unordered_map> #include"globalFile.h" using namespace std; class orderFile { public: orderFile(); void updateOrder(); unordered_map<int, unordered_map<string, string>>m_orderDate; //预约记录条数 int m_Size{ 0 }; };
student.h
#pragma once #include"Identity.h" #include<vector> #include"orderFile.h" #include"computerRoom.h" class student :public Identity { public: student() = default; student(int id, string name, string pwd); virtual void operMenu(); //申请预约 void applyOrder(); //查看我的预约 void showMyOrder(); //查看所有预约 void showAllOrder(); //取消预约 void cancelOrder(); void initComputerRoom(); public: int m_Id; vector<computerRoom>v1; };
teacher.h
#pragma once #include"Identity.h" #include"orderFile.h" class teacher :public Identity { public: teacher() = default; teacher(int empId, string name, string pwd); //界面显示 virtual void operMenu(); void showAllOrder(); //审核预约 void validOrder(); public: int m_EmpId;//教师编号 };
manager.cpp
#include"manager.h" #include"student.h" #include"computerRoom.h" manager::manager(string name, string pwd) { this->m_name = name; this->m_pwd = pwd; this->initVector(); //获取机房信息 this->initComputerRoom(); } void manager::initComputerRoom() { ifstream fd; fd.open(COMPUTER_FILE, ios::in); computerRoom c; while (fd >> c.m_Id && fd >> c.m_MaxNum) { vCom.push_back(c); } cout << "当前机房的数量:" << vCom.size() << endl; fd.close(); return; } void manager::initVector() { //读取学生文件信息 ifstream fs1; fs1.open(STUDENT_FILE, ios::in); if (!fs1.is_open()) { cout << "文件打开失败" << endl; return; } vStu.clear(); vTeac.clear(); student s; while (fs1 >> s.m_Id && fs1 >> s.m_name && fs1 >> s.m_pwd) { vStu.push_back(s); } cout << "当前学生的数量:" << vStu.size() << endl; fs1.close(); //读取老师文件信息 ifstream fs2; fs2.open(TEACHER_FILE, ios::in); if (!fs2.is_open()) { cout << "文件打开失败" << endl; return; } teacher q; while (fs2 >> q.m_EmpId && fs2 >> q.m_name && fs2 >> q.m_pwd) { vTeac.push_back(q); } cout << "当前老师的数量:" << vTeac.size() << endl; fs2.close(); } 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; } bool manager::checkRepeat(int id, int type){ if (type == 1) { for (auto it = vStu.begin(); it != vStu.end(); it++) { if (id == it->m_Id) { return true; } } } else { for (auto it = vTeac.begin(); it != vTeac.end(); it++) { if (id == it->m_EmpId) { return true; } } } return false; } void manager::addPerson() { cout << "请输入添加账户的类型:" << endl; cout << "1.添加学生" << endl; cout << "2.添加老师" << endl; string fileName; string tip; ofstream ofs; int select; cin >> select; 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 = checkRepeat(id, 1); if (ret) { cout << errorTip << endl; continue; } else { break; } } cout << "请输入姓名:" << endl; cin >> name; cout << "请输入密码:" << endl; cin >> pwd; ofs << id << " " << name << " " << pwd << " " << endl; cout << "添加成功" << endl; ofs.close(); system("pause") ; system("cls"); this->initVector(); return; } void manager::showPerson() { cout << "请选择查看的内容:" << endl; cout << "1.查看学生信息" << endl; cout << "2.查看老师信息" << endl; int select; cin >> select; string filename; if (select == 1) { for (auto it = vStu.begin(); it != vStu.end(); it++) { cout << "所有学生信息如下:" << endl; cout << "学号:"<<it->m_Id<< " " <<"姓名:"<< it->m_name << " "<<"密码:" << it->m_pwd << endl; } } else { for (auto it = vTeac.begin(); it != vTeac.end(); it++) { cout << "所有老师信息如下:" << endl; cout << "职工编号:"<<it->m_EmpId << " " <<"姓名:"<< it->m_name << " " <<"密码:"<< it->m_pwd << endl; } } system("pause"); system("cls"); return; } void manager::showComputer() { cout << "机房信息如下:" << endl; for (auto it = vCom.begin(); it != vCom.end(); it++) { cout << "机房编号:" << it->m_Id << " 机房最大容量:" << it->m_MaxNum << endl; } system("pause"); system("cls"); } void manager::cleanFile() { ofstream fd; fd.open(COMPUTER_FILE, ios::trunc); fd.close(); cout << "清空成功!" << endl; system("pause"); system("cls"); }
orderFile.cpp
#include"orderFile.h" #include"globalFile.h" #include<fstream> #include<string> orderFile:: orderFile() { ifstream fd; fd.open(ORDER_FILE, ios::in); string date; string time; string stuId; string stuName; string roomId; string status; while (fd >> date && fd >> time && fd >> stuId && fd >> stuName && fd >> roomId && fd >> status) { /*cout << "date:" << date << endl; cout << "time:" << time << endl; cout << "stuId:" << stuId << endl; cout << "stuName:" << stuName<< endl; cout << "roomId:" << roomId << endl; cout << "status:" << status << endl;*/ string key; string value; unordered_map<string, string>m; int pos = date.find(":"); if (pos != -1) { key = date.substr(0, pos); value = date.substr(pos + 1, date.size() - pos); m.insert(make_pair(key, value)); } pos = time.find(":"); if (pos != -1) { key = time.substr(0, pos); value = time.substr(pos + 1, time.size() - pos); 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); 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); 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); 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); m.insert(make_pair(key, value)); } this->m_orderDate.insert(make_pair(this->m_Size, m)); this->m_Size++; } fd.close(); //测试查看下map容器结果 /*for (auto it = m_orderDate.begin(); it != m_orderDate.end(); it++) { cout << "条数为:" << m_orderDate.size() << endl; for (auto iter = it->second.begin(); iter != it->second.end(); iter++) { cout << "key=" << iter->first << " " << "value:" << iter->second << " "; } cout << endl; }*/ } void orderFile::updateOrder() { if (this->m_Size == 0) { return; } ofstream fd; fd.open(ORDER_FILE, ios::out | ios::trunc); for (int i = 0; i < this->m_orderDate.size(); i++) { /*cout << "date:" << date << endl; cout << "time:" << time << endl; cout << "stuId:" << stuId << endl; cout << "stuName:" << stuName << endl; cout << "roomId:" << roomId << endl; cout << "status:" << status << endl;*/ fd << "date:" << this->m_orderDate[i]["date"] << " "; fd << "time:" << this->m_orderDate[i]["time"] << " "; fd << "stuId:" << this->m_orderDate[i]["stuId"] << " "; fd << "stuName:" << this->m_orderDate[i]["stuName"] << " "; fd << "roomId:" << this->m_orderDate[i]["roomId"] << " "; fd << "status:" << this->m_orderDate[i]["status"] << " "; } fd.close(); }
student.cpp
#include"student.h" #include<fstream> #include"globalFile.h" student::student(int id, string name, string pwd) { this->m_name = name; this->m_Id = id; this->m_pwd = pwd; this->initComputerRoom(); } void student::initComputerRoom() { ifstream fd; fd.open(COMPUTER_FILE, ios::in); computerRoom c; while (fd >> c.m_Id && fd >> c.m_MaxNum) { v1.push_back(c); } fd.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; } //1.申请预约 void student::applyOrder() { cout << "机房开放的预约时间为周一到周五!" << endl; cout << "请输入申请预约的时间:" << endl; cout << "1.周一" << endl; cout << "2.周二" << endl; cout << "3.周三" << endl; cout << "4.周四" << endl; cout << "5.周五" << endl; int date; int time; int id; while (true) { cin >> date; if (date >= 1 && date <= 5) { break; } cout << "输入有误,请重新输入!" << endl; } cout << "请输入申请预约的时间段" << endl; cout << "1.上午" << endl; cout << "2.下午" << endl; while (true) { cin >> time; if (time == 1 || time == 2) { break; } cout << "输入有误,请重新输入!" << endl; } cout << "请选择机房:" << endl; for (int i = 0; i < v1.size(); i++) { cout << v1[i].m_Id << "号机房的容量:" << v1[i].m_MaxNum << endl; } while (true) { cin >> id; if (id >= 1 && id <= 3) { break; } cout << "输入有误,请重新输入!" << endl; } cout << "预约成功!审核中" << endl; ofstream fd; fd.open(ORDER_FILE, ios::app); fd << "date:" << date << " "; fd << "time:" << time << " "; fd << "stuId:" << this->m_Id << " "; fd << "stuName:" << this->m_name << " "; fd << "roomId:" << id << " "; fd << "status:" << 1 << endl; fd.close(); system("pause"); system("cls"); return; } //2.查看我的预约 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_orderDate[i]["stuId"].c_str())== this->m_Id) { cout << "预约时间:周" << of.m_orderDate[i]["date"]<<" "; cout << "时段:" << (of.m_orderDate[i]["time"]=="1"?"上午":"下午")<<" "; cout << "机房:" << of.m_orderDate[i]["roomId"] << " "; string status = "状态:";//0 取消预约 1 审核中 2 已预约 -1 预约失败 if (of.m_orderDate[i]["status"] == "1") { status += "审核中"; } else if ((of.m_orderDate[i]["status"] == "2")) { status+="已预约"; } else if ((of.m_orderDate[i]["status"] == "-1")) { status += "预约失败"; } else { status += "取消预约"; } cout << status << endl; } } system("pause"); system("cls"); return; } //3.查看所有预约 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_orderDate[i]["date"]<<" "; cout << "时段:" << (of.m_orderDate[i]["time"]=="1"?"上午":"下午")<<" "; cout << "机房:" << of.m_orderDate[i]["roomId"] << " "; string status = "状态:";//0 取消预约 1 审核中 2 预约成功 -1 预约失败 if (of.m_orderDate[i]["status"] == "1") { status += "审核中"; } else if ((of.m_orderDate[i]["status"] == "2")) { status+="预约成功"; } else if ((of.m_orderDate[i]["status"] == "-1")) { status += "预约失败"; } else { status += "取消预约"; } cout << status << endl; } system("pause"); system("cls"); return; } //4.取消预约 void student::cancelOrder() { orderFile of; if (of.m_Size == 0) { cout << "无预约记录" << endl; system("pause"); system("cls"); return; } cout << "审核中或预约成功的记录是可以取消,请输入取消的记录" << endl; int index = 1; vector<int>v;//存放在最大容器中的下标编号 for (int i = 0; i < of.m_Size; i++) { if (atoi(of.m_orderDate[i]["stuId"].c_str()) == this->m_Id) { //再筛选状态 if (of.m_orderDate[i]["status"] == "1" || of.m_orderDate[i]["status"] == "2") { v.push_back(i); cout << index++ << "、"; cout << "预约时间:周" << of.m_orderDate[i]["date"] << " "; cout << "时段:" << (of.m_orderDate[i]["time"] == "1" ? "上午" : "下午") << " "; cout << "机房:" << of.m_orderDate[i]["roomId"] << " "; string status = "状态:"; //0 取消预约 1 审核中 2 已预约 -1 预约失败 if (of.m_orderDate[i]["status"] == "1") { status += "审核中"; } else { status += "预约成功"; } cout << status << endl; } } } cout << "请输入取消的记录,0代表返回" << endl; int select=0; while (true) { cin >> select; if (select == 0) { break; } else if (select > 0 && select <= v.size()) { of.m_orderDate[v[select - 1]]["status"] = "0"; of.updateOrder(); cout << "已取消预约" << endl; break; } else { cout << "输入有误,请重新输入" << endl; } } system("pause"); system("cls"); }
teacher.cpp
#include"teacher.h" 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 << "、" << endl; cout << "预约时间:周" << of.m_orderDate[i]["date"] << " "; cout << "时段:" << (of.m_orderDate[i]["time"] == "1" ? "上午" : "下午") << " "; cout << "机房:" << of.m_orderDate[i]["roomId"] << " "; string status = "状态:";//0 取消预约 1 审核中 2 预约成功 -1 预约失败 if (of.m_orderDate[i]["status"] == "1") { status += "审核中"; } else if ((of.m_orderDate[i]["status"] == "2")) { status += "预约成功"; } else if ((of.m_orderDate[i]["status"] == "-1")) { status += "预约失败"; } else { status += "取消预约"; } cout << status << endl; } system("pause"); system("cls"); return; } //审核预约 void teacher::validOrder() { orderFile of; if (of.m_Size == 0) { cout << "无预约记录" << endl; system("pause"); system("cls"); return; } cout << "审核中或预约成功的记录是可以取消,请输入取消的记录" << endl; int index = 1; vector<int>v;//存放在最大容器中的下标编号 for (int i = 0; i < of.m_Size; i++) { if (of.m_orderDate[i]["status"]=="1") { //再筛选状态 v.push_back(i); cout << index++ << "、"; cout << "预约时间:周" << of.m_orderDate[i]["date"] << " "; cout << "时段:" << (of.m_orderDate[i]["time"] == "1" ? "上午" : "下午") << " "; cout << "机房:" << of.m_orderDate[i]["roomId"] << " "; string status = "状态:"; //0 取消预约 1 审核中 2 预约成功 -1 预约失败 if (of.m_orderDate[i]["status"] == "1") { status += "审核中"; } cout << status << endl; } } cout << "请输入审核的预约记录,0代表返回" << endl; int select=0; while (true) { cin >> select; if (select == 0) { break; } else if (select > 0 && select <= v.size()) { cout << "请输入审核结果:" << endl; cout << "1.通过" << endl; cout << "2.不通过" << endl; int ret; cin >> ret; if (ret == 1) { of.m_orderDate[v[select - 1]]["status"] = "2"; } else { of.m_orderDate[v[select - 1]]["status"] = "-1"; } of.updateOrder(); cout << "审核完毕!" << endl; break; } else { cout << "输入有误,请重新输入" << endl; } } system("pause"); system("cls"); }
机房预约系统.cpp
#include<iostream> #include"Identity.h" #include<fstream> #include<string> #include"student.h" #include"teacher.h" #include"manager.h" #include"globalFile.h" using namespace std; void LoginIn(const string fileName, const int type); void showInterface(); void managerMenu(Identity* manager); void studentMenu(Identity* m_student); void teacherMenu(Identity* m_teacher); int main() { int select = 0; while (1) { showInterface(); 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; } } return 0; } //fileName:操作文件名 type:操作身份类型 void LoginIn(const string fileName, const int type) { Identity* person=NULL; ifstream ifs; ifs.open(fileName, ios::in); if (!ifs.is_open()) { cout << "文件打开失败" << endl; ifs.close(); return; } //准备接受用户信息 int 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; string fName; string fPwd; while (ifs >> fId && ifs >> fName && ifs >> fPwd) { if (fId == id && fName == name && fPwd == pwd) { cout << "学生验证登录成功!" << endl; system("pause"); system("cls"); person = new student(id, name, pwd); studentMenu(person); return; } } } else if (type == 2) { //老师身份验证 int fId; string fName; string fPwd; while (ifs >> fId && ifs >> fName && ifs >> fPwd) { if (fId == id && fName == name && fPwd == pwd) { 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 (fName == name && fPwd == pwd) { cout << "老师验证登录成功!" << endl; system("pause"); system("cls"); person = new manager(name, pwd); managerMenu(person); return; } } } cout << "身份验证失败" << endl; system("pause"); system("cls"); return; } void showInterface() { cout << "============欢迎来到机房预约系统================" << endl; cout << "请输入您的身份" << 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"; } void managerMenu(Identity* m_manager) { while (true) { //调用管理员子菜单 m_manager->operMenu(); //将父类指针转为子类指针 manager* man = (manager*)m_manager; int select = 0; cin >> select; switch (select) { case 1: man->addPerson(); break; case 2: man->showPerson(); break; case 3: man->showComputer(); break; case 4: man->cleanFile(); break; case 5: break; default: delete man; cout << "注销成功" << endl; system("pause"); system("cls"); return; } } } void studentMenu(Identity* m_student) { while (true) { m_student->operMenu(); student* p = (student*)m_student; int select = 0; cin >> select; switch(select) { case 1://申请预约 p->applyOrder(); break; case 2://查看自身预约 p->showMyOrder(); break; case 3://查看所有预约 p->showAllOrder(); break; case 4://取消预约 p->cancelOrder(); break; default: delete p; cout << "注销成功" << endl; system("pause"); system("cls"); return; } } } void teacherMenu(Identity* m_teacher) { while (true) { m_teacher->operMenu(); teacher* p = dynamic_cast<teacher*>(m_teacher); int select = 0; cin >> select; switch (select) { case 1://查看所有预约 p->showAllOrder(); break; case 2://审核预约 p->validOrder(); break; default://注销登录 delete p; cout << "注销成功" << endl; system("pause"); system("cls"); return; } } }