forked from muziing/CPP_Learning
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstudent.cpp
238 lines (218 loc) · 6.31 KB
/
student.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
#include "student.h"
string OrderStatusStr(string StatusCode)
{
string status = " 状态:";
// 0 取消的预约 1 审核中 2 已预约 -1 预约失败
int ofStatus = atoi(StatusCode.c_str());
switch (ofStatus)
{
case 1:
status += "审核中";
break;
case 2:
status += "已预约";
break;
case -1:
status += "预约失败";
default:
status += "预约已取消";
break;
}
return status;
}
Student::Student()
{
}
Student::Student(int id, string name, string pwd)
{
this->m_Id = id;
this->m_Name = name;
this->m_Pwd = pwd;
// 获取机房信息
this->initVector();
}
// 菜单界面
void Student::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 Student::applyOrder()
{
cout << "机房开放时间为周一至周五" << endl;
cout << "请输入申请预约时间:" << endl;
cout << "1.周一 2.周二 3.周三 4.周四 5.周五" << endl;
int date = 0;
int interval = 0;
int room = 0;
while (true)
{
cin >> date;
if (date >= 1 && date <= 5)
{
break;
}
cout << "输入有误,请重新输入" << endl;
}
cout << "请输入预约时间段:" << endl;
cout << "1.上午 2.下午" << endl;
while (true)
{
cin >> interval;
if (interval >= 1 && interval <= 2)
{
break;
}
cout << "输入有误,请重新输入" << endl;
}
cout << "请选择机房:" << endl;
cout << "1号机房容量" << vCom[0].m_MaxNum << endl;
cout << "2号机房容量" << vCom[1].m_MaxNum << endl;
cout << "3号机房容量" << vCom[2].m_MaxNum << endl;
while (true)
{
cin >> room;
if (room >= 1 && room <= 3)
{
break;
}
cout << "输入有误,请重新输入" << endl;
}
ofstream ofs(ORDER_FILE, ios::app);
ofs << "date:" << date << " ";
ofs << "interval:" << interval << " ";
ofs << "stuId:" << this->m_Id << " ";
ofs << "stuName:" << this->m_Name << " ";
ofs << "roomId:" << room << " ";
ofs << "status:" << 1 << endl;
ofs.close();
system("clear");
cout << "预约成功!审核中……" << endl;
}
// 查看自己的预约
void Student::showMyOrder()
{
system("clear");
OrderFile of;
if (of.m_Size == 0)
{
system("clear");
cout << "无预约记录" << endl;
return;
}
for (int i = 0; i < of.m_Size; i++)
{
// string 利用 .c_str() 转 const char *
// (const char *) 利用 atoi 转 int
if (atoi(of.m_orderData[i]["stuId"].c_str()) == this->m_Id)
{
cout << "预约日期:周" << of.m_orderData[i]["date"];
cout << " 时段:" << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
cout << " 机房:" << of.m_orderData[i]["roomId"];
string status = OrderStatusStr(of.m_orderData[i]["status"]);
cout << status << endl;
}
}
}
// 查看所有预约
void Student::showAllOrder()
{
system("clear");
OrderFile of;
if (of.m_Size == 0)
{
system("clear");
cout << "无预约记录" << endl;
return;
}
for (int i = 0; i < of.m_Size; i++)
{
cout << i + 1 << ". ";
cout << "预约日期:周" << of.m_orderData[i]["date"];
cout << " 时段:" << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
cout << " 学号:" << of.m_orderData[i]["stuId"];
cout << " 姓名:" << of.m_orderData[i]["stuName"];
cout << " 机房:" << of.m_orderData[i]["roomId"];
string status = OrderStatusStr(of.m_orderData[i]["status"]);
cout << status << endl;
}
}
// 取消预约
void Student::cancelOrder()
{
OrderFile of;
if (of.m_Size == 0)
{
system("clear");
cout << "无预约记录" << endl;
return;
}
system("clear");
cout << "审核中或预约成功的记录可以取消 \n"
<< endl;
vector<int> v;
int index = 1;
// 显示预约
for (int i = 0; i < of.m_Size; i++)
{
if (atoi(of.m_orderData[i]["stuId"].c_str()) == this->m_Id)
{
if (of.m_orderData[i]["status"] == "1" || of.m_orderData[i]["status"] == "2")
{
v.push_back(i);
cout << index++ << "、";
cout << "预约日期:周" << of.m_orderData[i]["date"];
cout << " 时段:" << (of.m_orderData[i]["interval"] == "1" ? "上午" : "下午");
cout << " 机房:" << of.m_orderData[i]["roomId"];
string status = OrderStatusStr(of.m_orderData[i]["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
{
of.m_orderData[v[select - 1]]["status"] = "0";
of.updateOrder();
cout << "已取消预约" << endl;
break;
}
}
cout << "输入有误,请重新输入" << endl;
}
}
void Student::initVector()
{
ifstream ifs;
ifs.open(COMPUTER_FILE, ios::in);
ComputerRoom c;
while (ifs >> c.m_ComId && ifs >> c.m_MaxNum)
{
vCom.push_back(c);
}
ifs.close();
}