diff --git a/Test.cpp b/Test.cpp index 6646d08..e88d3c6 100644 --- a/Test.cpp +++ b/Test.cpp @@ -3,21 +3,24 @@ using namespace std; const int MAX = 100; -template -int myfind(T* begin,T* end,F f) { - for (T* itr = begin; itr != end; itr++) { +template - ? +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; @@ -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; @@ -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(user) != nullptr) { return true; } + else { + return false; + } } private: @@ -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(user) != nullptr) { + return true; + } + else { + return false; + } + } + }; AbstractUser* current_user; @@ -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(current_user) == nullptr){ - cout << "permision denied"<(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; } @@ -140,7 +179,7 @@ int main() { int choice; while (1) { cout << "1.print" << endl; - cout<<"2.add student"<> choice; if (choice == 1) { print();