1、机房预约系统.cpp
1、机房预约系统.cpp
#include<iostream>
#include<fstream>
#include"Identity.h"
#include"globelFile.h"
#include"teacher.h"
#include"student.h"
#include"manager.h"
using namespace std;
void teacherMenu(Identity*&teacher1)
{
while (true)
{
teacher1->operMenu();
teacher*tea = (teacher*)teacher1;
int select = 0;
cin >> select;
if (select == 1)
{
tea->showAllOrder();
}
else if (select == 2)
{
tea->validOrder();
}
else
{
delete tea;
cout << "注销成功" << endl;
system("pause");
system("cls");
return;
}
}
}
void studentMenu(Identity*&student1)
{
while (true)
{
student1->operMenu();
student*stu = (student*)student1;
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 student1;
cout << "注销成功" << endl;
system("pause");
system("cls");
return;
}
}
}
void managerMenu(Identity* &manager)
{
while (true)
{
manager->operMenu();
Manager*man = (Manager*)manager;
int select = 0;
cin >> select;
if (select == 1)
{
man->addPerson();
}
else if (select == 2)
{
man->showPerson();
}
else if (select == 3)
{
man->showComputer();
}
else if (select == 4)
{
man->cleanFile();
}
else
{
delete manager;
cout << "注销成功" << endl;
system("pause");
system("cls");
return;
}
}
}
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;
}
string name;
string pwd;
int id;
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 (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;
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);
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;
cout << "\t\t|-------------------|" << endl;
cout << "\t\t|----1、学生--------|" << endl;
cout << "\t\t|----2、教师--------|" << endl;
cout << "\t\t|----3、管理员------|" << endl;
cout << "\t\t|----0、退出--------|" << endl;
cout << "\t\t|-------------------|" << endl;
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("cls");
break;
}
}
system("pause");
return 0;
}
2、Identity.h
#pragma once
#include<iostream>
#include<string>
using namespace std;
class Identity
{
public:
virtual void operMenu() = 0;
string m_Name;
string m_Pwd;
};
3、student.h
#pragma once
#include<iostream>
#include<vector>
#include<fstream>
using namespace std;
#include"Identity.h"
#include"computerRoom.h"
#include"globelFile.h"
#include"orderFile.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;
vector<computerRoom>vCom;
};
4、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 com;
while (ifs >> com.m_comID&&ifs >> com.m_maxNum)
{
vCom.push_back(com);
}
ifs.close();
}
void student::operMenu()
{
cout << "欢迎" << this->m_Name << "登录" << endl;
cout << "\t\t|-----------------------|" << endl;
cout << "\t\t|----1、申请预约--------|" << endl;
cout << "\t\t|----2、查看自身预约----|" << endl;
cout << "\t\t|----3、查看所有预约----|" << endl;
cout << "\t\t|----4、取消预约--------|" << endl;
cout << "\t\t|----0、注销登录--------|" << endl;
cout << "\t\t|-----------------------|" << endl;
cout << "请选择您的操作:" << endl;;
}
void student::applyOrder()
{
cout << "请输入您申请预约的时间:" << endl;
cout << "1、周一" << endl;
cout << "2、周二" << endl;
cout << "3、周三" << endl;
cout << "4、周四" << endl;
cout << "5、周五" << endl;
int data = 0;
int interval = 0;
int room = 0;
while (true)
{
cin >> data;
if (data >= 1 && data <= 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 << vCom[i].m_comID << "号机房" << "的最大容量为:" << vCom[i].m_maxNum << endl;
}
while (true)
{
cin >> room;
if (room >= 1 && room <= 3)
{
break;
}
cout << "输入有误,请重新输入" << endl;
}
cout << "预约成功,审核中" << endl;
ofstream ofs;
ofs.open(ORDER_FILE, ios::app);
ofs << "data:" << data<<" ";
ofs << "interval:" << interval << " ";
ofs << "stuName:" << this->m_Name << " ";
ofs << "stuId:" << this->m_Id << " ";
ofs << "roomId:" << room<< " ";
ofs << "status:" << 1 << endl;
ofs.close();
system("pause");
system("cls");
}
void student::showMyOrder()
{
OrderFile ord;
if (ord.m_size == 0)
{
cout << "无预约记录" << endl;
system("pause");
system("cls");
return;
}
for (int i = 0; i < ord.m_size; i++)
{
if (this->m_Id == atoi(ord.m_orderData[i]["stuId"].c_str()))
{
cout << "预约日期:周" << ord.m_orderData[i]["data"] << " ";
cout << "预约时间段:" << (ord.m_orderData[i]["interval"]=="1"?"上午":"下午") << " ";
cout << "机房号:" << ord.m_orderData[i]["roomId"] << " ";
string status="预约状态:";
if (ord.m_orderData[i]["status"] == "1")
{
status += "审核中";
}
else if (ord.m_orderData[i]["status"] == "2")
{
status += "预约成功";
}
else if (ord.m_orderData[i]["status"] == "-1")
{
status += "预约失败,审核未通过";
}
else
{
status += "预约已取消";
}
cout << status << endl;
}
}
system("pause");
system("cls");
}
void student::showAllOrder()
{
OrderFile ord;
if (ord.m_size == 0)
{
cout << "无预约记录" << endl;
system("pause");
system("cls");
return;
}
for (int i = 0; i < ord.m_size; i++)
{
cout << i + 1 << "、";
cout << "预约日期:周" << ord.m_orderData[i]["data"] << " ";
cout << "预约时间段:" << (ord.m_orderData[i]["interval"] == "1" ? "上午" : "下午") << " ";
cout << "机房号:" << ord.m_orderData[i]["roomId"] << " ";
cout << "学号:" << ord.m_orderData[i]["stuId"] << " ";
cout << "姓名:" << ord.m_orderData[i]["stuName"] << " ";
string status = "预约状态:";
if (ord.m_orderData[i]["status"] == "1")
{
status += "审核中";
}
else if (ord.m_orderData[i]["status"] == "2")
{
status += "预约成功";
}
else if (ord.m_orderData[i]["status"] == "-1")
{
status += "预约失败,审核未通过";
}
else
{
status += "预约已取消";
}
cout << status << endl;
}
system("pause");
system("cls");
}
void student::cancelOrder()
{
OrderFile ord;
if (ord.m_size == 0)
{
cout << "无预约记录" << endl;
system("pause");
system("cls");
return;
}
cout << "预约成功和审核中的记录可以取消,请输入要取消的记录" << endl;
vector<int>v;
int index = 1;
for (int i = 0; i < ord.m_size; i++)
{
if (this->m_Id == atoi(ord.m_orderData[i]["stuId"].c_str()))
{
if (ord.m_orderData[i]["status"] == "1" || ord.m_orderData[i]["status"] == "2")
{
v.push_back(i);
cout << index++ << "、";
cout << "预约日期:周" << ord.m_orderData[i]["data"] << " ";
cout << "预约时间段:" << (ord.m_orderData[i]["interval"] == "1" ? "上午" : "下午") << " ";
cout << "机房号:" << ord.m_orderData[i]["roomId"] << " ";
cout << "学号:" << ord.m_orderData[i]["stuId"] << " ";
cout << "姓名:" << ord.m_orderData[i]["stuName"] << " ";
string status = "预约状态:";
if (ord.m_orderData[i]["status"] == "1")
{
status += "审核中";
}
else if (ord.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
{
ord.m_orderData[v[select - 1]]["status"] = "0";
ord.updateOrder();
cout << "已取消预约" << endl;
break;
}
}
}
system("pause");
system("cls");
}
5、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 empId, string name, string pwd);
virtual void operMenu();
void showAllOrder();
void validOrder();
int empId;
};
6、teacher.cpp
#include "teacher.h"
teacher::teacher()
{
}
teacher::teacher(int empId, string name, string pwd)
{
this->empId = empId;
this->m_Name = name;
this->m_Pwd = pwd;
}
void teacher::operMenu()
{
cout << "欢迎" << this->m_Name << "登录" << endl;
cout << "\t\t|--------------------------------|" << endl;
cout << "\t\t|---------1、查看所有预约--------|" << endl;
cout << "\t\t|---------2、审核预约------------|" << endl;
cout << "\t\t|---------0、注销登录------------|" << endl;
cout << "\t\t|--------------------------------|" << endl;
cout << "请选择您的操作:" << endl;;
}
void teacher::showAllOrder()
{
OrderFile ord;
if (ord.m_size == 0)
{
cout << "无预约记录" << endl;
system("pause");
system("cls");
return;
}
for (int i = 0; i < ord.m_size; i++)
{
cout << i + 1 << "、";
cout << "预约日期:周" << ord.m_orderData[i]["data"] << " ";
cout << "预约时间段:" << (ord.m_orderData[i]["interval"] == "1" ? "上午" : "下午") << " ";
cout << "机房号:" << ord.m_orderData[i]["roomId"] << " ";
cout << "学号:" << ord.m_orderData[i]["stuId"] << " ";
cout << "姓名:" << ord.m_orderData[i]["stuName"] << " ";
string status = "预约状态:";
if (ord.m_orderData[i]["status"] == "1")
{
status += "审核中";
}
else if (ord.m_orderData[i]["status"] == "2")
{
status += "预约成功";
}
else if (ord.m_orderData[i]["status"] == "-1")
{
status += "预约失败,审核未通过";
}
else
{
status += "预约已取消";
}
cout << status << endl;
}
system("pause");
system("cls");
}
void teacher::validOrder()
{
OrderFile ord;
if (ord.m_size == 0)
{
cout << "无预约记录" << endl;
system("pause");
system("cls");
return;
}
cout << "待审核的记录如下:" << endl;
vector<int>v;
int index = 1;
for (int i = 0; i < ord.m_size; i++)
{
if (ord.m_orderData[i]["status"] == "1" )
{
v.push_back(i);
cout << index++ << "、";
cout << "预约日期:周" << ord.m_orderData[i]["data"] << " ";
cout << "预约时间段:" << (ord.m_orderData[i]["interval"] == "1" ? "上午" : "下午") << " ";
cout << "机房号:" << ord.m_orderData[i]["roomId"] << " ";
cout << "学号:" << ord.m_orderData[i]["stuId"] << " ";
cout << "姓名:" << ord.m_orderData[i]["stuName"] << " ";
string status = "预约状态:";
if (ord.m_orderData[i]["status"] == "1")
{
status += "审核中";
}
cout << status << endl;
}
}
cout << "请输入要审核的记录" << 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)
{
ord.m_orderData[v[select - 1]]["status"] = "2";
}
else
{
ord.m_orderData[v[select - 1]]["status"] = "-1";
}
ord.updateOrder();
cout << "审核完毕" << endl;
break;
}
}
}
system("pause");
system("cls");
}
7、manager.h
#pragma once
#include<iostream>
using namespace std;
#include"Identity.h"
#include<fstream>
#include<vector>
#include"student.h"
#include"teacher.h"
#include"computerRoom.h"
#include<algorithm>
class Manager :public Identity
{
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>vCom;
};
8、manager.cpp
#include"manager.h"
#include"globelFile.h"
Manager::Manager()
{
}
Manager::Manager(string name, string pwd)
{
this->m_Name = name;
this->m_Pwd = pwd;
this->initVector();
ifstream ifs;
ifs.open(COMPUTER_FILE, ios::in);
computerRoom com;
while (ifs >> com.m_comID&&ifs >> com.m_maxNum)
{
vCom.push_back(com);
}
ifs.close();
cout << "机房的数量为:" << vCom.size() << endl;
}
void Manager::operMenu()
{
cout << "欢迎" << this->m_Name << "登录" << endl;
cout << "\t\t|-----------------------|" << endl;
cout << "\t\t|----1、添加账号--------|" << endl;
cout << "\t\t|----2、查看账号--------|" << endl;
cout << "\t\t|----3、查看机房--------|" << endl;
cout << "\t\t|----4、清空预约--------|" << endl;
cout << "\t\t|----0、注销登录--------|" << endl;
cout << "\t\t|-----------------------|" << endl;
cout << "请选择您的操作:" << endl;
}
void Manager::addPerson()
{
cout << "请选择您要添加的账户类型" << endl;
cout << "1、添加学生" << endl;
cout << "2、添加老师" << endl;
string fileName;
string tip;
string errorTip;
ofstream ofs;
int select;
cin >> select;
if (select == 1)
{
fileName = STUDENT_FILE;
tip = "请输入学号";
errorTip = "学号重复,请重新输入";
}
else if (select == 2)
{
fileName = TEACHER_FILE;
tip = "请输入职工号";
errorTip = "职工号重复,请重新输入";
}
else
{
cout << "输入有误,请重新输入" << endl;
system("pause");
system("cls");
return;
}
ofs.open(fileName, ios::out | ios::app);
int id;
string name;
string pwd;
cout << tip << endl;
while (true)
{
cin >> id;
bool ret = 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.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 = vCom.begin(); it != vCom.end(); it++)
{
cout << "机房的id:" << 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");
}
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->empId)
{
return true;
}
}
}
return false;
}
void Manager::initVector()
{
vStu.clear();
vTea.clear();
ifstream ifs;
ifs.open(STUDENT_FILE, ios::in);
if (!ifs.is_open())
{
cout << "文件读取失败" << endl;
return;
}
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);
if (!ifs.is_open())
{
cout << "文件读取失败" << endl;
return;
}
teacher t;
while (ifs >> t.empId&&ifs >> t.m_Name&&ifs >> t.m_Pwd)
{
vTea.push_back(t);
}
cout << "老师的个数为" << vTea.size() << endl;
ifs.close();
}
9、orderFile.h
#pragma once
#include<iostream>
#include<vector>
#include"computerRoom.h"
#include"globelFile.h"
#include<map>
#include<fstream>
using namespace std;
#include<string>
class OrderFile
{
public:
OrderFile();
void updateOrder();
int m_size;
map<int, map<string, string>>m_orderData;
};
10、orderFile.cpp
#include"orderFile.h"
void separate(string&date, map<string, string>&m)
{
string key;
string value;
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));
}
}
OrderFile::OrderFile()
{
ifstream ifs;
ifs.open(ORDER_FILE, ios::in);
string data;
string interval;
string stuName;
string stuId;
string roomId;
string status;
this->m_size = 0;
while (ifs >> data && ifs >> interval && ifs >> stuName && ifs >> stuId &&
ifs >> roomId && ifs >> status)
{
map<string, string>m;
string key;
string value;
separate(data,m);
separate(interval, m);
separate(stuName, m);
separate(stuId, m);
separate(roomId, m);
separate(status, m);
this->m_orderData.insert(make_pair(this->m_size, m));
this->m_size++;
}
ifs.close();
}
void OrderFile::updateOrder()
{
if (this->m_size == 0)
{
return;
}
ofstream ofs;
ofs.open(ORDER_FILE, ios::out || ios::trunc);
for (int i = 0; i < this->m_size; ++i)
{
ofs << "data:" << this->m_orderData[i]["data"] << " ";
ofs << " interval:" << this->m_orderData[i][" interval"] << " ";
ofs << "stuName:" << this->m_orderData[i]["stuName"] << " ";
ofs << "stuId:" << this->m_orderData[i]["stuId"] << " ";
ofs << "roomId:" << this->m_orderData[i]["roomId"] << " ";
ofs << "status:" << this->m_orderData[i]["status"] << endl;
}
}
11、globalFile.h
#pragma once
#define ADMIN_FILE "admin.txt"
#define STUDENT_FILE "student.txt"
#define TEACHER_FILE "teacher.txt"
#define COMPUTER_FILE "computer.txt"
#define ORDER_FILE "order.txt"
12、computerRoom.h
#pragma once
#include<iostream>
using namespace std;
class computerRoom
{
public:
int m_comID;
int m_maxNum;
};
|