-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAgendaUI.cpp
408 lines (380 loc) · 11.2 KB
/
AgendaUI.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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
// Copyright [2013] chenzy
#include "AgendaUI.h"
#include <iostream>
#include <iomanip>
using std::cin;
using std::cout;
using std::endl;
AgendaUI::AgendaUI() {
userName_.clear();
userPassword_.clear();
agendaService_.startAgenda();
startAgenda();
}
void AgendaUI::startAgenda() {
if (userName_.empty()) {
cout << "-------------------- Agenda -----------------------\n"
<< "Action :\n"
<< "l - log in Agenda by user name and password\n"
<< "r - register an Agenda account\n"
<< "q - quit Agenda\n"
<< "---------------------------------------------------\n\n";
} else {
cout << "-------------------- Agenda -----------------------\n"
<< "Action :\n"
<< "o - log out Agenda\n"
<< "dc - delete Agenda account\n"
<< "lu - list all Agenda user\n"
<< "cm - create a meeting\n"
<< "la - list all meetings\n"
<< "las - list all sponsor meetings\n"
<< "lap - list all participate meetings\n"
<< "qm - query meeting by title\n"
<< "qt - query meeting by time interval\n"
<< "dm - delete meeting by title\n"
<< "da - delete all meetings\n"
<< "su - set user password\n"
<< "se - set email\n"
<< "sp - set phone\n"
<< "smp - set meeting participator\n"
<< "sms - set meeting start date\n"
<< "sme - set meeting end date\n"
<< "---------------------------------------------------\n\n";
}
} // 开始菜单
void AgendaUI::getOperation() {
std::string op;
if (userName_.empty()) {
while (1) {
bool isValid = true;
cout << "Agenda : ~$ ";
op = getCmd();
if (op == "l") {
userLogIn();
} else if (op == "r") {
userRegister();
} else if (op == "q") {
quitAgenda();
} else {
isValid = false;
}
if (isValid) break;
}
} else {
while (1) {
cout << "Agenda@" << userName_ << " : # ";
op = getCmd();
if (executeOperation(op)) break;
}
}
} // 获取操作指令
std::string AgendaUI::getCmd() {
std::string temp;
cin >> temp;
return temp;
} // Get the input.
bool AgendaUI::executeOperation(std::string op) {
if (op == "o") {
userLogOut();
return true;
} else if (op == "dc") {
deleteUser();
return true;
} else if (op == "lu") {
listAllUsers();
return true;
} else if (op == "cm") {
createMeeting();
return true;
} else if (op == "la") {
listAllMeetings();
return true;
} else if (op == "las") {
listAllSponsorMeetings();
return true;
} else if (op == "lap") {
listAllParticipateMeetings();
return true;
} else if (op == "qm") {
queryMeetingByTitle();
return true;
} else if (op == "qt") {
queryMeetingByTimeInterval();
return true;
} else if (op == "dm") {
deleteMeetingByTitle();
return true;
} else if (op == "da") {
deleteAllMeetings();
return true;
} else if (op == "su") {
setPassword();
return true;
} else if (op == "se") {
setEmail();
return true;
} else if (op == "sp") {
setPhone();
return true;
} else if (op == "smp") {
setParticipator();
return true;
} else if (op == "sms") {
setStartDate();
return true;
} else if (op == "sme") {
setEndDate();
return true;
}
return false;
}
void AgendaUI::userLogIn() {
cout << "\n[log in] [user name] [password]\n"
<< "[log in] ";
std::string name, password;
cin >> name >> password;
if (agendaService_.userLogIn(name, password)) {
userName_ = name;
userPassword_ = password;
cout << "[log in] succeed!\n\n";
} else {
cout << "[error] log in fail!\n\n";
}
startAgenda();
getOperation();
}
void AgendaUI::userRegister() {
cout << "\n[register] [user name] [password] [email] [phone]\n"
<< "[register] ";
std::string name, password, email, phone;
cin >> name >> password >> email >> phone;
if (agendaService_.userRegister(name, password, email, phone)) {
cout << "[register] succeed!\n\n";
} else {
cout << "[error] register fail!\n\n";
}
startAgenda();
getOperation();
}
void AgendaUI::quitAgenda() {
agendaService_.quitAgenda();
cout << "[quit agenda] succeed!\n";
}
void AgendaUI::userLogOut() {
userName_.clear();
userPassword_.clear();
agendaService_.quitAgenda();
startAgenda();
getOperation();
}
void AgendaUI::deleteUser() {
std::string password;
cout << "\nPlease input the password again: ";
cin >> password;
cout <<"\n[delete agenda account] ";
if (agendaService_.deleteUser(userName_, password)) {
userName_.clear();
userPassword_.clear();
cout << "succeed!\n\n";
} else {
cout << "delete agenda account fail!\n\n";
}
startAgenda();
getOperation();
} // 删除账户
void AgendaUI::listAllUsers() {
cout << "\n[list all users]\n\n"
<< "name email phone\n";
std::list<User> tem = agendaService_.listAllUsers();
std::list<User>::iterator p;
for (p = tem.begin(); p != tem.end(); p++) {
cout << std::setiosflags(std::ios::left) << std::setw(8) << p->getName()
<< std::setw(15) << p->getEmail() << p->getPhone() << endl;
}
cout << endl;
startAgenda();
getOperation();
}
void AgendaUI::createMeeting() {
cout << "[create meeting] [title] [participator] "
<< "[start time(yyyy-mm-dd/hh::mm)] "
<< "[end time](yyyy-mm-dd/hh:mm)\n"
<< "[create meeting] ";
std::string title, participator, stime, etime;
cin >> title >> participator >> stime >> etime;
if (agendaService_.createMeeting(userName_, title,
participator, stime, etime)) {
cout << "[create meeting] succeed!\n\n";
} else {
cout << "[error] create meeting fail!\n\n";
}
startAgenda();
getOperation();
} // 添加会议
void AgendaUI::listAllMeetings() {
cout << "\n[list all meetings]\n";
std::list<Meeting> tem = agendaService_.listAllMeetings(userName_);
printMeetings(tem);
startAgenda();
getOperation();
}
void AgendaUI::listAllSponsorMeetings() {
cout << "\n[list all sponsor meetings]\n";
std::list<Meeting> tem = agendaService_.listAllSponsorMeetings(userName_);
printMeetings(tem);
startAgenda();
getOperation();
}
void AgendaUI::listAllParticipateMeetings() {
cout << "\n[list all participate meetings]\n";
std::list<Meeting> tem = agendaService_.listAllParticipateMeetings(userName_);
printMeetings(tem);
startAgenda();
getOperation();
}
void AgendaUI::queryMeetingByTitle() {
cout << "\n[query meeting] [title]:\n"
<< "[query meeting] ";
std::string meeting;
cin >> meeting;
std::list<Meeting> tem = agendaService_.meetingQuery(userName_, meeting);
if (!tem.empty()) {
cout << "\nsponsor participator "
<< "start time end time\n";
for (auto i : tem)
cout << std::setiosflags(std::ios::left)
<< std::setw(15) << i.getSponsor()
<< std::setw(15) << i.getParticipator()
<< std::setw(20) << Date::dateToString(i.getStartDate())
<< Date::dateToString(i.getEndDate()) << endl << endl;
} else {
cout << "\n[error] query meeting fail!\n\n";
} // 会议名不存在
startAgenda();
getOperation();
}
void AgendaUI::queryMeetingByTimeInterval() {
cout << "\n[query meetings] [start time(yyyy- mm-dd/hh:mm)] "
<< "[end time(yyyy-mm-dd/hh:mm)]\n"
<< "[query meetings] ";
std::string stime, etime;
cin >> stime >> etime;
Date start = Date::stringToDate(stime), end = Date::stringToDate(etime);
if (start > end || Date::isValid(start) || Date::isValid(end)) {
// start晚于end或者不合法
cout << "\n[error] query meeting fail!\n\n";
} else {
std::list<Meeting> tem = agendaService_.meetingQuery
(userName_, stime, etime);
// What if not existed?
printMeetings(tem);
}
startAgenda();
getOperation();
}
void AgendaUI::deleteMeetingByTitle() {
cout << "\n[delete meeting] [title]\n" << "[delete meeting] ";
std::string title;
cin >> title;
if (agendaService_.deleteMeeting(userName_, title)) {
cout << "\n[delete meeting by title] succeed!\n\n";
} else {
cout << "\n[error] delete meeting fail!\n\n";
}
startAgenda();
getOperation();
}
void AgendaUI::deleteAllMeetings() {
if (agendaService_.deleteAllMeetings(userName_)) {
cout << "\n[delete all meetings] succeed!\n\n";
} else {
cout << "\n[error] delete meetings fail!\n\n";
}
startAgenda();
getOperation();
}
void AgendaUI::printMeetings(std::list<Meeting> meetings) {
std::list<Meeting>::iterator p;
cout << "\ntitle sponsor participator "
<< "start time end time\n";
for (p = meetings.begin(); p != meetings.end(); p++) {
cout << std::setiosflags(std::ios::left) << std::setw(15) << p->getTitle()
<< std::setw(15) << p->getSponsor()
<< std::setw(15) << p->getParticipator()
<< std::setw(20) << Date::dateToString(p->getStartDate())
<< Date::dateToString(p->getEndDate()) << endl;
}
cout << endl;
} // 打印会议列表
void AgendaUI::setPassword() {
cout << "\n[set password] [new password]\n"
<< "[set password] ";
std::string password;
cin >> password;
if (agendaService_.setPassword(userName_, password))
cout << "[set password] succeed!\n\n";
else
cout << "[error] set password fail!\n\n";
startAgenda();
getOperation();
} // 修改密码
void AgendaUI::setEmail() {
cout << "\n[set email] [new email]\n"
<< "[set email] ";
std::string email;
cin >> email;
if (agendaService_.setEmail(userName_, email))
cout << "[set email] succeed!\n\n";
else
cout << "[error] set email fail!\n\n";
startAgenda();
getOperation();
} // 修改邮箱
void AgendaUI::setPhone() {
cout << "\n[set phone] [new phone]\n"
<< "[set phone] ";
std::string phone;
cin >> phone;
if (agendaService_.setPhone(userName_, phone))
cout << "[set phone] succeed!\n\n";
else
cout << "[error] set phone fail!\n\n";
startAgenda();
getOperation();
} // 修改电话
void AgendaUI::setParticipator() {
cout << "\n[set participator] [title] [new participator]\n"
<< "[set participator] ";
std::string title, participator;
cin >> title >> participator;
if (agendaService_.setParticipator(userName_, title, participator))
cout << "[set participator] succeed!\n\n";
else
cout << "[error] set participator fail!\n\n";
startAgenda();
getOperation();
} // 修改参与者
void AgendaUI::setStartDate() {
cout << "\n[set start date] [title] [new start date]\n"
<< "[set start date] ";
std::string title, stime;
cin >> title >> stime;
if (agendaService_.setStartDate(userName_, title, stime))
cout << "[set start date] succeed!\n\n";
else
cout << "[error] set start date fail!\n\n";
startAgenda();
getOperation();
} // 修改起始时间
void AgendaUI::setEndDate() {
cout << "\n[set end date] [title] [new end date]\n"
<< "[set end date] ";
std::string title, etime;
cin >> title >> etime;
if (agendaService_.setStartDate(userName_, title, etime))
cout << "[set end date] succeed!\n\n";
else
cout << "[error] set end date fail!\n\n";
startAgenda();
getOperation();
} // 修改终止时间