diff options
Added work from todays practice
9 files changed, 188 insertions, 0 deletions
diff --git a/Year 1/Object-oriented Programming/09-Practice-19.04.2022/HranaLemur.cpp b/Year 1/Object-oriented Programming/09-Practice-19.04.2022/HranaLemur.cpp new file mode 100644 index 0000000..4dc9c22 --- /dev/null +++ b/Year 1/Object-oriented Programming/09-Practice-19.04.2022/HranaLemur.cpp @@ -0,0 +1,30 @@ +#include "HranaLemur.h" +#include <cstring> +#include <iostream> + +int HranaLemur::indexOfRestaurant(const char* name) { + for (int i = 0; i < restaurantsCount; i++) { + if (strcmp(restaurants[i].get_name(), name) == 0) + return i; + } + return -1; +} + +void HranaLemur::handleOrder(Order order) { + int rest = indexOfRestaurant(order.get_restaurantName()); + if (rest < 0) { + std::cout << "Restaurant " << order.get_restaurantName() << " doesn't exist!" << std::endl; + return; + } + + try { + int time = restaurants[rest].giveOrder(order); + std::cout << "Order will take: " << time << std::endl; + } + catch (int errorCode) { + if (errorCode == 1) + std::cout << "The restaurant doesn't offer some product!" << std::endl; + else + throw errorCode; + } +} diff --git a/Year 1/Object-oriented Programming/09-Practice-19.04.2022/HranaLemur.h b/Year 1/Object-oriented Programming/09-Practice-19.04.2022/HranaLemur.h new file mode 100644 index 0000000..00bb87e --- /dev/null +++ b/Year 1/Object-oriented Programming/09-Practice-19.04.2022/HranaLemur.h @@ -0,0 +1,13 @@ +#include "Restaurant.h" + +class HranaLemur { + Restaurant* restaurants; + int restaurantsCount; + + int indexOfRestaurant(const char* name); + +public: + HranaLemur(); + + void handleOrder(Order order); +}; diff --git a/Year 1/Object-oriented Programming/09-Practice-19.04.2022/Order.cpp b/Year 1/Object-oriented Programming/09-Practice-19.04.2022/Order.cpp new file mode 100644 index 0000000..530769e --- /dev/null +++ b/Year 1/Object-oriented Programming/09-Practice-19.04.2022/Order.cpp @@ -0,0 +1,34 @@ +#include "Order.h" +#include <cstring> + +void Order::free() { + delete[] products; +} + +void Order::copyFrom(const Order& other) { + strcpy(restaurantName, other.restaurantName); + productCount = other.productCount; + products = new Product[productCount]; + for (int i = 0; i < productCount; i++) + products[i] = other.products[i]; +} + +void Order::move(Order&& other) { + strcpy(restaurantName, other.restaurantName); + other.restaurantName = nullptr; + s +} + +Order::Order(const char* restaurant) { + strcpy(this->restaurantName, restaurant); + products = new Product[8]; + this->productCount = 0; +} + +Order::~Order() { + free(); +} + +void Order::addProduct(const Product& product) { + this->products[productCount++] = product; +} diff --git a/Year 1/Object-oriented Programming/09-Practice-19.04.2022/Order.h b/Year 1/Object-oriented Programming/09-Practice-19.04.2022/Order.h new file mode 100644 index 0000000..d044c2d --- /dev/null +++ b/Year 1/Object-oriented Programming/09-Practice-19.04.2022/Order.h @@ -0,0 +1,29 @@ +#include "Product.h" + +class Order { + char restaurantName[25]; + Product* products; + int productCount; + + void free(); + void copyFrom(const Order& other); + + void move(Order&& other); + +public: + Order(const char* restaurant); + + ~Order(); + Order(const Order& other); + Order& operator=(const Order& other); + + Order(Order&& other); + Order& operator=(Order&& other); + + + void addProduct(const Product& product); + + const char* get_restaurantName() const; + int get_productCount() const; + const Product* get_products() const; +}; diff --git a/Year 1/Object-oriented Programming/09-Practice-19.04.2022/Product.cpp b/Year 1/Object-oriented Programming/09-Practice-19.04.2022/Product.cpp new file mode 100644 index 0000000..209ad74 --- /dev/null +++ b/Year 1/Object-oriented Programming/09-Practice-19.04.2022/Product.cpp @@ -0,0 +1,14 @@ +#include "Product.h" +#include <cstring> + +Product::Product() : Product("") +{} + +Product::Product(const char* name) { + this->name = new char[strlen(name) + 1]; + strcpy(this->name, name); +} + +Product::~Product() { + delete[] this->name; +} diff --git a/Year 1/Object-oriented Programming/09-Practice-19.04.2022/Product.h b/Year 1/Object-oriented Programming/09-Practice-19.04.2022/Product.h new file mode 100644 index 0000000..5f5f4d7 --- /dev/null +++ b/Year 1/Object-oriented Programming/09-Practice-19.04.2022/Product.h @@ -0,0 +1,11 @@ +class Product { + char* name; + +public: + Product(); + Product(const char* name); + + ~Product(); + + const char* get_name() const; +}; diff --git a/Year 1/Object-oriented Programming/09-Practice-19.04.2022/Restaurant.cpp b/Year 1/Object-oriented Programming/09-Practice-19.04.2022/Restaurant.cpp new file mode 100644 index 0000000..0fc297a --- /dev/null +++ b/Year 1/Object-oriented Programming/09-Practice-19.04.2022/Restaurant.cpp @@ -0,0 +1,33 @@ +#include "Restaurant.h" +#include <cstring> + +int Restaurant::indexOf(const Product& prod) { + for (int i = 0; i < productsCount; i++) { + if (strcmp(products[i].get_name(), prod.get_name()) == 0) + return i; + } + return -1; +} + +Restaurant::Restaurant() : Restaurant("") +{} + +Restaurant::Restaurant(const char* name) { + strcpy(this->name, name); + products = new Product[8]; + productsCount = 0; +} + +void Restaurant::addProduct(Product prod) { + products[productsCount++] = prod; +} + +unsigned int Restaurant::giveOrder(Order order) { + unsigned int time = 0; + for (int i = 0; i < order.get_productCount(); i++) { + if (indexOf(order.get_products()[i]) < 0) + throw 1; + time += strlen(order.get_products()[i].get_name()) * 2.7182818284590452353602874 * 3.14159265358979323846; + } + return time; +} diff --git a/Year 1/Object-oriented Programming/09-Practice-19.04.2022/Restaurant.h b/Year 1/Object-oriented Programming/09-Practice-19.04.2022/Restaurant.h new file mode 100644 index 0000000..1d5d28d --- /dev/null +++ b/Year 1/Object-oriented Programming/09-Practice-19.04.2022/Restaurant.h @@ -0,0 +1,19 @@ +#include "Product.h" +#include "Order.h" + +class Restaurant { + char name[25]; + Product* products; + int productsCount; + + int indexOf(const Product& prod); + +public: + Restaurant(); + Restaurant(const char* name); + + void addProduct(Product prod); + unsigned int giveOrder(Order order); + + const char* get_name() const; +}; diff --git a/Year 1/Object-oriented Programming/09-Practice-19.04.2022/source.cpp b/Year 1/Object-oriented Programming/09-Practice-19.04.2022/source.cpp new file mode 100644 index 0000000..08d3514 --- /dev/null +++ b/Year 1/Object-oriented Programming/09-Practice-19.04.2022/source.cpp @@ -0,0 +1,5 @@ +#include <iostream> + +int main() { + std::cout << "Hello World!" << std::endl; +} |
