-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathadmin.cpp
145 lines (128 loc) · 3.17 KB
/
admin.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
#include "admin.h"
#include "mianFunction.h"
Admin::Admin() {}
Admin::Admin(string name, string password) {
name = name;
password = password;
initVector();
}
//菜单界面
void Admin::openMenu() {
cout << "\t+----------------+\n";
cout << "\t| 1.添加账号 |\n";
cout << "\t| 2.查看账号 |\n";
cout << "\t| 3.查看机房 |\n";
cout << "\t| 4.清空预约 |\n";
cout << "\t| 0.注销登陆 |\n";
cout << "\t+----------------+\n";
}
//功能
void Admin::addPerson() {
cout << "情输入你添加的类型:" << endl;
cout << "1.学生账户;" << endl;
cout << "2.教师账户;" << endl;
//用户做选择
int select;
cin >> select;
//接受用户输入
string name;
string pwd;
string id;
string file;
if (select == 1) {
cout << "学生号:\n";
cin >> id;
while (checkRepeat(id, select)) {//输入与现有id重复
cout << "输入学生号重复!请重新输入..." << endl;
cin >> id;
}
file = STUDENT_FILE;
}
else if (select == 2) {
cout << "教职工号:\n";
cin >> id;
while (checkRepeat(id, select)) {//输入与现有id重复
cout << "输入教职工号重复!请重新输入..." << endl;
cin >> id;
}
file = TEACHER_FILE;
}
cout << "姓名:\n";
cin >> name;
cout << "密码:\n";
cin >> pwd;
//存储数据
ofstream ofs;
ofs.open(file, ios::out | ios::app);
ofs << id << " " << name << " " << pwd << endl;
ofs.close();
printMessage("添加成功!");
//添加成功后,调用容器初始化更新添加的数据
initVector();
}
void Admin::showPerson() {
cout << "请选择查看的内容:" << endl;
cout << "1.所有学生账户信息;" << endl;
cout << "2.所有教师账户信息;" << endl;
int select = 0;
cin >> select;
if (select == 1) {
//show student
cout << "学生账号信息如下:" << endl;
for (auto it = vStudent.begin(); it != vStudent.end(); it++)
cout << "学生ID:" << it->getID() << "; 账户姓名:" << it->name << "; 账户密码:" << it->password << endl;
}
else {
//show teacher
cout << "教师账号信息如下:" << endl;
for (auto it = vTeacher.begin(); it != vTeacher.end(); it++)
cout << "职工ID:" << it->getID() << "; 账户姓名:" << it->name << "; 账户密码:" << it->password << endl;
}
}
void Admin::showComputer() {}
void Admin::clearFile() {}
void Admin::initVector() {
ifstream ifs;
vStudent.clear();
ifs.open(STUDENT_FILE, ios::in);
if (!ifs.is_open()) {
printMessage("文件打开失败!");
return;
}
//Student stu;
string id, name, pwd;
while (ifs >> id && ifs >> name && ifs >> pwd) {
Student stu = Student(id, name, pwd);
vStudent.push_back(stu);
}
cout << "当前学生数量为:" << vStudent.size() << endl;
ifs.close();
vTeacher.clear();
ifs.open(TEACHER_FILE, ios::in);
if (!ifs.is_open()) {
printMessage("文件打开失败!");
return;
}
//string id, name, pwd;
while (ifs >> id && ifs >> name && ifs >> pwd) {
Teacher teacher = Teacher(id, name, pwd);
vTeacher.push_back(teacher);
}
cout << "当前教师数量为:" << vTeacher.size() << endl;
ifs.close();
}
bool Admin::checkRepeat(string id, int type) {
if (type == 1) {//检测学生id
for (auto it = vStudent.begin(); it != vStudent.end(); it++) {
if (it->getID() == id)
return true;
}
}
else {//检测教师id
for (auto it = vTeacher.begin(); it != vTeacher.end(); it++) {
if (it->getID() == id)
return true;
}
}
return false;
}