Skip to content
Snippets Groups Projects
Commit d15b1aac authored by Guttmann, Michael's avatar Guttmann, Michael
Browse files

added product with instance counter

parent 1675a93f
No related branches found
No related tags found
No related merge requests found
#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();
}
#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
#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;
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment