-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBook.cpp
More file actions
650 lines (579 loc) · 12.9 KB
/
Book.cpp
File metadata and controls
650 lines (579 loc) · 12.9 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
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
#include "Book.h"
#include "Admin.h"
vector<string> StatusWord{ "未借","已借","遗失未缴费","遗失已缴费" ,"遗失"};//状态,图书的状态,未借0,已借1,遗失未缴费2,遗失已缴费3,遗失4
vector<string>BookWord{ "编号","标题","作者","出版社","定价","状态","是否可借","借阅时间","归还时间","借书人","借书人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;
void display_book_title() {
cout << "----------------------------------------------------------------------------------------------------------------------------------------------------------------" << endl;
cout << setiosflags(ios::left)
<< setw(10) << BookWord[0]
<< setw(10) << BookWord[1];
cout << setw(8) << BookWord[2];
cout << setw(8) << " ";
cout << setw(15) << BookWord[3]
<< setw(5) << BookWord[4];
cout << setw(8) << BookWord[5];
cout << setw(10) << BookWord[6];
//显示借书时间,还书时间
cout << setw(25) << BookWord[7];
cout << setw(25) << BookWord[8];
cout << setw(8) << BookWord[9];
cout << setw(8) << BookWord[10];
cout << endl;
cout << "----------------------------------------------------------------------------------------------------------------------------------------------------------------" << endl;
}
void Book::display()
{
cout <<setiosflags(ios::left) << setw(10) << ID
<< setw(10) << Title;
int temp_i=0;
for (string author:Author)
{
cout << setw(8) << author;
temp_i++;
if (temp_i>2)//如果有两个以上的作者就省略
{
break;
}
}
if (temp_i==1)//将每一行显示的信息补齐,以保证显示的作者信息上下一致。
{
cout << setw(8)<<" ";
}
if (temp_i==0)
{
cout << setw(8) << " " << setw(8) << " ";
}
cout << setw(15) << Press
<< setw(5) << Price ;
switch (Status)
{
case 0:
cout << setw(8) << StatusWord[Status]; break;
case 1:
cout << setw(8) << StatusWord[Status]; break;
case 2:
cout << setw(8) << StatusWord[Status]; break;
case 3:
cout << setw(8) << StatusWord[Status]; break;
case 4:
cout << setw(8) << StatusWord[Status]; break;
default:
cout<<setw(8)<<"未知"; break;
}
//显示借书状态
if (BorrowStatus)
{
cout << setw(10) << "可借";
}
else
{
cout << setw(10) << "不可借";
}
//显示借书时间,还书时间
if (BorrowStatus==0)
{
time_display(BorrowTime);
cout << setw(6) << " ";
time_display(ReturnTime);
cout << setw(6) << " ";
cout << setw(8) << Borrower->Name;//借书人的名字
cout << setw(8) << BorrowerID;
}
else
{
cout << setw(25) << " ";
cout << setw(25) << " ";
cout << setw(8) << " ";
}
cout << endl;
cout << "----------------------------------------------------------------------------------------------------------------------------------------------------------------" << endl;
}
void Book::modify()
{
size_t i;
int SNumber;
while (true)
{
i = 0;
cout << "选择修改相应的序号" << endl;
for (string temp : BookWord)
{
if (i == 5)
{
break;
}
cout << "[" << i++ << "]" << ":" << temp << endl;
}
cout << "[" << i++ << "]" << ":" << "退出" << endl;
cin >> SNumber;
//"编号","标题","作者","出版社","定价","状态","是否可借","借阅时间","归还时间","借书人","借书人ID"
switch (SNumber)
{
case 0:
cout << "请确保修改后的ID没有被占用" << endl;
cout << BookWord[SNumber] << ":";
cin >> ID;
break;
case 1:
cout << BookWord[SNumber] << ":";
cin >> Title;
break;
case 2:
cout << BookWord[SNumber] << ":";
for (string temp_author : Author) {
cout << "请输入:";
cin >> temp_author;
}
break;
case 3:
cout << BookWord[SNumber] << ":";
cin >> Press;
break;
case 4:
cout << BookWord[SNumber] << ":";
cin >> Price;
break;
case 5:
return;
break;
default:
cout << "输入序号错误,请重试" << endl;
break;
}
}
}
bool Book::borrow()
{
chrono::system_clock::time_point* p_temp_time;
//先判断是否可以借
if (BorrowStatus)
{
BorrowStatus = false;//将可借状态转为不可借
Status = 1;
//先得到当前的借阅时间
p_temp_time = get_Now();
BorrowTime = *p_temp_time;
delete(p_temp_time);//释放时间内存
return true;
}
else
{
cout << "当前不可借," << setw(10) << "原因:" << StatusWord[Status] << endl;
if (Status == 1)
{
cout << BookWord[8] << ":"; time_display(ReturnTime);//如果已经借走就显示归还时间
cout << endl;
}
return false;
}
}
/// <summary>
/// 借书函数
/// 必须保证借书人ID可以找到,在使用前需要判断
/// </summary>
/// <param name="borrowerID"></param>
/// <returns>
/// 返回借书的情况
/// </returns>
bool Book::borrow(int borrowerID)
{
chrono::system_clock::time_point* p_temp_time;
//先判断是否可以借
if (BorrowStatus)
{
BorrowStatus = false;//将可借状态转为不可借
Status = 1;
//先得到当前的借阅时间
BorrowerID = borrowerID;
p_temp_time = get_Now();
BorrowTime = *p_temp_time;
delete(p_temp_time);//释放时间内存
return true;
}
else
{
cout << "当前不可借,"<<setw(10) << "原因:" << StatusWord[Status] << endl;
if (Status==1)
{
cout << BookWord[8] << ":"; time_display(ReturnTime);//如果已经借走就显示归还时间
cout << endl;
}
return false;
}
}
bool Book::borrow(Person borrower)
{
chrono::system_clock::time_point* p_temp_time;
//先判断是否可以借
if (BorrowStatus)
{
BorrowStatus = false;//将可借状态转为不可借
Status = 1;
//先得到当前的借阅时间
BorrowerID = borrower.ID;
p_temp_time = get_Now();
BorrowTime = *p_temp_time;
delete(p_temp_time);//释放时间内存
return true;
}
else
{
cout << "当前不可借," << setw(10) << "原因:" << StatusWord[Status] << endl;
if (Status == 1)
{
cout << BookWord[8] << ":"; time_display(ReturnTime);//如果已经借走就显示归还时间
cout << endl;
}
return false;
}
}
/// <summary>
/// 还书函数
///
/// </summary>
void Book::return_book()
{
if (Status==1)
{
Status = 0;
BorrowStatus = 1;
chrono::system_clock::time_point* p_temp_time = get_Now();
if (BorrowTime>*p_temp_time)
{
cout << "当前还书时间已超出,请缴费" << endl;
}
else
{
cout << "还书成功" << endl;
}
}
}
Book::Book(const InitBookStruct& book_list)
{
ID = book_list.ID;//由bookpackage创建
Title = book_list.Title;
Author = book_list.Author;
Press = book_list.Press;
Price = book_list.Price;
Status = book_list.Status;
BorrowStatus = book_list.BorrowStatus;
if (BorrowStatus==0)//如果已经有人借了
{
BorrowTime = book_list.BorrowTime;
ReturnTime = book_list.ReturnTime;
BorrowerID = book_list.BorrowerID;
}
}
void Book::save(ofstream& book_file)
{
book_file << setiosflags(ios::left) << setw(10) << ID
<< setw(10) << Title;
int temp_i = 0;
book_file << " / ";
for (string author : Author)
{
book_file << setw(8) << author;
}
book_file << " / ";
if (temp_i == 1)//将每一行显示的信息补齐,以保证显示的作者信息上下一致。
{
book_file << setw(8) << " ";
}
if (temp_i == 0)
{
book_file << setw(8) << " " << setw(8) << " ";
}
book_file << setw(15) << Press
<< setw(5) << Price;
book_file << setw(8) << Status;
//显示借书状态
book_file << setw(8)<<BorrowStatus;
//显示借书时间,还书时间
if (BorrowStatus == 0)
{
time_display_file(BorrowTime,book_file);
book_file << setw(6) << " ";
time_display_file(ReturnTime,book_file);
book_file << setw(6) << " ";
book_file << setw(8) << Borrower->Name;//借书人的名字
book_file << setw(8) << BorrowerID;
}
else
{
book_file << setw(5) << "*";
}
book_file << endl;
}
void Book::read(ifstream& book_file)
{
//读取ID与标题
book_file >> ID
>> Title;
//设置一个temp用来判断当前的字符
string temp_string,temp_string1;
book_file >> temp_string;
if (temp_string=="/")
{
while (1)//用来读取作者,直到遇到“/”
{
book_file >> temp_string;
if (temp_string == "/")
{
break;
}
else
{
Author.push_back(temp_string);
}
}
}
book_file >> Press
>> Price
>> Status
>> BorrowStatus;//读取出版社,价格以及状态
if (BorrowStatus==1)//没人借,就不读取
{
//book_file.ignore();//直接到下一行
book_file >> temp_string;
}
else
{
tm borrowed_time{}, return_time{};
//book_file >> get_time(&borrowed_time, "%y-%m-%d") >> get_time(&borrowed_time, "%H-%M-%S")
// >> get_time(&return_time, "%y-%m-%d") >> get_time(&return_time, "%H-%M-%S");
book_file >> temp_string >> temp_string1;
string ss,ss1;
ss = temp_string + " " + temp_string1;
stringstream borrow_time_string(ss);
book_file >> temp_string >> temp_string1;
ss1 = temp_string + " " + temp_string1;
stringstream return_time_string(ss1);
borrow_time_string >> get_time(&borrowed_time, "%Y-%m-%d %H:%M:%S");
return_time_string >> get_time(&return_time, "%Y-%m-%d %H:%M:%S");
//std::stringstream ss;
//ss << "2021-01-06 18:47:35";
//std::tm tm{};
//ss >> std::get_time(&tm, "%Y-%m-%d %H:%M:%S");
BorrowTime = chrono::system_clock::from_time_t(mktime(&borrowed_time));
ReturnTime = chrono::system_clock::from_time_t(mktime(&return_time));
book_file >> BorrowerName
>> BorrowerID;
//cout << tp.time_since_epoch().count() << endl;
//book_file >> temp_string>>temp_string1;//分别读取日期时间
//temp_string = temp_string +" "+ temp_string1;//将日期与时间组合
//int len = temp_string.length();
//const char* d = " -:";
//char* p;
//char* s=new char[len+1];
//char* ptr=NULL;
//strcpy_s(s, len+1,temp_string.c_str());
//p = strtok_s(s, d,&ptr);
//tm temp_time;
//
//int n = 0;
//while (p!=NULL)
//{
// stringstream temp_sstream(p);
// switch (n++)
// {
// case 0 :
// temp_sstream >> temp_time.tm_year;
// cout << temp_time.tm_year;
// break;
// case 1:
// temp_sstream >> temp_time.tm_mon;
// cout << temp_time.tm_mon;
// break;
// case 2:
// temp_sstream >> temp_time.tm_wday;
// cout << temp_time.tm_wday;
// break;
// case 3:
// temp_sstream >> temp_time.tm_hour;
// cout << temp_time.tm_hour;
// break;
// case 4:
// temp_sstream >> temp_time.tm_min;
// cout << temp_time.tm_min;
// break;
// case 5:
// temp_sstream >> temp_time.tm_sec;
// cout << temp_time.tm_sec;
// break;
// default:
// break;
// }
// cout << p << endl;
// p = strtok_s(NULL, d,&ptr);
//}
}
}
/// <summary>
/// 搜索函数,用于判断ID与当前图书是否一致
/// </summary>
/// <param name="图书的ID"></param>
/// <returns>
/// 搜索是否成功
/// </returns>
bool Book::searchBookID(int bookID)
{
if (bookID==ID)
{
return true;
}
else
{
return false;
}
}
/// <summary>
/// 判断是否可以借
/// </summary>
/// <returns>
/// 可以:true
/// 不可以:false
/// </returns>
bool Book::borrowEnable()
{
if (BorrowStatus)
{
return true;//可以借
}
return false;
}
bool Book::operator<(const Book& b)
{
if (this->ID<b.ID)
{
return true;
}
return false;
}
bool Book::operator>(const Book& b)
{
if (this->ID>b.ID)
{
return true;
}
return false;
}
void Book::operator<<(Book& b)
{
if (*this<b)
{
return;
}
else
{
InitBookStruct booklist;
Book temp_book(booklist);
temp_book = *this;
*this = b;
b = temp_book;
}
}
Book* Book::Search_Book_ID(int id)
{
for (auto& temp_book : Books)
{
if (temp_book.searchBookID(id))
{
return &temp_book;
}
}
cout << "未找到书籍" << endl;
return NULL;
}
bool Book::Borrow_Book(int id)
{
return false;
}
void Book::Return_a_Book()
{
cout << " 当前为还书模块 " << endl;
int temp_ID;//暂存ID
int temp_BookID;
int temp_Number;//序号
bool searchStatus = false;
temp_ID = present_borrower->ID;
//得到当前的序号
temp_Number = searchPerson(Borrowers, temp_ID);
//如果没有借书直接结束
if (Borrowers[temp_Number].BooksID.size() == 0)
{
cout << "当前没有借书" << endl;
return;
}
if (temp_Number >= 0)
{
display_person_title();//显示表头
Borrowers[temp_Number].display();//显示信息
Borrowers[temp_Number].display_all_books(Books);
cout << "请检查信息是否正确" << endl;
cout << "请输入要还书的ID:";
cin >> temp_BookID;
//检查当前书的ID是否可以找到
for (Book& temp_book : Books)
{
if (temp_book.searchBookID(temp_BookID))
{
searchStatus = true;
Borrowers[temp_Number].return_book(temp_book);
//显示当前的信息
cout << "请检查以下信息是否正确" << endl;
display_book_title();
temp_book.display();
display_person_title();
Borrowers[temp_Number].display();
Borrowers[temp_Number].display_all_books(Books);
break;
}
}
//找到了书
if (searchStatus == true)
{
}
//没有找到
else
{
cout << "没有找到对应书ID,请重试" << endl;
}
}
//没有找到ID
else
{
cout << "没有找到对应ID,请重试" << endl;
}
}
/// <summary>
/// 在一个借书人vector中找到同名ID以实现对于借书人的关联
/// </summary>
/// <param name="借书人容器"></param>
void Book::BulidRelationship(vector <Person>& borrowers)
{
if (BorrowStatus==false)
{
bool build_realationship_success = false;
for (Person& temp_person : borrowers)
{
if (temp_person.ID == ID)
{
Borrower = &temp_person;
BorrowerName = temp_person.Name;
build_realationship_success = true;
break;
}
}
if (!build_realationship_success)
{
cout << "关系建立不成功,未找到ID:" << BorrowerID << "姓名:" << BorrowerName << endl;
}
}
}