diff --git a/2021-oop1/ku/stream-0317/product.cpp b/2021-oop1/ku/stream-0317/product.cpp new file mode 100755 index 0000000000000000000000000000000000000000..2c25733606a617fa9d26b535a4ad90bae1b56aa7 --- /dev/null +++ b/2021-oop1/ku/stream-0317/product.cpp @@ -0,0 +1,190 @@ +#include <cstdio> // stdio.h +#include <cstring> // string.h +#include <vector> + +class Product +{ + private: + + char name_[32]; + int count_; + + int cost_per_unit_; + int total_cost_; + + public: + + Product() = default; + Product(const char* name, int count, int cost_per_unit); + + char* getName() { return name_; } + int getCount() { return count_; } + int getCostPerUnit() { return cost_per_unit_; } + int getTotalCost() { return total_cost_; } + + void setName(const char* name); + void setCount(int count); + void setCostPerUnit(int cost_per_unit); + + void increaseCount(int value); + void updateTotalCost(); + void print(); + + void merge(Product& prod); +}; + +Product::Product(const char* name, int count, int cost_per_unit) : + name_(), + count_(count), + cost_per_unit_(cost_per_unit), + total_cost_(-1) +{ + strncpy(name_, name, 31); + name_[31] = 0; + + if (count_ < 0) + { + count_ = 0; + } + + if (cost_per_unit_ < 0) + { + cost_per_unit_ = 0; + } +} + +void Product::setName(const char* name) +{ + strncpy(name_, name, 31); + name_[31] = 0; +} + +void Product::setCount(int count) +{ + count_ = count; + if (count_ < 0) + { + count_ = 0; + } +} + +void Product::setCostPerUnit(int cost_per_unit) +{ + cost_per_unit_ = cost_per_unit; + if (cost_per_unit_ < 0) + { + cost_per_unit_ = 0; + } +} + +void Product::increaseCount(int increment) +{ + if (increment >= 1) + count_ += increment; +} + +void Product::updateTotalCost() +{ + total_cost_ = cost_per_unit_ * count_; +} + +void Product::print() +{ + printf("%s\n", name_); + printf("\tCount: %d - " + "Cost: %d - " + "Total Cost: %d\n", count_, cost_per_unit_, total_cost_); +} + +void Product::merge(Product& prod) +{ + if (strcmp(name_, prod.name_) != 0) + return; + + if (cost_per_unit_ != prod.cost_per_unit_) + return; + + count_ += prod.count_; + prod.count_ = 0; + updateTotalCost(); + prod.updateTotalCost(); +} + + +//-------------------------------------------------------------- +// Vector added by David BEGIN +void printShoppingList(std::vector<Product> shopping_list) +{ + printf("------------------\n"); + printf("Shopping List: \n"); + for(Product product : shopping_list) + { + product.print(); + } + printf("------------------\n"); +} + +void goShopping() +{ + + std::vector<Product> shopping_list; + + Product onion("onion", 10, 2); + Product banana("banana", 3, 1); + Product beer("beer", 20, 1); + + shopping_list.push_back(onion); + shopping_list.push_back(banana); + shopping_list.push_back(beer); + + printShoppingList(shopping_list); + + // WARNING: size() - 1 can be a problem if the shopping_list is empty + Product last_product = shopping_list.at(shopping_list.size() - 1); + + last_product.print(); + + shopping_list.pop_back(); + shopping_list.clear(); + printShoppingList(shopping_list); +} +// Vector added by David END +//-------------------------------------------------------------- + + +int main(void) +{ + Product prod; + prod.setName("Zwiebel"); + prod.setCount(-2); + prod.setCostPerUnit(5); + + printf("%s\n", prod.getName()); + printf("\tCount: %d - " + "Cost: %d - " + "Total Cost: %d\n", prod.getCount(), prod.getCostPerUnit(), prod.getTotalCost()); + + printf("----------\n"); + + prod.increaseCount(4); + prod.updateTotalCost(); + prod.print(); + + printf("----------\n"); + + Product prod2("Zwiebel", 2, 5); + prod2.updateTotalCost(); + prod2.print(); + + printf("----------\n"); + + prod.merge(prod2); + prod.print(); + prod2.print(); + + printf("----------\n"); + + goShopping(); + + return 0; +} diff --git a/2021-oop1/ku/stream-0317/vector.cpp b/2021-oop1/ku/stream-0317/vector.cpp new file mode 100755 index 0000000000000000000000000000000000000000..db1c70dc1241445f78d8956a21750ac557ccee41 --- /dev/null +++ b/2021-oop1/ku/stream-0317/vector.cpp @@ -0,0 +1,41 @@ +// +// program creates an int-vector and uses methods +// to manipulate the vector +// + + +#include <cstdio> +#include <vector> + +void printVector(std::vector<int> int_vector) +{ + printf("(size %lu) ", int_vector.size()); + for(int element : int_vector) + { + printf("[%d] ", element); + } + printf("\n"); +} + +int main() +{ + std::vector<int> myvector; + printVector(myvector); + + myvector.push_back(42); + myvector.push_back(4); + printVector(myvector); + + if(!myvector.empty() && myvector.at(0) == 42) + { + printf("life, the universe and everything\n"); + } + + myvector.pop_back(); + printVector(myvector); + + myvector.clear(); + printVector(myvector); + + return 0; +} \ No newline at end of file