-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPerson.cpp
More file actions
544 lines (470 loc) · 10.6 KB
/
Person.cpp
File metadata and controls
544 lines (470 loc) · 10.6 KB
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
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
#include "Person.h"
#include "User.h"
#include"Menu.h"
vector <string> PersonWord{ "编号","姓名","年龄","性别","电话","数量限制" ,"已借书数目","已借的所有书ID","组别"};
//显示标题
extern User* present_user;
extern Person* present_borrower;
extern Admin* present_admin;
extern vector<Admin> Admins;
extern vector<User> Users;
extern vector<Person>Borrowers;
extern vector<Book> Books;
/// <summary>
/// 显示标题
/// </summary>
void display_person_title() {
cout << "--------------------------------------------------------------------------------"<<endl;
cout << setw(10) << PersonWord[0]
<< setw(10) << PersonWord[1]
<< setw(8) << PersonWord[2]
<< setw(8) << PersonWord[3]
<< setw(15) << PersonWord[4]
<< setw(10) << PersonWord[5]
<< setw(12) << PersonWord[6]
<< setw(10) << PersonWord[8];
cout << endl;
cout << "--------------------------------------------------------------------------------" << endl;
}
//新增一个对象函数
void add_a_person() {
//TODO:增加一个person对象函数
}
Person::Person(const InitPersonStruct& person_list)
{
ID=person_list.ID;//0编号
Name =person_list.Name;//1姓名
Age = person_list.Age;//2年龄
Gender = person_list.Gender;//3性别,男为1,女为0
PhoneNumber = person_list.PhoneNumber;//4电话
BorrowedNumber = person_list.BorrowedNumber;//6已借数目
BooksID=person_list.BooksID;//7接到的书的ID数组
Type=person_list.Type;//8组别,表示人的类型,不同人有不同的借书数目,以及借书的时长
switch (Type)
{
case 0 ://学生
BookNumberLimit = 10;
BorrowDuration = 30 * chrono::hours(24);//30天
break;
case 1://老师
BookNumberLimit = 20;
BorrowDuration = 50 * chrono::hours(24);//50天
break;
default:
BookNumberLimit = 5;
BorrowDuration = 15 * chrono::hours(24);//15天
break;
}
}
//"编号","姓名","年龄","性别","电话","图书借阅数量限制" ,"已借书数目","已借的所有书ID","组别"
void Person::display()
{
string sex;
(Gender) ? (sex = "男") : (sex = "女");
cout << setw(10) << ID
<< setw(10) << Name
<< setw(8) << Age
<< setw(8) << sex
<< setw(15) << PhoneNumber
<< setw(10) << BookNumberLimit
<< setw(12) << BorrowedNumber;
switch (Type)
{
case 0://学生
cout <<setw(10) << "学生";
break;
case 1://老师
cout <<setw(10) << "老师";
break;
default:
cout <<setw(10)<< "其他";
break;
}
cout<< endl;
}
//Person修改函数的接口
void person_modify(Person & person)
{
}
//"编号","姓名","年龄","性别","电话","数量限制" ,"已借书数目","已借的所有书ID","组别"
void Person::modify()
{
size_t i = 0;
int SNumber = 0;
cout << "选择修改相应的序号" << endl;
for (string temp : PersonWord)
{
if (i == 8)
{
continue;
}
cout << "[" << i++ << "]" << ":" << temp << endl;
}
cin >> SNumber;
string temp_name;
int temp_id;
User* temp_user;
switch (SNumber)
{
case 0:
cout << PersonWord[SNumber]<<":"; cin >> temp_id;
temp_user = User::Search_ID(temp_id);
(*temp_user).ID = temp_id;
ID = temp_id;
break;
case 1:
cout << PersonWord[SNumber]<<":"; cin >> temp_name;
temp_user = User::Search_ID(ID);
(*temp_user).Name = temp_name;
Name = temp_name;
break;
case 2:
cout << PersonWord[SNumber] << ":"; cin >> Age;
break;
case 3:
cout << PersonWord[SNumber] <<"(男“1”,女“0”)" << ":"; cin >> Gender;
break;
case 4:
cout << PersonWord[SNumber] << ":"; cin >> PhoneNumber;
break;
case 5:
cout << PersonWord[SNumber] << ":"; cin >> BookNumberLimit;
break;
case 6:
cout << PersonWord[SNumber] << ":"; cin >> BorrowedNumber;
break;
case 7:
SNumber = SNumber + 1;
cout << PersonWord[SNumber] <<"(学生“0”,老师“1”)" << ":"; cin >> Type;
break;
default:
cout << "输入数字错误,请重新选择" << endl;
break;
}
}
void Person::personal_modify()
{
size_t i = 0;
int SNumber = 0;
size_t n = 0;
cout << "选择修改相应的序号" << endl;
for (string temp : PersonWord)
{
i++;
if (i==1)
{
continue;
}
if (i == 8)
{
continue;
}
n++;
cout << "[" << n << "]" << ":" << temp << endl;
}
cin >> SNumber;
string temp_name;
User* temp_user;
switch (SNumber)
{
case 1:
cout << PersonWord[SNumber] << ":"; cin >> temp_name;
temp_user = User::Search_ID(ID);
(*temp_user).Name = temp_name;
Name = temp_name;
break;
case 2:
cout << PersonWord[SNumber] << ":"; cin >> Age;
break;
case 3:
cout << PersonWord[SNumber] << "(男“1”,女“0”)" << ":"; cin >> Gender;
break;
case 4:
cout << PersonWord[SNumber] << ":"; cin >> PhoneNumber;
break;
case 5:
cout << PersonWord[SNumber] << ":"; cin >> BookNumberLimit;
break;
case 6:
cout << PersonWord[SNumber] << ":"; cin >> BorrowedNumber;
break;
case 7:
SNumber = SNumber + 1;
cout << PersonWord[SNumber] << "(学生“0”,老师“1”)" << ":"; cin >> Type;
break;
default:
cout << "输入数字错误,请重新选择" << endl;
break;
}
}
/// <summary>
/// 借书函数
/// </summary>
/// <param name="borrowing_book"></param>
void Person::borrow( Book& borrowing_book)
{
//TODO:后续添加判断是否有书过期,如有过期就不允许借书
//判断当前是否可以借书
if (BorrowedNumber<BookNumberLimit)
{
//再通过book类的借书函数判断当前书是否可以借
if (borrowing_book.borrow())
{
borrowing_book.ReturnTime = borrowing_book.BorrowTime + BorrowDuration;
borrowing_book.BorrowerID = ID;//把借书人的ID传到book中
borrowing_book.Borrower = this;
BooksID.push_back(borrowing_book.ID);
BorrowedNumber++;
cout << "借书成功" << endl;
}
}
else
{
cout << "当前借书数量超出范围,借书操作失败" << endl;
}
}
//还书函数,通过指定书的ID来实现对书的还
void Person::return_book(Book& returning_book)
{
//首先判断书是否是自己借的
bool return_status;
if (BorrowedNumber==0)//当前没有借书
{
return_status = false;
cout << "当前没有借书" << endl;
}
else
{
//判断当前书是否已借
for (vector<int>::iterator iter = BooksID.begin(); iter != BooksID.end(); iter++)
{ //从vector中删除指定的某一个元素
if (*iter == returning_book.ID)
{
BooksID.erase(iter);//把当前书ID删除
BorrowedNumber--;
return_status = true;
//处理book类
returning_book.return_book();
return;
break;
}
}
return_status = false;
cout << "当前的书并没有借" << endl;
}
}
/// <summary>
/// 显示所有已经借过的书
/// </summary>
/// <param name="books"></param>
void Person::display_all_books( vector<Book> &books)
{
cout << "当前已借的书:";
for (auto temp_bookID:BooksID)
{
for (Book temp_book:books)
{
if (temp_book.ID==temp_bookID)
{
cout << "(" << temp_book.ID << ")"
<< temp_book.Title<<" ";
}
}
}
cout << endl;
cout << "--------------------------------------------------------------------------------" << endl;
}
void Person::display_all_books()
{
extern vector <Book>Books;
cout << setw(10) << "ID" << setw(10) << "书名" << setw(25) << "借书时间" << setw(25) << "还书时间" << endl;
cout << "--------------------------------------------------------------------------------" << endl;
for (auto temp_bookID : BooksID)
{
for (Book &temp_book : Books)
{
if (temp_book.ID == temp_bookID)
{
cout <<setw(10)<< temp_book.ID << setw(10)
<< temp_book.Title << setw(10);
time_display(temp_book.BorrowTime);
cout << " ";
time_display(temp_book.ReturnTime);
cout << endl;
}
}
}
cout << endl;
cout << "--------------------------------------------------------------------------------" << endl;
}
/// <summary>
/// 搜索人员ID函数
/// </summary>
/// <param name="personID"></param>
/// <returns>
/// 正确返回true
/// 失败返回false
/// </returns>
bool Person::searchPersonID(int personID)
{
if (personID==ID)
{
return true;
}
else
{
return false;
}
}
bool Person::searchPersonName(string name)
{
if (name==Name)
{
return true;
}
return false;
}
//"编号","姓名","年龄","性别","电话","数量限制" ,"已借书数目","已借的所有书ID","组别"
void Person::save(ofstream &person_file)
{
person_file <<setiosflags(ios::left)
<<setw(8) << ID << " "
<<setw(10) << Name << " "
<<setw(5) << Age << " "
<<setw(15) << PhoneNumber << " "
<<setw(3) << BorrowedNumber << " "
<<setw(3) << Type << " "
<< "/"<<" ";
for (int temp_ID : BooksID) {
person_file << temp_ID << " ";
}
person_file << endl;
}
void Person::read(ifstream& person_file)
{
string temp;
person_file >> ID >> Name >> Age >> PhoneNumber >> BorrowedNumber >> Type>>temp;
if (temp=="/")
{
if (BorrowedNumber>0)
{
for (size_t i = 0; i < BorrowedNumber; i++)
{
int temp_ID;
person_file >> temp_ID;
BooksID.push_back(temp_ID);
}
}
}
switch (Type)
{
case 0://学生
BookNumberLimit = 10;
BorrowDuration = 30 * chrono::hours(24);//30天
break;
case 1://老师
BookNumberLimit = 20;
BorrowDuration = 50 * chrono::hours(24);//50天
break;
default:
BookNumberLimit = 5;
BorrowDuration = 15 * chrono::hours(24);//15天
break;
}
}
bool Person::operator<(const Person& b)
{
if (ID<b.ID)
{
return true;
}
else
{
return false;
}
}
void Person::operator<<( Person& b)
{
InitPersonStruct personlist;
Person temp_Person(personlist);
if (*this < b)
{
return;
}
else
{
temp_Person = b;
b = *this;
*this = b;
}
}
bool Person::operator>(const Person& b)
{
if (ID>b.ID)
{
return true;
}
else
{
return false;
}
}
void Person::display_header()
{
cout << "--------------------------------------------------------------------------------" << endl;
cout << setw(10) << PersonWord[0]
<< setw(10) << PersonWord[1]
<< setw(8) << PersonWord[2]
<< setw(8) << PersonWord[3]
<< setw(15) << PersonWord[4]
<< setw(10) << PersonWord[5]
<< setw(12) << PersonWord[6]
<< setw(10) << PersonWord[8];
cout << endl;
cout << "--------------------------------------------------------------------------------" << endl;
}
void Person::delete_a_Borrower()
{
cout << " 当前为人员信息删除模块 " << endl;
int temp_ID;
cout << "请输入要删除人员的ID(请确保当前人员没有借书):" << endl;
cin >> temp_ID;
for (vector<Person>::iterator iter = Borrowers.begin(); iter != Borrowers.end(); iter++)
{
//从vector中删除指定的某一个元素
if ((*iter).searchPersonID(temp_ID))
{
if ((*iter).BorrowedNumber == 0)
{
Borrowers.erase(iter);//把当前书ID删除
cout << "删除信息成功" << endl;
for (vector<User>::iterator ite = Users.begin(); ite != Users.end(); ite++)
{
if ((*ite).search_ID(temp_ID))
{
Users.erase(ite);
cout << "删除账号成功" << endl;
return;
}
}
cout << "删除账号失败" << endl;
break;
}
else
{
cout << "当前人员已借书,无法删除" << endl;
return;
}
}
}
cout << "未找到ID相关信息,请检查ID是否正确" << endl;
}
void Person::display_all_books_header()
{
cout << "当前已借的书:" << endl;
cout << "--------------------------------------------------------------------------------" << endl;
}
void Person::add_a_Borrower()
{
User_Register();
}