-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFacebook.cpp
executable file
·152 lines (134 loc) · 3.93 KB
/
Facebook.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
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
#include "Facebook.h"
Facebook::Facebook()
{
}
Facebook::~Facebook()
{
for (vector<User*>::iterator currUser=this->users.begin();currUser<this->users.end();currUser++)
delete *currUser;
}
void Facebook::addPerson(Person& newPers)
{
this->users.push_back(&newPers);
}
void Facebook::addFanPage(FanPage &fpage)
{
this->users.push_back(&fpage);
}
void Facebook::addStatus(Status& newStatus)
{
// This function adding new status.
this->allStatuses.push_back(&newStatus);
}
void Facebook::addStatusToUser(const string& userName, const string& statusContent, STATUSTYPE statusType,USERTYPE userType) throw (const char*)
{
User* user=findUser(userName,userType);
if (user!=nullptr)
{
user->addStatus(statusType, statusContent);
this->addStatus(*user->getPastStatus()[user->getNumOfStatuses()-1]);
}
else
throw "User not exist.";
}
User* Facebook::findUser(const string& userName,USERTYPE userType) const
{
vector<User*>::const_iterator currUser=this->users.begin();
while (currUser<this->users.end() && (userName.compare((*currUser)->getName())!=0 || getUserType(*(*currUser))!=userType))
currUser++;
if (currUser<this->users.end())
return *currUser;
else
{
return nullptr;
}
}
void Facebook::linkFriends(Person &p1, Person &p2) throw (const char*)
{
// This function link 2 persons.
if (areEquals(p1,p2))
throw "Person can't be friend with himself";
if (!p1.isPersonFriend(p2))
{
p1+=p2;
p2+=p1;
}
else
throw "Already linked.";
}
void Facebook::addFanToFanPage(Person &pers, FanPage &fPage) throw (const char*)
{
// This function add person to fan page,
// and add fan page to person.
if (pers.isSubToFP(fPage))
throw "Already subscriped.";
pers.addFanPage(fPage);
fPage+=pers;
}
void Facebook::showAllPersons() const
{
// This function show all persons.
for(vector<User*>::const_iterator currUser=this->users.begin();currUser<this->users.end();currUser++)
{
if (getUserType(**currUser)==PERSON)
(*currUser)->show();
}
}
void Facebook::showAllFanPages() const
{
// This function show all fan pages.
for(vector<User*>::const_iterator currUser=this->users.begin();currUser<this->users.end();currUser++)
{
if (getUserType(**currUser)==FANPAGE)
(*currUser)->show();
}
}
void Facebook::showAll() const
{
// This function show all users.
this->showAllPersons();
this->showAllFanPages();
}
void Facebook::showPersonsFriendsStatuses(const string& personName,int numOfStatuses) const
{ // This function show numOfStatuses (parameter) statuses of person's friends.
int statusesShowed=0;
vector<Status*>::const_iterator currStatus=this->allStatuses.begin();
Person* pers=dynamic_cast<Person*>(findUser(personName,PERSON));
Person* currWriter;
while (currStatus<this->allStatuses.end() && statusesShowed<numOfStatuses)
{
currWriter=dynamic_cast<Person*>(this->findUser((*currStatus)->getWriter(),PERSON));
if (currWriter!=nullptr)
{
if (pers->isPersonFriend(*currWriter))
{
(*currStatus)->show();
statusesShowed++;
}
}
currStatus++;
}
}
bool Facebook::isUserExist(const string& name,USERTYPE userType)
{
// Check if the user exist.
return findUser(name,userType)!=nullptr;
}
USERTYPE getUserType(User& user)
{
if (strcmp(typeid(user).name()+1,"Person")==0)
return PERSON;
else if (strcmp(typeid(user).name()+1,"FanPage")==0)
return FANPAGE;
return USER;
}
void checkUserType(int userTypeNum) throw (const char*)
{
if(userTypeNum>FANPAGE || userTypeNum<PERSON)
throw "Invalid user type.";
}
void checkStatusType(int statusType) throw (const char*)
{
if(statusType>TEXT_AND_VIDEO || statusType<VIDEO)
throw "Invalid status type.";
}