file.cpp
#include <iostream>
#include <string>
#include "file_h.h"
namespace STORE {
int Product::totalProducts = 0;
Product::Product() {
totalProducts++;
m_name = "UnknownName";
m_price = 0.0;
m_quantity = 0;
m_discount = new double(0.0);
m_obj = totalProducts;
setTotalPrice();
}
Product::Product(std::string name, double price, int quantity) {
totalProducts++;
m_name = name;
m_price = price;
m_quantity = quantity;
m_discount = new double(0.0);
m_obj = totalProducts;
setTotalPrice();
}
Product::Product(const Product& another) {
totalProducts++;
m_name = another.m_name;
m_price = another.m_price;
m_quantity = another.m_quantity;
m_discount = new double(*(another.m_discount));
m_obj = totalProducts;
m_totalPrice = another.m_totalPrice;
};
Product::~Product() {
delete m_discount;
totalProducts--;
}
void Product::sell(int amount) {
if(m_quantity >= amount) {
m_quantity -= amount;
} else {
std::cout << "QUANTITY IS NOT ENOUGH" << std::endl
<< "-----------------------" << std::endl; ;
}
setTotalPrice();
}
void Product::restock(int amount) {
m_quantity += amount;
setTotalPrice();
}
void Product::show() const {
std::cout << "Name: " << m_name << std::endl
<< "Price: " << m_price << std::endl
<< "Quantity: " << m_quantity << std::endl
<< "Discount: " << *(m_discount) << std::endl
<< "Total price: " << m_totalPrice << std::endl
<< "Obj #" << m_obj << std::endl
<< "-----------------------" << std::endl;
}
void Product::setDiscount(double discount) {
*m_discount = discount;
setTotalPrice();
}
void Product::setTotalPrice() {
m_totalPrice = (1 - *m_discount) * m_price * m_quantity;
}
Product& Product::operator=(const Product& another) {
if(this == &another) {
return *this;
}
m_name = another.m_name;
m_price = another.m_price;
m_quantity = another.m_quantity;
delete m_discount;
m_discount = new double(*(another.m_discount));
m_totalPrice = another.m_totalPrice;
return *this;
}
Product& Product::operator+=(int amount) {
m_quantity += amount;
setTotalPrice();
return *this;
}
std::ostream& operator<<(std::ostream& os, const Product& p) {
os << "Name: " << p.m_name << std::endl
<< "Price: " << p.m_price << std::endl
<< "Quantity: " << p.m_quantity << std::endl
<< "Discount: " << *p.m_discount << std::endl
<< "Total price: " << p.m_totalPrice << std::endl
<< "Obj #" << p.m_obj << std::endl
<< "-----------------------" << std::endl;
return os;
}
};
int main() {
STORE::Product p1;
p1w.sho();
p1.sell(10);
p1.restock(100);
p1w.sho();
{
using STORE::Product;
Product p2("Internal block");
p2.show();
}
std::cout << "NUMBER OF OBJECTS: " << STORE::Product::getTotalProducts() << std::endl;
std::cout << "-----------------------" << std::endl;
STORE::Product p3("Jojo", 1000, 20);
p3 += 55;
std::cout << p3;
p3.setDiscount(0.3);
std::cout << p3;
p1 = p3;
std::cout << p1;
std::cout << "NUMBER OF OBJECTS: " << STORE::Product::getTotalProducts() << std::endl;
return 0;
}