StefiIOE

Social Network

Jun 2nd, 2020
187
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include<iostream>
  2. #include<cstring>
  3. using namespace std;
  4.  
  5. class InvalidPassword {
  6. private:
  7.   char error[50];
  8. public:
  9.   InvalidPassword(const char * er) {
  10.     strcpy(error,er);
  11.   }
  12.   void message() {
  13.     cout << error << endl;
  14.   }
  15. };
  16.  
  17. class InvalidEmail {
  18. private:
  19.   char error[50];
  20. public:
  21.   InvalidEmail(const char * er) {
  22.     strcpy(error,er);
  23.   }
  24.   void message() {
  25.     cout << error << endl;
  26.   }
  27. };
  28.  
  29. class MaximumSizeLimit {
  30. private:
  31.   int n;
  32. public:
  33.   MaximumSizeLimit(int num) {
  34.     n = num;
  35.   }
  36.  
  37.   void message() {
  38.     cout << "You can't add more than " << n << " users." << endl;
  39.   }
  40. };
  41.  
  42. class User {
  43. private:
  44.   char username[30];
  45.   char password[30];
  46.   char email[30];
  47. public:
  48.   User(const char* _username="", const char * _password="", const char * _email="") {
  49.     strcpy(username,_username);
  50.     setPassword(_password);
  51.     setEmail(_email);
  52.   }
  53.  
  54.   void setPassword(const char * _password) {
  55.     int upper = 0;
  56.     int lower = 0;
  57.     int number = 0;
  58.     for (int i=0; i < strlen(_password); ++i) {
  59.       if (isupper(_password[i]))
  60.         upper++;
  61.       else if (islower(_password[i]))
  62.         lower++;
  63.       else if (isdigit(_password[i]))
  64.         number++;
  65.     }
  66.     if (!upper || !lower || !number)
  67.       throw InvalidPassword("Password is too weak.");
  68.     strcpy(password,_password);
  69.   }
  70.  
  71.   void setEmail(const char * mail) {
  72.     int contains = 0;
  73.       for (int i=0; i < strlen(mail); ++i) {
  74.         if (mail[i] == '@')
  75.             contains++;
  76.       }
  77.       if (contains != 1)
  78.       throw InvalidEmail("Mail is not valid.");
  79.     strcpy(email,mail);
  80.   }
  81.  
  82.   virtual double popularity() = 0;
  83. };
  84.  
  85. class FacebookUser : public User {
  86. private:
  87.   int friends;
  88.   int likes;
  89.   int comments;
  90. public:
  91.   FacebookUser(const char* _username="", const char * _password="", const char * _email="",
  92.                const int fr = 0, const int l = 0, const int comm = 0) : User(_username,_password,_email) {
  93.                  friends = fr;
  94.                  likes = l;
  95.                  comments = comm;
  96.                }
  97.   double popularity() {
  98.     return friends + 0.1 * likes + 0.5 * comments;
  99.   }
  100. };
  101.  
  102. class TwitterUser : public User {
  103. private:
  104.   int followers;
  105.   int tweets;
  106.   static double TWEET_VALUE;
  107. public:
  108.   TwitterUser(const char* _username="", const char * _password="", const char * _email="",
  109.                const int fl = 0, const int tw = 0) : User(_username,_password,_email) {
  110.                  followers = fl;
  111.                  tweets = tw;
  112.                }
  113.   double popularity() {
  114.     return followers + tweets * TWEET_VALUE;
  115.   }
  116.  
  117. };
  118.  
  119. double TwitterUser::TWEET_VALUE = 0.5;
  120.  
  121. class SocialNetwork {
  122.  
  123. private:
  124.   static int MAXIMUM_SIZE;
  125.   User ** users;
  126.   int currentUsers;
  127. public:
  128.   SocialNetwork(){
  129.     users = new User*[0];
  130.     currentUsers = 0;
  131.   }
  132.  
  133.   SocialNetwork& operator += (User * u) {
  134.     if (currentUsers == MAXIMUM_SIZE)
  135.       throw MaximumSizeLimit(MAXIMUM_SIZE);
  136.     User ** tmp = new User*[currentUsers + 1];
  137.     for (int i=0; i < currentUsers; ++i) {
  138.       tmp[i] = users[i];
  139.     }
  140.     tmp[currentUsers++] = u;
  141.     delete [] users;
  142.     users = tmp;
  143.     return *this;
  144.   }
  145.  
  146.   static void changeMaximumSize(int number) {
  147.     MAXIMUM_SIZE = number;
  148.   }
  149.  
  150.   double avgPopularity() {
  151.     double pop = 0;
  152.     for (int i=0; i < currentUsers; ++i) {
  153.       pop += users[i] -> popularity();
  154.       //cout << users[i] -> popularity() << endl;
  155.     }
  156.     return pop / currentUsers;
  157.   }
  158.  
  159.   ~SocialNetwork() {
  160.     delete [] users;
  161.   }
  162.  
  163. };
  164.  
  165. int SocialNetwork::MAXIMUM_SIZE = 5;
  166.  
  167. int main() {
  168.  
  169.   SocialNetwork network = SocialNetwork();
  170.     int n;
  171.     cin >> n;
  172.     char username[50];
  173.     char password[50];
  174.     char email[50];
  175.     int userType;
  176.     for (int i=0; i < n; ++i) {
  177.       cin >> username;
  178.       cin >> password;
  179.       cin >> email;
  180.       cin >> userType;
  181.       if (userType == 1) {
  182.         int friends;
  183.         int likes;
  184.         int comments;
  185.         cin >> friends >> likes >> comments;
  186.         try {
  187.         User * u = new FacebookUser(username,password,email,friends,likes,comments);
  188.         network += u;
  189.       } catch(InvalidPassword & e) {
  190.         e.message();
  191.       }
  192.       catch (InvalidEmail &e) {
  193.         e.message();
  194.       }
  195.       catch(MaximumSizeLimit &e) {
  196.         e.message();
  197.       }
  198.       }
  199.       else {
  200.         int followers;
  201.         int tweets;
  202.         cin >> followers >> tweets;
  203.         try {
  204.         User * u= new TwitterUser(username,password,email,followers,tweets);
  205.         network += u;
  206.       }catch(InvalidPassword & e) {
  207.         e.message();
  208.       }
  209.       catch (InvalidEmail &e) {
  210.         e.message();
  211.       }
  212.       catch(MaximumSizeLimit &e) {
  213.         e.message();
  214.       }
  215.       }
  216.  
  217.     }
  218.     network.changeMaximumSize(6);
  219.     cin >> username;
  220.     cin >> password;
  221.     cin >> email;
  222.     int followers;
  223.     int tweets;
  224.     cin >> followers >> tweets;
  225.     try {
  226.     User * u= new TwitterUser(username,password,email,followers,tweets);
  227.     network += u;
  228.   }catch(InvalidPassword & e) {
  229.     e.message();
  230.   }
  231.   catch (InvalidEmail &e) {
  232.     e.message();
  233.   }
  234.   catch(MaximumSizeLimit &e) {
  235.     e.message();
  236.   }
  237.     cout << network.avgPopularity();
  238.  
  239. }
RAW Paste Data