Advertisement
irmantas_radavicius

Untitled

Mar 16th, 2022
787
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.81 KB | None | 0 0
  1.  
  2. #include <sstream>
  3. #include <stdexcept>
  4.  
  5.  
  6. #include "all.h"
  7.  
  8. namespace Sandbox {
  9.  
  10.     using namespace std;
  11.  
  12.  
  13.     class Item::Implementation {           
  14.         private:
  15.             unsigned int id;
  16.             string name;
  17.             double price;  
  18.             static unsigned int lastId;
  19.             static unsigned int aliveCount;
  20.         private:
  21.             void setId();  
  22.         friend class Item;
  23.             friend std::ostream& operator<<(std::ostream& o, const Item &item);
  24.             friend std::istream& operator>>(std::istream& i, Item &item);
  25.     }; 
  26.  
  27.     unsigned int Item::Implementation::lastId = 0;
  28.     unsigned int Item::Implementation::aliveCount = 0;
  29.  
  30.  
  31.     Item::Item(const string &name, const double price){
  32.         impl = new Implementation();       
  33.         impl->setId();
  34.         setName(name);
  35.         setPrice(price);
  36.         ++Implementation::aliveCount;
  37.     }
  38.     Item::Item(const Item &other){
  39.         impl = new Implementation(*(other.impl));      
  40.         ++Implementation::aliveCount;
  41.        
  42.     }
  43.     Item& Item::operator=(const Item &other){
  44.         if(this == &other)
  45.             return *this;
  46.            
  47.         delete impl;
  48.         impl = new Implementation(*(other.impl));      
  49.         return *this;
  50.     }
  51.     Item::~Item(){         
  52.         --Implementation::aliveCount;
  53.         delete impl;
  54.     }
  55.    
  56.    
  57.     unsigned int Item::getId() const {
  58.         return impl->id;
  59.     }
  60.  
  61.     void Item::Implementation::setId(){
  62.         id = Implementation::lastId++;
  63.     }
  64.    
  65.  
  66.     string Item::getName() const {
  67.         return impl->name;
  68.     }
  69.     void Item::setName(const string &name) {
  70.         impl->name = name;
  71.     }
  72.    
  73.     double Item::getPrice() const {
  74.         return impl->price;
  75.     }
  76.     void Item::setPrice(const double price){           
  77.         if (price >= 0){
  78.             impl->price = price;               
  79.         } else {
  80.             throw invalid_argument("Error: price should not be negative");         
  81.         }
  82.     }
  83.    
  84.     Item& Item::operator++(){
  85.         setPrice(getPrice() + 1.0);
  86.         return *this;
  87.     }
  88.    
  89.     Item Item::operator++(int){
  90.         Item temp = *this;
  91.         setPrice(getPrice() + 1.0);
  92.         return temp;
  93.     }
  94.    
  95.     string Item::operator=(const string &name){
  96.         setName(name);
  97.         return name;
  98.     }
  99.  
  100.     Item Item::operator+(const Item &i2){
  101.         if (getName() == i2.getName()){    
  102.             return Item(getName(), getPrice() + i2.getPrice());
  103.         } else {
  104.             throw NameMismatchException();
  105.         }
  106.     }
  107.  
  108.  
  109.        
  110.     void Item::operator+=(const double change){
  111.         impl->price += change;
  112.         //setPrice(getPrice() + change);
  113.     }
  114.     void Item::operator-=(const double change){
  115.         impl->price -= change;         
  116.         //setPrice(getPrice() - change);
  117.     }
  118.    
  119.    
  120.     unsigned int Item::getAliveCount() {
  121.         return Implementation::aliveCount;
  122.     }
  123.    
  124.     string Item::toString() const {
  125.         stringstream ss;
  126.         ss << getId() << " " << getName() << " " << getPrice();
  127.         return ss.str();
  128.     }
  129.  
  130.     std::ostream& operator<<(std::ostream& o, const Item &item){
  131.         o << item.toString();
  132.         return o;      
  133.     }
  134.    
  135.     std::istream& operator>>(std::istream& i, Item &item){
  136.         i >> item.impl->id;
  137.         i >> item.impl->name;
  138.         i >> item.impl->price;
  139.         return i;      
  140.     }
  141. }
  142.  
  143.    
  144.        
  145.  
  146.  
  147.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement