20【综合案例:机房预约系统】

课程完结✿✿ヽ(°▽°)ノ✿

 

globalFile.h

 1 #pragma once
 2 
 3 
 4 
 5 //管理员文件
 6 #define ADMIN_FILE "admin.txt"
 7 
 8 //学生文件
 9 #define STUDENT_FILE "student.txt"
10 
11 //教师文件
12 #define TEACHER_FILE "teacher.txt"
13 
14 //机房信息文件
15 #define COMPUTER_FILE "computerRoom.txt"
16 
17 //订单文件
18 #define ORDER_FILE "order.txt"
View Code

 

computerRoom.h

 1 #pragma once
 2 #include<iostream>
 3 #include<cstdlib>
 4 using namespace std;
 5 
 6 
 7 
 8 class ComputerRoom
 9 {
10 public:
11     int id_com; //机房编号
12     int max_num; //最大容量
13 };
View Code

 

identity.h

 1 #pragma once
 2 #include<iostream>
 3 using namespace std;
 4 
 5 
 6 //身份抽象类;基类/父类
 7 class Identity
 8 {
 9 public:
10     virtual void oper_menu() = 0; //操作菜单;纯虚函数-抽象类-多态https://www.cnblogs.com/yppah/p/14697150.html
11 
12 public:
13     string name;
14     string pswd;
15 };
View Code

 

orderFile.h

 1 #pragma once
 2 #include<iostream>
 3 #include<cstdlib>
 4 using namespace std;
 5 #include "globalFile.h"
 6 #include <map>
 7 #include<fstream>
 8 
 9 
10 
11 class OrderFile
12 {
13 public:
14     OrderFile(); //构造
15     void update_order(); //更新预约记录
16 
17 public:
18     map<int, map<string, string> > m_order_data; //记录预约信息的容器;key为记录条数,value为记录中每一项的键值对
19     int size_current; //当前预约记录的条数
20 };
View Code

 

orderFile.cpp

  1 #include "orderFile.h"
  2 
  3 
  4 
  5 OrderFile::OrderFile()
  6 {
  7     this->size_current = 0;
  8 
  9     string date;
 10     string interval;
 11     string room;
 12     string stu_id;
 13     string stu_name;
 14     string status;
 15 
 16     ifstream ifs;
 17     ifs.open(ORDER_FILE, ios::in);
 18     while(ifs>>date && ifs>>interval && ifs>>room && ifs>>stu_id && ifs>>stu_name && ifs>>status)
 19     {
 20         //cout << date << interval << room << stu_id << stu_name << status << endl;
 21 
 22         /*
 23         //逆向解析从文件中读取到的字符串记录,分析验证
 24         int pos = date.find(":"); //例"date:1111", pos=4
 25         string key = date.substr(0, pos); //从下标0位置开始截取,截4个字符
 26         string value = date.substr(pos+1, date.size()-pos-1);
 27         cout << "key=" << key << " value=" << value << endl;
 28         */
 29         string key, value;
 30         map<string, string> m;
 31 
 32         int pos = date.find(":");
 33         if(pos != -1)
 34         {
 35             key = date.substr(0, pos);
 36             value = date.substr(pos+1, date.size()-pos-1);
 37             m.insert(make_pair(key, value));
 38         }
 39 
 40         pos = interval.find(":");
 41         if(pos != -1)
 42         {
 43             key = interval.substr(0, pos);
 44             value = interval.substr(pos+1, interval.size()-pos-1);
 45             m.insert(make_pair(key, value));
 46         }
 47 
 48         pos = room.find(":");
 49         if(pos != -1)
 50         {
 51             key = room.substr(0, pos);
 52             value = room.substr(pos+1, room.size()-pos-1);
 53             m.insert(make_pair(key, value));
 54         }
 55 
 56         pos = stu_id.find(":");
 57         if(pos != -1)
 58         {
 59             key = stu_id.substr(0, pos);
 60             value = stu_id.substr(pos+1, stu_id.size()-pos-1);
 61             m.insert(make_pair(key, value));
 62         }
 63 
 64         pos = stu_name.find(":");
 65         if(pos != -1)
 66         {
 67             key = stu_name.substr(0, pos);
 68             value = stu_name.substr(pos+1, stu_name.size()-pos-1);
 69             m.insert(make_pair(key, value));
 70         }
 71 
 72         pos = status.find(":");
 73         if(pos != -1)
 74         {
 75             key = status.substr(0, pos);
 76             value = status.substr(pos+1, status.size()-pos-1);
 77             m.insert(make_pair(key, value));
 78         }
 79 
 80         this->m_order_data.insert(make_pair(this->size_current, m)); //将临时小map容器放入到大map中
 81         this->size_current++;
 82     }
 83     ifs.close();
 84 
 85     /*
 86     //测试大map
 87     for(map<int, map<string, string> >::iterator it=m_order_data.begin(); it!=m_order_data.end(); it++)
 88     {
 89         cout << "大key(第几条记录)=" << it->first+1 << " 大value(该条记录具体信息):" << endl;
 90         for(map<string, string>::iterator it2=(*it).second.begin(); it2!=it->second.end(); it2++)
 91         {
 92             cout << "小key(属性)=" << it2->first << " 小value(实值)=" << it2->second << endl;
 93         }
 94     }
 95     */
 96 }
 97 
 98 
 99 
100 void OrderFile::update_order()
101 {
102     if(this->size_current == 0)
103     {
104         return;
105     }
106 
107     ofstream ofs(ORDER_FILE, ios::out|ios::trunc);
108     for(int i=0; i<this->size_current; i++)
109     {
110         ofs << "date:" << this->m_order_data[i]["date"] << " ";
111         ofs << "interval:" << this->m_order_data[i]["interval"] << " ";
112         ofs << "room:" << this->m_order_data[i]["room"] << " ";
113         ofs << "stu_id:" << this->m_order_data[i]["stu_id"] << " ";
114         ofs << "stu_name:" << this->m_order_data[i]["stu_name"] << " ";
115         ofs << "status:" << this->m_order_data[i]["status"] << endl;
116     }
117     ofs.close();
118 }
View Code

 

manager.h

 1 #pragma once
 2 #include<iostream>
 3 #include<cstdlib>
 4 using namespace std;
 5 #include "identity.h"
 6 #include "student.h"
 7 #include "teacher.h"
 8 #include "globalFile.h"
 9 #include "computerRoom.h"
10 #include<string>
11 #include<fstream>
12 #include<vector>
13 #include<algorithm>
14 
15 
16 
17 //管理员类
18 class Manager : public Identity
19 {
20 public:
21     Manager(); //默认构造
22     Manager(string _name, string _pswd); //有参构造
23 
24     virtual void oper_menu(); //操作界面
25     void add_person(); //添加账号
26     void show_person(); //查看账号
27     void show_computer(); //查看机房信息
28     void clear_file(); //清空预约记录
29     void init_vector(); //初始化容器
30     bool check_repeat(int id, int type); //(传入ID, 传入类型);true重复
31 
32 public:
33     vector<Student> v_stu;
34     vector<Teacher> v_tea;
35     vector<ComputerRoom> v_com;
36 };
View Code

 

manager.cpp

  1 #include "manager.h"
  2 
  3 
  4 
  5 Manager::Manager()
  6 {
  7 
  8 }
  9 
 10 
 11 
 12 Manager::Manager(string _name, string _pswd)
 13 {
 14     //初始化管理员信息
 15     this->name = _name;
 16     this->pswd = _pswd;
 17 
 18     //初始化容器,获取到文件中现有学生、老师信息
 19     this->init_vector();
 20 
 21     //初始化机房信息
 22     ifstream ifs;
 23     ifs.open(COMPUTER_FILE, ios::in);
 24     ComputerRoom com;
 25     while(ifs>>com.id_com && ifs>>com.max_num)
 26     {
 27         v_com.push_back(com);
 28     }
 29     ifs.close();
 30     //cout << "机房数量:" << v_com.size() << endl; //测试
 31 
 32 }
 33 
 34 
 35 
 36 void Manager::oper_menu()
 37 {
 38     cout << "欢迎管理员:" << this->name << "登录!" << endl;
 39     cout << "\t\t ---------------------------------\n";
 40     cout << "\t\t|                                  |\n";
 41     cout << "\t\t| 1.添加账号                       |\n";
 42     cout << "\t\t|                                  |\n";
 43     cout << "\t\t| 2.查看账号                       |\n";
 44     cout << "\t\t|                                  |\n";
 45     cout << "\t\t| 3.查看机房                       |\n";
 46     cout << "\t\t|                                  |\n";
 47     cout << "\t\t| 4.清空预约                       |\n";
 48     cout << "\t\t|                                  |\n";
 49     cout << "\t\t| 0.注销登录                       |\n";
 50     cout << "\t\t|                                  |\n";
 51     cout << "\t\t ---------------------------------\n";
 52 }
 53 
 54 
 55 
 56 void Manager::add_person()
 57 {
 58     string file_name;
 59     string tip;
 60     string error_tip;
 61     ofstream ofs;
 62 
 63     cout << "请输入添加账号的类型:(1-学生;2-教师)" << endl;
 64     int select = 0;
 65     cin >> select;
 66     if(select == 1)
 67     {
 68         file_name = STUDENT_FILE;
 69         tip = "请输入学号:";
 70         error_tip = "学号已存在,请重新输入";
 71     }
 72     else if(select == 2)
 73     {
 74         file_name = TEACHER_FILE;
 75         tip = "请输入职工号:";
 76         error_tip = "职工号已存在,请重新输入";
 77     }
 78     else
 79     {
 80         cout << "输入错误" << endl;
 81         system("pause");
 82         system("cls");
 83         return;
 84     }
 85 
 86     int id;
 87     string name;
 88     string pswd;
 89     while(true) //ID去重
 90     {
 91         cout << tip << endl;
 92         cin >> id;
 93         bool ret = this->check_repeat(id, select);
 94         if(ret)
 95         {
 96             cout << error_tip << endl;
 97         }
 98         else
 99         {
100             break;
101         }
102     }
103     cout << "请输入姓名:" << endl;
104     cin >> name;
105     cout << "请输入密码:" << endl;
106     cin >> pswd;
107 
108     ofs.open(file_name.c_str(), ios::out|ios::app);
109     ofs << id << " " << name << " " << pswd << " " << endl;
110     cout << "添加成功" << endl;
111     ofs.close();
112 
113     system("pause");
114     system("cls");
115 
116     this->init_vector();
117 }
118 
119 
120 
121 void print_student(Student & s)
122 {
123     cout << "学号:" << s.id_stu << " 姓名:" << s.name << " 密码:" << s.pswd << endl;
124 }
125 
126 
127 
128 void print_teacher(Teacher & t)
129 {
130     cout << "职工号:" << t.id_tea << " 姓名:" << t.name << " 密码:" << t.pswd << endl;
131 }
132 
133 
134 
135 void Manager::show_person()
136 {
137     cout << "请输入查看哪种账号:(1-学生;2-教师)" << endl;
138     int select = 0;
139     cin >> select;
140 
141     if(select == 1)
142     {
143         cout << "所有学生的信息如下:" << endl;
144         for_each(v_stu.begin(), v_stu.end(), print_student);
145         cout << endl;
146     }
147     else if(select == 2)
148     {
149         cout << "所有教师的信息如下:" << endl;
150         for_each(v_tea.begin(), v_tea.end(), print_teacher);
151         cout << endl;
152     }
153     else
154     {
155         cout << "输入错误" << endl;
156     }
157 
158     system("pause");
159     system("cls");
160 }
161 
162 
163 
164 void Manager::show_computer()
165 {
166     cout << "机房信息如下:" << endl;
167     for(vector<ComputerRoom>::iterator it=v_com.begin(); it!=v_com.end(); it++)
168     {
169         cout << "机房编号:" << it->id_com << " 最大容量:" << it->max_num << endl;
170     }
171     system("pause");
172     system("cls");
173 }
174 
175 
176 
177 void Manager::init_vector()
178 {
179     //确保容器是清空状态
180     v_stu.clear();
181     v_tea.clear();
182 
183     //读取学生信息
184     ifstream ifs;
185     ifs.open(STUDENT_FILE, ios::in);
186     if(!ifs.is_open())
187     {
188         cout << "文件读取失败" << endl;
189         return;
190     }
191     else
192     {
193         Student s;
194         while(ifs>>s.id_stu && ifs>>s.name && ifs>>s.pswd)
195         {
196             v_stu.push_back(s);
197         }
198         //cout << "当前学生数量:" << v_stu.size() << endl; //测试
199     }
200     ifs.close();
201 
202     //读取教师信息
203     ifs.open(TEACHER_FILE, ios::in);
204     if(!ifs.is_open())
205     {
206         cout << "文件读取失败" << endl;
207         return;
208     }
209     else
210     {
211         Teacher t;
212         while(ifs>>t.id_tea && ifs>>t.name && ifs>>t.pswd)
213         {
214             v_tea.push_back(t);
215         }
216         //cout << "当前老师数量:" << v_tea.size() << endl; //测试
217     }
218     ifs.close();
219 }
220 
221 
222 
223 
224 bool Manager::check_repeat(int id, int type)
225 {
226     if(type == 1)
227     {
228         for(vector<Student>::iterator it=v_stu.begin(); it!=v_stu.end(); it++)
229         {
230             if(id == it->id_stu)
231             {
232                 return true;
233             }
234         }
235     }
236     else if(type == 2)
237     {
238         for(vector<Teacher>::iterator it=v_tea.begin(); it!=v_tea.end(); it++)
239         {
240             if(id == it->id_tea)
241             {
242                 return true;
243             }
244         }
245     }
246     return false;
247 }
248 
249 
250 
251 void Manager::clear_file()
252 {
253     ofstream ofs(ORDER_FILE, ios::trunc);
254     ofs.close();
255     cout << "清空成功" << endl;
256     system("pause");
257     system("cls");
258 }
View Code

 

student.h

 1 #pragma once
 2 #include<iostream>
 3 using namespace std;
 4 #include "identity.h"
 5 #include "globalFile.h"
 6 #include "computerRoom.h"
 7 #include "orderFile.h"
 8 #include<string>
 9 #include<fstream>
10 #include<vector>
11 
12 
13 
14 //学生类
15 class Student : public Identity
16 {
17 public:
18     Student(); //默认构造
19     Student(int _id, string _name, string _pswd); //有参构造
20 
21     virtual void oper_menu(); //操作界面
22     void apply_order(); //申请预约
23     void show_my_order(); //查看我的预约
24     void show_all_order(); //查看所有预约
25     void cancel_order(); //取消预约
26 
27 public:
28     int id_stu;
29 
30     vector<ComputerRoom> v_com; //机房容器
31 };
View Code

 

student.cpp

  1 #include "student.h"
  2 
  3 
  4 
  5 Student::Student()
  6 {
  7 
  8 }
  9 
 10 
 11 
 12 Student::Student(int _id, string _name, string _pswd)
 13 {
 14     this->id_stu = _id;
 15     this->name = _name;
 16     this->pswd = _pswd;
 17 
 18     ifstream ifs;
 19     ifs.open(COMPUTER_FILE, ios::in);
 20     ComputerRoom com;
 21     while(ifs>>com.id_com && ifs>>com.max_num)
 22     {
 23         v_com.push_back(com);
 24     }
 25     ifs.close();
 26 }
 27 
 28 
 29 
 30 void Student::oper_menu()
 31 {
 32     cout << "欢迎学生代表:" << this->name << "登录!" << endl;
 33     cout << "\t\t ----------------------------------\n";
 34     cout << "\t\t|                                  |\n";
 35     cout << "\t\t| 1.申请预约                       |\n";
 36     cout << "\t\t|                                  |\n";
 37     cout << "\t\t| 2.查看我的预约                   |\n";
 38     cout << "\t\t|                                  |\n";
 39     cout << "\t\t| 3.查看所有预约                   |\n";
 40     cout << "\t\t|                                  |\n";
 41     cout << "\t\t| 4.取消预约                       |\n";
 42     cout << "\t\t|                                  |\n";
 43     cout << "\t\t| 0.注销登录                       |\n";
 44     cout << "\t\t|                                  |\n";
 45     cout << "\t\t ----------------------------------\n";
 46 }
 47 
 48 
 49 
 50 void Student::apply_order()
 51 {
 52     int date = 0;
 53     int interval = 0;
 54     int room;
 55 
 56     cout << "提示:机房仅工作日开放!" << endl;
 57     cout << "1、周一" << endl;
 58     cout << "2、周二" << endl;
 59     cout << "3、周三" << endl;
 60     cout << "4、周四" << endl;
 61     cout << "5、周五" << endl;
 62     cout << "请选择申请预约的时间:" << endl;
 63 
 64     while(true)
 65     {
 66         cin >> date;
 67         if(date>=1 && date<=5)
 68         {
 69             break;
 70         }
 71         cout << "输入错误,请重新输入:" << endl;
 72     }
 73 
 74     cout << "提示:机房仅上午和下午开放!" << endl;
 75     cout << "1、上午" << endl;
 76     cout << "2、下午" << endl;
 77     cout << "请选择申请预约的时间段:" << endl;
 78 
 79     while(true)
 80     {
 81         cin >> interval;
 82         if(interval>=1 && interval<=2)
 83         {
 84             break;
 85         }
 86         cout << "输入错误,请重新输入:" << endl;
 87     }
 88 
 89     cout << "提示:目前仅三个机房可供预约!" << endl;
 90     cout << "1、一号机房" << " 容量:" << v_com[0].max_num << endl;
 91     cout << "2、二号机房" << " 容量:" << v_com[1].max_num << endl;
 92     cout << "3、三号机房" << " 容量:" << v_com[2].max_num << endl;
 93     cout << "请选择申请预约的机房:" << endl;
 94 
 95     while(true)
 96     {
 97         cin >> room;
 98         if(room>=1 && room<=3)
 99         {
100             break;
101         }
102         cout << "输入错误,请重新输入:" << endl;
103     }
104 
105     ofstream ofs(ORDER_FILE, ios::app);
106     ofs << "date:" << date << " ";
107     ofs << "interval:" << interval << " ";
108     ofs << "room:" << room << " ";
109     ofs << "stu_id:" << this->id_stu << " ";
110     ofs << "stu_name:" << this->name << " ";
111     ofs << "status:" << 1 << endl;
112     ofs.close();
113     cout << "预约成功!审核中..." << endl;
114 
115     system("pause");
116     system("cls");
117 }
118 
119 
120 
121 void Student::show_my_order()
122 {
123     OrderFile of;
124 
125     if(of.size_current == 0)
126     {
127         cout << "无预约记录!" << endl;
128         system("pause");
129         system("cls");
130         return;
131     }
132 
133     for(int i=0; i<of.size_current; i++)
134     {
135         //string(C++类型字符串)利用.c_str()转换为const char *[](C类型字符串)
136         //atoi(const char *[])可将C类型字符串转换为int
137         if(this->id_stu == atoi(of.m_order_data[i]["stu_id"].c_str()))//找到自身预约信息
138         {
139             cout << "预约时间:周" << of.m_order_data[i]["date"];
140             cout << " 预约时间段:" << (of.m_order_data[i]["interval"]=="1" ? "上午" : "下午");
141             cout << " 预约机房:" <<of.m_order_data[i]["room"];
142             string status = " 状态:";
143             if(of.m_order_data[i]["status"] == "1") //审核中/待审核
144             {
145                 status += "预约审核中";
146             }
147             else if(of.m_order_data[i]["status"] == "2") //已预约/预约成功
148             {
149                 status += "预约成功啦";
150             }
151             else if(of.m_order_data[i]["status"] == "-1") //预约失败/未通过审核
152             {
153                 status += "未通过审核";
154             }
155             else if(of.m_order_data[i]["status"] == "0") //取消预约/学生自己取消
156             {
157                 status += "已取消预约";
158             }
159             cout << status << endl;
160         }
161     }
162 
163     system("pause");
164     system("cls");
165 }
166 
167 
168 
169 void Student::show_all_order()
170 {
171     OrderFile of;
172 
173     if(of.size_current == 0)
174     {
175         cout << "无预约记录!" << endl;
176         system("pause");
177         system("cls");
178         return;
179     }
180 
181     for(int i=0; i<of.size_current; i++)
182     {
183         cout << i+1 << "" ;
184         cout << "预约时间:周" << of.m_order_data[i]["date"];
185         cout << " 预约时间段:" << (of.m_order_data[i]["interval"]=="1" ? "上午" : "下午");
186         cout << " 预约机房:" << of.m_order_data[i]["room"];
187         cout << " 学号:" << of.m_order_data[i]["stu_id"];
188         cout << " 姓名:" << of.m_order_data[i]["stu_name"];
189         string status = " 状态:";
190         if(of.m_order_data[i]["status"] == "1")
191         {
192             status += "预约审核中";
193         }
194         else if(of.m_order_data[i]["status"] == "2")
195         {
196             status += "预约成功啦";
197         }
198         else if(of.m_order_data[i]["status"] == "-1")
199         {
200             status += "未通过审核";
201         }
202         else //0
203         {
204             status += "已取消预约";
205         }
206         cout << status << endl;
207     }
208 
209     system("pause");
210     system("cls");
211 }
212 
213 
214 
215 void Student::cancel_order()
216 {
217     OrderFile of;
218 
219     if(of.size_current == 0)
220     {
221         cout << "无预约记录!" << endl;
222         system("pause");
223         system("cls");
224         return;
225     }
226 
227     cout << "提示:仅审核中或预约成功的记录可以取消预约!" << endl;
228 
229     vector<int> v;
230     int index = 1; //此处也可以设置为0,=0时下面输出改为++index
231 
232     for(int i=0; i<of.size_current; i++)
233     {
234         if(this->id_stu == atoi( of.m_order_data[i]["stu_id"].c_str() ))//先找到自身预约记录
235         {
236             if(of.m_order_data[i]["status"]=="1" || of.m_order_data[i]["status"]=="2") //再找到符合状态条件的预约记录
237             {
238                 v.push_back(i);
239                 cout << index++ << "";
240                 cout << "预约时间:周" << of.m_order_data[i]["date"];
241                 cout << " 预约时间段:" << (of.m_order_data[i]["interval"]=="1" ? "上午" : "下午");
242                 cout << " 预约机房:" << of.m_order_data[i]["room"];
243                 cout << " 学号:" << of.m_order_data[i]["stu_id"];
244                 cout << " 姓名:" << of.m_order_data[i]["stu_name"];
245                 string status = " 状态:";
246                 if(of.m_order_data[i]["status"] == "1")
247                 {
248                     status += "预约审核中";
249                 }
250                 else if(of.m_order_data[i]["status"] == "2")
251                 {
252                     status += "预约成功啦";
253                 }
254                 cout << status << endl;
255             }
256         }
257     }
258 
259     cout << "请输入取消的预约:(0表示返回上一步)" << endl;
260     int select = 0;
261     while(true)
262     {
263         cin >> select;
264         if(select>=0 && select<=v.size())
265         {
266             if(select == 0)
267             {
268                 break;
269             }
270             else
271             {
272                 of.m_order_data[ v[select-1] ]["status"] = "0";
273                 of.update_order();
274                 cout << "预约取消成功" << endl;
275                 break;
276             }
277         }
278         cout << "输入有误,请重新输入:" << endl;
279     }
280 
281     system("pause");
282     system("cls");
283 }
View Code

 

teacher.h

 1 #pragma once
 2 #include<iostream>
 3 using namespace std;
 4 #include "identity.h"
 5 //#include "globalFile.h"
 6 #include "orderFile.h"
 7 #include<string>
 8 #include<fstream>
 9 #include<vector>
10 
11 
12 
13 //教师类
14 class Teacher : public Identity
15 {
16 public:
17     Teacher(); //默认构造
18     Teacher(int _id, string _name, string _pswd); //有参构造
19 
20     virtual void oper_menu(); //操作界面
21     void show_all_order(); //查看所有预约
22     void valid_order(); //审核预约
23 
24 public:
25     int id_tea;
26 };
View Code

 

teacher.cpp

  1 #include "teacher.h"
  2 
  3 
  4 
  5 Teacher::Teacher()
  6 {
  7 
  8 }
  9 
 10 
 11 
 12 Teacher::Teacher(int _id, string _name, string _pswd)
 13 {
 14     this->id_tea = _id;
 15     this->name = _name;
 16     this->pswd = _pswd;
 17 }
 18 
 19 
 20 
 21 void Teacher::oper_menu()
 22 {
 23     cout << "欢迎教师:" << this->name << "登录!" << endl;
 24     cout << "\t\t ----------------------------------\n";
 25     cout << "\t\t|                                  |\n";
 26     cout << "\t\t| 1.查看所有预约                   |\n";
 27     cout << "\t\t|                                  |\n";
 28     cout << "\t\t| 2.审核预约                       |\n";
 29     cout << "\t\t|                                  |\n";
 30     cout << "\t\t| 0.注销登录                       |\n";
 31     cout << "\t\t|                                  |\n";
 32     cout << "\t\t ----------------------------------\n";
 33 }
 34 
 35 
 36 
 37 void Teacher::show_all_order() //同void Student::show_all_order()
 38 {
 39     OrderFile of;
 40 
 41     if(of.size_current == 0)
 42     {
 43         cout << "无预约记录!" << endl;
 44         system("pause");
 45         system("cls");
 46         return;
 47     }
 48 
 49     for(int i=0; i<of.size_current; i++)
 50     {
 51         cout << i+1 << "" ;
 52         cout << "预约时间:周" << of.m_order_data[i]["date"];
 53         cout << " 预约时间段:" << (of.m_order_data[i]["interval"]=="1" ? "上午" : "下午");
 54         cout << " 预约机房:" << of.m_order_data[i]["room"];
 55         cout << " 学号:" << of.m_order_data[i]["stu_id"];
 56         cout << " 姓名:" << of.m_order_data[i]["stu_name"];
 57         string status = " 状态:";
 58         if(of.m_order_data[i]["status"] == "1")
 59         {
 60             status += "预约审核中";
 61         }
 62         else if(of.m_order_data[i]["status"] == "2")
 63         {
 64             status += "预约成功啦";
 65         }
 66         else if(of.m_order_data[i]["status"] == "-1")
 67         {
 68             status += "未通过审核";
 69         }
 70         else //0
 71         {
 72             status += "已取消预约";
 73         }
 74         cout << status << endl;
 75     }
 76 
 77     system("pause");
 78     system("cls");
 79 }
 80 
 81 
 82 
 83 void Teacher::valid_order()
 84 {
 85     OrderFile of;
 86 
 87     if(of.size_current == 0)
 88     {
 89         cout << "无预约记录" << endl;
 90         system("pause");
 91         system("cls");
 92         return;
 93     }
 94 
 95     cout << "待审核记录如下:" << endl;
 96 
 97     vector<int> v;
 98     int index = 0;
 99 
100     for(int i=0; i<of.size_current; i++)
101     {
102         if(of.m_order_data[i]["status"] == "1")
103         {
104             v.push_back(i);
105             cout << ++index << "";
106             cout << "预约时间:周" << of.m_order_data[i]["date"];
107             cout << " 预约时间段:" << (of.m_order_data[i]["interval"]=="1" ? "上午" : "下午");
108             cout << " 预约机房:" << of.m_order_data[i]["room"];
109             cout << " 学号:" << of.m_order_data[i]["stu_id"];
110             cout << " 姓名:" << of.m_order_data[i]["stu_name"];
111             cout << " 状态:预约待审核" << endl;
112         }
113     }
114 
115     cout << "请输入审核的预约记录:(输入0返回上一层)" << endl;
116     int select = 0;
117     int ret = 0;
118 
119     while(true)
120     {
121         cin >> select;
122         if(select>=0 && select<=v.size())
123         {
124             if(select == 0)
125             {
126                 break;
127             }
128             else
129             {
130                 cout << "请输入审核结果:(1-通过;2不通过)" << endl;
131                 while(true)
132                 {
133                     cin >> ret;
134                     if(ret == 1) //通过
135                     {
136                         of.m_order_data[ v[select-1] ]["status"] = "2";
137                         break;
138                     }
139                     else if(select == 2) //不通过
140                     {
141                         of.m_order_data[ v[select-1] ]["status"] = "-1";
142                         break;
143                     }
144                     cout << "审核结果输入错误,请重新输入" << endl;
145                 }
146                 of.update_order();
147                 cout << "审核完毕" << endl;
148                 break;
149             }
150         }
151         cout << "审核记录输入有误,请重新输入:" << endl;
152     }
153 
154     system("pause");
155     system("cls");
156 }
View Code

 

机房预约系统.cpp

  1 #include<iostream>
  2 #include<cstdlib>
  3 using namespace std;
  4 #include "identity.h"
  5 #include "student.h"
  6 #include "teacher.h"
  7 #include "manager.h"
  8 #include "globalFile.h"
  9 #include<string>
 10 #include<fstream>
 11 
 12 
 13 
 14 void manager_menu(Identity * &manager) //父类指针创建子类对象
 15 {
 16     while(true)
 17     {
 18         manager->oper_menu(); //多态:父类指针所创建的子类对象调用共同接口(重写的纯虚函数)
 19         Manager * man = (Manager *)manager; //强转;将父类指针转为子类指针,才可以调用子类特有的接口
 20 
 21         int select = 0;
 22         cout << "请选择您的操作:" << endl;
 23         cin >> select;
 24 
 25         if(select == 1)
 26         {
 27             //cout << "添加账号" << endl;
 28             man->add_person();
 29         }
 30         else if(select == 2)
 31         {
 32             //cout << "查看账号" << endl;
 33             man->show_person();
 34         }
 35         else if(select == 3)
 36         {
 37             //cout << "查看机房" << endl;
 38             man->show_computer();
 39         }
 40         else if(select == 4)
 41         {
 42             //cout << "清空预约" << endl;
 43             man->clear_file();
 44         }
 45         else if(select == 0)
 46         {
 47             delete manager; //销毁堆区对象
 48             //delete man; //实测这俩销毁那个都行,但同时销毁时dos报错
 49             cout << "注销成功" << endl;
 50             system("pause");
 51             system("cls");
 52             return;
 53         }
 54         else
 55         {
 56             cout << "输入错误" << endl;
 57             system("pause");
 58             system("cls");
 59         }
 60     }
 61 }
 62 
 63 
 64 
 65 void student_menu(Identity * &student)
 66 {
 67     while(true)
 68     {
 69         student->oper_menu();
 70         Student * stu = (Student *)student;
 71 
 72         int select = 0;
 73         cout << "请选择您的操作:" << endl;
 74         cin >> select;
 75 
 76         if(select == 1) //申请预约
 77         {
 78             stu->apply_order();
 79         }
 80         else if(select == 2) //查看自身预约
 81         {
 82             stu->show_my_order();
 83         }
 84         else if(select == 3) //查看所有预约
 85         {
 86             stu->show_all_order();
 87         }
 88         else if(select == 4) //取消预约
 89         {
 90             stu->cancel_order();
 91         }
 92         else if(select == 0) //退出登录
 93         {
 94             delete student;
 95             cout << "注销成功" << endl;
 96             system("pause");
 97             system("cls");
 98             return;
 99         }
100         else
101         {
102             cout << "输入错误" << endl;
103             system("pause");
104             system("cls");
105         }
106     }
107 }
108 
109 
110 
111 void teacher_menu(Identity * &teacher)
112 {
113     while(true)
114     {
115         teacher->oper_menu();
116         Teacher * tea = (Teacher *)teacher;
117 
118         int select = 0;
119         cout << "请选择您的操作:" << endl;
120         cin >> select;
121 
122         if(select == 1) //查看全部预约
123         {
124             tea->show_all_order();
125         }
126         else if(select == 2) //审核学生预约
127         {
128             tea->valid_order();
129         }
130         else if(select == 0) //退出登录
131         {
132             delete teacher;
133             cout << "注销成功" << endl;
134             system("pause");
135             system("cls");
136             return;
137         }
138         else
139         {
140             cout << "输入错误" << endl;
141             system("pause");
142             system("cls");
143         }
144     }
145 }
146 
147 
148 
149 void login(string file_name, int type) //(所操作的文件, 登入身份类型)
150 {
151     ifstream ifs;
152     //ifs.open(file_name, ios::in); //error原因C++的string类可能无法直接作为open的参数。解决方案:使用C的字符串或者filename.c_str()。
153     ifs.open(file_name.c_str(), ios::in);
154     if(!ifs.is_open())
155     {
156         cout << "文件不存在" << endl;
157         ifs.close();
158         return;
159     }
160 
161 
162     int id = 0;
163     string name;
164     string pswd;
165     if(type == 1)
166     {
167         cout << "请输入学号:" << endl;
168         cin >> id;
169     }
170     else if(type == 2)
171     {
172         cout << "请输入职工号:" << endl;
173         cin >> id;
174     }
175     cout << "请输入用户名:" << endl;
176     cin >> name;
177     cout << "请输入密码:" << endl;
178     cin >> pswd;
179 
180 
181     Identity * person = NULL; //父类指针,用于指向子类对象
182     string f_name; //从文件中获取的姓名
183     string f_pswd; //从文件中获取的密码
184     if(type == 1)
185     {
186         int f_id; //从文件中获取的ID
187         while(ifs>>f_id && ifs>>f_name && ifs>>f_pswd)
188         {
189             /*
190             cout << f_id << endl;
191             cout << f_name << endl;
192             cout << f_pswd << endl;
193             //中文乱码解决:txt用记事本打开另存为ANSI编码格式
194             //打印时中文乱码说明程序读取到的就是乱码,这样的话在对比信息时会出错,所以所有txt手动添加/修改后都要另存
195             */
196 
197             if(f_id==id && f_name==name && f_pswd==pswd) //对比信息
198             {
199                 cout << "学生验证登录成功" << endl;
200                 system("pause");
201                 system("cls");
202                 person = new Student(id, name, pswd); //创建在堆区
203                 //进入学生子菜单
204                 student_menu(person);
205                 return;
206             }
207         }
208     }
209     else if(type == 2)
210     {
211         int f_id;
212         while(ifs>>f_id && ifs>>f_name && ifs>>f_pswd)
213         {
214             if(f_id==id && f_name==name && f_pswd==pswd)
215             {
216                 cout << "教师验证登录成功" << endl;
217                 system("pause");
218                 system("cls");
219                 person = new Teacher(id, name, pswd);
220                 //进入教师子菜单
221                 teacher_menu(person);
222                 return;
223             }
224         }
225     }
226     else if(type == 3)
227     {
228         while(ifs>>f_name && ifs>>f_pswd)
229         {
230             if(f_name==name && f_pswd==pswd)
231             {
232                 cout << "管理员验证登录成功" << endl;
233                 system("pause");
234                 system("cls");
235                 person = new Manager(name, pswd);
236                 manager_menu(person); //进入管理员子菜单
237                 return;
238             }
239         }
240     }
241 
242 
243     cout << "登录验证失败" << endl; //输入非123选项或账号、密码错误都会显示验证失败
244     system("pause");
245     system("cls");
246     return;
247 }
248 
249 
250 
251 int main()
252 {
253     int select = 0;
254     while(true)
255     {
256         cout << "====================== 欢迎来到机房预约系统====================="<< endl;
257         cout << "\t\t -------------------------------\n";
258         cout << "\t\t|                               |\n";
259         cout << "\t\t| 1.学生代表                    |\n";
260         cout << "\t\t|                               |\n";
261         cout << "\t\t| 2.老 师                       |\n";
262         cout << "\t\t|                               |\n";
263         cout << "\t\t| 3.管 理 员                    |\n";
264         cout << "\t\t|                               |\n";
265         cout << "\t\t| 0.退 出                       |\n";
266         cout << "\t\t|                               |\n";
267         cout << "\t\t -------------------------------\n";
268         cout << "请输入您的选择: " << endl;
269 
270         cin >> select;
271         switch(select)
272         {
273         case 1:
274             login(STUDENT_FILE, 1);
275             break;
276         case 2:
277             login(TEACHER_FILE, 2);
278             break;
279         case 3:
280             login(ADMIN_FILE, 3);
281             break;
282         case 0:
283             cout << "欢迎下次使用 ヾ( ̄▽ ̄)Bye~Bye~" << endl;
284             system("pause");
285             return 0;
286             break;
287         default:
288             cout << "输入错误" << endl;
289             system("pause");
290             system("cls");
291             break;
292         }
293     }
294 
295     system("pause");
296     return 0;
297 }
View Code

 

 

posted @ 2021-05-15 11:11  yub4by  阅读(313)  评论(0编辑  收藏  举报