/
Cargo.cpp
122 lines (96 loc) · 2.87 KB
/
Cargo.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
#include <cassert>
#include <sstream>
//#include <vector>
#include "Cargo.hpp"
Cargo::Cargo() : cargo(std::map<CargoType, unsigned int>()) {}
Cargo::~Cargo()
{
}
void Cargo::addCargo(const CargoType& type, unsigned int quantity) {
assert(quantity != 0);
Cargo::iterator current = this->cargo.find(type);
if (current != this->end()) {
quantity += current->second;
}
this->cargo[type] = quantity;
}
void Cargo::addCargo(const Cargo& newCargo) {
if(newCargo.cargo.empty()){return;}
Cargo::const_iterator newStock;
for (newStock = newCargo.begin(); newStock != newCargo.end(); ++newStock)
{
this->addCargo(newStock->first, newStock->second);
}
}
bool Cargo::delCargo(const Cargo& newCargo) {
if (this == &newCargo) {
this->cargo.clear();
return true;
}
if(newCargo.cargo.empty()){
return true;
}
if (this->contains(newCargo) == false) {
return false;
}
for (Cargo::const_iterator newStock = newCargo.begin(); newStock != newCargo.end(); ++newStock) {
this->cargo[newStock->first] -= newStock->second;
if (this->cargo[newStock->first] == 0) {
this->cargo.erase(newStock->first);
}
}
return true;
}
unsigned int Cargo::getCount(const CargoType& type) const {
unsigned int result = 0;
std::map<CargoType, unsigned int>::const_iterator thing = this->cargo.find(type);
if (thing != this->cargo.end()){
result = thing->second;
}
return result;
}
bool Cargo::contains(const Cargo& newCargo) const {
if (newCargo.cargo.empty()) {
return true;
}
if (this->cargo.empty()) {
return false;
}
Cargo::const_iterator newStock;
for (newStock = newCargo.begin(); newStock != newCargo.end(); ++newStock) {
if (this->cargo.find(newStock->first) == this->cargo.end()) {
return false;
}
#if __cplusplus > 199711L
if (this->cargo.at(newStock->first) < newStock->second) {return false;}
#else
if (this->cargo.find(newStock->first)->second< newStock->second) {return false;}
#endif
}
return true;
}
bool Cargo::operator==(const Cargo &that) const {
return this->cargo == that.cargo;
}
/** String representation of this Cargo
* @by ezee */
std::string Cargo::getName() const
{
for(Cargo::const_iterator i = this->cargo.begin();
i != this->cargo.end(); ++i)
{return i->first.getName();}
}
std::string Cargo::getXML() const {
std::stringstream xmlstream;
xmlstream<<"<Cargo>";
for(Cargo::const_iterator i = this->cargo.begin();
i != this->cargo.end(); ++i){
xmlstream<<"<pair>"<<i->first.getXML()<<"<value>"<<i->second<<"</value></pair>";
}
xmlstream<<"</Cargo>";
return xmlstream.str();
}
Cargo::const_iterator Cargo::begin() const { return this->cargo.begin(); }
Cargo::iterator Cargo::begin() { return this->cargo.begin(); }
Cargo::const_iterator Cargo::end() const { return this->cargo.end(); }
Cargo::iterator Cargo::end() { return this->cargo.end(); }