blob: 4ed8ce30de96bfed1656f0fc151fc93f89c146f5 (
plain) (
blame)
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
|
#include "Shop.h"
#include <cstring>
#include <utility>
void Shop::free() {
delete[] sales;
}
void Shop::copyFrom(const Shop& other) {
salesAlloc = other.salesAlloc;
salesCount = other.salesCount;
sales = new Sale[salesAlloc];
for (unsigned i = 0; i < salesCount; ++i)
sales[i] = other.sales[i];
}
void Shop::resize() {
salesAlloc = salesAlloc == 0 ? 8 : salesAlloc * 2;
Sale* salesLarger = new Sale[salesAlloc];
for (unsigned i = 0; i < salesCount; ++i)
salesLarger[i] = sales[i];
delete[] sales;
sales = salesLarger;
}
Shop::Shop() : Inventory(), sales(), salesCount(), salesAlloc()
{ }
Shop::~Shop() {
free();
}
Shop::Shop(const Shop& other) : Inventory(other) {
copyFrom(other);
}
Shop& Shop::operator=(const Shop& other) {
if (this != &other) {
Inventory::operator=(other);
free();
copyFrom(other);
}
return *this;
}
Shop::Shop(Shop&& other) : Inventory(std::move(other)) {
sales = other.sales;
salesCount = other.salesCount;
salesAlloc = other.salesAlloc;
other.sales = nullptr;
other.salesAlloc = other.salesCount = 0;
}
Shop& Shop::operator=(Shop&& other) {
if (this != &other) {
Shop::operator=(std::move(other));
free();
sales = other.sales;
salesCount = other.salesCount;
salesAlloc = other.salesAlloc;
other.sales = nullptr;
other.salesAlloc = other.salesCount = 0;
}
return *this;
}
bool Shop::Purchase(const Sale& sale) {
unsigned productIndex = 0;
while (productIndex < productsCount &&
strcmp(sale.productName, products[productIndex].name) != 0)
{
++productIndex;
}
if (productIndex == productsCount || products[productIndex].quantity >= sale.quantity)
return false;
if (salesCount >= salesAlloc)
resize();
sales[salesCount++] = sale;
products[productIndex].quantity -= sale.quantity;
return true;
}
|