forked from DeMonkeyCoder/stackoverflow-in-cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
219 lines (192 loc) · 6.8 KB
/
main.cpp
File metadata and controls
219 lines (192 loc) · 6.8 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
//
// main.cpp
// HW2
//
// Created by mahdi on 10/24/18.
// Copyright © 2018 mahdi. All rights reserved.
//
#include <iostream>
#include <vector>
using namespace std;
/**
* In the name of God
* Homework 2
* TODO: Complete this code using given TODO comments :). then remove TODOs
* feel free to edit code as you want and make it better
* Any questions? ask @devcom_support on telegram
* Good luck!
**/
enum UserType{
ADMIN,
MEMBER
};
class UserAlreadyExistsException{}; //TODO: Give exceptions a better structure. search google (optional)
class AbstractUser{ // User structure
public:
virtual bool authenticate(string username, string password) = 0;
virtual void deleteAccount(vector<AbstractUser*> *users) = 0; //TODO: 1. implement this in User class. (You can't compile code and create instance of User until then). DON'T TOUCH ABSTRACT USER!
string username;
string getPass(){
return password;
}
protected:
string password;
UserType type;
};
class User : public AbstractUser{
public:
User(string username, string password, UserType type){
this->username = username;
this->password = password;
this->type = type;
}
bool authenticate(string username, string password){
return this->username == username && this->password == password;
}
void deleteAccount(vector<AbstractUser*> *users){
for(auto user = users->begin(); user != users->end(); user++) {
if (*user == this) {
users->erase(user);
break;
}
}
}
static User* login(vector<AbstractUser*> *users, string username, string password){ //TODO: 2. handle user login errors with exceptions
for(auto user = users->begin(); user != users->end(); user++){
if((*user)->authenticate(username, password)){
return (User*) *user;
}
else{
int usN = 0;
int paS = 0;
if ((*user)->username==username){
usN = 1;
}
if ((*user)->getPass()==password) {
paS = 1;
}
if (usN == 0) {
throw 0;
}
if (usN == 1 && paS == 0) {
throw 10;
}
}
}
return nullptr;
}
static void signup(vector<AbstractUser*> *users, string username, string password){
//Check if user with that username exists and throw UserAlreadyExistsException in that case
for(auto user = users->begin(); user != users->end(); user++) { //TODO: 3. this doesn't work. fix it!!
if ((*user)->username == username) {
UserAlreadyExistsException ex;
throw ex;
}
}
//Create user and add it to vector
users->push_back(new User(username, password, UserType::MEMBER));
}
};
enum MenuState{
START,
LOGGED_IN,
END
};
class AppDatabase { //Just holds runtime data. doesn't save anything
public:
vector<AbstractUser *> appUsers;
AppDatabase() { //Load initial data
appUsers.push_back(new User("admin",
"admin" /* password is unsafe! for test only */,
UserType::ADMIN)
);
}
};
int main(){
User * loggedInUser = nullptr;
AppDatabase appDatabase;
MenuState menuState = MenuState::START;
char choice;
cout << "Welcome!" << endl;
while(menuState != MenuState::END){
switch (menuState){
case MenuState::START: {
cout << "1. login\n2. signup\ne. exit\n";
cin >> choice;
switch(choice) {
case '1': {
string username, password;
cout << "Enter Username" << endl;
cin >> username;
cout << "Enter Password" << endl;
cin >> password;
try {
loggedInUser = User::login(&appDatabase.appUsers, username, password);
if (loggedInUser == nullptr) {
cout << "couldn't login with given credentials.";
} else {
menuState = MenuState::LOGGED_IN;
}
} catch (int exc) {
if (exc == 0) {
cout<<"username is incorrect"<<endl;
}
if (exc == 10) {
cout<<"password is incorrect"<<endl;
}
}
break;
}
case '2': {
string username, password;
cout << "Enter Username" << endl;
cin >> username;
cout << "Enter Password" << endl;
cin >> password;
try{
User::signup(&appDatabase.appUsers, username, password);
} catch (UserAlreadyExistsException e) {
cout << "Error: username already exists";
}
break;
}
case 'e': {
menuState = MenuState::END;
break;
}
default: {
cout << "Unknown Input" << endl;
}
}
break;
}
case MenuState::LOGGED_IN: {
cout << "d.delete account\nl. logout\ne. exit\n";
cin >> choice;
switch(choice) {
case 'd': {
loggedInUser->deleteAccount(&appDatabase.appUsers);
cout << "Account successfully deleted";
loggedInUser = nullptr;
menuState = MenuState::START;
break;
}
case 'l': {
loggedInUser = nullptr;
menuState = MenuState::START;
break;
}
case 'e': {
menuState = MenuState::END;
break;
}
default: {
cout << "Unknown Input" << endl;
}
}
break;
}
}
}
return 0;
}