-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathSocialNetwork.cpp
More file actions
121 lines (107 loc) · 2.65 KB
/
Copy pathSocialNetwork.cpp
File metadata and controls
121 lines (107 loc) · 2.65 KB
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
#include "SocialNetwork.h"
#include "Friendships.h"
#include "PersonNotFoundException.h"
namespace codesmith {
void SocialNetwork::clear()
{
m_friendships.clear();
m_people.clear();
}
void SocialNetwork::addPerson(std::shared_ptr<Person> person)
{
if (!hasPerson((*person).getId())) {
(*person).setId(++m_lastPersonId);
m_people.push_back(person);
}
}
void SocialNetwork::removePerson(int id)
{
std::vector<std::shared_ptr<Person>>::iterator it =
findPersonIterById(id);
if (it != m_people.end()) {
m_people.erase(it);
}
}
std::vector<std::shared_ptr<Person>> SocialNetwork::searchPersonByAge(
int age)
{
std::vector<std::shared_ptr<Person>> result;
for (auto it = m_people.begin(); it != m_people.end(); ++it) {
Person& person = *(*it);
if (person.getAge() == age) {
result.push_back(*it);
}
}
return result;
}
std::vector<std::shared_ptr<Person>> SocialNetwork::searchPersonByName(const std::string& name)
{
std::vector<std::shared_ptr<Person>> result;
for (auto it = m_people.begin(); it != m_people.end(); ++it) {
Person& person = *(*it);
if (person.getName() == name) {
result.push_back(*it);
}
}
return result;
}
std::vector<std::shared_ptr<Person>> SocialNetwork::searchPersonByHobbies(const std::vector<Hobby>& hobbies)
{
std::vector<std::shared_ptr<Person>> result;
for (auto it = m_people.begin(); it != m_people.end(); ++it) {
Person& person = *(*it);
if (person.hasAllHobbies(hobbies)) {
result.push_back(*it);
}
}
return result;
}
std::vector<std::shared_ptr<Person>> SocialNetwork::searchPersonByHobbies(const std::vector<std::string>& hobbyNames)
{
std::vector<std::shared_ptr<Person>> result;
for (auto it = m_people.begin(); it != m_people.end(); ++it) {
Person& person = *(*it);
if (person.hasAllHobbies(hobbyNames)) {
result.push_back(*it);
}
}
return result;
}
std::shared_ptr<Person> SocialNetwork::getPerson(int id)
{
std::vector<std::shared_ptr<Person>>::iterator it =
findPersonIterById(id);
if (it != m_people.end()) {
return *it;
}
else {
throw PersonNotFoundException(id);
}
}
bool SocialNetwork::hasPerson(int id)
{
bool res = false;
for (auto p : m_people) {
if ((*p).getId() == id) {
res = true;
break;
}
}
return res;
}
std::vector<int> SocialNetwork::getFriendsOfPerson(const Person& person)
{
return m_friendships.getFriends(person);
}
std::vector<std::shared_ptr<Person>>::iterator SocialNetwork::findPersonIterById(int id)
{
auto it = m_people.begin();
for (; it != m_people.end(); it++) {
Person& p = *(*it);
if (p.getId() == id) {
break;
}
}
return it;
}
}