-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmanager.cpp
239 lines (207 loc) · 5.51 KB
/
manager.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
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
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
#include <algorithm>
#include "manager.h"
void printStudent(Student &s)
{
cout << "学号:" << s.m_Id << "姓名:" << s.m_Name << "密码:" << s.m_Pwd << endl;
}
void printTeacher(Teacher &t)
{
cout << "职工号:" << t.m_EmpId << "姓名:" << t.m_Name << "密码:" << t.m_Pwd << endl;
}
Manager::Manager()
{
}
Manager::Manager(string name, string pwd)
{
this->m_Name = name;
this->m_Pwd = pwd;
// 初始化容器
this->initVector();
}
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;
}
void Manager::addPerson()
{
string fileName;
string tip;
string errorTip; // 重复错误提示
ofstream ofs;
cout << "请输入添加帐号的类型" << endl;
cout << "1.添加学生" << endl;
cout << "2.添加老师" << endl;
// 接收用户输入帐号类型
int type_select = 0;
while (true)
{
cin >> type_select;
if (type_select == 1)
{
fileName = STUDENT_FILE;
tip = "请输入学号:";
errorTip = "学号重复,请重新输入";
break;
}
else if (type_select == 2)
{
fileName = TEACHER_FILE;
tip = "请输入职工编号:";
errorTip = "职工号重复,请重新输入";
break;
}
else
{
cout << "输入错误,请重试" << endl;
}
}
// 接收新账户的信息
int id;
string name;
string pwd;
cout << tip << endl;
// 输入ID,并检测重复
while (true)
{
cin >> id;
bool ret = this->checkRepeat(id, type_select);
if (ret) // 有重复
{
cout << errorTip << endl;
}
else
{
break;
}
}
cout << "请输入姓名:" << endl;
cin >> name;
cout << "请输入密码:" << endl;
cin >> pwd;
// 添加到vector容器中
if (type_select == 1)
{
this->vStu.push_back(Student(id, name, pwd));
}
else if (type_select == 2)
{
this->vTea.push_back(Teacher(id, name, pwd));
}
// 写入文件
ofs.open(fileName, ios::out | ios::app);
ofs << id << " " << name << " " << pwd << endl;
ofs.close();
system("clear");
cout << "添加成功" << endl;
}
void Manager::showPerson()
{
cout << "请选择查看内容:" << endl;
cout << "1.查看所有学生" << endl;
cout << "2.查看所有老师" << endl;
int select = 0;
cin >> select;
system("clear");
if (select == 1)
{
cout << "全部学生信息如下:" << endl;
for_each(vStu.begin(), vStu.end(), printStudent);
}
else if (select == 2)
{
cout << "全部教师信息如下:" << endl;
for_each(vTea.begin(), vTea.end(), printTeacher);
}
}
void Manager::showComputer()
{
system("clear");
cout << "机房信息如下:" << endl;
for (vector<ComputerRoom>::iterator it = vCom.begin(); it != vCom.end(); it++)
{
cout << "机房编号:" << it->m_ComId << "机房最大容量:" << it->m_MaxNum << endl;
}
}
void Manager::cleanFile()
{
ofstream ofs(ORDER_FILE, ios::trunc);
ofs.close();
system("clear");
cout << "清空成功!" << endl;
}
void Manager::initVector()
{
// 读取学生文件中信息
ifstream ifs;
ifs.open(STUDENT_FILE, ios::in);
if (!ifs.is_open())
{
cout << "文件读取失败" << endl;
return;
}
vStu.clear();
vTea.clear();
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);
Teacher t;
while (ifs >> t.m_EmpId && ifs >> t.m_Name && ifs >> t.m_Pwd)
{
vTea.push_back(t);
}
cout << "当前教师数量为:" << vTea.size() << endl;
ifs.close();
// 机房信息
ifs.open(COMPUTER_FILE, ios::in);
ComputerRoom c;
while (ifs >> c.m_ComId && ifs >> c.m_MaxNum)
{
vCom.push_back(c);
}
cout << "当前机房数量为:" << vCom.size() << endl;
ifs.close();
}
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 if (type == 2)
{
for (vector<Teacher>::iterator it = vTea.begin(); it != vTea.end(); it++)
{
if (id == it->m_EmpId)
{
return true;
}
}
}
return false;
}