forked from Yasna1998/stackoverflow-in-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
executable file
·205 lines (199 loc) · 7.57 KB
/
main.cpp
File metadata and controls
executable file
·205 lines (199 loc) · 7.57 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
#include <iostream>
#include <string>
#include <ctime>
#include "AbstractUser.h"
#include "Exceptions.h"
#include "User.h"
#include "Database.h"
#ifdef _WIN32
#define CLEAR "cls"
#else //In any other OS
#define CLEAR "clear"
#endif
using namespace std;
enum MenuState {
START,
LOGGED_IN,
MY_CONTENT,
END
};
int main() {
// init();
User::init("SECRET_KEY");
User * loggedInUser = nullptr;
MenuState menuState = MenuState::START;
string last_message;
string user_text;
int ind = 0;
char choice;
while(menuState != MenuState::END) {
if (menuState != MenuState::MY_CONTENT) {
system(CLEAR);
}
if (!last_message.empty())
cout << last_message << endl;
last_message = "";
switch (menuState) {
case MenuState::START: {
cout << "1. login\n2. signup\ne. exit\n";
cin >> choice;
switch (choice) {
case '1': { // login
try {
string username, password;
cout << "Enter Username: ";
cin >> username;
cout << "Enter Password: ";
cin >> password;
loggedInUser = &User::login(username,password);
loggedInUser->log_user(username);
menuState = MenuState::LOGGED_IN;
} catch (WrongUsernameOrPasswordException &e) {
last_message = e.what();
}
break;
}
case '2': { // signup
try {
string username, password, email;
cout << "Enter Email: ";
cin >> email;
cout << "Enter Username: ";
cin >> username;
cout << "Enter Password: ";
cin >> password;
loggedInUser = &User::signup(username, password, email);
menuState = MenuState::LOGGED_IN;
last_message = "User signed up!\n";
} catch (UsernameAlreadyExistsException &e) {
last_message = e.what();
break;
} catch (EmailAlreadyExistsException &e) {
last_message = e.what();
}
break;
}
case 'e': { // exit
menuState = MenuState::END;
break;
}
default: { // unknown input
last_message = "Unknown Input\n";
break;
}
}
break;
}
case MenuState::LOGGED_IN: {
cout << "d.delete account\nl. logout\nq. add question\ns. search a question\nm. my content\ne. exit\n";
cin >> choice;
switch (choice) {
case 'd': {
try {
loggedInUser->deleteAccount();
cout << "Account successfully deleted\n";
loggedInUser = nullptr;
menuState = MenuState::START;
}
catch (DeleteAdminException &e) {
last_message = e.what();
}
break;
}
case 'l': { // logout
loggedInUser = nullptr;
menuState = MenuState::START;
last_message = "GOOD BYE\n";
break;
}
case 'e': { // exit
menuState = MenuState::END;
break;
}
case 'q': { //add question
cout<<"Please enter your question:"<<endl;
//getline(cin,user_text); //TODO find a better way to get a line including spaces
cin>>user_text;
loggedInUser->add_question(user_text);
last_message = "your question was successfully uploaded\n";
break;
}
case 's': {
cout<<"search here:"<<endl;
cin>> user_text;
// a search
break;
}
case 'm': {
if (loggedInUser->contents.size() == 0){
last_message = "you don't have any contents yet\n";
}
else {
system(CLEAR);
loggedInUser->get_my_content(ind);
menuState = MenuState::MY_CONTENT;
}
break;
}
default: { // unknown input
last_message = "Unknown Input\n";
break;
}
}
break;
}
case MenuState ::MY_CONTENT:{
cout<<"n. next\np. previous\nd. delete content\ne. edit content\nb. back"<<endl;
cin>>choice;
system(CLEAR);
switch (choice){
case 'n': {
ind++;
if (ind == loggedInUser->contents.size()){
ind--;
last_message = "no contents after this";
}
loggedInUser->get_my_content(ind);
break;
}
case 'p': {
ind--;
if (ind < 0){
ind++;
last_message = "no content prior to this";
}
loggedInUser->get_my_content(ind);
break;
}
case 'd': {
loggedInUser->remove_content(*(loggedInUser->contents[ind]));
if (loggedInUser->contents.size() == 0) {
menuState = MenuState::LOGGED_IN;
last_message = "content successfully deleted\nyou have no contents";
} else {
last_message = "content successfully deleted\n";
}
ind = 0;
break;
}
case 'e':{
cout<<"Please enter your text to replace:"<<endl;
cin>>user_text;
loggedInUser->contents[ind]->edit_content(user_text);
loggedInUser->get_my_content(ind);
break;
}
case 'b':{
ind = 0;
menuState = MenuState::LOGGED_IN;
break;
}
}
break;
}
}
}
system(CLEAR);
cout << "GOODBYE" << endl;
return 0;
}