forked from ErfanGerami/ap
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTest.cpp
More file actions
192 lines (169 loc) · 3.83 KB
/
Test.cpp
File metadata and controls
192 lines (169 loc) · 3.83 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
#include <iostream>
#include <functional> // For std::hash
#include <string>
using namespace std;
const int MAX = 100;
template <typename T, typename F>
int myfind(T *begin, T *end, F f)
{
for (T *itr = begin; itr != end; itr++)
{
if (f(*itr))
{
return itr - begin;
}
}
return -1;
}
class AbstractUser
{
public:
AbstractUser(const string &username, const string &password)
: username(username), password(password) {}
virtual ~AbstractUser() = default;
virtual void displayRole() const = 0;
virtual bool hasAccessToSee(AbstractUser *) const = 0;
string getUsername() const
{
return username;
}
bool isPasswordCorrect(const string &pass)
{
return (pass == password);
}
protected:
string username;
string password;
};
class Student : public AbstractUser
{
public:
Student(const string &username, const string &password, int grade)
: AbstractUser(username, password), grade(grade) {}
void displayRole() const override
{
cout << "Student: " << username << ", Grade: " << grade << endl;
}
bool hasAccessToSee(AbstractUser *user) const override
{
return false;
}
private:
int grade;
};
class Staff : public AbstractUser
{
public:
Staff(const string &username, const string &password, int salary)
: AbstractUser(username, password), salary(salary) {}
void displayRole() const override
{
cout << "Staff: " << username << ", Salary: " << salary << endl;
}
bool hasAccessToSee(AbstractUser *user) const override
{
if (dynamic_cast<Student *>(user) != nullptr)
{ // not the best way
return true;
}
}
private:
int salary;
};
class Admin : public Staff
{
public:
Admin(const string &username, const string &password, int salary)
: Staff(username, password, salary) {}
void displayRole() const override
{
cout << "Admin ";
Staff::displayRole();
}
bool hasAccessToSee(AbstractUser *user) const override
{
if (dynamic_cast<Admin *>(user) == nullptr)
{ // not the best way
return true;
}
}
};
AbstractUser *current_user;
AbstractUser *users[100];
int user_cnt;
void print()
{
if (!current_user)
{
return;
}
for (AbstractUser *user : users)
{
if (current_user->hasAccessToSee(user))
{
user->displayRole();
}
}
}
void addStudent(string username, string password, int grade)
{
int index = myfind(users, users + user_cnt, [=](AbstractUser *u1)
{ return (u1->getUsername() == username); });
if (index != -1)
{
return;
}
if (dynamic_cast<Admin *>(current_user) == nullptr)
{
cout << "permision denied" << endl;
return;
}
Student *st = new Student(username, password, grade);
users[user_cnt++] = st;
}
int main()
{
Student s("st1", "pass1", 12);
Student s2("st2", "pass2", 12);
Staff st("staff1", "staffpass", 1000);
Admin a("admin1", "adminpass", 1000);
user_cnt = 4;
users[0] = &s;
users[1] = &s2;
users[2] = &st;
users[3] = &a;
while (1)
{
string username, password;
cout << "username: ";
cin >> username;
cout << "password:";
cin >> password;
int index = myfind(users, users + user_cnt, [=](AbstractUser *u1)
{ return (u1->getUsername() == username && u1->isPasswordCorrect(password)); });
if (index != -1)
{
current_user = users[index];
break;
}
}
int choice;
while (1)
{
cout << "1.print" << endl;
cout << "2.adduser" << endl;
cin >> choice;
if (choice == 1)
{
print();
}
else
{
string user, pass;
int grade;
cin >> user >> pass >> grade;
addStudent(user, pass, grade);
}
}
return 0;
}