diff options
feat(Week10): Add solutions
7 files changed, 375 insertions, 0 deletions
| -rw-r--r-- | Week10/Exercise03/Date.h | 10 | ||||
| -rw-r--r-- | Week10/Exercise03/Inventory.cpp | 73 | ||||
| -rw-r--r-- | Week10/Exercise03/Inventory.h | 36 | ||||
| -rw-r--r-- | Week10/Exercise03/Shop.cpp | 91 | ||||
| -rw-r--r-- | Week10/Exercise03/Shop.h | 35 | ||||
| -rw-r--r-- | Week10/Exercise03/Warehouse.cpp | 94 | ||||
| -rw-r--r-- | Week10/Exercise03/Warehouse.h | 36 |
diff --git a/Week10/Exercise03/Date.h b/Week10/Exercise03/Date.h new file mode 100644 index 0000000..716efef --- /dev/null +++ b/Week10/Exercise03/Date.h @@ -0,0 +1,10 @@ +#ifndef _DATE +#define _DATE + +struct Date { + unsigned day; + unsigned month; + unsigned year; +}; + +#endif /* _DATE */ diff --git a/Week10/Exercise03/Inventory.cpp b/Week10/Exercise03/Inventory.cpp new file mode 100644 index 0000000..699997e --- /dev/null +++ b/Week10/Exercise03/Inventory.cpp @@ -0,0 +1,73 @@ +#include "Inventory.h" + +void Inventory::free() { + delete[] products; +} + +void Inventory::copyFrom(const Inventory& other) { + productsCount = other.productsCount; + productsAlloc = other.productsAlloc; + + products = new Product[productsAlloc]; + for (unsigned i = 0; i < productsCount; ++i) + products[i] = other.products[i]; +} + +void Inventory::resize() { + productsAlloc = productsAlloc == 0 ? 8 : productsAlloc * 2; + Product* productsLarger = new Product[productsAlloc]; + + for (unsigned i = 0; i < productsCount; ++i) + productsLarger[i] = products[i]; + + delete[] products; + products = productsLarger; +} + +Inventory::Inventory() : products(), productsCount(), productsAlloc() +{ } + +Inventory::~Inventory() { + free(); +} + +Inventory::Inventory(const Inventory& other) { + copyFrom(other); +} + +Inventory& Inventory::operator=(const Inventory& other) { + if (this != &other) { + free(); + copyFrom(other); + } + return *this; +} + +Inventory::Inventory(Inventory&& other) { + products = other.products; + productsCount = other.productsCount; + productsAlloc = other.productsAlloc; + + other.products = nullptr; + other.productsAlloc = other.productsCount = 0; +} + +Inventory& Inventory::operator=(Inventory&& other) { + if (this != &other) { + free(); + + products = other.products; + productsCount = other.productsCount; + productsAlloc = other.productsAlloc; + + other.products = nullptr; + other.productsAlloc = other.productsCount = 0; + } + return *this; +} + +void Inventory::AddProduct(const Product& product) { + if (productsCount >= productsAlloc) + resize(); + products[productsCount++] = product; +} diff --git a/Week10/Exercise03/Inventory.h b/Week10/Exercise03/Inventory.h new file mode 100644 index 0000000..7f5eeca --- /dev/null +++ b/Week10/Exercise03/Inventory.h @@ -0,0 +1,36 @@ +#ifndef _INVENTORY +#define _INVENTORY + +const unsigned PRODUCT_NAME_LENGTH = 1024; + +struct Product { + char name[PRODUCT_NAME_LENGTH]; + unsigned quantity; +}; + +class Inventory { +protected: + Product* products; + unsigned productsCount; + unsigned productsAlloc; + +private: + void free(); + void copyFrom(const Inventory& other); + + void resize(); + +public: + Inventory(); + ~Inventory(); + Inventory(const Inventory& other); + Inventory& operator=(const Inventory& other); + + Inventory(Inventory&& other); + Inventory& operator=(Inventory&& other); + +protected: + void AddProduct(const Product& product); +}; + +#endif /* _INVENTORY */ diff --git a/Week10/Exercise03/Shop.cpp b/Week10/Exercise03/Shop.cpp new file mode 100644 index 0000000..4ed8ce3 --- /dev/null +++ b/Week10/Exercise03/Shop.cpp @@ -0,0 +1,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; +} diff --git a/Week10/Exercise03/Shop.h b/Week10/Exercise03/Shop.h new file mode 100644 index 0000000..ce1c8de --- /dev/null +++ b/Week10/Exercise03/Shop.h @@ -0,0 +1,35 @@ +#ifndef _SHOP +#define _SHOP + +#include "Inventory.h" +#include "Date.h" + +struct Sale { + char productName[PRODUCT_NAME_LENGTH]; + unsigned quantity; + Date date; +}; + +class Shop : public Inventory { + Sale* sales; + unsigned salesCount; + unsigned salesAlloc; + + void free(); + void copyFrom(const Shop& other); + + void resize(); + +public: + Shop(); + ~Shop(); + Shop(const Shop& other); + Shop& operator=(const Shop& other); + + Shop(Shop&& other); + Shop& operator=(Shop&& other); + + bool Purchase(const Sale& sale); +}; + +#endif /* _SHOP */ 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; +} diff --git a/Week10/Exercise03/Warehouse.h b/Week10/Exercise03/Warehouse.h new file mode 100644 index 0000000..b71d08c --- /dev/null +++ b/Week10/Exercise03/Warehouse.h @@ -0,0 +1,36 @@ +#ifndef _WAREHOUSE +#define _WAREHOUSE + +#include "Inventory.h" +#include "Date.h" + +struct Delivery { + Product products[8]; + unsigned productsCount; + Date date; + unsigned trackingNumber; +}; + +class Warehouse : public Inventory { + Delivery* deliveries; + unsigned deliveriesCount; + unsigned deliveriesAlloc; + + void free(); + void copyFrom(const Warehouse& other); + + void resize(); + +public: + Warehouse(); + ~Warehouse(); + Warehouse(const Warehouse& other); + Warehouse& operator=(const Warehouse& other); + + Warehouse(Warehouse&& other); + Warehouse& operator=(Warehouse&& other); + + bool Receive(const Delivery& delivery); +}; + +#endif /* _WAREHOUSE */ |
