-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathReaderOperation.cpp
More file actions
612 lines (544 loc) · 18.4 KB
/
ReaderOperation.cpp
File metadata and controls
612 lines (544 loc) · 18.4 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
#include"ReaderOperation.h"
string getPasswd() {
string passwd;
#ifdef _WIN32
char ch;
while ((ch = _getch()) != '\r') { // 读取键盘输入直到按下回车键
if (ch == '\b') { // 处理退格键
if (!passwd.empty()) {
passwd.pop_back();
cout << "\b \b";
}
}
else {
passwd.push_back(ch);
cout << '*';
}
}
#endif
return passwd;
}
void readerConstructed(string username) {//初始化一个user,从文件读数据;
ifstream readerFile(USER_DATA_PATH+username + ".txt");
if (readerFile) {
int bookId;
long long borrowedTime;
int i=0;
while (readerFile >> bookId>> borrowedTime) {//读入,判断异常还书
borrowedBooks[i].bookId=bookId;
borrowedBooks[i].borrowedTime = borrowedTime;
if (!isRightCommit(borrowedTime)) {
recordWrongCommit(username,borrowedBooks[i]);
}
i++;
}
readerFile.close();
bookNumber = i;//读入借阅单,记下本数;
}
}
void constructedAll() {
clearTxt(WRONG_COMMIT);
User users[100];
ifstream userFile(USER_PATH);
int numUsers = 0;
string line;
// 读取文件内容并解析用户信息
while (getline(userFile, line)) {
//录入user信息
size_t delimiterPos = line.find(' ');
string temp = line.substr(delimiterPos + 1);
size_t delimiterPos2 = temp.find(' ');
if (delimiterPos != string::npos) {
users[numUsers].username = line.substr(0, delimiterPos);
readerConstructed(users[numUsers].username);//初始化用每个户库
numUsers++;
//录入user信息
}
}
userFile.close();
////文件读入到users[]
//for (int i = 0; i < numUsers; i++) {
//}
}
void borrowBook(string username, int bookId) {
// Check if the book exists in the database
DATA_BASE_PATH;//Library path
Book* thisbooks = getBooks();
int numBooks = getNumBooks();
for (int i = 0; i < numBooks; i++) {
if (books[i].bookId == bookId&& books[i].quantity>0) {
// Book found in the database, add it to the reader's borrowed books
borrowedBooks[bookNumber].bookId=bookId;
borrowedBooks[bookNumber].borrowedTime = time(0);
bookNumber++;
saveData(username);//更新到username的数据库内
// sortBookIds(username);
removeBook(bookId);//remove from library
cout << "Book borrowed successfully." << endl;
return;
}
}
cout << "Book not found in the database." << endl;
}
bool returnBook(string username, int bookId) {
bookId--;
// Check if the book exists in the database
Book* thisbooks = getBooks();
int bookIdTemp;
for (int j = 0; j < numBooks; j++) {
if (books[j].bookId == borrowedBooks[bookId].bookId) {
// Book found in the database, add it
borrowedBooks[bookId].bookId = 0;
borrowedBooks[bookId].borrowedTime = 0;
bookIdTemp = books[j].bookId;
numBooks--;
bookNumber--;
break;
}
}
saveData(username);//更新到username的数据库内
bool judge = addBook(bookIdTemp);//add to library
if (judge)
cout << "Book returned successfully." << numBooks << " " << bookNumber << endl;
return true;
cout << "Book returned failed." << endl;
return false;
cout << "Book not borrowed by the reader." << endl;
return false;
}
//bool returnBook(string username, int bookId) {
// // Check if the book exists in the database
// Book* thisbooks = getBooks();
// for (int i = 0; i < bookNumber; i++) {
// if (borrowedBooks[i].bookId == bookId) {
// for (int j = 0; j < numBooks; j++) {
// if (books[j].bookId == bookId) {
// // Book found in the database, add it
// borrowedBooks[i].bookId = 0;
// borrowedBooks[i].borrowedTime = 0;
// numBooks--;
// bookNumber--;
// break;
// }
// }
// saveData(username);//更新到username的数据库内
// bool judge = addBook(bookId);//add to library
// if (judge)
// {
// cout << "Book returned successfully."<< numBooks<<" "<< bookNumber << endl;
// return true;
// }
// cout << "Book returned failed." << endl;
// return false;
//
//
// }
//
// }
//
// cout << "Book not borrowed by the reader." << endl;
// return false;
//}
bool printBorrowedBooks(string username) {
readerConstructed(username);//初始化用每个户库
// USER_DATA_PATH + username + ".txt"
// Print the borrowed books of the reader
if (bookNumber<=0) {
cout << "-------------------------------------------------------------------------------------------\n";
cout << "No books borrowed by the reader." << endl;
cout << "-------------------------------------------------------------------------------------------\n";
return false;
}
// cout << "Borrowed Books: " << endl;
cout << "-------------------------------------------------------------------------------------------------------------------------------------\n";
for (int i = 0; i < bookNumber; i++) {
cout << "条目编号: " << i + 1;//输出序号
printSimpleItem(borrowedBooks[i].bookId);//格式输出借阅信息
cout << "借阅时间: " <<setw(10)<<showTime(borrowedBooks[i].borrowedTime) << endl<<endl;
}
cout << "-------------------------------------------------------------------------------------------------------------------------------------\n"; return true;
// clearDatabase(username);
}
bool saveData(string username) {//写到txt中
// Save reader's borrowed books to file
sortBookIds(username);
ofstream readerFile(USER_DATA_PATH+username + ".txt");
if (!readerFile) {
cout << "Failed to save reader's data."<< endl;
return false;
}
for (BorrowedBooks toGetBookId : borrowedBooks) {
if(toGetBookId.bookId !=0)
readerFile << toGetBookId.bookId <<" "<< toGetBookId.borrowedTime<< endl;
}
readerFile.close();
return true;
}
bool cmp(BorrowedBooks a, BorrowedBooks b) {
return a.bookId < b.bookId;
}
bool sortBookIds(string username) {
// Sort the book IDs in ascending order
sort(borrowedBooks, borrowedBooks+bookNumber,cmp);
return true;
}
bool clearDatabase(string username) {
for (int i = 0; i < bookNumber; i++) {
borrowedBooks[i].bookId = 0;
borrowedBooks[i].borrowedTime = 0;
}
bookNumber = 0;
saveData(username);
return true;
}
void addTxtFile(const string& filename) {
time_t ts = time(NULL);
ofstream outFile(filename, ios::app);//| ios::trunc
if (!outFile) {
cerr << "Error opening file: " << filename << endl;
return;
}
//Enter text for the file (Enter '0' on a new line to finish)
string line;
outFile << "-----"<<ctime(&ts);
while (getline(cin, line) && line != "0") {
outFile << line << '\n';
}
outFile << '\n';
outFile.close();
cout << "已添加!\n";
}
void editTxtFile(const string& filename) {
time_t ts = time(NULL);
ofstream outFile(filename, ios::out | ios::trunc);
if (!outFile) {
cerr << "Error opening file: " << filename << endl;
return;
}
//Enter text for the file (Enter '0' on a new line to finish)
string line;
outFile << "-----" << ctime(&ts);
while (getline(cin, line) && line != "0") {
outFile << line << '\n';
}
outFile << '\n';
outFile.close();
cout << "已发布!\n";
}
void printTxtFile(const string& filename) {
ifstream inFile(filename, ios::in);
if (!inFile) {
cerr << "Error opening file: " << filename << endl;
return;
}
string line;
// cout << "Content of the file:\n";
while ( getline(inFile, line)) {
cout << line << '\n';
}
inFile.close();
}
bool searchBooks(const string& searchTerm) {
// Search for books by title or author in the database
int numBooks = getNumBooks();
loadDatabase();
int i = 0;
for (i = 0; i < numBooks; i++) {
if (AllisNum(searchTerm)) {
if (books[i].bookName == searchTerm || books[i].authorName == searchTerm || books[i].bookId == stoi(searchTerm)) {
cout << "-------------------------" << endl;
cout << "Book ID: " << books[i].bookId << endl;
cout << "Book Name: " << books[i].bookName << endl;
cout << "Author: " << books[i].authorName << endl;
cout << "Quantity: " << books[i].quantity << endl;
cout << "-------------------------" << endl;
}
}
else {
if (books[i].bookName == searchTerm || books[i].authorName == searchTerm) {
cout << "-------------------------" << endl;
cout << "Book ID: " << books[i].bookId << endl;
cout << "Book Name: " << books[i].bookName << endl;
cout << "Author: " << books[i].authorName << endl;
cout << "Quantity: " << books[i].quantity << endl;
cout << "-------------------------" << endl;
}
}
}
}
const int MAX_USERS = 100;
bool passwdChange(string username,string newPassword) {
ifstream userFile(USER_PATH);
User users[MAX_USERS];
int numUsers = 0;
string line;
// 读取文件内容并解析用户信息
while (getline(userFile, line)) {
if (numUsers >= MAX_USERS) {
cout << "达到最大用户数" << endl;
break;
}
//录入user信息
size_t delimiterPos = line.find(' ');
string temp = line.substr(delimiterPos + 1);
size_t delimiterPos2 = temp.find(' ');
if (delimiterPos != string::npos) {
users[numUsers].username = line.substr(0, delimiterPos);
users[numUsers].password = temp.substr(0, delimiterPos2);
users[numUsers].userCatgory = temp.substr(delimiterPos2 + 1);
numUsers++;
//录入user信息
}
}
userFile.close();
// 查找指定用户名并更改密码
for (int i = 0; i < numUsers; i++) {
if (users[i].username == username) {
users[i].password = newPassword;
break;
}
}
// 保存更改后的用户信息回到文件
ofstream outFile(USER_PATH);
for (int i = 0; i < numUsers; i++) {
outFile << users[i].username <<" " << users[i].password <<" " << users[i].userCatgory << '\n';
}
outFile.close();
return true;
}
void printUsers() {
ifstream userFile(USER_PATH);
User users[MAX_USERS];
int numUsers = 0;
string line;
// 读取文件内容并解析用户信息
while (getline(userFile, line)) {
if (numUsers >= MAX_USERS) {
cout << "达到最大用户数" << endl;
break;
}
//录入user信息
size_t delimiterPos = line.find(' ');
string temp = line.substr(delimiterPos + 1);
size_t delimiterPos2 = temp.find(' ');
if (delimiterPos != string::npos) {
users[numUsers].username = line.substr(0, delimiterPos);
users[numUsers].password = temp.substr(0, delimiterPos2);
users[numUsers].userCatgory = temp.substr(delimiterPos2 + 1);
numUsers++;
//录入user信息
}
}
userFile.close();
//文件读入到users[]
for (int i = 0; i < numUsers; i++) {
cout << endl;
cout << setw(10) << users[i].username <<" ( " << users[i].userCatgory << " )";
cout <<setw(10) << "已借阅书籍如下:\n";
readerConstructed(users[i].username);//初始化用每个户库
printBorrowedBooks(users[i].username);
cout << endl << endl;
readerConstructed(currentUser.username);//初始化用每个户库
}
return;
}
void addUser(string username, string password) {
ifstream userFile(USER_PATH);
User users[MAX_USERS];
int numUsers = 0;
string line;
// 读取文件内容并解析用户信息
while (getline(userFile, line)) {
if (numUsers >= MAX_USERS) {
cout << "达到最大用户数" << endl;
break;
}
//录入user信息
size_t delimiterPos = line.find(' ');
string temp = line.substr(delimiterPos + 1);
size_t delimiterPos2 = temp.find(' ');
if (delimiterPos != string::npos) {
users[numUsers].username = line.substr(0, delimiterPos);
users[numUsers].password = temp.substr(0, delimiterPos2);
users[numUsers].userCatgory=temp.substr(delimiterPos2+1);
numUsers++;
//录入user信息
}
}
userFile.close();
int i;
for (i = 0; i < numUsers; i++) {
if (users[i].username == username) {
cout << "Have same users.";
return;
}
}
// Add a new user to the database
ofstream userFile1(USER_PATH, ios::app);
if (!userFile1) {
cout << "Failed to add user." << endl;
return;
}
userFile1 << username << " " << password << " " << "user" << endl;
ofstream file(USER_DATA_PATH+username+".txt");//初始化文件
file.close();
userFile1.close();
cout << "User added successfully." << endl;
return;
}
bool deleteUser(string username) {
// Delete a user from the database
ifstream userFile(USER_PATH);
User users[MAX_USERS];
int numUsers = 0;
string line;
// 读取文件内容并解析用户信息
while (getline(userFile, line)) {
if (numUsers >= MAX_USERS) {
cout << "达到最大用户数" << endl;
break;
}
//录入user信息
size_t delimiterPos = line.find(' ');
string temp = line.substr(delimiterPos + 1);
size_t delimiterPos2 = temp.find(' ');
if (delimiterPos != string::npos) {
users[numUsers].username = line.substr(0, delimiterPos);
users[numUsers].password = temp.substr(0, delimiterPos2);
users[numUsers].userCatgory = temp.substr(delimiterPos2 + 1);
numUsers++;
//录入user信息
}
}
userFile.close();
// 查找指定用户名并删除
int i,num;
for (i = 0; i < numUsers; i++) {
if (users[i].username == username) {
users[i].username = "";
users[i].password = "";
users[i].userCatgory="";
string path = USER_DATA_PATH + username + ".txt";
const char* cstr = path.c_str();
remove(cstr);//删除用户文件
num = i;
break;
}
}
if (i >= numUsers)cout << "No matched users.";
// 保存更改后的用户信息回到文件
ofstream outFile(USER_PATH);
for ( i = 0; i < numUsers; i++) {
if(num!=i)
outFile << users[i].username << ' ' << users[i].password <<' '<< users[i].userCatgory << '\n';
}
outFile.close();
return true;
}
void addSU() {
ifstream userFile(USER_PATH);
User users[MAX_USERS];
int numUsers = 0;
string line;
// 读取文件内容并解析用户信息
while (getline(userFile, line)) {
if (numUsers >= MAX_USERS) {
cout << "达到最大用户数" << endl;
break;
}
//录入user信息
size_t delimiterPos = line.find(' ');
string temp = line.substr(delimiterPos + 1);
size_t delimiterPos2 = temp.find(' ');
if (delimiterPos != string::npos) {
users[numUsers].username = line.substr(0, delimiterPos);
users[numUsers].password = temp.substr(0, delimiterPos2);
users[numUsers].userCatgory = temp.substr(delimiterPos2 + 1);
numUsers++;
//录入user信息
}
}
userFile.close();
for (int i = 0; i < numUsers; i++) {
cout << endl;
cout << "\t"<<users[i].username << " ( " << users[i].userCatgory << " )\n";
}
string username,newCatdory;
cout << "-请选择要更改的用户: (输入0返回上级) ";
cin >> username;
if (username.length() == 1)
if (stoi(username) == 0) {
back(false);
return;
}
cout << "--请选择要更改的身份: ";
int choice;
cout << "\n---1.图书管理员 \n---2.普通用户 \n---3.超级用户" << endl;
cin >> choice;
switch (choice) {
case 1:
newCatdory = "librarian";
break;
case 2:
newCatdory = "user";
break;
case 3:
newCatdory = "admin";
break;
default:
cout << "----输入错误" << endl;
}
int i;
for (i = 0; i < numUsers; i++) {
if (users[i].username == username) {
users[i].userCatgory =newCatdory;
break;
}
}
if (i >= numUsers)cout << "No matched users.";
// 保存更改后的用户信息回到文件
ofstream outFile(USER_PATH);
for (i = 0; i < numUsers; i++) {
outFile << users[i].username << ' ' << users[i].password <<' '<< users[i].userCatgory << '\n';
}
outFile.close();
return;
}
bool isRightCommit(time_t lastExecutionTime) {
time_t days = (time(0) - lastExecutionTime) / (60);//(60 * 60 * 24)天数,用int取整
if (days > 5)return false;
return true;
}
string showTime(long long time1) {
time_t time = static_cast<time_t>(time1);//格式转化
tm* timeinfo = localtime(&time);
char buffer[11];
strftime(buffer, sizeof(buffer), "%Y/%m/%d", timeinfo);
return buffer;
}
void recordWrongCommit(string username,BorrowedBooks borrowedBook) {
// string WRONG_COMMIT;//异常还书记录
ofstream outputFile(WRONG_COMMIT,ios::app);//增加式写入
if (outputFile.is_open()) {
outputFile << username<<" "<< borrowedBook.bookId<<" "<< showTime(borrowedBook.borrowedTime)<<endl;
outputFile.close();
}
}
void printWrongCommit() {
ifstream inFile(WRONG_COMMIT, ios::in);
if (!inFile) {
cerr << "Error opening file: " << WRONG_COMMIT << endl;
return;
}
string line;
string userename;
int bookid;
string borredtime;
while (inFile>>userename) {
inFile >> bookid >> borredtime;
cout <<setw(10)<< userename <<" 借阅日期"<< setw(12) << borredtime;
printSimpleItem(bookid);
cout<< endl<<endl;
}
inFile.close();
}