Advertisement
SKREFI

Coco-ColocviuPOOv1

Jan 30th, 2021 (edited)
100
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 6.73 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. // #include <cstdint>
  3. // #include <iostream>
  4.  
  5. // BEGIN Clasele voastre aici
  6.  
  7. using namespace std;
  8.  
  9. class AccessPoint {
  10.     bool mUnlocked;
  11.  
  12.    public:
  13.     bool isUnlocked() { return this->mUnlocked; }
  14.     void toggle() { this->mUnlocked = !this->mUnlocked; }
  15.  
  16.     AccessPoint() {
  17.         this->mUnlocked = false;
  18.     }
  19. };
  20.  
  21. class AccessDeniedException : exception {};
  22.  
  23. class User {
  24.     string mUserId;
  25.     set<uint64_t> mCards;
  26.     vector<AccessPoint*> mAccessPoints;
  27.     uint32_t mActivity;
  28.  
  29.    public:
  30.     User(const string& userId) {
  31.         this->mUserId = userId;
  32.     }
  33.  
  34.     void toggleDoor(AccessPoint* accessPoint) {
  35.         for (auto ap : this->mAccessPoints) {
  36.             if (accessPoint == ap) {  // sper ca nu o sa fie probleme la operatorul ==
  37.                 accessPoint->toggle();
  38.                 return;  // l-as face bool si return 1 daca s-a deschis si 0 daca nu
  39.             }
  40.         }
  41.         throw AccessDeniedException();
  42.     }
  43.  
  44.     void addAccessPoint(AccessPoint* accessPoint) {
  45.         if (!count(this->mAccessPoints.begin(), this->mAccessPoints.end(), accessPoint))
  46.             this->mAccessPoints.push_back(accessPoint);
  47.     }
  48.  
  49.     void addCard(uint64_t card) {
  50.         // nu trebuie sa verificam intrucat este set
  51.         this->mCards.insert(card);
  52.     }
  53.  
  54.     bool hasCard(uint64_t card) {
  55.         if (count(this->mCards.begin(), this->mCards.end(), card))
  56.             return true;
  57.         return false;
  58.     }
  59.  
  60.     uint32_t countCards() {
  61.         return this->mCards.size();
  62.     }
  63.  
  64.     uint32_t getActivity() {
  65.         return this->mActivity;
  66.     }
  67. };
  68.  
  69. class AdminUser : public User {
  70.    public:
  71.     void toggleDoor(AccessPoint* accessPoint) {
  72.         accessPoint->toggle();
  73.     }
  74.  
  75.     AdminUser(const string& userId) : User(userId){};
  76. };
  77.  
  78. class Database {
  79.     map<string, User*> mUsers;
  80.     unordered_map<uint64_t, AccessPoint*> mAccesPoints;
  81.  
  82.    public:  //sau mergea bool isAdmin
  83.     void addUser(const string& userId, char userOrAdmin) {
  84.         if (this->mUsers.find(userId) == this->mUsers.end()) {
  85.             if (userOrAdmin == 'U') {
  86.                 this->mUsers.insert(pair<string, User*>(userId, new User(userId)));
  87.             } else if (userOrAdmin == 'A') {
  88.                 this->mUsers.insert(pair<string, AdminUser*>(userId, new AdminUser(userId)));
  89.             } else {
  90.                 cout << "Error. Wrong use of parameters.\n";
  91.             }
  92.         }
  93.     }
  94.  
  95.     void addAccessPoint(uint64_t accessPoint) {
  96.         if (this->mAccesPoints.find(accessPoint) == this->mAccesPoints.end()) {
  97.             this->mAccesPoints[accessPoint] = new AccessPoint();
  98.         }
  99.     }
  100.  
  101.     void addCardToUser(const string& userId, uint64_t cardId) {
  102.         this->mUsers[userId]->addCard(cardId);
  103.     }
  104.  
  105.     void addAccessPointToUser(const string& userId, uint64_t accessPointId) {
  106.         this->mUsers[userId]->addAccessPoint(this->mAccesPoints[accessPointId]);
  107.     }
  108.  
  109.     void parseEvent(uint64_t cardId, uint64_t doorId) {
  110.         // map<string, User*>::iterator it;
  111.         for (auto const& user : this->mUsers) {
  112.             if (user.second->hasCard(doorId)) {
  113.                 this->mAccesPoints[doorId]->toggle();
  114.                 return;
  115.             }
  116.         }
  117.         throw AccessDeniedException();
  118.     }
  119.  
  120.     uint32_t countUnlockedDoors() {
  121.         uint32_t total = 0;
  122.         for (auto const& door : this->mAccesPoints)
  123.             total += int(door.second->isUnlocked());
  124.         return total;
  125.     }
  126.  
  127.     uint32_t countActivity() {
  128.         uint32_t total = 0;
  129.         for (auto const& user : this->mUsers)
  130.             total += user.second->getActivity();
  131.         return total;
  132.     }
  133.  
  134.     vector<string> findUsersWithNoCards() {
  135.         vector<string> users;
  136.         for (auto const& user : this->mUsers)
  137.             if (user.second->countCards() == 0)
  138.                 users.push_back(user.first);
  139.         return users;
  140.     }
  141.  
  142.     uint32_t countCards() {
  143.         uint32_t total = 0;
  144.         for (auto const& user : this->mUsers)
  145.             total += user.second->countCards();
  146.         return total;
  147.     }
  148. };
  149.  
  150. // END Clasele voastre aici
  151.  
  152. int main() {
  153.     // AccessPoints / Users / Events
  154.     cout << "N, M, K: ";
  155.     uint32_t n, m, k;
  156.     std::cin >> n >> m >> k;
  157.     Database database;
  158.     // pentru fiecare usa
  159.     while (n--) {
  160.         // creeam un acces point si-l adaugam in db
  161.         cout << "Insert ID for door no. " << n << ": ";
  162.         uint64_t accessPointId;
  163.         std::cin >> accessPointId;
  164.         database.addAccessPoint(accessPointId);
  165.     }
  166.     while (m--) {
  167.         std::string userId;
  168.         char type;
  169.         cout << "Insert UserId and Type (U/A) for user no. " << m << ": ";
  170.         std::cin >> userId >> type;
  171.         database.addUser(userId, type);
  172.         uint32_t c;
  173.         cout << "Inser number of cards for the user with ID: " << userId << ": ";
  174.         std::cin >> c;
  175.         while (c--) {
  176.             cout << "Insert ID for card no. " << c << " of user with ID: " << userId << ": ";
  177.             uint64_t cardId;
  178.             std::cin >> cardId;
  179.             database.addCardToUser(userId, cardId);
  180.         }
  181.         uint32_t u;
  182.         cout << "Inser number of doors for the user with ID: " << userId << ": ";
  183.         std::cin >> u;
  184.         while (u--) {
  185.             cout << "Insert ID for door no. " << u << " of user with ID: " << userId << ": ";
  186.             uint64_t accessPointId;
  187.             std::cin >> accessPointId;
  188.             database.addAccessPointToUser(userId, accessPointId);
  189.         }
  190.     }
  191.     uint32_t errors = 0;
  192.  
  193.     while (k--) {
  194.         uint64_t accessPointId, cardId;
  195.         cout << "Event no. " << k << ".\nInsert accesPointId and cardId: ";
  196.         std::cin >> accessPointId >> cardId;
  197.         try {
  198.             database.parseEvent(cardId, accessPointId);
  199.         } catch (AccessDeniedException& ex) {
  200.             errors++;
  201.         }
  202.     }
  203.  
  204.     cout << "Subiect: ";
  205.     uint32_t subject;
  206.     std::cin >> subject;
  207.     switch (subject) {
  208.         case 1: {
  209.             std::cout << database.countUnlockedDoors();
  210.             break;
  211.         }
  212.         case 2: {
  213.             std::cout << database.countActivity();
  214.             break;
  215.         }
  216.         case 3: {
  217.             auto users = database.findUsersWithNoCards();
  218.             // afisam newline daca nu exista
  219.             if (users.size() == 0) {
  220.                 cout << '\n';
  221.             }
  222.             for (const auto& user : users) std::cout << user << " ";
  223.             break;
  224.         }
  225.         case 4: {
  226.             std::cout << database.countCards();
  227.             break;
  228.         }
  229.         case 5: {
  230.             std::cout << errors;
  231.             break;
  232.         }
  233.     }
  234.     return 0;
  235. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement