Advertisement
irmantas_radavicius

Untitled

Mar 10th, 2022
593
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.13 KB | None | 0 0
  1. #include <iostream>
  2. #include <string>
  3. #include <sstream>
  4. #include <cassert>
  5. #include <stdexcept>
  6.  
  7. using namespace std;
  8.  
  9. class Item {   
  10.     private:
  11.         int id;
  12.         static int lastId;
  13.         static int aliveObjects;
  14.         string name;
  15.         double price;  
  16.         int amount;
  17.     public:
  18.         Item(string name, double price){
  19.             init(name, price, 1);          
  20.         }
  21.         Item(string name, double price, int amount){
  22.             init(name, price, amount);
  23.         }
  24.         ~Item(){           
  25.             --aliveObjects;
  26.         }
  27.     private:   
  28.         void init(string name, double price, int amount){          
  29.             ++aliveObjects;
  30.             setId();
  31.             setName(name);
  32.             setPrice(price);
  33.             setAmount(amount);
  34.         }
  35.     public:
  36.         static int getAliveObjects(){
  37.             return aliveObjects;
  38.         }
  39.     private:
  40.         void setId(){
  41.             this->id = lastId++;
  42.         }
  43.     public:
  44.         int getId(){
  45.             return id;
  46.         }
  47.     private:
  48.         void setName(string name){
  49.             this->name = name;
  50.         }
  51.        
  52.     public:
  53.         string getName() {
  54.             return name;
  55.         }
  56.        
  57.         void setPrice(double price){
  58.             if (price >= 0){
  59.                 this->price = price;               
  60.             } else {
  61.                 throw invalid_argument("Price cannot be negative");            
  62.             }
  63.         }
  64.         double getPrice(){
  65.             return price;
  66.         }
  67.        
  68.         void setAmount(int amount){
  69.             if (amount >= 0){
  70.                 this->amount = amount;
  71.             } else {
  72.                 throw invalid_argument("Amount cannot be negative");               
  73.             }
  74.         }
  75.         int getAmount(){
  76.             return amount;
  77.         }
  78.        
  79.         string toString(){
  80.             stringstream ss;
  81.             ss << getId() << " " << getName() << " " << getPrice() << " " << getAmount();
  82.             return ss.str();
  83.         }
  84. };
  85.  
  86. int Item::lastId = 0;
  87. int Item::aliveObjects = 0;
  88.  
  89. class ItemHandler {
  90.     private:
  91.         Item *item;
  92.     public:
  93.         ItemHandler(Item *item){
  94.             this->item = item;
  95.         }
  96.         Item * get(){
  97.             return this->item;
  98.         }
  99.         ~ItemHandler(){
  100.             delete item;
  101.         }
  102. };
  103.  
  104. int main(){        
  105.    
  106.     try {  
  107.         ItemHandler ih(new Item("Chocolate", 1.0));    
  108.         cout << ih.get()->toString() << endl;                      
  109.         Item item1("Chocolate", 1.0);  
  110.         item1.setPrice(-1);
  111.         cout << item1.toString() << endl;  
  112.     } catch(exception &e){
  113.         cout << e.what() << endl;
  114.     } catch(...){
  115.         cout << "Unexpected error" << endl;
  116.     }
  117.    
  118.     assert(Item::getAliveObjects() == 0);
  119.     return 0;
  120. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement