Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
89 changes: 64 additions & 25 deletions Test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@
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++) {
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;

return -1;
}

class AbstractUser {
public:
AbstractUser(const string& username, const string& password)
: username(username), password(password) {}
: username(username), password(password) {
}

virtual ~AbstractUser() = default;

Expand All @@ -39,7 +42,8 @@ class AbstractUser {
class Student : public AbstractUser {
public:
Student(const string& username, const string& password, int grade)
: AbstractUser(username, password), grade(grade) {}
: AbstractUser(username, password), grade(grade) {
}

void displayRole() const override {
cout << "Student: " << username << ", Grade: " << grade << endl;
Expand All @@ -55,15 +59,19 @@ class Student : public AbstractUser {
class Staff : public AbstractUser {
public:
Staff(const string& username, const string& password, int salary)
: AbstractUser(username, password), salary(salary) {}
: AbstractUser(username, password), salary(salary) {
}

void displayRole() const override {
cout << "Staff: " << username << ", Salary: " << salary << endl;
}
bool hasAccessToSee(AbstractUser* user) const override {
if (? != nullptr) {
if (user != nullptr && dynamic_cast<Student*>(user) != nullptr) {
return true;
}
else {
return false;
}
}

private:
Expand All @@ -73,14 +81,24 @@ class Staff : public AbstractUser {
class Admin : public Staff {
public:
Admin(const string& username, const string& password, int salary)
: Staff(username, password, salary) {}
: Staff(username, password, salary) {
}

void displayRole() const override {

cout << "Admin ";
Staff::displayRole();
}
?

bool hasAccessToSee(AbstractUser* user) const override {
if (user != nullptr && dynamic_cast<Admin*>(user) != nullptr) {
return true;
}
else {
return false;
}
}

};

AbstractUser* current_user;
Expand All @@ -92,46 +110,67 @@ void print() {
return;
}
for (AbstractUser* user : users) {
if (?) {
if (user != nullptr) {
user->displayRole();
}
}
}

void addStudent(string username, string password, int grade) {
int index = ?;

int index = -1;
for (int i = 0; users[i] != nullptr && i < 100; i++) {
index = i;
user_cnt = i;
}

if (index != -1) {
return;
}
if (dynamic_cast<Admin*>(current_user) == nullptr){
cout << "permision denied"<<endl;

if (dynamic_cast<Admin*>(current_user) == nullptr) {
cout << "permision denied" << endl;
return;
}
Student* st = new Student(username, password, grade);
users[user_cnt ++] = st;
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);
Staff st2("kiyan", "staffpass", 1000);
Admin a("yalda", "adminpass", 1200);

user_cnt = 5;
users[0] = &s;
?
?
?
?
users[1] = &s2;
users[2] = &st;
users[3] = &st2;
users[4] = &a;

while (1) {

string username, password;
cout << "username: ";
cin >> username;
cout << "password:";
cin >> password;
int index = myfind(users, users + user_cnt, ?);
if (index!=-1) {

auto is_existed = [username](AbstractUser* user) {

for (int i = 0; users[i] != nullptr && i < 100; i++) {
if (users[i]->getUsername() == username) {
return true;
}
}
return false;
};

int index = myfind(users, users + user_cnt, is_existed);
if (index != -1) {
current_user = users[index];
break;
}
Expand All @@ -140,7 +179,7 @@ int main() {
int choice;
while (1) {
cout << "1.print" << endl;
cout<<"2.add student"<<endl;
cout << "2.add student" << endl;
cin >> choice;
if (choice == 1) {
print();
Expand Down