-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbook_store.cpp
353 lines (307 loc) · 11.9 KB
/
book_store.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
#include<iostream>
#include<vector>
#include<deque>
#include<set>
#include<tuple>
#include<ctime>
class user;
class book;
class history;
class loginSignUPMenu;
class usingMode;
std::string invalidityFlag = "-1"; //made a mistake building function return by reference and there's a case where i send temporary variable so i had to make this variable to just fix it fast
// our dataBase
std::vector<book*> allBooks;
std::vector<user*> admins;
std::vector<user*> readers;
//Backend Side
#include "book_store.h"
// definition of book Member Functions
book::book(std::string ISBN,std::string title,std::string authorname,std::vector<std::string> pages):
ISBN(ISBN),title(title),authorname(authorname),pages(pages){}
book::book(std::string ISBN,std::string title,std::string authorname,int numberOfPages):ISBN(ISBN),title(title),authorname(authorname){ //Constructor definition for book
for(int pageNumber= 1 ; pageNumber<=numberOfPages ; pageNumber++){
std::cout<<"Enter Page#"<<pageNumber<<": ";
std::string temp;
std::cin>>temp;
pages.push_back(temp);
std::cout<<std::endl;
}
}
const std::string& book::Page(const int & currentPage) const{
if ((currentPage> (int)pages.size()) || currentPage<=0)
return invalidityFlag ;// if -1 ,then means the page you're asking for invalid page
return pages[currentPage-1];
}
const std::string& book::nextPage(const int ¤tPage) const{
return Page(currentPage+1);
}
const std::string& book::previousPage(const int ¤tPage) const{
return Page(currentPage-1);
}
const std::string& book::gettitle()const {
return title;
}
//Definition Of ->History<- Member Functions
const std::pair<book*,int> history::chooseSession(const int & choice) const{
if (choice>(int)sessions.size() || choice<1)
return std::make_pair(allBooks[0],-1); // if invalid choice, then return -1
//just to index properly use Choice Variable decremented (0 index issue)
return std::make_pair(std::get<0>(sessions[choice-1]),std::get<1>(sessions[choice-1]));
}
void history::viewHisotry() const{
for(int session = 0 ; session<(int)sessions.size();session++){
std::cout<<session+1<<") "<<(std::get<0>(sessions[session])->gettitle())<<" |page: "<<(std::get<1>(sessions[session]))<<" | "<<(std::get<2>(sessions[session]))<<std::endl;
}
}
void history::addSession(book* currentbook, int currentpage, char* date){
sessions.push_front(std::make_tuple(currentbook,currentpage,date));
}
bool history::isEmpty() const{
return (sessions.size()==0);
}
//Definition of User Member Functions
user::user(std::string username,std::string email,std::string name,std::string password):
username(username),email(email),name(name),password(password){
}
std::vector<std::string> user::viewProfile() const{
std::vector<std::string> temp {username,email,name};
return temp;
}
history* user::getUserHistory() {
return (&userHistory);
}
//FrontEnd side
//Login and Signup menu
int loginSignUPMenu::theMessage(){
std::cout<<"MENU:"<<std::endl;
std::cout<<" 1) Log In"<<std::endl;
std::cout<<" 2) Sign Up"<<std::endl;
std::cout<<"Enter Your Choice in Range 1 - 2: ";
std::string userChoice;
std::cin>>userChoice;
if (userChoice == "1" )
return 1;
if(userChoice == "2")
return 2;
return -1;
}
std::pair<user*,int> loginSignUPMenu::loginMenu(){
int adminpermission = 1;
int readerpermission= 0;
int invalid= -1;
user* who{};
std::cout<<"Enter Your UserName: ";
std::string userNameEnrty;
std::cin.ignore(3,'\n');
std::getline(std::cin,userNameEnrty);
std::cout<<"Enter Your Password: ";
std::string passwordEnrty;
std::getline(std::cin,passwordEnrty);
std::cout<<std::endl;
for(user* &admin :admins){
if (admin->password == passwordEnrty && admin->username ==userNameEnrty){
return std::make_pair(admin,adminpermission);
}
}
for (user* &reader :readers){
if(reader->password == passwordEnrty && reader->username ==userNameEnrty){
return std::make_pair(reader,readerpermission);
}
}
return std::make_pair(who,invalid);
}
std::pair<user*,int> loginSignUPMenu::signupMenu(){
std::cout<<"Enter Your UserName (No Spaces): ";
std::string userNameEnrty;
std::cin.ignore(3,'\n');
std::getline(std::cin,userNameEnrty);
std::cout<<"Enter Your Password (No Spaces): ";
std::string PasswordEnrty;
std::getline(std::cin,PasswordEnrty);
std::cout<<"Enter Your Name (No Spaces): ";
std::string NameEnrty;
std::getline(std::cin,NameEnrty);
std::cout<<"Enter Your Email (No Spaces): ";
std::string EmailEnrty;
std::getline(std::cin,EmailEnrty);
int valid = -1;
user* createdUser;
for(user* &admin :admins){
if (admin->username ==userNameEnrty)
return std::make_pair(createdUser,valid);
}
for (user* &reader :readers){
if(reader->username ==userNameEnrty)
return std::make_pair(createdUser,valid);
}
user* newUser = new user(userNameEnrty,EmailEnrty,NameEnrty,PasswordEnrty);
valid= 1;
readers.push_back(newUser);
return std::make_pair(newUser,valid);
}
//Controlling the flow when a user is Entering the Program
//start Method
std::pair<user*,bool> loginSignUPMenu::start(){
while(true){
int dontStartAgain=1; // indicates if the user want to display the login in signup message again after invalid input;
int choice;
while(true){
choice = theMessage();
if (choice != -1)
break;
else
std::cout<<"\nInvalid Input!\n";
}
user* validUser;
int userPremissionAndValidty;
if (choice == 1 ){ //login Mode
while(dontStartAgain){
std::tie(validUser,userPremissionAndValidty) = loginMenu(); //go for login method returning the user and the whether its valid or admin or reader.
if (userPremissionAndValidty != -1) //admin or reader depends on the userPremissionAndValidty variable.
return std::make_pair(validUser,userPremissionAndValidty); //we're good to go to using mode
else{ //not valid input
std::cout<<"Not Valid user !\n";
std::cout<<"\n1) Try Again\n"<<"2)Try To SignUp\n";
int choiceBackOrAgain;
std::cin>>choiceBackOrAgain;
if(choiceBackOrAgain == 2) dontStartAgain =0;
}
}
}
if (choice == 2){ //SignUp Mode
while(dontStartAgain){
user* validUser;
int userPremissionAndValidty;
std::tie(validUser,userPremissionAndValidty) = signupMenu();
if(userPremissionAndValidty == -1){
std::cout<<"User Exist Before !\n";
std::cout<<"\n1) Try Again\n"<<"2)Try To Signin\nEnter Your Choice 1- 2: ";
int choiceBackOrAgain;
std::cin>>choiceBackOrAgain;
if(choiceBackOrAgain == 2) dontStartAgain =0;
}
else{
bool readerpremission = 0; // means the user passed will Enter to the using Mode as reader only
return std::make_pair(validUser,readerpremission); //good to go to using mode
}
}
}
}
}
//using Mode Constructor
usingMode::usingMode(user* currentuser, bool isadminMode):currentuser(currentuser) , isadminMode(isadminMode) {}
//using Mode -Reader Menu
int usingMode::readerMenu() const{
std::cout<<"\n\n\n 1)View Profile\n 2)List & Select From My Histroy\n 3)List & Select From Available Books\n 4)Logout\n";
while (true){
std::cout<<"\nEnter Your Choice 1- 4:";
int userChoice;
std::cin>>userChoice;
if (userChoice<1 || userChoice>4)
std::cout<<"Invalid Input!\n\n";
else
return userChoice;
}
}
//using mode admin menu
int usingMode::adminMenu()const{
std::cout<<"1)View Profile\n2)AddBook\n3)Logout\n";
while (true){
std::cout<<"\nEnter Your Choice 1- 3: ";
int userChoice;
std::cin>>userChoice;
if (userChoice<1 || userChoice>3)
std::cout<<"Invalid Input!\n\n";
else
return userChoice;
}
}
//using mode display user info
void usingMode::displayProfile()const{
std::vector<std::string> allData =currentuser->viewProfile(); //vector of username & email & name
std::cout<<"\nName: "<<allData[2]<<std::endl;
std::cout<<"UserName: "<<allData[0]<<std::endl;
std::cout<<"Email: "<<allData[1]<<"\n\n";
}
//view books in the all books vector
void usingMode::viewBooks()const {
int num = 1;
for (auto &book :allBooks)
std::cout<<(num++)<<") "<<(book->gettitle())<<std::endl;
}
// add book to the all books vector
void usingMode::addBook(){
std::string ISBN,title,authorName;
int howManyPages;
std::cout<<"\nEnter Book ISBN: ";
std::cin>>ISBN;
std::cin.ignore(256,'\n');
std::cout<<"\nEnter Book Title: ";
std::getline(std::cin,title);
std::cout<<"\nEnter Book Author Name: ";
std::getline(std::cin,authorName);
std::cout<<"\nEnter Book How Many Pages: ";
std::cin>>howManyPages;
book* newBook = new book(ISBN,title,authorName,howManyPages);
allBooks.push_back(newBook);
}
//READING mode
void usingMode::readingMode(book* chosenBook,int currentpage){
std::cout<<"\n"<<(chosenBook->Page(currentpage));
while(true){ //To stand still in the reading mode till you choose to stop
int readingChoice;
std::cout<<"\n 1) Next Page\n 2) Previous Page\n 3) Stop Reading\n Enter you Choice 1-3: ";
std::cin>>readingChoice;
if (readingChoice == 1){ //next page
std::string res = chosenBook->nextPage(currentpage);
if (res == "-1"){
std::cout<<"\nEnd Of Book!\n";
std::cout<<"\n\n"<<chosenBook->Page(currentpage)<<std::endl<<std::endl;
}
else{
std::cout<<currentpage<<std::endl;
std::cout<<"\n\n"<<res<<std::endl;
currentpage++;
}
}
else if (readingChoice == 2){ //Previous Page
std::string res = chosenBook->previousPage(currentpage);
if (res == "-1"){
std::cout<<"\nStart Of Book!\n";
std::cout<<"\n"<<chosenBook->Page(currentpage)<<std::endl<<std::endl;
}
else{
std::cout<<"\n"<<res<<std::endl;
currentpage--;
}
}
else if (readingChoice == 3){ //Stop Reading in exist the loop
time_t now = time(NULL);
char* dateString = ctime(&now);
currentuser->getUserHistory()->addSession(chosenBook,currentpage,dateString);
break;
}
else
std::cout<<"\nInvalid Enrty!\n";
}
}
int main(){
//
std::vector<std::string> dummypages = {"Hello","to the book","we're happy to see you"};
book* firstbook = new book{"112233","c++ How To Program","Assem",dummypages};
allBooks.push_back(firstbook);
book* secondbook = new book{"5566","The Grokking Algorithm","medhat",dummypages};
allBooks.push_back(secondbook);
user* admin1 = new user("assemmedhat","[email protected]","Assem Bob","0000");
user* admin2 = new user("anonymous","[email protected]","The Ghost","8888");
user* reader1 = new user("reader1","[email protected]","the reader1","9999");
user* reader2 = new user("reader2","[email protected]","the reader1","7777");
admins.push_back(admin1);
admins.push_back(admin2);
readers.push_back(reader1);
readers.push_back(reader2);
program initiateIt;//calling the program function
initiateIt.startprogram();
return 0;
}