-
Notifications
You must be signed in to change notification settings - Fork 34
Expand file tree
/
Copy pathTest.cpp
More file actions
159 lines (132 loc) · 3.36 KB
/
Test.cpp
File metadata and controls
159 lines (132 loc) · 3.36 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
#include <iostream>
#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++) {
?
}
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;//doesnt have access for any type
}
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 (? != nullptr) {
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();
}
?
};
AbstractUser* current_user;
AbstractUser* users[100];
int user_cnt;
void print() {
if (!current_user) {
return;
}
for (AbstractUser* user : users) {
if (?) {
user->displayRole();
}
}
}
void addStudent(string username, string password, int grade) {
int index = ?;
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("erfan", "pass1", 12);
Student s2("rasool", "pass2", 12);
Staff st("alireza", "staffpass", 1000);
Staff st("kiyan", "staffpass", 1000);
Admin a("yalda", "adminpass",1200);
user_cnt = 5;
users[0] = &s;
?
?
?
?
while (1) {
string username, password;
cout << "username: ";
cin >> username;
cout << "password:";
cin >> password;
int index = myfind(users, users + user_cnt, ?);
if (index!=-1) {
current_user = users[index];
break;
}
}
int choice;
while (1) {
cout << "1.print" << endl;
cout<<"2.add student"<<endl;
cin >> choice;
if (choice == 1) {
print();
}
else {
string user, pass;
int grade;
cin >> user >> pass >> grade;
addStudent(user, pass, grade);
}
}
return 0;
}