-
Notifications
You must be signed in to change notification settings - Fork 0
/
Company.cpp
67 lines (57 loc) · 1.72 KB
/
Company.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
#include <ostream>
#include <string>
#include <map>
#include "Definitions.h"
//default c'tor
Company::Company() {
}
//insertion operator.
void Company::operator+=(Employee* toInsert) {
if (toInsert == NULL) {
//if we need to insert null, it is illegal!!
throw IllegalArguments();
}
else if(!this->contains(toInsert->getID())) {
//if we don't have the id that we need to insert in the structure, we will insert.
Employee* copy = toInsert->clone();
std::pair<int, Employee*> pair(copy->getID(), copy);
this->container.insert(pair);
}
}
void Company::operator-=(int id) {
//getting the iterator of the givven employee id.
std::map<int, Employee*>::iterator element = this->container.find(id);
if (element != this->container.end()) {
//if the id is in the structure, we will remove the employee ffrom the structure
Employee* valueOfElement = (*element).second;
this->container.erase(element);
delete valueOfElement;
}
}
Employee* Company::operator[](int id) {
//regular find method.
std::map<int, Employee*>::iterator element = this->container.find(id);
if (element != this->container.end()) {
return (*element).second->clone();
}
else {
return NULL;
}
}
bool Company::contains(int id) {
//regular contains method.
return (*this)[id] != NULL;
}
std::ostream& operator<<(std::ostream& out, Company& toPrint) {
//operator <<
if (toPrint.container.empty()) {
out << "Company is empty" << std::endl;
}
else {
out << "Company employees: " << std::endl;
for (std::map<int, Employee*>::iterator element = toPrint.container.begin();element!=toPrint.container.end();++element) {
out << *((*element).second) <<std::endl;
}
}
return out;
}