From d15b1aac11d41a073b0e9834b463ac4bd02b19d1 Mon Sep 17 00:00:00 2001
From: Michael Guttmann <michael.guttmann@student.tugraz.at>
Date: Thu, 25 Mar 2021 11:30:41 +0100
Subject: [PATCH] added product with instance counter

---
 2021-oop1/ku/stream-03/productv2/Prod.cpp | 101 ++++++++++++++++++++++
 2021-oop1/ku/stream-03/productv2/Prod.hpp |  43 +++++++++
 2021-oop1/ku/stream-03/productv2/main.cpp |  26 ++++++
 3 files changed, 170 insertions(+)
 create mode 100644 2021-oop1/ku/stream-03/productv2/Prod.cpp
 create mode 100644 2021-oop1/ku/stream-03/productv2/Prod.hpp
 create mode 100644 2021-oop1/ku/stream-03/productv2/main.cpp

diff --git a/2021-oop1/ku/stream-03/productv2/Prod.cpp b/2021-oop1/ku/stream-03/productv2/Prod.cpp
new file mode 100644
index 0000000..01395ef
--- /dev/null
+++ b/2021-oop1/ku/stream-03/productv2/Prod.cpp
@@ -0,0 +1,101 @@
+#include <cstdio>
+#include <cstring>
+#include "Prod.hpp"
+
+//----------------------------------------------------------------------------------------------------------------------
+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;
+  }
+
+  counter_++;
+}
+
+//----------------------------------------------------------------------------------------------------------------------
+Product::Product(Product& prod) :
+  name_(),
+  count_(prod.count_),
+  cost_per_unit_(prod.cost_per_unit_),
+  total_cost_(prod.total_cost_)
+{
+  strcpy(name_, prod.name_);
+  counter_++;
+}
+
+//----------------------------------------------------------------------------------------------------------------------
+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();
+}
diff --git a/2021-oop1/ku/stream-03/productv2/Prod.hpp b/2021-oop1/ku/stream-03/productv2/Prod.hpp
new file mode 100644
index 0000000..2cfedb4
--- /dev/null
+++ b/2021-oop1/ku/stream-03/productv2/Prod.hpp
@@ -0,0 +1,43 @@
+#ifndef PROD_HPP
+#define PROD_HPP
+
+class Product
+{
+  private:
+
+    static int counter_;
+
+    char name_[32];
+    int count_;
+
+    int cost_per_unit_;
+    int total_cost_;
+
+  public:
+
+    static int getCounter() { return counter_; }
+
+    Product() { counter_++; };
+    Product(const char* name, int count, int cost_per_unit);
+    Product(Product& prod);
+    ~Product() { counter_--; }
+
+    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);
+};
+
+inline int Product::counter_ = 0;
+
+#endif // PROD_HPP
diff --git a/2021-oop1/ku/stream-03/productv2/main.cpp b/2021-oop1/ku/stream-03/productv2/main.cpp
new file mode 100644
index 0000000..e278d3e
--- /dev/null
+++ b/2021-oop1/ku/stream-03/productv2/main.cpp
@@ -0,0 +1,26 @@
+#include <cstdio>
+#include "Prod.hpp"
+
+int main(void)
+{
+  {
+    Product prod_stack("Zwiebel", 5, 3);
+    Product* prod_heap = new Product("Salami", 4, 7);
+    printf("Number of products: %d\n", Product::getCounter());
+
+    Product* prod_ptr = &prod_stack;
+    Product& prod_ref = *prod_heap;
+    printf("Number of products: %d\n", Product::getCounter());
+
+    Product prod_copy(prod_ref);
+    Product* prod_copy_heap = new Product(*prod_ptr);
+    printf("Number of products: %d\n", Product::getCounter());
+
+    delete prod_copy_heap;
+    delete prod_heap;
+    printf("Number of products: %d\n", Product::getCounter());
+  }
+
+  printf("Number of products: %d\n", Product::getCounter());
+  return 0;
+}
-- 
GitLab