-
Notifications
You must be signed in to change notification settings - Fork 0
/
Member.cpp
105 lines (86 loc) · 1.79 KB
/
Member.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
#include "Member.h"
#include <vector>
#include <iostream>
using namespace std;
//init static members
int Member::counter = 0;
static int id = 0;
//constructor
Member::Member(): followers(0), following(0)
{
this->counter++;
vector<Member*> myMemberFollow;
vector<Member*> myMemberFollower;
id++;
this->idMember = id;
}
void Member::follow(Member& member)
{
if(this->idMember != member.idMember){
if(!this->myMemberFollow.empty()){
int i =findIndex(myMemberFollow,member);
if(i ==-1){
this->following++;
member.followers++;
this->myMemberFollow.push_back(&member);
member.myMemberFollower.push_back(this);
}
}
else{
this->following++;
member.followers++;
this->myMemberFollow.push_back(&member);
member.myMemberFollower.push_back(this);
}
}
}
void Member::unfollow(Member& member)
{
if(!this->myMemberFollow.empty()){
int i =findIndex(myMemberFollow,member);
if(i !=-1){
this->following--;
member.followers--;
this->myMemberFollow.erase(myMemberFollow.begin() + i);
int j = findIndex(member.myMemberFollower, *this);
member.myMemberFollower.erase(member.myMemberFollower.begin() + j);
}
}
}
int Member::findIndex(vector<Member*> vec, Member& member){
int i = 0;
while(i < vec.size() && vec[i]->idMember != member.idMember){
i++;
}
if(i != vec.size()){
return i;
}
else{
return -1;
}
}
void Member::setFollowers(Member& member, int num){
member.followers = num;
}
int Member::numFollowers()
{
return this->myMemberFollower.size();
}
int Member::numFollowing()
{
return this->myMemberFollow.size();
}
int Member::count()
{
return counter;
}
Member::~Member()
{
while(!myMemberFollow.empty()){
unfollow(*myMemberFollow[0]);
}
while(!myMemberFollower.empty()){
myMemberFollower[0]->unfollow(*this);
}
counter--;
}