Website Summary Files Commits Branches @commit @diff @stats
path: root / Week10 / Exercise03 / Warehouse.cpp
diff options
context:
space:
mode:
Commit7546f0d0da487e16f0a8671c09a46e1cee2bc03a (patch)
Author
 Syndamia <kamen@syndamia.com>
2026-05-02 14:57:36 +0300
Committer
 Syndamia <kamen@syndamia.com>
2026-05-02 14:57:36 +0300
Files6462868cebe114a87ca38252af9a49bce6603286 /Week10/Exercise03/Warehouse.cpp
oop-2025-solutions-7546f0d0da487e16f0a8671c09a46e1cee2bc03a.tar
oop-2025-solutions-7546f0d0da487e16f0a8671c09a46e1cee2bc03a.tar.gz
oop-2025-solutions-7546f0d0da487e16f0a8671c09a46e1cee2bc03a.zip
Parentc98c633b7087e3c52f4b05236f3509a9ec5f2bba (diff)
feat(Week10): Add solutions
1 files changed, 94 insertions, 0 deletions
-rw-r--r--Week10/Exercise03/Warehouse.cpp94
diff --git a/Week10/Exercise03/Warehouse.cpp b/Week10/Exercise03/Warehouse.cpp
new file mode 100644
index 0000000..2ce5cca
--- /dev/null
+++ b/Week10/Exercise03/Warehouse.cpp
@@ -0,0 +1,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;
+}