-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOnlineBookReader.cpp
More file actions
581 lines (539 loc) · 16.5 KB
/
OnlineBookReader.cpp
File metadata and controls
581 lines (539 loc) · 16.5 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
#include<iostream>
#include <map>
#include <vector>
using namespace std;
/*
* TODO Sign up not written
*/
class User {
private:
string name;
string password;
string email;
string userName;
public:
// Default constructors
User() {}
User(const string& name, const string& password, const string& email, const string& userName) {
setName(name);
setPassword(password);
setEmail(email);
setUserName(userName);
}
void printInfo() {
cout << "Name: " << getName() << endl;
cout << "Email: " << getEmail() << endl;
cout << "user_name: " << getUserName() << endl;
}
// setter and getters
void setName(const string& name) {
this->name = name;
}
const string& getName() const {
return name;
}
void setPassword(const string& password) {
this->password = password;
}
const string& getPassword() const {
return password;
}
void setEmail(const string& email) {
this->email = email;
}
const string& getEmail() const {
return email;
}
void setUserName(const string& userName) {
this->userName = userName;
}
const string& getUserName() const {
return userName;
}
};
class Book {
private:
int isbn{};
string title;
string author;
int nPages{};
vector<string> content;
public:
// Default constructors
Book() : isbn(0),nPages(0) {}
Book(const int isbn,const string& title,const string& author,const int nPages) {
setIsbn(isbn), setTitle(title), setAuthor(author), setnPages(nPages);
}
// setters and getters
void setIsbn(const int isbn) {
this->isbn = isbn;
}
const int& getIsbn() const {
return isbn;
}
void setTitle(const string& title) {
this->title = title;
}
const string& getTitle() const {
return title;
}
void setAuthor(const string& author) {
this->author = author;
}
const string& getAuthor() const {
return author;
}
void setnPages(const int nPages) {
this->nPages = nPages;
}
const int& getnPages() const {
return nPages;
}
void setContent(vector<string>& content) {
this->content = content;
}
const vector<string>& getContent() {
return content;
}
// returning content of specific page
const string& getContentByPage(const int page) {
return content[page];
}
};
class BooksManager {
private:
vector<Book> books;
map<string, int> sessions; // title to last page map
public:
// Default constructors with fake database
BooksManager() {
}
// Seeding fake data
void loadBooks() {
vector<string> v(5);
v[0] = "Chapter 1";
v[1] = "Chapter 2";
v[2] = "Chapter 3";
v[3] = "Chapter 4";
v[4] = "Chapter 5";
Book a;
a.setTitle("The Lord of the Rings");
a.setAuthor("J.R.R. Tolkien");
a.setnPages(5);
a.setIsbn(34524);
a.setContent(v);
books.push_back(a);
Book b;
b.setTitle("The Hobbit");
b.setAuthor("J.R.R. Tolkien");
b.setnPages(5);
b.setIsbn(34525);
b.setContent(v);
books.push_back(b);
}
// setters and getters
const vector<Book>& getBooks() const {
return books;
}
// add book to the vector
void addBook() {
cout << "Enter the ISBN: ";
int isbn;
cin >> isbn;
cout << "Enter the title: ";
string title;
cin >> title;
cout << "Enter the author: ";
string author;
cin >> author;
cout << "Enter the number of pages: ";
int nPages;
cin >> nPages;
vector<string> v(nPages);
for (int i = 0; i < nPages; ++i) {
cout << "Enter the content of chapter " << i + 1 << ": ";
string content;
cin >> content;
v[i] = content;
}
Book a;
a.setTitle(title);
a.setAuthor(author);
a.setnPages(nPages);
a.setIsbn(isbn);
a.setContent(v);
books.push_back(a);
}
// show all available books and pick one to read
// store the last page of the book in the map
void pickBook() {
cout << "\nOur current book collection: " << endl;
for (int i = 0; i < books.size(); ++i) {
cout << "\t" << i + 1 << ": " << books[i].getTitle() << endl;
}
int choice;
cout << "\nEnter the number of the book you want to read: ";
cin >> choice;
if (choice > 0 && choice <= books.size()) {
beginSession(books[choice - 1]);
}
else {
cout << "Invalid choice. Try again\n";
}
}
// begin a session of reading a book
void beginSession(Book& book) {
string title = book.getTitle();
sessions[title] = 1;
int choice;
while (true) {
getPage(book, sessions[title]);
cout << "\nMenu: " << endl;
cout << '\t' <<"1: View Next Page\n";
cout << '\t' <<"2: View Previous Page\n";
cout << '\t' <<"3: Stop Reading\n";
cout << "Enter number in range 1 - 3: ";
cin >> choice;
if (!(1 <= choice && choice <= 3)) {
cout << "Invalid choice. Try again\n";
continue;
}
if (choice == 1) {
getPage(book, ++sessions[title]);
}
else if (choice == 2) {
getPage(book, --sessions[title]);
}
else if (choice == 3) {
// after breaking .. the last session will be stored in the sessions map
break;
}
}
}
void getPage(Book& book,int pagen) {
string title = book.getTitle();
cout << "You are now reading " << title << endl;
cout << "Current page: " << sessions[title] << "/" << book.getnPages() << endl;
cout << book.getContentByPage(sessions[title] - 1) << endl;
}
void pickSession() {
cout << "\nyour current sessions: " << endl;
int i = 1;
for (auto it = sessions.begin(); it != sessions.end(); ++it, i++) {
cout << "\t"<< i << " " <<it->first <<
"Page: " << it->second << "/" << searchBookByTitle(it->first).getnPages() << endl;
}
cout << "Enter the number of the session you want to read: ";
int choice;
cin >> choice;
if (choice > 0 && choice <= sessions.size()) {
int &page = sessions[sessions.begin()->first];
Book book = searchBookByTitle(sessions.begin()->first);
while (true) {
getPage(book, page);
cout << "\nMenu: " << endl;
cout << '\t' <<"1: View Next Page\n";
cout << '\t' <<"2: View Previous Page\n";
cout << '\t' <<"3: Stop Reading\n";
cout << "Enter number in range 1 - 3: ";
cin >> choice;
if (!(1 <= choice && choice <= 3)) {
cout << "Invalid choice. Try again\n";
continue;
}
if (choice == 1) {
getPage(book, ++page);
}
else if (choice == 2) {
getPage(book, --page);
}
else if (choice == 3) {
// after breaking .. the last session will be stored in the sessions map
break;
}
}
}
else {
cout << "Invalid choice. Try again\n";
}
}
/*
* Searching and removing functions will be in use
* ... but in the next version :)
*/
// remove book from the vector
void removeBook(const int isbn) {
for (int i = 0; i < books.size(); i++) {
if (books[i].getIsbn() == isbn) {
books.erase(books.begin() + i);
}
}
}
// search book by isbn
const Book& searchBookByIsbn(const int isbn) const {
for (int i = 0; i < books.size(); i++) {
if (books[i].getIsbn() == isbn) {
return books[i];
}
}
}
// search book by title
const Book& searchBookByTitle(const string& title) const {
for (int i = 0; i < books.size(); i++) {
if (books[i].getTitle() == title) {
return books[i];
}
}
}
// search book by author
const Book& searchBookByAuthor(const string& author) const {
for (int i = 0; i < books.size(); i++) {
if (books[i].getAuthor() == author) {
return books[i];
}
}
}
};
BooksManager booksManager;
class UsersManager {
private:
map<string, User> userName_userObj_map;
User current_user;
public:
// Default constructors with fake users for testing
UsersManager() {
userName_userObj_map["hany"] = User("Hany", "123", "test@mail", "hany");
}
// setter and getters
void setCurrentUser(const string& userName) {
current_user = userName_userObj_map[userName];
}
const User& getCurrentUser() const {
return current_user;
}
// return user by userName
const User& getUser(const string& userName) const {
return userName_userObj_map.at(userName);
}
// add user to the map
void addUser(const User& user) {
userName_userObj_map[user.getUserName()] = user;
}
// check if the user name is already exist in the map
bool isUserNameExist(const string& userName) {
return userName_userObj_map.find(userName) != userName_userObj_map.end();
}
// check if the user name and password are correct
bool isUserNameAndPasswordCorrect(const string& userName, const string& password) {
if (isUserNameExist(userName)) {
return userName_userObj_map[userName].getPassword() == password;
}
return false;
}
void accessSystem() {
while (true) {
int choice = userMenu();
if (choice == 1)
current_user.printInfo();
else if (choice == 2)
booksManager.pickSession();
else if (choice == 3)
booksManager.pickBook();
else if (choice == 4)
break;
}
}
int userMenu() {
int choice = -1;
while (choice == -1) {
cout << "\nHello " << current_user.getName() << " | User View" << endl;
cout << "\nMenu: " << endl;
cout << '\t' <<"1: View Profile\n";
cout << '\t' <<"2: List & Select from My Reading History\n";
cout << '\t' <<"3: List & Select from Available Books\n";
cout << '\t' <<"4: Logout\n";
cout << "Enter number in range 1 - 4: ";
cin >> choice;
if (!(1 <= choice && choice <= 4)) {
cout << "Invalid choice. Try again\n";
choice = -1; // loop keep working
}
}
return choice;
}
};
class Admin {
private:
string name;
string password;
string email;
string user_name;
public:
// Default constructor with initializer list -- delegating constructor
Admin() : Admin("", "", "", "") {}
Admin(const string& name, const string& password, const string& email, const string& user_name) {
setName(name);
setPassword(password);
setEmail(email);
setUserName(user_name);
}
void printInfo() {
cout << "Name: " << getName() << endl;
cout << "Email: " << getEmail() << endl;
cout << "user_name: " << getUserName() << endl;
}
// Setters & Getters
void setName(const string& name) {
this->name = name;
}
void setPassword(const string& password) {
this->password = password;
}
void setEmail(const string& email) {
this->email = email;
}
void setUserName(const string& user_name) {
this->user_name = user_name;
}
const string& getName() const {
return name;
}
const string& getPassword() const {
return password;
}
const string& getEmail() const {
return email;
}
const string& getUserName() const {
return user_name;
}
};
class AdminsManager {
private:
map<string, Admin> username_adminObj_map;
Admin current_admin;
public:
// Default constructor creates admin for simplicity
AdminsManager() {
// we could have a separate file to read from and store data
// but for simplicity we will use a single admin object
// also it is not a good idea to store password in plain text
// but anyway, the purpose of this project is to enhance designing skills
string name = "Hussein Hany", password = "123", email = "admin@mail", user_name = "3ein39";
Admin admin(name, password, email, user_name);
username_adminObj_map[user_name] = admin;
}
const Admin& getAdmin(const string& user_name) {
return username_adminObj_map.at(user_name);
}
void setCurrentAdmin(const string& user_name) {
current_admin = username_adminObj_map.at(user_name);
}
bool isAdminExist(const string& user_name) {
return username_adminObj_map.find(user_name) != username_adminObj_map.end();
}
void accessSystem() {
while (true) {
int choice = adminMenu();
if (choice == 1)
current_admin.printInfo();
else if (choice == 2)
booksManager.addBook();
else if (choice == 3)
break;
}
}
int adminMenu() {
int choice = -1;
while (choice == -1) {
cout << "\nHello " << current_admin.getName() << " | Admin View" << endl;
cout << "\nMenu: " << endl;
cout << '\t' <<"1: View Profile\n";
cout << '\t' <<"2: Add Book\n";
cout << '\t' <<"3: Logout\n";
cout << "Enter number in range 1 - 3: ";
cin >> choice;
if (!(1 <= choice && choice <= 3)) {
cout << "Invalid choice. Try again\n";
choice = -1; // loop keep working
}
}
return choice;
}
};
class readerSystem {
private:
UsersManager usersManager;
AdminsManager adminsManager;
void doLogin() {
string user_name, password;
cout << "Enter user name: ";
cin >> user_name;
cout << "Enter password: ";
cin >> password;
if (adminsManager.isAdminExist(user_name) && adminsManager.getAdmin(user_name).getPassword() == password) {
cout << "Admin Login successful\n" << endl;
adminsManager.setCurrentAdmin(user_name);
adminsManager.accessSystem();
} else if (usersManager.isUserNameExist(user_name) && usersManager.getUser(user_name).getPassword() == password) {
cout << "User Login successful\n" << endl;
usersManager.setCurrentUser(user_name);
usersManager.accessSystem();
} else {
cout << "Login failed" << endl;
doLogin();
}
}
void doSignup() {
// signup for a new user
string name, password, email, user_name;
cout << "Enter name: ";
cin >> name;
cout << "Enter password: ";
cin >> password;
cout << "Enter email: ";
cin >> email;
cout << "Enter user name: ";
cin >> user_name;
usersManager.addUser(User(name, password, email, user_name));
}
int menu() {
int choice = -1;
while (choice == -1) {
cout << "\nWelcome to Online Book Reader" << endl;
cout << "\nMenu: " << endl;
cout << '\t' <<"1: Login\n";
cout << '\t' <<"2: Signup\n";
cout << "Enter number in range 1 - 2: ";
cin >> choice;
if (!(1 <= choice && choice <= 2)) {
cout << "Invalid choice. Try again\n";
choice = -1; // loop keep working
}
}
return choice;
}
public:
void Run() {
accessSystem();
}
void accessSystem() {
booksManager.loadBooks();
while (true) {
int choice = menu();
if (choice == 1)
doLogin();
else if (choice == 2)
doSignup();
}
}
};
int main() {
readerSystem readerSystem;
readerSystem.Run();
return 0;
}
/*
* Admin : 3ein39 123
* User: hany 123
*/