-
Notifications
You must be signed in to change notification settings - Fork 0
/
LibraryManager.cpp
481 lines (424 loc) · 15 KB
/
LibraryManager.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
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
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <fstream>
#include <thread>
#include <chrono>
using namespace std;
class LibraryManager {
private:
string book_name;
vector<string> books;
const string books_txt = "books.txt";
string last_operation;
string last_book_name;
string last_new_name;
void load_books() {
ifstream file(books_txt);
if (!file.is_open()) {
cerr << "Error opening the file!" << endl;
return;
}
string line;
while (getline(file, line)) {
books.push_back(line);
}
file.close();
}
void save_books_to_file() {
ofstream file(books_txt);
if (!file.is_open()) {
cerr << "Unable to open file!" << endl;
return;
}
for (const auto& book : books) {
file << book << endl;
}
cout << "Books saved successfully!" << endl;
this_thread::sleep_for(chrono::seconds(1));
file.close();
}
public:
LibraryManager() {
load_books();
}
void main() {
while (true) {
int choice;
cout << "****************************************************************\n"
<< "* *\n"
<< "* Welcome To The Library Manager *\n"
<< "* *\n"
<< "****************************************************************\n"
<< "* *\n"
<< "* What would you like to do? *\n"
<< "* *\n"
<< "* 1. Add Books *\n"
<< "* 2. Delete Books *\n"
<< "* 3. Display Books *\n"
<< "* 4. Update Books *\n"
<< "* 5. Save Books *\n"
<< "* 6. Search Books *\n"
<< "* 7. Sort Books *\n"
<< "* 8. Reload Books From File *\n"
<< "* 9. Undo Last Operation *\n"
<< "* 10. Display Statistics *\n"
<< "* 11. Exit the Program *\n"
<< "* *\n"
<< "****************************************************************\n"
<< endl << "> ";
cin >> choice;
if (cin.fail()) {
cin.clear();
cin.ignore(numeric_limits<streamsize>::max(), '\n');
cout << "Invalid input! Please enter a number." << endl;
continue;
}
cin.ignore();
switch (choice) {
case 1:
add_books();
break;
case 2:
delete_books();
break;
case 3:
display_books();
break;
case 4:
update_books();
break;
case 5:
save_books();
break;
case 6:
search_books();
break;
case 7:
sort_books();
break;
case 8:
reload_books();
break;
case 9:
undo_last_operation();
break;
case 10:
display_statistics();
break;
case 11:
cout << "\nExiting...\n";
return;
default:
cout << "Invalid choice! Please enter a number between 1 and 11." << endl;
}
}
}
#pragma region Helper functions
void add_books() {
while (true) {
cout << "Please enter the name of the book:" << endl;
getline(cin, book_name);
if (book_name.empty()) {
cout << "Book name cannot be empty! Please try again." << endl;
continue;
}
books.push_back(book_name);
last_operation = "add";
last_book_name = book_name;
cout << "\"" << book_name << "\" added successfully." << endl;
this_thread::sleep_for(chrono::seconds(1));
char query;
cout << "Type 'y' to add another book, or anything else to go back." << endl;
cin >> query;
cin.ignore();
if (query != 'y') {
break;
}
}
}
void save_books() {
if (books.empty()) {
cout << "No books to save!" << endl;
this_thread::sleep_for(chrono::seconds(1));
return;
}
save_books_to_file();
}
void delete_books() {
if (books.empty()) {
cout << "No books in the library!" << endl;
return;
}
cout << "Please enter the name of the book you want to delete:" << endl;
getline(cin, book_name);
auto it = find(books.begin(), books.end(), book_name);
if (it != books.end()) {
books.erase(it);
last_operation = "delete";
last_book_name = book_name;
cout << "\"" << book_name << "\" deleted successfully." << endl;
} else {
cout << "Book not found!" << endl;
}
this_thread::sleep_for(chrono::seconds(1));
}
void display_books() {
if (books.empty()) {
cout << "No books in the library!" << endl;
return;
}
cout << "\nBooks in the library:" << endl;
cout << "------------------------" << endl;
for (const auto& book : books) {
cout << book << endl;
}
cout << "------------------------" << endl;
cout << "\nReturning to main menu..." << endl;
this_thread::sleep_for(chrono::seconds(2));
}
void update_books() {
if (books.empty()) {
cout << "No books in the library!" << endl;
return;
}
cout << "Please enter the name of the book you want to update:" << endl;
getline(cin, book_name);
cout << "Enter the new name of the book:" << endl;
string new_name;
getline(cin, new_name);
if (new_name.empty()) {
cout << "New book name cannot be empty! Please try again." << endl;
return;
}
auto it = find(books.begin(), books.end(), book_name);
if (it != books.end()) {
last_operation = "update";
last_book_name = book_name;
last_new_name = new_name;
*it = new_name;
cout << "\"" << book_name << "\" updated successfully to \"" << new_name << "\"." << endl;
} else {
cout << "Book not found!" << endl;
}
this_thread::sleep_for(chrono::seconds(1));
}
void search_books() {
cout << "Please enter the name of the book you want to search for:" << endl;
getline(cin, book_name);
auto it = find(books.begin(), books.end(), book_name);
if (it != books.end()) {
cout << "Book \"" << book_name << "\" found in the library." << endl;
} else {
cout << "Book not found!" << endl;
}
this_thread::sleep_for(chrono::seconds(1));
}
void sort_books() {
sort(books.begin(), books.end());
cout << "Books sorted alphabetically." << endl;
this_thread::sleep_for(chrono::seconds(1));
}
void reload_books() {
books.clear();
load_books();
cout << "Books reloaded from the file." << endl;
this_thread::sleep_for(chrono::seconds(1));
}
void undo_last_operation() {
if (last_operation.empty()) {
cout << "No operations to undo!" << endl;
this_thread::sleep_for(chrono::seconds(1));
return;
}
if (last_operation == "add") {
auto it = find(books.begin(), books.end(), last_book_name);
if (it != books.end()) {
books.erase(it);
cout << "Undo successful: \"" << last_book_name << "\" has been removed." << endl;
}
} else if (last_operation == "delete") {
books.push_back(last_book_name);
cout << "Undo successful: \"" << last_book_name << "\" has been restored." << endl;
} else if (last_operation == "update") {
auto it = find(books.begin(), books.end(), last_new_name);
if (it != books.end()) {
*it = last_book_name;
cout << "Undo successful: \"" << last_new_name << "\" has been reverted to \"" << last_book_name << "\"." << endl;
}
}
last_operation.clear();
this_thread::sleep_for(chrono::seconds(1));
}
void display_statistics() {
cout << "Total number of books: " << books.size() << endl;
this_thread::sleep_for(chrono::seconds(1));
}
#pragma endregion
};
class Accounts {
public:
string account_username;
string account_password;
Accounts(string accname, string accpass)
: account_username(accname), account_password(accpass) {}
void set_account_username(const string& accname) {
account_username = accname;
}
void set_account_password(const string& accpass) {
account_password = accpass;
}
void display_account_info(){
cout << "Username: " << account_username << endl;
cout << "Password: " << account_password << endl;
}
};
class AccountManagement {
private:
const string filename = "accounts.txt";
void load_accounts_from_file(){
ifstream file(filename);
if (file.is_open()) {
string username, password;
while (getline(file, username, ',')) {
getline(file, password);
accounts.push_back(Accounts(username, password));
}
file.close();
} else {
cout << "Error opening file: " << filename << endl;
}
}
void save_accounts_to_file(){
ofstream file(filename);
if (file.is_open()) {
for (const auto& acc : accounts) {
file << acc.account_username << "," << acc.account_password << endl;
}
file.close();
} else {
cout << "Error opening file: " << filename << endl;
}
}
public:
vector<Accounts> accounts;
AccountManagement() {
load_accounts_from_file();
}
void main_menu() {
int choice;
while(true) {
cout << "\n--- Main Menu ---" << endl;
cout << "1. Sign Up" << endl;
cout << "2. Sign In" << endl;
cout << "3. Save Account" << endl;
cout << "4. Delete Account" << endl;
cout << "5. Display Accounts" << endl;
cout << "6. Exit" << endl;
cout << "Enter your choice: ";
cin >> choice;
switch(choice) {
case 1:
sign_up();
break;
case 2:
if(sign_in()) {
LibraryManager lm;
lm.main();
}
break;
case 3:
save_accounts_to_file();
break;
case 4:
delete_account();
break;
case 5:
display_account_info();
break;
case 6:
exit(0);
default:
cout << "Invalid choice. Please try again." << endl;
}
}
}
void sign_up() {
string username, password;
cout << "Enter a username: ";
cin >> username;
for (auto& acc : accounts) {
if(acc.account_username == username) {
cout << "Username already exists. Please choose a different one." << endl;
return;
}
}
cout << "Enter a password: ";
cin >> password;
accounts.push_back(Accounts(username, password));
cout << "Account created successfully." << endl;
}
bool sign_in(){
if (accounts.empty()) {
cout << "No accounts found. Please sign up first." << endl;
return false;
}
string username, password;
cout << "Enter username: ";
cin >> username;
for (auto& acc : accounts) {
if (acc.account_username == username) {
cout << "Enter password: ";
cin >> password;
if(acc.account_password == password){
cout << "Sign in successful." << endl;
return true;
}
else{
cout << "Invalid password." << endl;
return false;
}
}
else{
cout << "Invalid username." << endl;
return false;
}
cout << "Invalid username or password." << endl;
return false;
}
}
void display_account_info() {
if(accounts.empty()) {
cout <<"No accounts!" << endl;
return;
}
for (auto& acc : accounts) {
cout << "-----------------------------" << endl;
acc.display_account_info();
cout << "-----------------------------" << endl;
}
return;
}
void delete_account() {
if(accounts.empty()) {
cout <<"No accounts!" << endl;
return;
}
string username;
cout << "Enter the username of the account to delete: " << endl;
cin >> username;
for (auto it = accounts.begin(); it!= accounts.end(); ++it) {
if (it->account_username == username) {
accounts.erase(it);
cout << "Account deleted successfully." << endl;
return;
}
}
cout << "Account not found." << endl;
}
};
int main() {
AccountManagement account_manager;
account_manager.main_menu();
}