forked from pezy/CppPrimer
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex15_30_Basket.h
47 lines (37 loc) · 1.42 KB
/
ex15_30_Basket.h
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
/*
=================================================================================
C++ Primer 5th Exercise Answer Source Code
Copyright (C) 2014-2015 https://github.com/pezy/Cpp-Primer
Basket.
If you have questions, try to connect with me: pezy<[email protected]>
=================================================================================
*/
#ifndef CP5_EX15_30_BASKET_H
#define CP5_EX15_30_BASKET_H
#include <memory>
#include <set>
#include "ex15_30_Quote_Bulk_quote.h"
namespace EX30 {
using std::shared_ptr;
class Basket {
public:
Basket() = default;
void add_item(const Quote &sale) { items.insert(shared_ptr<Quote>(sale.clone())); }
void add_item(Quote &&sale) { items.insert(shared_ptr<Quote>(std::move(sale).clone())); }
inline double total_receipt(std::ostream&) const;
private:
static bool compare(const shared_ptr<Quote> &lhs, const shared_ptr<Quote> &rhs) {
return lhs->isbn() < rhs->isbn();
}
std::multiset<shared_ptr<Quote>, decltype(compare)*> items{compare};
};
inline double Basket::total_receipt(std::ostream &os) const {
auto sum = 0.0;
for (auto iter = items.cbegin(); iter != items.cend(); iter = items.upper_bound(*iter)) {
sum += print_total(os, **iter, items.count(*iter));
}
os << "Total Sale: " << sum << std::endl;
return sum;
}
}
#endif //CP5_EX15_30_BASKET_H