blob: cf4b291bafebe9bcd3728506f52e789a2db95eb4 (
plain)
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
|
#include "person.hh"
#include <algorithm>
Person::Person(const std::string& name, uint money)
: name_{ name }
, money_{ money }
{}
std::vector<std::string> Person::enumerate_nfts() const
{
std::vector<std::string> res;
for (auto nft = nfts_.begin(); nft != nfts_.end(); nft++)
{
res.push_back(**nft);
}
return res;
}
void Person::add_nft(NFT nft)
{
nfts_.push_back(std::move(nft));
}
NFT Person::remove_nft(const std::string& name)
{
auto pos = std::find_if(nfts_.begin(), nfts_.end(),
[&](NFT const& nft) { return *nft == name; });
if (pos == nfts_.end())
return create_empty_nft();
nfts_.erase(pos);
return create_nft(name);
}
void Person::add_money(uint money)
{
money_ += money;
}
bool Person::remove_money(uint money)
{
if (money > money_)
return false;
money_ -= money;
return true;
}
uint Person::get_money() const
{
return money_;
}
const std::string& Person::get_name() const
{
return name_;
}
std::ostream& operator<<(std::ostream& os, const Person& p)
{
os << "Name: " << p.get_name() << "\nMoney: " << p.get_money() << "\nNFTs:";
if (const std::vector<std::string> nfts = p.enumerate_nfts(); !nfts.empty())
{
for (size_t i = 0; i < nfts.size(); ++i)
{
os << " " << nfts.at(i);
}
}
os << "\n";
return os;
}
|