Skip to content
Snippets Groups Projects
Commit 69196e46 authored by Kerschbaumer, David's avatar Kerschbaumer, David
Browse files

adding ku material 0317

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