-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPerson.cpp
More file actions
116 lines (104 loc) · 2.18 KB
/
Copy pathPerson.cpp
File metadata and controls
116 lines (104 loc) · 2.18 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
#include "Person.h"
namespace codesmith
{
bool Person::operator==(const Person& other)
{
// Really naive implementation, checks only the name for equality
// Should be updated to check other properties if needed, left for
// "future expansion" :-)
return(m_name == other.m_name);
}
void Person::operator=(const Person& other)
{
m_id = other.m_id;
m_name = other.m_name;
m_age = other.m_age;
m_height = other.m_height;
m_gender = other.m_gender;
m_hobbies = other.m_hobbies;
}
void Person::addHobby(const Hobby& hobby)
{
// Hobby should be unique, no duplicates
if (!hasHobby(hobby)) {
m_hobbies.push_back(hobby);
}
}
void Person::addHobby(const std::string& hobbyName)
{
// Hobby should be unique, no duplicates
if (!hasHobby(hobbyName)) {
m_hobbies.push_back(Hobby(hobbyName));
}
}
void Person::addHobbies(const std::vector<std::string> hobbyNames)
{
for (auto n : hobbyNames) {
addHobby(n);
}
}
void Person::removeHobby(const std::string& hobbyName)
{
Hobby tmp(hobbyName);
removeHobby(tmp);
}
void Person::removeHobby(const Hobby& hobby)
{
auto it = getHobbyIter(hobby);
if (it != m_hobbies.end()) {
m_hobbies.erase(it);
}
}
bool Person::hasHobby(const std::string& hobbyName)
{
Hobby h(hobbyName); // temp hobby for searching
if (hasHobby(h)) {
return true;
}
else {
return false;
}
}
bool Person::hasHobby(const Hobby& hobby)
{
std::vector<Hobby>::iterator it = getHobbyIter(hobby);
if (it != m_hobbies.end()) {
return true;
}
else {
return false;
}
}
bool Person::hasAllHobbies(const std::vector<std::string>& hobbyNames)
{
bool res = true;
for (auto n : hobbyNames) {
if (!hasHobby(n)) {
res = false;
break;
}
}
return res;
}
bool Person::hasAllHobbies(const std::vector<Hobby>& hobbies)
{
bool res = true;
for (auto h : hobbies) {
if (!hasHobby(h)) {
res = false;
break;
}
}
return res;
}
std::vector<Hobby>::iterator Person::getHobbyIter(const Hobby& hobby)
{
std::vector<Hobby>::iterator it = m_hobbies.begin();
for (; it != m_hobbies.end(); it++) {
if ((*it) == hobby) {
break;
}
}
return it;
}
} // namespace codesmith