Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- using namespace std;
- class InvalidPassword {
- private:
- char text[40];
- public:
- InvalidPassword(const char *text) {
- strcpy(this->text, text);
- }
- void message() {
- cout<< text << endl;
- }
- };
- class InvalidEmail {
- private:
- char text[40];
- public:
- InvalidEmail(const char *text) {
- strcpy(this->text, text);
- }
- void message() {
- cout<< text << endl;
- }
- };
- class MaximumSizeLimit {
- private:
- int n;
- public:
- MaximumSizeLimit(int n) {
- this->n = n;
- }
- void message() {
- cout << "You can't add more than "<< n << " users." << endl;
- }
- };
- class User {
- private:
- char username[30];
- char password[30];
- char email[30];
- public:
- User(const char *_username="", const char *_password = "", const char *_email = "") {
- strcpy(username, _username);
- setPw(_password);
- setEmail(_email);
- }
- void setPw(const char *_password) {
- int upper = 0;
- int lower = 0;
- int number = 0;
- for(int i = 0; i<strlen(_password); ++i) {
- if(isupper(_password[i]))
- upper++;
- else if(islower(_password[i]))
- lower++;
- else if(isdigit(_password[i]))
- number++;
- }
- if(!upper || !lower || !number)
- throw InvalidPassword("Password is too weak.");
- strcpy(password, _password);
- }
- void setEmail(const char *mail) {
- int count = 0;
- for(int i = 0; i <strlen(mail); i++) {
- if (mail[i] == '@')
- count++;
- }
- if(count !=1)
- throw InvalidEmail("Mail is not valid.");
- strcpy(email, mail);
- }
- virtual double popularity() = 0;
- };
- class FacebookUser: public User {
- private:
- int friends;
- int likes;
- int comments;
- public:
- FacebookUser(const char *_username="", const char *_password="", const char *_email = "", const int fr = 0, const int l = 0, const int comm = 0) : User(_username, _password, _email) {
- friends = fr;
- likes = l;
- comments = comm;
- }
- double popularity() {
- return friends + likes * 0.1 + comments * 0.5;
- }
- };
- class TwitterUser: public User {
- private:
- int followers;
- int tweets;
- static double TWEET_VALUE;
- public:
- TwitterUser(const char *_username="", const char *_password ="", const char *_email="", const int fo=0, const int tw=0):User(_username, _password, _email) {
- followers = fo;
- tweets = tw;
- }
- double popularity() {
- return followers + tweets * TWEET_VALUE;
- }
- };
- double TwitterUser::TWEET_VALUE = 0.5;
- class SocialNetwork {
- private:
- User **users;
- int n;
- static int max;
- public:
- SocialNetwork() {
- users = new User*[0];
- n = 0;
- }
- SocialNetwork &operator+=(User *u) {
- if(n == max) {
- throw(MaximumSizeLimit(max));
- }
- User **tmp = new User*[n+1];
- for (int i = 0; i < n; ++i)
- tmp[i] = users[i];
- tmp[n++] = u;
- delete[] users;
- users = tmp;
- return *this;
- }
- static void changeMaximumSize(int number) {
- SocialNetwork::max = number;
- }
- double avgPopularity() {
- double total = 0;
- for(int i = 0; i < n; ++i) {
- total+=users[i]->popularity();
- }
- return total / n;
- }
- ~SocialNetwork() {
- delete [] users;
- }
- };
- int SocialNetwork::max = 5;
- int main() {
- SocialNetwork network = SocialNetwork();
- int n;
- cin >> n;
- char username[50];
- char password[50];
- char email[50];
- int userType;
- for (int i=0; i < n; ++i) {
- cin >> username;
- cin >> password;
- cin >> email;
- cin >> userType;
- if (userType == 1) {
- int friends;
- int likes;
- int comments;
- cin >> friends >> likes >> comments;
- // TODO: Try-catch
- try{
- User * u = new FacebookUser(username,password,email,friends,likes,comments);
- network += u;
- } catch(InvalidPassword &ip){
- ip.message();
- }
- catch(InvalidEmail &i){
- i.message();
- }
- catch(MaximumSizeLimit &msl){
- msl.message();
- }
- }
- else {
- int followers;
- int tweets;
- cin >> followers >> tweets;
- // TODO: Try-catch
- try{
- User * u= new TwitterUser(username,password,email,followers,tweets);
- network += u;
- }
- catch(InvalidPassword &ip){
- ip.message();
- }
- catch(InvalidEmail &i){
- i.message();
- }
- catch(MaximumSizeLimit &msl){
- msl.message();
- }
- }
- }
- network.changeMaximumSize(6);
- cin >> username;
- cin >> password;
- cin >> email;
- int followers;
- int tweets;
- cin >> followers >> tweets;
- // TODO: Try-catch
- try{
- User * u= new TwitterUser(username,password,email,followers,tweets);
- network += u;
- }
- catch(InvalidPassword &ip){
- ip.message();
- }
- catch(InvalidEmail &i){
- i.message();
- }
- catch(MaximumSizeLimit &msl){
- msl.message();
- }
- cout << network.avgPopularity();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement