-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathperson.cpp
More file actions
291 lines (260 loc) · 7.25 KB
/
person.cpp
File metadata and controls
291 lines (260 loc) · 7.25 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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
#include "person.h"
#include <algorithm>
#include <cassert>
#include <cmath>
#include <iostream>
#include <sstream>
#include "buy_winners_strategy.h"
#include "bank.h"
#include "calendar.h"
#include "company.h"
#include "helper.h"
#include "money.h"
#include "person.h"
#include "transfer_strategy.h"
#include "winner.h"
#include "winner_package.h"
int ribi::imcw::person::sm_current_id = 0;
ribi::imcw::person::person(
const std::string& name,
const date& end_date,
const winner_package_name package
) noexcept
:
m_buy_winners_strategy{always_buy()},
m_balance{"Personal balance of " + name},
m_bank_wallet{"BankWallet of " + name},
m_click_cards{},
m_end_date{end_date},
m_id{sm_current_id++},
m_name{name},
m_package{package},
m_shop_wallet{"ShopWallet of " + name},
m_tranfer_strategy{after_expiration_of_click_card()},
m_winners{}
{
assert(will_buy_winners(boost::gregorian::day_clock::local_day()) == true
|| will_buy_winners(boost::gregorian::day_clock::local_day()) == false
);
assert(m_end_date > boost::gregorian::day_clock::local_day());
}
void ribi::imcw::person::add_click_card(const click_card& w)
{
m_click_cards.push_back(w);
}
void ribi::imcw::person::add_winner(const winner& w)
{
m_winners.push_back(w);
}
void ribi::imcw::person::buy_click_card(
bank& the_bank,
calendar& the_calendar,
company& the_company
)
{
assert(!has_valid_click_card(the_calendar.get_today()));
assert(will_buy_click_card(the_calendar.get_today()));
the_company.buy_click_card(
*this,
m_balance,
the_bank,
the_calendar
);
//The ClickCard will be valid next month
assert(!has_valid_click_card(the_calendar.get_today()));
assert(has_valid_click_card(
the_calendar.get_today() + boost::gregorian::months(1)
)
);
}
bool ribi::imcw::person::has_account(const balance& an_account) const noexcept
{
return
an_account == m_balance
|| an_account == m_bank_wallet
|| an_account == m_shop_wallet
;
}
bool ribi::imcw::person::has_active_winners(const date& d) const noexcept
{
if (m_click_cards.empty()) return false;
const click_card& first_card = m_click_cards[0];
///Winners get active the first day of the next month after
///the purchase of a ClickCard
if (d < first_card.get_start_date() + boost::gregorian::months(1)) { return false; }
const auto cnt = std::count_if(
std::begin(m_click_cards), std::end(m_click_cards),
[d](const click_card& c) { return c.is_valid(d); }
);
return cnt > 0;
}
bool ribi::imcw::person::has_valid_click_card(const date& d) const noexcept
{
for (const auto c: m_click_cards) {
if (c.is_valid(d)) return true;
}
return false;
}
void ribi::imcw::person::process_winners(
bank& the_bank,
calendar& the_calendar,
company& the_company
)
{
const boost::gregorian::date the_day = the_calendar.get_today();
//Partition is Winners that will remain
//and Winners that will be converted to BankWallet and ShopWallet
const auto iter = std::partition(
std::begin(m_winners),
std::end(m_winners),
[](const winner& w) { return !w.is_full(); }
);
//Convert the Winners that will be converted to BankWallet and ShopWallet
std::for_each(
iter,
std::end(m_winners),
[this,&the_bank,the_calendar](winner& w)
{
assert(w.get_value() >= money(winner::max_value_euros));
#ifndef NDEBUG
const auto money_before = w.get_value();
#endif
the_bank.transfer(
w.get_balance(),
money(euros_from_full_winner_to_shopwallet),
this->m_shop_wallet,
the_calendar.get_today()
);
assert(w.get_value() > money(0.0));
assert(w.get_value() > money(40.0));
#ifndef NDEBUG
const auto money_after = w.get_value();
#endif
assert(money_after < money_before);
const money value_winner = w.get_value();
//Transfer rest from Winner to BankWallet
the_bank.transfer(
w.get_balance(),
value_winner,
this->m_bank_wallet,
the_calendar.get_today()
);
assert(w.get_value() == money(0.0));
}
);
//Remove the Winners that have been converted
m_winners.erase(iter,std::end(m_winners));
assert(
std::count_if(
std::begin(m_winners),
std::end(m_winners),
[](const winner& w) { return w.is_full(); }
) == 0
);
if (will_buy_winners(the_calendar.get_today()))
{
while (m_bank_wallet.get_value() >= money(winner::price_vat_exempt_euros))
{
#ifndef NDEBUG
const auto bank_wallet_before = m_bank_wallet.get_value();
#endif
the_company.buy_winner(*this,m_bank_wallet,the_bank,the_day);
#ifndef NDEBUG
const auto bank_wallet_after = m_bank_wallet.get_value();
assert(bank_wallet_after < bank_wallet_before);
#endif
}
}
}
void ribi::imcw::person::remove_card()
{
if (m_click_cards.empty())
{
std::stringstream s;
s << __func__
<< "Cannot remove a ClickCard when customer has none";
throw std::logic_error(s.str());
}
assert(m_click_cards.size() == 1);
m_click_cards.pop_back();
}
void ribi::imcw::person::set_winner_buy_strategy(
const std::function<bool(const date&)>& auto_buy
) noexcept
{
m_buy_winners_strategy = auto_buy;
assert(will_buy_winners(boost::gregorian::day_clock::local_day()) == true
|| will_buy_winners(boost::gregorian::day_clock::local_day()) == false
);
}
void ribi::imcw::person::transfer(bank& b, calendar& c)
{
#ifndef NDEBUG
const auto balance_before = m_balance.get_value();
#endif
const money all_bank_wallet_money = m_bank_wallet.get_value();
b.transfer(
m_bank_wallet,
all_bank_wallet_money,
m_balance,
c.get_today()
);
#ifndef NDEBUG
const auto balance_after = m_balance.get_value();
#endif
assert(balance_after >= balance_before);
assert(m_bank_wallet.get_value() == money(0.0));
}
bool ribi::imcw::person::will_buy_click_card(const date& d) const noexcept
{
using boost::gregorian::months;
if (has_valid_click_card(d)) { return false; }
if (m_click_cards.empty()) { return true; }
if (m_click_cards.back().is_valid(
d + boost::gregorian::months(2))
) {
return false;
}
return d < m_end_date;
}
bool ribi::imcw::person::will_buy_winners(const date& d) const noexcept
{
if (!has_valid_click_card(d)) return false;
return m_buy_winners_strategy(d);
}
bool ribi::imcw::person::will_tranfer(const date& d) const noexcept
{
return m_tranfer_strategy(*this,d);
}
bool ribi::imcw::operator==(const person& lhs, const person& rhs) noexcept
{
return lhs.m_id == rhs.m_id;
}
std::ostream& ribi::imcw::operator<<(std::ostream& os, const person& p) noexcept
{
std::stringstream s;
s
<< "Name: " << p.m_name << " (ID: " << p.m_id << ")\n"
<< "Auto buy: " << (p.m_buy_winners_strategy ? 'Y' : 'N') << '\n'
<< "Balance: " << p.m_balance << '\n'
<< "BankWallet: " << p.m_bank_wallet << '\n'
<< "ShopWallet: " << p.m_shop_wallet << '\n'
;
if (p.m_click_cards.empty()) {
s << "ClickCard: none" << '\n';
} else {
assert(!p.m_click_cards.empty());
s << p.m_click_cards.back() << '\n';
}
s
<< "#Winners: " << p.m_winners.size() << '\n'
;
for (const winner& w: p.m_winners) {
s << w << '\n';
}
std::string t{s.str()};
assert(!t.empty());
t.pop_back();
os << t;
return os;
}