C++ 总结大项目:机房预约系统项目(数据结构 +指针+class(类操作)+面向对象三大特性[继承+多态+封装]+文件(读,写,保存,重写,重建,清空)+string(比较,截取,追加),+容器多个操作,嵌套+算法+清空数据)


 

 1 /**
 2  * 项目名称:机房预约系统
 3  * 时    间:2021-08
 4  * 作    者:Bytezero!·zhenglei
 5  * 
 6  * 系统简介:
 7  *     学校有几个规格不同的机房,由于使用时经常出现“撞车”的现象,开发一套系统,解决这一问题
 8  * 身份简介:
 9  *     学生代表:申请使用机房
10  *     教    师:审核学生的预约申请
11  *     管 理 员:给学生,教师创建账号
12  * 机房简介:
13  *     1号机房 ----- 最大容量20人
14  *     2号机房 ----- 最多容量50人
15  *     3号机房 ----- 最多容量100人
16  * 申请简介:
17  *     申请的订单每周由管理员负责清空
18  *     学生可以预约未来一周机房内的使用,预约的日期周一至周五,预约是需要选择预约时段(上午,下午)
19  *     教师来审核预约,依据实际情况审核预约通过或者不通过
20  * 系统具体需求:
21  *     首先进入登录界面,可选择登录身份有:
22  *         学生代表
23  *         老师
24  *         管理员
25  *         退出
26  *     每个身份都需要进行验证后,进入子菜单
27  *         学生需要输入:学号,姓名,登录密码
28  *         老师需要输入:职工号,姓名,登录密码
29  *         管理需要输入:管理员姓名,登录密码
30  *     学生具体功能
31  *         申请预约 ------ 预约机房
32  *         查看自身预约 ----- 查看自己的预约状态
33  *         查看所有预约 ----- 查看全部预约信息以及预约状态
34  *         取消预约 ----- 取消自身预约,预约成功或审核中的预约均可以取消
35  *       
36  *     教师具体功能
37  *         查看所有预约 ----- 查看全部预约信息以及预约状态
38  *         审核预约 ----- 对学生的预约进行审核
39  *         注销登录 ----- 退出登录
40  *     管理员具体功能
41  *         添加账号 ----- 添加学生或教师的账号,需要检测学生编号或教师职工编号是否重复
42  *         查看账号 ----- 可以选择查看学生或教师的全部信息
43  *         查看机房 ----- 查看所有机房的信息
44  *         清空预约 ----- 清空所有预约记录
45  *         注销登录 ----- 退出登录
46  *
47  **/

//运行界面

 

 

 
  1 ComputerizedRoomReservationSystem.cpp
  2 #include<iostream>
  3 #include"Identity.h"
  4 #include<fstream>
  5 #include<string>
  6 #include"global.h"
  7 #include"manager.h"
  8 #include"teacher.h"
  9 #include"student.h"
 10 using namespace std;
 11 
 12 
 13 
 14 
 15 
 16 
 17 //进入学生子菜单界面
 18 void studentMenu(Identity * &student)
 19 {
 20     while (true)
 21     {
 22         //调用学生子菜单
 23         student->operMenu();
 24 
 25         Student* stu = (Student*)student;
 26 
 27         int select = 0;
 28         cin >> select;  //接收用户的选择
 29 
 30         if (select == 1)  //申请预约
 31         {
 32             stu->applyOrder();
 33 
 34         }
 35         else if (select == 2)//查看自身预约
 36         {
 37             stu->showMyOrder();
 38         }
 39         else if (select == 3) //查看所有人预约
 40         {
 41             stu->showAllOrder();
 42         }
 43         else if (select == 4) //取消预约
 44         {
 45             stu->cancelOrder();
 46         }
 47         else
 48         {
 49             //注销登录
 50             delete student;
 51             cout << "注销成功!" << endl;
 52             system("pause");
 53             system("cls");
 54 
 55             return;
 56 
 57         }
 58 
 59     }
 60 }
 61 
 62 //进入教师子菜单界面
 63 void teacherMenu(Identity*& teacher)
 64 {
 65     while (true)
 66     {
 67         //调用子菜单界面
 68         teacher->operMenu();
 69 
 70         Teacher* tea = (Teacher*)teacher;
 71 
 72         int select = 0;  //接收用户选择
 73 
 74         cin >> select;
 75         if (select == 1)   //查看所有预约
 76         {
 77             tea->showAllOrder();
 78         }
 79         else if (select == 2)   //审核预约
 80         {
 81             tea->validOrder();
 82         }
 83         else
 84         {
 85             delete teacher;
 86             cout << "注销成功!!" << endl;
 87             system("pause");
 88             system("cls");
 89             break;
 90         }
 91     }
 92 }
 93 
 94 
 95 
 96 
 97 
 98 
 99 
100 
101 
102 
103 
104 //进入管理员的子菜单
105 void managerMenu(Identity*& manager)
106 {
107     while (true)
108     {
109         //调用管理员子菜单
110         manager->operMenu();
111 
112         //将父类指针转换为 子类的指针 调用子类其他接口
113         Manager* man = (Manager*)manager;
114 
115         int select = 0;
116         //接收用户的选择
117         cin >> select;
118 
119         if (select == 1)
120         {
121             //添加账号
122             cout << "添加账号" << endl;
123             man->addPerson();
124 
125         }
126         else if (select == 2) //查看账号
127         {
128             cout << "查看账号" << endl;
129             man->showPerson();
130         }
131         else if (select == 3) //查看机房信息
132         {
133             cout << "查看机房" << endl;
134             man->showComputer();
135         }
136         else if (select == 4)//清空预约
137         {
138             cout << "清空预约" << endl;
139             man->cleanFile();
140         }
141         else if (select == 0) //注销登录
142         {
143             delete manager; //销毁堆区对象
144             cout << "注销成功!" << endl;
145             system("pause");
146             system("cls");
147             return;
148         }
149         else                //输入有误!
150         {
151             cout << "您输入有误,请重新输入!" << endl;
152             system("pause");
153             system("cls");
154 
155         }
156 
157     }
158 }
159 
160 //登录功能 参数1 操做文件名   参数2   操作身份类型
161 void LoginIn(string fileName, int type)
162 {
163     //父类指针 用于 指向子类对象
164     Identity* person = NULL;
165 
166     //读文件
167     ifstream ifs;
168     ifs.open(fileName,ios::in);
169 
170     //判断文件是否存在
171     if (!ifs.is_open())
172     {
173         cout << "文件不存在!" << endl;
174         ifs.close();
175         return;
176     }
177 
178     //准备接收用户信息
179     int id = 0;
180     string name;
181     string pwd;
182 
183 
184     //判断身份
185     if (type == 1)  //学生身份
186     {
187         cout << "请输入你的学号:" << endl;
188         cin >> id;
189     }
190     else if (type == 2) //老师身份
191     {
192         cout << "请输入您的职工号:" << endl;
193         cin >> id;
194 
195     }
196     cout << "请输入用户名:" << endl;
197     cin >> name;
198 
199     cout << "请输入密码:" << endl;
200     cin >> pwd;
201 
202     if (type == 1)
203     {
204         //学生身份验证
205         int fId;       //从文件中读取的id号
206         string fName;  //从文件中获取的姓名
207         string fPwd;   //从文件中获取的密码
208 
209         while (ifs >> fId && ifs >> fName && ifs >> fPwd )
210         {
211             //cout << fId << endl;
212             //cout << fName << endl;
213             //cout << fPwd << endl;
214             //cout << endl;
215             //与用户输入的信息做对比
216             if (id == fId && name == fName && pwd == fPwd)
217             {
218                 cout << "学生验证登录成功!" << endl;
219                 system("pause");
220                 system("cls");
221 
222                 person = new Student(id,name,pwd);
223 
224                 //进入学生身份的子菜单
225                 studentMenu(person);
226 
227 
228                 return;
229             }
230 
231 
232         }
233 
234 
235     }
236     else if (type == 2)
237     {
238         //教师身份验证
239         int fId;
240         string fName;
241         string fPwd;
242 
243         while (ifs>>fId && ifs>>fName&&ifs>>fPwd)
244         {
245             if (fId == id && fName == name && fPwd == pwd)
246             {
247                 cout << "教师登录验证成功!" << endl;
248                 system("pause");
249                 system("cls");
250 
251                 person = new Teacher(id, name, pwd);
252 
253                 //进入教师子菜单
254                 teacherMenu(person);
255                 return;
256 
257             }
258 
259 
260 
261         }
262 
263 
264 
265 
266     }
267     else if (type == 3)
268     {
269         //管理员身份验证
270         string fName;
271         string fPwd;
272         while (ifs >> fName && ifs>>fPwd)
273         {
274             if (name == fName && pwd == fPwd)
275             {
276                 cout << "管理员登录验证成功!" << endl;
277                 system("pause");
278                 system("cls");
279 
280                 person = new Manager(name, pwd);
281 
282                 //进入管理员子菜单
283                 managerMenu(person);
284 
285                 return;
286             }
287         }
288 
289 
290 
291     }
292 
293     
294     
295         cout << "验证登录失败!" << endl;
296 
297         system("pause");
298         system("cls");
299 
300     //    return;
301     
302 
303 
304 
305 }
306 
307 int main()
308 {
309 
310     int select = 0;   //用于接收用户的选择
311     while (true)
312     {
313         cout << "===================================  欢迎来到(Bytezero!·zhenglei)计算机房预约系统  ===================================" << endl;
314         cout << endl << "亲爱的用户:\n  根据下面提示的信息,请输入您的身份:\n" << endl;
315         cout << "\t\t\t\t\t --------------------------------\n";
316         cout << "\t\t\t\t\t|                                |\n";
317         cout << "\t\t\t\t\t|          1.学生代表            |\n";
318         cout << "\t\t\t\t\t|                                |\n";
319         cout << "\t\t\t\t\t|          2.老    师            |\n";
320         cout << "\t\t\t\t\t|                                |\n";
321         cout << "\t\t\t\t\t|          3.管 理 员            |\n";
322         cout << "\t\t\t\t\t|                                |\n";
323         cout << "\t\t\t\t\t|          0.退    出            |\n";
324         cout << "\t\t\t\t\t|                                |\n";
325         cout << "\t\t\t\t\t|                                |\n";
326         cout << "\t\t\t\t\t --------------------------------\n";
327         cout << "  \n请输入您的选择:";
328 
329         cin >> select;   //接受用户的选择
330 
331         switch (select)  //根据用户选择  实现不同的接口
332         {
333         case 1:          //学生身份
334             LoginIn(STUDENT_FILE, 1);
335             break;
336         case 2:          //老师身份
337             LoginIn(TEACHER_FILE, 2);
338             break;
339         case 3:          //管理员身份
340             LoginIn(ADMIN_FILE,3);
341             break;
342         case 0:          //退出系统
343             cout << "\t欢迎下一次使用,再见!" << endl;
344             system("pause");
345             return 0;
346             break;
347         default:         //输入有误
348             cout << "输入有误,请重新选择!" << endl;
349             system("pause");
350             system("cls");
351             break;
352         }
353 
354     }
355 
356     
357 
358 
359 
360 
361 
362 
363     system("pause");
364     return 0;  
365 }
  1 manager.cpp
  2 #include"manager.h"
  3 
  4 //默认构造
  5 Manager::Manager()
  6 {
  7 
  8 }
  9 
 10 //有参构造
 11 Manager::Manager(string name, string pwd)
 12 {
 13 
 14     //初始化管理员信息
 15     this->m_Name = name;
 16     this->m_Pwd = pwd;
 17 
 18     //初始化容器 获取到所有文件中 老师 学生信息
 19     this->inttVector();
 20 
 21     //初始化机房的信息
 22     ifstream ifs;
 23     ifs.open(COMPUTER_FILE, ios::in);
 24 
 25     ComputerRoon com;
 26     while (ifs >> com.m_ComId && ifs >> com.m_MaxNum)
 27     {
 28         vCom.push_back(com);
 29     }
 30     ifs.close();
 31     cout << "当前机房的数量为:" << vCom.size() << endl;
 32 }
 33 
 34 //菜单构造
 35 void Manager::operMenu()
 36 {
 37     cout << "===============================================  欢迎管理员:"<<this->m_Name  <<" 登录 =============================================== " << endl;
 38     cout << endl << "亲爱的用户:\n  根据下面提示的信息,请输入您的身份:\n" << endl;
 39     cout << "\t\t\t\t\t --------------------------------\n";
 40     cout << "\t\t\t\t\t|                                |\n";
 41     cout << "\t\t\t\t\t|          1.添加账号            |\n";
 42     cout << "\t\t\t\t\t|                                |\n";
 43     cout << "\t\t\t\t\t|          2.查看账号            |\n";
 44     cout << "\t\t\t\t\t|                                |\n";
 45     cout << "\t\t\t\t\t|          3.查看机房            |\n";
 46     cout << "\t\t\t\t\t|                                |\n";
 47     cout << "\t\t\t\t\t|          4.清空预约            |\n";
 48     cout << "\t\t\t\t\t|                                |\n";
 49     cout << "\t\t\t\t\t|          0.注销登录            |\n";
 50     cout << "\t\t\t\t\t|                                |\n";
 51     cout << "\t\t\t\t\t --------------------------------\n";
 52     cout << "  \n请输入您的选择:";
 53  }
 54 
 55 //添加账号
 56 void Manager::addPerson()
 57 {
 58     cout << "请输入添加的账号类型:" << endl;
 59     cout << "1 --- 添加学生" << endl;
 60     cout << "2 --- 添加老师" << endl;
 61 
 62     string fileName;    //操作文件名
 63     string tip;         //提示Id号
 64     string errorTip;    //重复错误提示
 65 
 66     ofstream ofs;       //文件操作对象
 67 
 68     int select = 0;
 69     cin >> select;   //接收用户的选项
 70     while (true)
 71     {
 72         if (select == 1)
 73         {
 74             //添加的是学生
 75             fileName = STUDENT_FILE;
 76             tip = "请输入学号:";
 77             errorTip = "学号重复,请重新输入!";
 78 
 79         }
 80         else if (select == 2)
 81         {
 82             //添加教师
 83             fileName = TEACHER_FILE;
 84             tip = "请输入职工号:";
 85             errorTip = "职工号重复,请重新输入!";
 86             
 87         }
 88         else
 89         {
 90             cout << "您输入有误,请重新输入!" << endl;
 91             system("pause");
 92             system("cls");
 93             return;
 94         }
 95 
 96         //利用追加的方式 写文件
 97         ofs.open(fileName, ios::out | ios::app);
 98 
 99         int id;         //学号  或者  职工号
100         string name; //姓名
101         string pwd;  //密码
102 
103         cout << tip << endl;
104 
105         while (true)
106         {
107             cin >> id;
108             bool ret = checkRepeat(id, select);
109             if (ret)  //有重复
110             {
111                 cout << errorTip << endl;
112             }
113             else
114             {
115                 break;
116             }
117         }
118 
119 
120         
121         
122         cout << "请输入姓名:" << endl;
123         cin >> name;
124 
125         cout << "请输入密码:" << endl;
126         cin >> pwd;
127 
128         //文件中添加数据
129         ofs << id << " " << name << " " << pwd << " " << endl;
130         cout << "添加成功!!!" << endl;
131 
132         system("pause");
133         system("cls");
134 
135         ofs.close();
136 
137         //初始化信息 重新获取文件中的数据
138         this->inttVector();
139 
140         return;
141 
142 
143     }
144 
145 
146 
147 }
148 //打印学生
149 void printStudent(Student& s)
150 {
151     cout << "学号:" << s.m_Id << "\t姓名:" << s.m_Name << "\t密码:" << s.m_Pwd << endl;
152 
153 }
154 
155 //打印老师
156 void printTeacher(Teacher& t)
157 {
158     cout << "职工号:" << t.m_EmpId << "\t姓名:" << t.m_Name << "\t密码:" << t.m_Pwd << endl;
159 
160 }
161 
162 //查看账号
163 void Manager::showPerson()
164 {
165     cout << "请选择查看的内容:" << endl;
166     cout << "1 --- 查看所有的学生" << endl;
167     cout << "2 --- 查看所有的老师" << endl;
168 
169     int select = 0;  //接收用户的选择
170 
171     cin >> select;
172     if (select == 1)
173     {
174         //查看学生
175         cout << "查看所有学生信息如下:" << endl;
176         for_each(vStu.begin(), vStu.end(), printStudent);
177     }
178     else
179     {
180         //查看教师
181         cout << "查看所有教师信息如下:" << endl;
182         for_each(vTea.begin(), vTea.end(), printTeacher);
183     }
184     system("pause");
185     system("cls");
186 
187 }
188 
189 
190 //查看机房信息
191 void Manager::showComputer()
192 {
193     cout << "机房的信息如下:" << endl;
194     for (vector<ComputerRoon>::iterator it = vCom.begin(); it != vCom.end(); it++)
195     {
196         cout << "机房编号:" << it->m_ComId << "\t机房最大容量:" << it->m_MaxNum << endl;
197     }
198     system("pause");
199     system("cls");
200 }
201 
202 
203 
204 
205 //清空预约记录
206 void Manager::cleanFile()
207 {
208 
209     cout << "确定要清空吗?" << endl;
210     cout << "1 --- 确定" << endl;
211     cout << "2 --- 返回" << endl;
212 
213     int select = 0;
214     cin >> select;
215 
216     if (select == 1)
217     {
218         ofstream ofs(ORDER_FILE, ios::trunc);
219         ofs.close();
220         cout << "清空成功!" << endl;
221         system("pause");
222         system("cls");
223 
224     }
225     else if (select == 2)
226     {
227         
228         system("pause");
229         system("cls");
230         return;
231     }
232     else
233     {
234         cout << "输入有误,请重新输入!" << endl;
235         system("pause");
236         system("cls");
237         return;
238     }
239 
240 }
241 
242 //初始化容器
243 void Manager::inttVector()
244 {
245     //确保容器清空状态
246     vStu.clear();
247     vTea.clear();
248 
249     //读取信息  学生 
250     ifstream ifs;
251     ifs.open(STUDENT_FILE, ios::in);
252     if (!ifs.is_open())
253     {
254         cout << "文件读取失败!" << endl;
255         return;
256     }
257 
258     Student s;
259     while (ifs>>s.m_Id&&ifs>>s.m_Name&&ifs>>s.m_Pwd)
260     {
261         vStu.push_back(s);
262     }
263     cout << "当前学生数量为:" << vStu.size() << endl;
264     ifs.close();
265 
266     //读取信息  老师
267     ifs.open(TEACHER_FILE, ios::in);
268     Teacher t;
269     while (ifs >> t.m_EmpId && ifs >> t.m_Name && ifs >> t.m_Pwd)
270     {
271         vTea.push_back(t);
272     }
273 
274     cout << "当前老师数量为:" << vTea.size() << endl;
275     ifs.close();
276 
277 
278 
279 }
280 
281 //检测重复 参数1 检测 学号/职工号   参数2 检测类型
282 bool Manager::checkRepeat(int id, int type)
283 {
284     if (type == 1)
285     {
286         //检测学生
287         for (vector<Student>::iterator it = vStu.begin(); it != vStu.end(); it++)
288         {
289             if (id == it->m_Id)
290             {
291                 return true;
292             }
293         }
294     }
295     else
296     {
297         //检测老师
298         for (vector<Teacher>::iterator it = vTea.begin(); it != vTea.end(); it++)
299         {
300             if (id == it->m_EmpId)
301             {
302                 return true;
303             }
304         }
305 
306     }
307 
308     return false;
309 }
  1 orderFile.cpp
  2 #include"orderFile.h"
  3 
  4 
  5 //构造函数
  6 OrderFile::OrderFile()
  7 {
  8     ifstream ifs;
  9     ifs.open(ORDER_FILE, ios::in);
 10 
 11     string date;       //日期
 12     string interval;   //时间段
 13     string stuId;      //学生编号
 14     string stuName;    //学生姓名
 15     string roomId;     //机房编号
 16     string status;     //预约状态
 17 
 18     this->m_Size = 0;   //记录条数
 19 
 20     while (ifs >> date && ifs >> interval && ifs 
 21         >> stuId && ifs >> stuName && ifs 
 22         >> roomId && ifs >> status)
 23     {
 24         //cout << date << endl;
 25         //cout << interval << endl;
 26         //cout <<  stuId<< endl;
 27         //cout << stuName << endl; 
 28         //cout << roomId << endl;
 29         //cout << status << endl;
 30 
 31 
 32 
 33         //date : 1
 34 
 35         string key;
 36         string value;
 37         map<string, string>m;
 38 
 39         //cout << date << endl;
 40         //截取日期
 41         int pos = date.find(":"); // 4
 42         if (pos != -1)
 43         {
 44             key = date.substr(0, pos);
 45             value = date.substr(pos + 1, date.size() - pos - 1);  //size = 9  pos = 4    size-pos = 5
 46 
 47             m.insert(make_pair(key, value));
 48 
 49         }
 50 
 51         //cout << "key = " << key << endl;
 52         //cout << "value = " << value << endl;
 53         
 54         //cout << interval << endl;
 55         //截取时间段
 56          pos = interval.find(":"); // 4
 57         if (pos != -1)
 58         {
 59             key = interval.substr(0, pos);
 60             value = interval.substr(pos + 1, interval.size() - pos - 1);  //size = 9  pos = 4    size-pos = 5
 61 
 62             m.insert(make_pair(key, value));
 63 
 64         }
 65         //cout <<  stuId<< endl;
 66         //截取学生Id
 67         pos = stuId.find(":"); // 4
 68         if (pos != -1)
 69         {
 70             key = stuId.substr(0, pos);
 71             value = stuId.substr(pos + 1, stuId.size() - pos - 1);  //size = 9  pos = 4    size-pos = 5
 72 
 73             m.insert(make_pair(key, value));
 74 
 75         }
 76 
 77         //cout << stuName << endl;
 78         //截取学生姓名
 79         pos = stuName.find(":"); // 4
 80         if (pos != -1)
 81         {
 82             key = stuName.substr(0, pos);
 83             value = stuName.substr(pos + 1, stuName.size() - pos - 1);  //size = 9  pos = 4    size-pos = 5
 84 
 85             m.insert(make_pair(key, value));
 86 
 87         }
 88 
 89 
 90         //cout << roomId << endl;
 91         //截取机房Id
 92 
 93         pos = roomId.find(":"); // 4
 94         if (pos != -1)
 95         {
 96             key = roomId.substr(0, pos);
 97             value = roomId.substr(pos + 1, roomId.size() - pos - 1);  //size = 9  pos = 4    size-pos = 5
 98 
 99             m.insert(make_pair(key, value));
100 
101         }
102 
103         //cout << status << endl;
104         //截取预约状态
105         pos = status.find(":"); // 4
106         if (pos != -1)
107         {
108             key = status.substr(0, pos);
109             value = status.substr(pos + 1, status.size() - pos - 1);  //size = 9  pos = 4    size-pos = 5
110 
111             m.insert(make_pair(key, value));
112 
113         }
114 
115         //将小 map 容器 放入 大的 map容器中
116         this->m_orderData.insert(make_pair(this->m_Size, m));
117         this->m_Size++;
118 
119 
120     }
121     ifs.close();
122 
123     //测似 最大map容器
124     //for (map<int, map<string, string>>::iterator it = m_orderData.begin(); it != m_orderData.end(); it++)
125     //{
126     //    cout << "条数为: = " << it->first << "\tvalue = " << endl;
127     //    for (map<string, string>::iterator mit = (*it).second.begin(); mit != it->second.end(); mit++)
128     //    {
129     //        cout << " key = " << mit->first << " value = " << mit->second << " ";
130 
131 
132     //    }
133     //    cout << endl;
134     //}
135 
136 }
137 
138 //更新预约记录
139 void OrderFile::updateOrder()
140 {
141     if (this->m_Size == 0)
142     {
143         return;   //预约记录为条,直接 return
144     }
145 
146     ofstream ofs(ORDER_FILE, ios::out | ios::trunc);
147     for (int i = 0; i < this->m_Size; i++)
148     {
149         ofs << "date:" << this->m_orderData[i]["date"] << "  ";
150         ofs << "interval:" << this->m_orderData[i]["interval"] << "  ";
151         ofs << "stuId:" << this->m_orderData[i]["stuId"] << "  ";
152         ofs << "stuName:" << this->m_orderData[i]["stuName"] << "  ";
153         ofs << "roomId:" << this->m_orderData[i]["roomId"] << "  ";
154         ofs << "status:" << this->m_orderData[i]["status"] << endl;
155 
156 
157     }
158     ofs.close();
159 }
  1 student.cpp
  2 #include"student.h"
  3 
  4 
  5 //默认构造
  6 Student::Student()
  7 {
  8 
  9 }
 10 
 11 //有参构造 参数 学号 姓名 密码
 12 Student::Student(int id, string name, string pwd)
 13 {
 14     //初始化属性
 15     this->m_Id = id;
 16     this->m_Name = name;
 17     this->m_Pwd = pwd;
 18 
 19     //初始化机房信息
 20     ifstream ifs;
 21     ifs.open(COMPUTER_FILE, ios::in);
 22 
 23     ComputerRoon com;
 24     while (ifs >> com.m_ComId && ifs >> com.m_MaxNum)
 25     {
 26         //将读取的信息放入到 容器中
 27         vCom.push_back(com);
 28     }
 29     ifs.close();
 30 }
 31 
 32 
 33 //菜单界面
 34  void Student::operMenu()
 35 {
 36      cout << "=============================================  欢迎学生用户:" << this->m_Name << " 登录  ============================================= " << endl;
 37      cout << endl << "亲爱的学生用户:\n  根据下面提示的信息,请输入您的选择:\n" << endl;
 38      cout << "\t\t\t\t\t --------------------------------\n";
 39      cout << "\t\t\t\t\t|                                |\n";
 40      cout << "\t\t\t\t\t|          1.申请预约            |\n";
 41      cout << "\t\t\t\t\t|                                |\n";
 42      cout << "\t\t\t\t\t|          2.查看我的预约        |\n";
 43      cout << "\t\t\t\t\t|                                |\n";
 44      cout << "\t\t\t\t\t|          3.查看所有预约        |\n";
 45      cout << "\t\t\t\t\t|                                |\n";
 46      cout << "\t\t\t\t\t|          4.取消预约            |\n";
 47      cout << "\t\t\t\t\t|                                |\n";
 48      cout << "\t\t\t\t\t|          0.注销登录            |\n";
 49      cout << "\t\t\t\t\t|                                |\n";
 50      cout << "\t\t\t\t\t --------------------------------\n";
 51      cout << "  \n请输入您的选择:";
 52 }
 53 
 54 
 55 //申请预约
 56 void Student::applyOrder()
 57 {
 58     cout << "机房开放时间为:周一至周五!" << endl;
 59     cout << "请输入申请预约的时间:" << endl;
 60     cout << "1 ··· 周一" << endl;
 61     cout << "2 ··· 周二" << endl;
 62     cout << "3 ··· 周三" << endl;
 63     cout << "4 ··· 周四" << endl;
 64     cout << "5 ··· 周五" << endl;
 65 
 66     int  date = 0;      //日期
 67     int interval = 0;   //时间段
 68     int room = 0;       //机房编号
 69 
 70     while (true)
 71     {
 72         cin >> date;
 73         if (date >= 1 && date <= 5)
 74         {
 75             break;
 76         }
 77         cout << "输入日期有误,请重新输入" << endl;
 78     }
 79 
 80     cout << "请输入申请预约的时间段:" << endl;
 81     cout << "1 --- 上午" << endl;
 82     cout << "2 --- 下午" << endl;
 83 
 84     while (true)
 85     {
 86         cin >> interval;
 87         if (interval >= 1 && interval <= 2)
 88         {
 89             break;
 90         }
 91         cout << "输入时间段有误,请重新输入" << endl;
 92     }
 93 
 94     cout << "请选择机房:" << endl;
 95     for (int i = 0; i < vCom.size(); i++)
 96     {
 97         cout << vCom[i].m_ComId << "号机房容量为:" << vCom[i].m_MaxNum << endl;
 98 
 99     }
100 
101     while (true)
102     {
103         cin >> room;
104         if (room >= 1 && room <= 3)
105         {
106             break;
107         }
108         cout << "机房输入有误,请重新输入!" << endl;
109     }
110     cout << "预约成功,审核中" << endl;
111 
112     ofstream ofs;
113     ofs.open(ORDER_FILE,ios::app);
114 
115     ofs << "date:" << date << "\t";
116     ofs << "interval:" << interval << "\t";
117     ofs << "stuId:" << this->m_Id << "\t";
118     ofs << "stuName:" << this->m_Name << "\t";
119     ofs << "roomId:" << date << "\t";
120     ofs << "status:" << 1 << endl;
121 
122     ofs.close();
123     system("pause");
124     system("cls");
125 
126 
127 }
128 
129 //查看自身的预约
130 void Student::showMyOrder()
131 {
132     OrderFile of;
133     if (of.m_Size == 0)
134     {
135         cout << "没有预约记录!" << endl;
136         system("pause");
137         system("cls");
138         return;
139     }
140 
141     for (int i = 0; i < of.m_Size; i++)
142     {
143         //找到自身预约
144         //sting 装 int
145         //stting 利用 .c_str() 转 const char *
146         //利用 atoi (const char *)转 int
147         if (this->m_Id == atoi(of.m_orderData[i]["stuId"].c_str()))
148         {
149             cout << "预约日期:周" << of.m_orderData[i]["date"];
150             cout << "\t时间段:" << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
151             cout << "\t机房编号:" << of.m_orderData[i]["roomId"];
152             string status = "\t状态:";
153             //1.审核中  2.已预约  -1 预约失败  0取消预约
154             if (of.m_orderData[i]["status"] == "1")
155             {
156                 status += "审核中";
157             }
158             else if (of.m_orderData[i]["status"] == "2")
159             {
160                 status += "预约成功";
161             }
162             else if (of.m_orderData[i]["status"] == "3")
163             {
164                 status += "预约失败,审核未通过!";
165             }
166             else
167             {
168                 status += "预约已取消!!";
169             }
170             cout << status << endl;
171 
172         }
173     }
174     system("pause");
175     system("cls");
176 
177 
178 }
179 
180 
181 //查看所有预约
182 void Student::showAllOrder()
183 {
184 
185     OrderFile of;
186     if (of.m_Size == 0)
187     {
188         cout << "无预约记录!" << endl;
189 
190         system("pause");
191         system("cls");
192         return;
193     }
194     for (int i = 0; i < of.m_Size; i++)
195     {
196         cout << i + 1 << "";
197         cout << "预约的日期: 周" << of.m_orderData[i]["date"];
198         cout << "\t时间段:" << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
199         cout << "\t学号:" << of.m_orderData[i]["stuId"];
200         cout << "\t姓名:" << of.m_orderData[i]["stuName"];
201         cout << "\t机房编号:" << of.m_orderData[i]["roomId"];
202         string status = "状态:";
203 
204         //1. 审核中  2.已预约  -1预约失败   0取消预约
205         if (of.m_orderData[i]["status"] == "1")
206         {
207             status += "审核中";
208         }
209         else if (of.m_orderData[i]["status"] == "2")
210         {
211             status += "预约成功!";
212         }
213         else if (of.m_orderData[i]["status"] == "-1")
214         {
215             status += "预约失败,审核未通过!";
216         }
217         else
218         {
219             status += "预约已取消!!";
220         }
221         cout << status << endl;
222     }
223     system("pause");
224     system("cls");
225 
226 
227 }
228 
229 
230 //取消预约
231 void Student::cancelOrder()
232 {
233     OrderFile of;
234     if (of.m_Size == 0)
235     {
236         cout << "无预约记录" << endl;
237         system("pause");
238         system("cls");
239         return;
240     }
241     cout << "审核中或者预约成功的记录可以取消,请输入取消的记录" << endl;
242     vector<int>v;   //存放在最大容器中的下标编号
243     int index = 1;
244     for (int i = 0; i < of.m_Size; i++)
245     {
246         //判断是自身的学号
247         if (this->m_Id == atoi(of.m_orderData[i]["stuId"].c_str()))
248         {
249             //再筛选状态  审核中 或者 预约成功
250             if (of.m_orderData[i]["status"] == "1" || of.m_orderData[i]["stuId"] == "2")
251             {
252                 v.push_back(i);
253                 cout << index++ << "";
254                 cout << "预约日期: 周" << of.m_orderData[i]["date"];
255                 cout << "\t时间段:" << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
256                 cout << "\t机房编号:" << of.m_orderData[i]["roomId"];
257                  
258                 string status = "状态:";
259                 if (of.m_orderData[i]["status"] == "1")
260                 {
261                     status += "审核中";
262                 }
263                 else if (of.m_orderData[i]["status"] == "2")
264                 {
265                     status += "预约成功!";
266                 }
267                 cout << status<<endl;
268             
269             
270             }
271 
272 
273         }
274     }
275 
276     cout << "请输入取消的记录,0代表返回" << endl;
277     int select = 0;
278     while (true)
279     {
280         cin >> select;
281         if (select >= 0 && select <= v.size())
282         {
283             if (select == 0)
284             {
285                 break;
286             }
287             else
288             {
289                 of.m_orderData[v[select - 1]]["status"] = "0";
290              
291                 of.updateOrder();
292 
293                 cout << "已取消预约!!" << endl;
294                 break;
295             
296             }
297 
298         }
299 
300         cout << "输入有误,重新输入!" << endl;
301     }
302 
303     system("pause");
304     system("cls");
305 }
  1 teacher.cpp
  2 #include"teacher.h"
  3 
  4 
  5 //默认构造
  6 Teacher::Teacher()
  7 {
  8 
  9 }
 10 
 11 
 12 //有参构造 
 13 Teacher::Teacher(int empId, string name, string pwd)
 14 {
 15     //初始化属性
 16     this->m_EmpId = empId;
 17     this->m_Name = name;
 18     this->m_Pwd = pwd;
 19 
 20 }
 21 
 22 //菜单界面
 23 void Teacher::operMenu()
 24 {
 25     cout << "=============================================  欢迎教师用户:" << this->m_Name << " 登录  ============================================= " << endl;
 26     cout << endl << "亲爱的教师用户:\n  根据下面提示的信息,请输入您的选择:\n" << endl;
 27     cout << "\t\t\t\t\t --------------------------------\n";
 28     cout << "\t\t\t\t\t|                                |\n";
 29     cout << "\t\t\t\t\t|          1.查看所有预约        |\n";
 30     cout << "\t\t\t\t\t|                                |\n";
 31     cout << "\t\t\t\t\t|                                |\n";
 32     cout << "\t\t\t\t\t|                                |\n";
 33     cout << "\t\t\t\t\t|          2.审核预约            |\n";
 34     cout << "\t\t\t\t\t|                                |\n";
 35     cout << "\t\t\t\t\t|                                |\n";
 36     cout << "\t\t\t\t\t|                                |\n";
 37     cout << "\t\t\t\t\t|          0.注销登录            |\n";
 38     cout << "\t\t\t\t\t|                                |\n";
 39     cout << "\t\t\t\t\t --------------------------------\n";
 40     cout << "  \n请输入您的选择:";
 41 }
 42 
 43 
 44 //查看所有预约
 45 void Teacher::showAllOrder()
 46 {
 47     OrderFile of;
 48     if (of.m_Size == 0)
 49     {
 50         cout << "无预约记录!" << endl;
 51         system("pause");
 52         system("cls");
 53         return;
 54     }
 55     for (int i = 0; i < of.m_Size; i++)
 56     {
 57         cout << i + 1 << "";
 58         cout << "预约日期:周" << of.m_orderData[i]["date"];
 59         cout << "\t时间段:" << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
 60         cout << "\t学号:" << of.m_orderData[i]["stuId"];
 61         cout << "\t姓名:" << of.m_orderData[i]["stuName"];
 62         cout << "\t机房编号:" << of.m_orderData[i]["roomId"];
 63         
 64         string status = "\t状态: ";
 65         //1 审核中  2已预约  -1预约失败  0取消预约
 66         if (of.m_orderData[i]["status"] == "1")
 67         {
 68             status += "审核中";
 69         }
 70         else if (of.m_orderData[i]["status"] == "2")
 71         {
 72             status += "预约成功!!";
 73         }
 74         else if (of.m_orderData[i]["status"] == "-1")
 75         {
 76             status += "预约失败,审核未通过!!";
 77         }
 78         else
 79         {
 80             status += "预约已经取消!!";
 81         }
 82         cout << status << endl;
 83 
 84     }
 85     system("pause");
 86     system("cls");
 87 }
 88 
 89 //审核预约
 90 void Teacher::validOrder()
 91 {
 92     OrderFile of;
 93     if (of.m_Size == 0)
 94     {
 95         cout << "无预约记录" << endl;
 96         system("pause");
 97         system("cls");
 98 
 99         return;
100     }
101 
102     vector<int>v;
103     int index = 0;
104     cout << "待审核的预约记录如下:" << endl;
105 
106     for (int i = 0; i < of.m_Size; i++)
107     {
108         if (of.m_orderData[i]["status"] == "1")
109         {
110             v.push_back(i);
111             cout << ++index << "";
112             cout << "预约日期:周" << of.m_orderData[i]["date"];
113             cout << "\t时间段:" << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
114             cout << "\t学生编号:" << of.m_orderData[i]["stuId"];
115             cout << "\t学生姓名:" << of.m_orderData[i]["stuName"];
116             cout << "\t机房编号:" << of.m_orderData[i]["roomId"];
117         
118             string status = "\t状态:审核中!!!";
119 
120             cout << status << endl;
121           
122         }
123     }
124 
125     cout << "请输入审核的预约记录:      0代表返回!" << endl;
126     int select = 0;    //接收用户选择的预约记录
127     int ret = 0;       //接收预约结果记录
128 
129     while (true)
130     {
131         cin >> select;
132         if (select >= 0 && select <= v.size())
133         {
134             if (select == 0)
135             {
136                 break;
137             }
138             else
139             {
140                 cout << "请输入审核结果:" << endl;
141                 cout << "1、通过" << endl;
142                 cout << "2、不通过" << endl;
143                 cin >> ret;
144                 if (ret == 1)
145                 {
146                     //通过
147                     of.m_orderData[v[select - 1]]["status"] = "2";
148                 }
149                 else
150                 {
151                     //不通过
152                     of.m_orderData[v[select - 1]]["status"] = "-1";
153 
154                 }
155                 of.updateOrder();  //更新预约记录
156                 cout << "审核完毕!!!" << endl;
157                 break;
158             }
159 
160         }
161         cout << "输入有误,请重新输入!!" << endl;
162     }
163     system("pasue");
164     system("cls");
165 }
  1 computerRoom.h
  2 #pragma once
  3 #include <iostream>
  4 using namespace  std;
  5 
  6 //机房类
  7 class ComputerRoon
  8 {
  9 public:
 10     
 11 
 12     int m_ComId;   //机房id号
 13 
 14     int m_MaxNum;  //机房最大容量
 15 };
 16 
 17 
 18 
 19 
 20 
 21 
 22 
 23 global.h
 24 #pragma once
 25 
 26 //管理员文件
 27 #define ADMIN_FILE          "admin.txt"
 28 
 29 //学生文件
 30 #define STUDENT_FILE        "student.txt"
 31 
 32 //教师文件
 33 #define TEACHER_FILE        "teacher.txt"
 34 
 35 //机房信息文件
 36 #define COMPUTER_FILE       "computer.txt"
 37 
 38 //订单文件
 39 #define ORDER_FILE           "order.txt"
 40 
 41 
 42 
 43 
 44 
 45 
 46 Identity.h
 47 #pragma once
 48 #include<iostream>
 49 
 50 using namespace std;
 51 
 52 //身份抽象类
 53 class Identity
 54 {
 55 public:
 56 
 57     //操作菜单 纯虚函数
 58     virtual void operMenu() = 0;
 59 
 60 
 61     //用户名
 62     string m_Name;
 63 
 64 
 65     //密码
 66     string m_Pwd;
 67 
 68 };
 69 
 70 
 71 
 72 
 73 
 74 
 75 manager.h
 76 #pragma once
 77 
 78 #include<iostream>
 79 using namespace std;
 80 #include"Identity.h"
 81 #include<string>
 82 #include<fstream>
 83 #include"global.h"
 84 #include<vector>
 85 #include"student.h"
 86 #include"teacher.h"
 87 #include<algorithm>
 88 #include"computerRoom.h"
 89 
 90 //管理员的类
 91 class Manager:public Identity
 92 {
 93 public:
 94 
 95     //默认构造
 96      Manager();
 97 
 98     //有参构造
 99      Manager(string name,string pwd);
100 
101     //菜单构造
102      virtual void operMenu();
103 
104     //添加账号
105      void addPerson();
106 
107     //查看账号
108      void showPerson();
109 
110 
111     //查看机房信息
112      void showComputer();
113 
114     //清空预约记录
115      void cleanFile();
116 
117      //初始化容器
118      void inttVector();
119      
120      //检测重复 参数1 检测 学号/职工号   参数2 检测类型
121      bool checkRepeat(int id, int type);
122 
123      //学生容器
124      vector<Student>vStu;
125 
126      //教师容器
127      vector<Teacher>vTea;
128 
129      //机房信息
130      vector<ComputerRoon>vCom;
131 
132 
133 };
134 
135 
136 
137 
138 
139 
140 
141 
142 
143 
144 orderFile.h
145 #pragma once
146 #include<iostream>
147 using namespace std;
148 #include"global.h"
149 #include<fstream>
150 #include<map>
151 #include<string>
152 
153 class OrderFile
154 {
155 
156 public:
157 
158     //构造函数
159     OrderFile();
160 
161     //更新预约记录
162     void updateOrder();
163 
164     //记录预约条数
165     int m_Size;
166 
167     //记录所有预约信息的容器  key记录条数  value具体记录 键值对信息
168     map<int, map<string, string>>m_orderData;
169 
170 
171 
172 };
173 
174 
175 
176 
177 
178 
179 student.h
180 #pragma once
181 #include<iostream>
182 using namespace std;
183 #include"Identity.h"
184 #include<vector>
185 #include"computerRoom.h"
186 #include<fstream>
187 #include"global.h"
188 #include"orderFile.h"
189 
190 
191 //学生类
192 class Student :public Identity
193 {
194 public:
195 
196     //默认构造
197     Student();
198 
199     //有参构造 参数 学号 姓名 密码
200     Student(int id, string name, string pwd);
201     
202 
203     //菜单界面
204     virtual void operMenu();
205 
206 
207     //申请预约
208     void applyOrder();
209 
210     //查看自身的预约
211     void showMyOrder();
212 
213 
214     //查看所有预约
215     void showAllOrder();
216 
217 
218     //取消预约
219     void cancelOrder();
220 
221     //学生学号
222     int m_Id;
223 
224     //机房容器
225     vector<ComputerRoon>vCom;
226 
227 
228 };
229 
230 
231 
232 
233 
234 
235 
236 teacher.h
237 
238 #pragma once
239 #include<iostream>
240 using namespace std;
241 #include"Identity.h"
242 #include"orderFile.h"
243 #include<vector>
244 
245 
246 //老师类
247 class Teacher :public Identity
248 {
249 public:
250 
251     //默认构造
252     Teacher();
253 
254 
255     //有参构造
256     Teacher(int empId, string name, string pwd);
257 
258     //菜单界面
259     virtual void operMenu();
260 
261 
262     //查看所有预约
263     void showAllOrder();
264 
265     //审核预约
266     void validOrder();
267 
268 
269 
270     //职工号
271     int m_EmpId;
272 
273 
274 };

//后台给出管理员的 用户名和密码  +登录  (如果添加相同Id,会提示添加失败)

3.管理员  登录错误 

 

 管理员登录正确

 

 并进入下一页

 

 

 

 

 

可以添加账号  老师 和 学生 

//添加学生

 

 //显示学生账号   没有会有提示

 

 //注册教师账号+显示

 

 

 

 3.机房信息

 

 0.注销登录

 

 1.学生登录

 

 0

 

 //申请

 

 //2查看

 

 

3查看所有

 

 4.取消预约

 

 0.注销登录

 

 3.教师登录

 

 登录教师子界面

 

 //查看预约

 

 

 

 

 2审核预约

 

 //通过 (结果老师 学生 均可看见)

 

 //学生角度

 

 //不通过

 

 //学生角度

 

posted on 2021-08-20 19:15  Bytezero!  阅读(701)  评论(0编辑  收藏  举报