blob: 2ce5cca0a129eaa2a9c1528abd993088b7a5d7b8 (
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
92
93
94
|
#include "Warehouse.h"
#include <cstring>
#include <utility>
void Warehouse::free() {
delete[] deliveries;
}
void Warehouse::copyFrom(const Warehouse& other) {
deliveriesAlloc = other.deliveriesAlloc;
deliveriesCount = other.deliveriesCount;
deliveries = new Delivery[deliveriesAlloc];
for (unsigned i = 0; i < deliveriesCount; ++i)
deliveries[i] = other.deliveries[i];
}
void Warehouse::resize() {
deliveriesAlloc = deliveriesAlloc == 0 ? 8 : deliveriesAlloc * 2;
Delivery* deliveriesLarger = new Delivery[deliveriesAlloc];
for (unsigned i = 0; i < deliveriesCount; ++i)
deliveriesLarger[i] = deliveries[i];
delete[] deliveries;
deliveries = deliveriesLarger;
}
Warehouse::Warehouse() : Inventory(), deliveries(), deliveriesCount(), deliveriesAlloc()
{ }
Warehouse::~Warehouse() {
free();
}
Warehouse::Warehouse(const Warehouse& other) : Inventory(other) {
copyFrom(other);
}
Warehouse& Warehouse::operator=(const Warehouse& other) {
if (this != &other) {
Inventory::operator=(other);
free();
copyFrom(other);
}
return *this;
}
Warehouse::Warehouse(Warehouse&& other) : Inventory(std::move(other)) {
deliveries = other.deliveries;
deliveriesCount = other.deliveriesCount;
deliveriesAlloc = other.deliveriesAlloc;
other.deliveries = nullptr;
other.deliveriesCount = other.deliveriesAlloc = 0;
}
Warehouse& Warehouse::operator=(Warehouse&& other) {
if (this != &other) {
Warehouse::operator=(std::move(other));
free();
deliveries = other.deliveries;
deliveriesCount = other.deliveriesCount;
deliveriesAlloc = other.deliveriesAlloc;
other.deliveries = nullptr;
other.deliveriesCount = other.deliveriesAlloc = 0;
}
return *this;
}
bool Warehouse::Receive(const Delivery& delivery) {
for (unsigned i = 0; i < delivery.productsCount; ++i) {
unsigned productIndex = 0;
while (productIndex < productsCount &&
strcmp(delivery.products[i].name, products[productIndex].name) != 0)
{
++productIndex;
}
if (productIndex == productsCount)
AddProduct(delivery.products[i]);
else
products[i].quantity += delivery.products[i].quantity;
}
if (deliveriesCount >= deliveriesAlloc)
resize();
deliveries[deliveriesCount++] = delivery;
return true;
}
|