diff --git a/q1.cpp b/q1.cpp new file mode 100644 index 0000000..90ecae7 --- /dev/null +++ b/q1.cpp @@ -0,0 +1,170 @@ +#include + #include + using namespace std; + const int MAX = 100; + + template + 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(user) != 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(); + } + + bool hasAccessToSee(AbstractUser* user) const override { + if (dynamic_cast(user) != nullptr) { + return true; + } + + if(dynamic_cast(user) != nullptr) { + 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* u) { + return u->getUsername() == username;}); + + if (index != -1) { + return; + } + if (dynamic_cast(current_user) == nullptr){ + cout << "permision denied"<> username; + cout << "password:"; + cin >> password; + int index = myfind(users, users + user_cnt, [&](AbstractUser* u) {return u->getUsername() == username && u->isPasswordCorrect(password);}); + if (index!=-1) { + current_user = users[index]; + break; + } + + } + int choice; + while (1) { + cout << "1.print" << endl; + cout<<"2.add student"<> choice; + if (choice == 1) { + print(); + } + else { + string user, pass; + int grade; + cin >> user >> pass >> grade; + addStudent(user, pass, grade); + + } + + } + + return 0; + } \ No newline at end of file