-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLibrary.cpp
419 lines (397 loc) · 9.4 KB
/
Library.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
#include "Library.h"
Library::Library()
{
isOpened = false;
isSaved = false;
isChanged = false;
FileName = "";
}
void Library::open(const String& filename)
{
if (isOpened)
{
std::cout << "Database is already loaded! Save or close before trying to open a new one!\n";
return;
}
if (!isOpened)
{
size_t size = filename.getSize();
if ((filename[0] != 'C' && filename[0] != 'D') || filename[1] != ':' || filename[2] != '\\')
{
std::cout << "Please use the format DISK:\\PATH\\PATH\\...\\filename.txt\n";
return;
}
if (filename[size - 4] != '.' || filename[size - 3] != 't' || filename[size - 2] != 'x' || filename[size - 1] != 't')
{
std::cout << "File name must end with .txt !\n";
return;
}
std::ifstream in(filename.getText());
in.seekg(std::ios::beg);
users.loadUsers(in);
if (users.getSize() == 0)
return;
size_t skip = users.getSize() * 4 + 1;
in.seekg(0,std::ios::beg);
books.loadBooks(in,skip);
std::cout << "Database successfully loaded!\n";
this->FileName = filename;
this->isOpened = true;
}
}
void Library::save()
{
if (!isOpened)
{
std::cout << "Open a file before executing any other commands!\n";
return;
}
if (!isChanged)
{
std::cout << "No changes have been made!\n";
return;
}
std::ofstream out(this->FileName.getText());
users.saveUsers(out);
books.saveBooks(out);
std::cout << "Database successfully updated!\n";
this->isChanged = false;
}
void Library::saveAs(const String& filename)
{
if (!isOpened)
{
std::cout << "Open a file before executing any other commands!\n";
return;
}
size_t size = filename.getSize();
if ((filename[0] != 'C' && filename[0] != 'D') || filename[1] != ':' || filename[2] != '\\')
{
std::cout << "Please use the format DISK:\\PATH\\PATH\\...\\filename.txt\n";
return;
}
if (filename[size - 4] != '.' || filename[size - 3] != 't' || filename[size - 2] != 'x' || filename[size - 1] != 't')
{
std::cout << "File name must end with .txt !\n";
return;
}
this->FileName = filename;
this->isChanged = 1;
this->save();
}
void Library::close()
{
if (!isOpened)
{
std::cout << "Open a file before executing any other commands!\n";
return;
}
if (isChanged)
{
std::cout << "There are unsaved changes! Are you sure you want to exit?\n0 - Save&Close | 1 - Close : ";
bool choice = 0;
std::cin >> choice;
if (choice)
{
std::cin.ignore();
std::cout << "Successfully closed " << this->FileName << "!\n";
this->clear();
return;
}
std::cin.ignore();
this->save();
std::cout << "Successfully closed " << this->FileName << "!\n";
this->clear();
return;
}
std::cout << "Successfully closed " << this->FileName << "!\n";
this->clear();
this->isOpened = 0;
}
void Library::logIn()
{
if (!isOpened)
{
std::cout << "Open a file before executing any other commands!\n";
return;
}
size_t size = users.getSize();
if (users.activeUserIndex() != -1)
{
std::cout << "Another user is already logged in! Please log out!\n";
return;
}
size_t counter = 0;
int index = -1;
String name;
String password;
do
{
std::cout << "Enter your username: ";
std::cin >> name;
for (size_t i = 0; i < size; ++i)
{
if (name == users[i].getName())
{
index = i;
break;
}
}
if (index == -1)
{
counter++;
std::cout << "User with that username not found! Remaining tries: " << 5 - counter << std::endl;
name = "";
}
} while (index == -1 && counter <5);
if (counter >= 5)
{
std::cout << "Try again later!\n";
return;
}
counter = 0;
do
{
std::cout << "Enter your password: ";
std::cin >> password;
if (users[index].getPassword() == password)
{
users[index].setLoggedIn(true);
std::cout << "Welcome " << name << "!\n";
return;
}
counter++;
std::cout << "Incorrect password! Remaining tries: " << 5 - counter << std::endl;
password = "";
} while (counter < 5);
std::cout << "Try again later!\n";
return;
}
void Library::logOut()
{
if (!isOpened)
{
std::cout << "Open a file before executing any other commands!\n";
return;
}
if (users.activeUserIndex() == -1)
{
std::cout << "No one is logged in!\n";
return;
}
users[users.activeUserIndex()].setLoggedIn(false);
std::cout << "You have successfully logged out!\n";
return;
}
void Library::exit()
{
if (users.activeUserIndex() == -1)
{
std::cout << "Good bye!\n";
return;
}
std::cout << "Good bye " << users[users.activeUserIndex()].getName() << "!\n";
}
void Library::help() const
{
std::cout << "Available commands:\n";
std::cout << "help opens the help menu\n";
std::cout << "open <location> opens <file name>\n";
std::cout << "close closes the current file\n";
std::cout << "save saves the current file\n";
std::cout << "save as <location> saves the current file in <location>\n";
std::cout << "exit exits the program\n";
std::cout << "login logs in in the system\n";
std::cout << "logout(*) logs out of the system\n";
std::cout << "books all(*) prints the info of all books\n";
std::cout << "books find <option> <description>(*) prints the IDs of those books\n";
std::cout << "books info <id>(*) prints the info of a book\n";
std::cout << "books sort <by what (asc|desc)>(*) sorts the books\n";
std::cout << "books add(*)(^) adds a new book\n";
std::cout << "books remove(*)(^) removes a book\n";
std::cout << "users add <username> <password>(*)(^) adds a new user\n";
std::cout << "users remove <username>(*)(^) removes a user\n\n";
std::cout << "(*) - requires to be logged in | (^) - requires admin rights\n";
}
void Library::booksAll() const
{
if (!isOpened)
{
std::cout << "Open a file before executing any other commands!\n";
return;
}
if (users.activeUserIndex() == -1)
{
std::cout << "You have to be logged in!\n";
return;
}
if (books.getSize() == 0)
{
std::cout << "No available books!\n";
return;
}
books.allBooksBrieflyInfo();
}
void Library::booksInfo(const size_t id) const
{
if (!isOpened)
{
std::cout << "Open a file before executing any other commands!\n";
return;
}
if (users.activeUserIndex() == -1)
{
std::cout << "You have to be logged in!\n";
return;
}
books.bookDetailedInfo(id);
}
void Library::booksFind(const String& option, const String& description) const
{
if (!isOpened)
{
std::cout << "Open a file before executing any other commands!\n";
return;
}
if (users.activeUserIndex() == -1)
{
std::cout << "You have to be logged in!\n";
return;
}
books.findBook(option, description);
}
void Library::booksSortAscending(const String& option)
{
if (!isOpened)
{
std::cout << "Open a file before executing any other commands!\n";
return;
}
if (users.activeUserIndex() == -1)
{
std::cout << "You have to be logged in!\n";
return;
}
books.sortBooksAscending(option);
this->isChanged = true;
}
void Library::booksSortDescending(const String& option)
{
if (!isOpened)
{
std::cout << "Open a file before executing any other commands!\n";
return;
}
if (users.activeUserIndex() == -1)
{
std::cout << "You have to be logged in!\n";
return;
}
books.sortBooksDescending(option);
this->isChanged = true;
}
void Library::booksAdd()
{
if (!isOpened)
{
std::cout << "Open a file before executing any other commands!\n";
return;
}
if (users.activeUserIndex() == -1)
{
std::cout << "You have to be logged in!\n";
return;
}
User active;
active = activeUser();
if (active.getAdmin() == false)
{
std::cout << "Admin rights required!\n";
return;
}
books.addBook();
this->isChanged = true;
}
void Library::booksRemove(const String& title )
{
if (!isOpened)
{
std::cout << "Open a file before executing any other commands!\n";
return;
}
if (users.activeUserIndex() == -1)
{
std::cout << "You have to be logged in!\n";
return;
}
User active;
active = activeUser();
if (active.getAdmin() == false)
{
std::cout << "Admin rights required!\n";
return;
}
books.removeBook(title);
this->isChanged = true;
}
void Library::usersAdd(const String& name, const String& password)
{
if (!isOpened)
{
std::cout << "Open a file before executing any other commands!\n";
return;
}
if (users.activeUserIndex() == -1)
{
std::cout << "You have to be logged in!\n";
return;
}
User active;
active = activeUser();
if (active.getAdmin() == false)
{
std::cout << "Admin rights required!\n";
return;
}
users.addUser(name, password);
std::cout << "New user successfully added!\n";
this->isChanged = true;
}
void Library::usersRemove(const String& name)
{
if (!isOpened)
{
std::cout << "Open a file before executing any other commands!\n";
return;
}
if (users.activeUserIndex() == -1)
{
std::cout << "You have to be logged in!\n";
return;
}
User active;
active = activeUser();
if (active.getAdmin() == false)
{
std::cout << "Admin rights required!\n";
return;
}
if (name == active.getName())
{
std::cout << "You can't remove yourself!\n";
return;
}
users.removeUser(name);
this->isChanged = true;
}
const User& Library::activeUser()
{
return users[users.activeUserIndex()];
}
void Library::clear()
{
this->users.clear();
this->books.clear();
this->isOpened = false;
this->FileName = "";
}