forked from jaege/Cpp-Primer-5th-Exercises
-
Notifications
You must be signed in to change notification settings - Fork 0
/
15.30.cpp
304 lines (252 loc) · 8.57 KB
/
15.30.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
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
292
293
294
295
296
297
298
299
300
301
302
303
304
// based on ex15.29
#include <string>
#include <iostream>
#include <vector>
#include <memory>
#include <set>
#define DEBUG_LEVEL 1
class Quote {
public:
#if DEBUG_LEVEL == 2
Quote() {
std::cout << "Quote" << std::endl;
}
Quote(const std::string &book, double pri) : bookNo(book), price(pri) {
std::cout << "Quote(const std::string &book, double pri)" << std::endl;
}
virtual ~Quote() {
std::cout << "~Quote" << std::endl;
}
Quote(const Quote &rhs) : price(rhs.price), bookNo(rhs.bookNo) {
std::cout << "Quote(const Quote &rhs)" << std::endl;
}
Quote(Quote &&rhs) : price(std::move(rhs.price)),
bookNo(std::move(rhs.bookNo)) {
std::cout << "Quote(Quote &&rhs)" << std::endl;
}
Quote &operator=(const Quote &rhs) {
std::cout << "operator=(const Quote &rhs)" << std::endl;
price = rhs.price;
bookNo = rhs.bookNo;
return *this;
}
Quote &operator=(Quote &&rhs) {
std::cout << "operator=(Quote &&rhs)" << std::endl;
price = std::move(rhs.price);
bookNo = std::move(rhs.bookNo);
return *this;
}
#else
Quote() = default;
Quote(const std::string &book, double pri) : bookNo(book), price(pri) { }
virtual ~Quote() = default;
Quote(const Quote &) = default;
Quote(Quote &&) = default;
Quote &operator=(const Quote &) = default;
Quote &operator=(Quote &&) = default;
#endif
std::string isbn() const { return bookNo; }
virtual double net_price(std::size_t n) const { return n * price; }
virtual Quote *clone() const & { return new Quote(*this); }
virtual Quote *clone() && { return new Quote(std::move(*this)); }
#if DEBUG_LEVEL > 0
virtual std::ostream &debug(std::ostream &os) const {
os << "Quote::bookNo " << bookNo << " Quote::price " << price;
return os;
}
#endif
protected:
double price = 0.0;
private:
std::string bookNo;
};
class Disc_quote : public Quote {
public:
#if DEBUG_LEVEL == 2
Disc_quote() {
std::cout << "Disc_quote" << std::endl;
}
Disc_quote(const std::string &book, double pri,
std::size_t qty, double disc)
: Quote(book, pri), quantity(qty), discount(disc) {
std::cout << "Disc_quote(const std::string &book, ... )" << std::endl;
}
~Disc_quote() {
std::cout << "~Disc_quote" << std::endl;
}
Disc_quote(const Disc_quote &rhs)
: Quote(rhs), quantity(rhs.quantity), discount(rhs.discount) {
std::cout << "Disc_quote(const Disc_quote &rhs)" << std::endl;
}
Disc_quote(Disc_quote &&rhs) : Quote(std::move(rhs)),
quantity(std::move(rhs.quantity)),
discount(std::move(rhs.discount)) {
std::cout << "Disc_quote(Disc_quote &&rhs)" << std::endl;
}
Disc_quote &operator=(const Disc_quote &rhs) {
Quote::operator=(rhs);
std::cout << "operator=(const Disc_quote &rhs)" << std::endl;
quantity = rhs.quantity;
discount = rhs.discount;
return *this;
}
Disc_quote &operator=(Disc_quote &&rhs) {
Quote::operator=(std::move(rhs));
std::cout << "operator=(Disc_quote &&rhs)" << std::endl;
quantity = std::move(rhs.quantity);
discount = std::move(rhs.discount);
return *this;
}
#else
Disc_quote() = default;
Disc_quote(const std::string &book, double pri,
std::size_t qty, double disc)
: Quote(book, pri), quantity(qty), discount(disc) { }
virtual ~Disc_quote() = default;
Disc_quote(const Disc_quote &) = default;
Disc_quote(Disc_quote &&) = default;
Disc_quote &operator=(const Disc_quote &) = default;
Disc_quote &operator=(Disc_quote &&) = default;
#endif
double net_price(std::size_t n) const = 0;
#if DEBUG_LEVEL > 0
std::ostream &debug(std::ostream &os) const override {
Quote::debug(os) << " Disc_quote::quantity " << quantity
<< " Disc_quote::discount " << discount;
return os;
}
#endif
protected:
std::size_t quantity = 0;
double discount = 0.0;
};
class Bulk_quote : public Disc_quote {
public:
using Disc_quote::Disc_quote;
double net_price(std::size_t n) const override {
if (n >= quantity)
return n * price * (1 - discount);
else
return n * price;
}
Bulk_quote *clone() const & override { return new Bulk_quote(*this); }
Bulk_quote *clone() && override { return new Bulk_quote(std::move(*this)); }
};
class Limit_quote : public Disc_quote {
public:
using Disc_quote::Disc_quote;
double net_price(std::size_t n) const override {
if (n <= quantity)
return n * price * (1 - discount);
else
return (n - quantity) * price + quantity * price * (1 - discount);
}
Limit_quote *clone() const & override { return new Limit_quote(*this); }
Limit_quote *clone() && override { return new Limit_quote(std::move(*this)); }
};
double print_total(std::ostream &os, const Quote &item, size_t n) {
double ret = item.net_price(n);
os << "ISBN: " << item.isbn() << " # sold: " << n << " total due: " << ret
<< std::endl;
return ret;
}
class Basket {
public:
void add_item(const std::shared_ptr<Quote> &sale) { items.insert(sale); }
void add_item(const Quote &sale) {
items.insert(std::shared_ptr<Quote>(sale.clone()));
}
void add_item(Quote &&sale) {
items.insert(std::shared_ptr<Quote>(std::move(sale).clone()));
}
double total_receipt(std::ostream &) const;
private:
static bool compare(const std::shared_ptr<Quote> &lhs,
const std::shared_ptr<Quote> &rhs) {
return lhs->isbn() < rhs->isbn();
}
std::multiset<std::shared_ptr<Quote>, decltype(compare) *> items{compare};
};
double Basket::total_receipt(std::ostream &os) const {
double sum = 0;
for (auto it = items.cbegin(); it != items.cend();
it = items.upper_bound(*it))
sum += print_total(os, **it, items.count(*it));
os << "Total sale: " << sum << std::endl;
return sum;
}
void test_copy_control_members() {
Quote basic0;
basic0.debug(std::cout) << '\n' << std::endl;
Quote basic1("abc", 5.5);
basic1.debug(std::cout) << '\n' << std::endl;
Quote basic2(basic1);
basic2.debug(std::cout) << '\n' << std::endl;
Quote basic3(std::move(basic2));
basic3.debug(std::cout) << std::endl;
basic2.debug(std::cout) << '\n' << std::endl;
basic2 = basic3;
basic2.debug(std::cout) << '\n' << std::endl;
basic3 = std::move(basic1);
basic3.debug(std::cout) << std::endl;
basic1.debug(std::cout) << '\n' << std::endl;
std::cout << "============================" << std::endl;
Bulk_quote bulk0;
bulk0.debug(std::cout) << '\n' << std::endl;
Bulk_quote bulk1("abc", 5.5, 10, 0.2);
bulk1.debug(std::cout) << '\n' << std::endl;
Bulk_quote bulk2(bulk1);
bulk2.debug(std::cout) << '\n' << std::endl;
Bulk_quote bulk3(std::move(bulk2));
bulk3.debug(std::cout) << std::endl;
bulk2.debug(std::cout) << '\n' << std::endl;
bulk2 = bulk3;
bulk2.debug(std::cout) << '\n' << std::endl;
bulk3 = std::move(bulk1);
bulk3.debug(std::cout) << std::endl;
bulk1.debug(std::cout) << '\n' << std::endl;
std::cout << "============================" << std::endl;
Limit_quote limit0;
limit0.debug(std::cout) << '\n' << std::endl;
Limit_quote limit1("abc", 5.5, 5, 0.1);
limit1.debug(std::cout) << '\n' << std::endl;
Limit_quote limit2(limit1);
limit2.debug(std::cout) << '\n' << std::endl;
Limit_quote limit3(std::move(limit2));
limit3.debug(std::cout) << std::endl;
limit2.debug(std::cout) << '\n' << std::endl;
limit2 = limit3;
limit2.debug(std::cout) << '\n' << std::endl;
limit3 = std::move(limit1);
limit3.debug(std::cout) << std::endl;
limit1.debug(std::cout) << '\n' << std::endl;
}
void test_shared_ptr_vector() {
std::vector<std::shared_ptr<Quote>> v;
v.push_back(std::make_shared<Quote>("abc", 5.5));
v.push_back(std::make_shared<Bulk_quote>("abc", 5.5, 10, 0.2));
v.push_back(std::make_shared<Limit_quote>("abc", 5.5, 10, 0.2));
for (const auto &q : v)
print_total(std::cout, *q, 20);
}
int main() {
//test_copy_control_members();
//test_shared_ptr_vector();
Basket basket;
for (auto i = 0; i < 20; ++i)
basket.add_item(Quote("abc", 5));
for (auto i = 0; i < 20; ++i)
basket.add_item(Bulk_quote("def", 5, 10, 0.2));
for (auto i = 0; i < 20; ++i)
basket.add_item(Limit_quote("ghi", 5, 10, 0.2));
// FIXME Note that if we add book with same ISBN but different type (i.e.
// price, discount type), then the type of the first added book with same
// ISBN will be used as the type for all other same ISBN books. e.g.:
basket.add_item(Bulk_quote("jkl", 5, 10, 0.2));
for (auto i = 0; i < 5; ++i)
basket.add_item(Quote("jkl", 5));
for (auto i = 0; i < 4; ++i)
basket.add_item(Limit_quote("jkl", 5, 10, 0.2));
basket.total_receipt(std::cout);
return 0;
}