Proff_Ust

Goods_money_nasledovanie

Jan 8th, 2020
81
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 7.21 KB | None | 0 0
  1. #include <iostream>
  2. #include <sstream>
  3. /*
  4. Создать класс money для работы с денежными суммами. число должно быть представлено двумы полями:
  5. типа long для рублей и типа unsigned char для копеек. дробная часть(копейки) должна быть
  6. отделена от целой части запятой. реализовать сложение, вычитание, деление сумм, деление суммы
  7. на дробное число и операции сравнения.
  8.  
  9. Создать класс Goods. В классе должны быть представлены поля нименование, дата ояормления,
  10. цена товара, кол-во единиц, номер накладной. реализовать методы изменения цены товара,
  11. изменения кол-ва товара(уеньшение и увеличение), вичислекния стоимости. метод toString
  12. должен выбавать в виде строки стоимость товара.
  13.  
  14. */
  15. using namespace std;
  16.  
  17. class CMoney
  18. {
  19. private:
  20.     long ruble;
  21.     uint8_t kop;
  22. public:
  23.     CMoney(){};
  24.     CMoney(long newRubles, uint8_t newKop)
  25.     {
  26.         this->ruble = newRubles + (newKop/100);
  27.         this->kop = newKop%100;
  28.     }
  29.  
  30.     string toString()
  31.     {
  32.         ostringstream temp;
  33.         temp<<this->ruble<<","<<int(this->kop)<<" руб.";
  34.         return temp.str();
  35.     }
  36.  
  37.     CMoney operator+(CMoney &add)
  38.     {
  39.         CMoney temp;
  40.         int resultInKop = (this->ruble*100+this->kop) + (add.ruble*100+add.kop);
  41.         temp.ruble = resultInKop/100;
  42.         temp.kop = resultInKop%100;
  43.         return temp;
  44.     }
  45.  
  46.     CMoney operator-(CMoney &diff)
  47.     {
  48.         CMoney temp;
  49.         int resultInKop = (this->ruble*100+this->kop) - (diff.ruble*100+diff.kop);
  50.         temp.ruble = resultInKop/100;
  51.         temp.kop = resultInKop%100;
  52.         return temp;
  53.     }
  54.  
  55.     CMoney operator*(double const &mult)
  56.     {
  57.         CMoney temp;
  58.         int resultInKop = (this->ruble*100+this->kop) * mult;
  59.         temp.ruble = resultInKop/100;
  60.         temp.kop = resultInKop%100;
  61.         return temp;
  62.     }
  63.  
  64.     CMoney operator*(int const &mult)
  65.     {
  66.         CMoney temp;
  67.         int resultInKop = (this->ruble*100+this->kop) * mult;
  68.         temp.ruble = resultInKop/100;
  69.         temp.kop = resultInKop%100;
  70.         return temp;
  71.     }
  72.  
  73.     CMoney operator/(double const &div)
  74.     {
  75.         CMoney temp;
  76.         int resultInKop = (this->ruble*100+this->kop) / div;
  77.         temp.ruble = resultInKop/100;
  78.         temp.kop = resultInKop%100;
  79.         return temp;
  80.     }
  81.  
  82.     CMoney operator/(int const &div)
  83.     {
  84.         CMoney temp;
  85.         int resultInKop = (this->ruble*100+this->kop) / div;
  86.         temp.ruble = resultInKop/100;
  87.         temp.kop = resultInKop%100;
  88.         return temp;
  89.     }
  90.  
  91.     bool operator>(CMoney &op)
  92.     {
  93.         return ((this->ruble*100+this->kop)>(op.ruble*100+op.kop));
  94.     }
  95.  
  96.     bool operator>=(CMoney &op)
  97.     {
  98.         return ((this->ruble*100+this->kop)>=(op.ruble*100+op.kop));
  99.     }
  100.  
  101.     bool operator<(CMoney &op)
  102.     {
  103.         return ((this->ruble*100+this->kop)<(op.ruble*100+op.kop));
  104.     }
  105.  
  106.     bool operator<=(CMoney &op)
  107.     {
  108.         return ((this->ruble*100+this->kop)<=(op.ruble*100+op.kop));
  109.     }
  110.  
  111.     bool operator==(CMoney &op)
  112.     {
  113.         return ((this->ruble*100+this->kop)==(op.ruble*100+op.kop));
  114.     }
  115.  
  116. };
  117.  
  118. class CGoodsOpen
  119. {
  120. private:
  121.     string name;
  122.     string receiptDate;
  123.     CMoney price;
  124.     int amount;
  125.     unsigned int invoice;
  126. public:
  127.     CGoodsOpen(){};
  128.     CGoodsOpen(string newName, string newReceiptDate, CMoney newPrice,
  129.            int newAmount, unsigned int newInvoice)
  130.     {
  131.         this->name = newName;
  132.         this->receiptDate = newReceiptDate;
  133.         this->price = newPrice;
  134.         this->amount = newAmount;
  135.         this->invoice = newInvoice;
  136.     }
  137.  
  138.     string toString()
  139.     {
  140.         return this->cost().toString();
  141.     }
  142.  
  143.     int increaseAmount(int incr)
  144.     {
  145.         this->amount += incr;
  146.         return 0;
  147.     }
  148.  
  149.     int decreaseAmount(int decr)
  150.     {
  151.         this->amount -= decr;
  152.         return 0;
  153.     }
  154.  
  155.     int setPrice(CMoney newPrice)
  156.     {
  157.         this->price = newPrice;
  158.         return 0;
  159.     }
  160.  
  161.     CMoney cost()
  162.     {
  163.         return this->price * this->amount;
  164.     }
  165.  
  166.     int discount(unsigned int expirationDays)
  167.     {
  168.         if(expirationDays)
  169.             this->price = this->price * (1-expirationDays*0.01);
  170.         return 0;
  171.     }
  172. };
  173.  
  174. class CGoodsClose
  175. {
  176. private:
  177.     string name;
  178.     string receiptDate;
  179.     CMoney price;
  180.     int amount;
  181.     unsigned int invoice;
  182. public:
  183.     CGoodsClose(){};
  184.     CGoodsClose(string newName, string newReceiptDate, CMoney newPrice,
  185.            int newAmount, unsigned int newInvoice)
  186.     {
  187.         this->name = newName;
  188.         this->receiptDate = newReceiptDate;
  189.         this->price = newPrice;
  190.         this->amount = newAmount;
  191.         this->invoice = newInvoice;
  192.     }
  193.  
  194.     string toString()
  195.     {
  196.         return this->cost().toString();
  197.     }
  198.  
  199.     int increaseAmount(int incr)
  200.     {
  201.         this->amount += incr;
  202.         return 0;
  203.     }
  204.  
  205.     int decreaseAmount(int decr)
  206.     {
  207.         this->amount -= decr;
  208.         return 0;
  209.     }
  210.  
  211.     int setPrice(CMoney newPrice)
  212.     {
  213.         this->price = newPrice;
  214.         return 0;
  215.     }
  216.  
  217.     CMoney cost()
  218.     {
  219.         return this->price * this->amount;
  220.     }
  221.  
  222.     int discount(unsigned int expirationDays)
  223.     {
  224.         if(expirationDays)
  225.             this->price = this->price * (1-expirationDays*0.01);
  226.         return 0;
  227.     }
  228. };
  229.  
  230.  
  231. class CFoodsItem: public CGoodsOpen
  232. {
  233. public:
  234.     CFoodsItem(){};
  235.     CFoodsItem(string newName, string newReceiptDate, CMoney newPrice,
  236.            int newAmount, unsigned int newInvoice)
  237.     {
  238.         CGoodsOpen(newName, newReceiptDate, newPrice, newAmount, newInvoice);
  239.     }
  240.  
  241. };
  242.  
  243. class CNonFoodsItem: private CGoodsClose
  244. {
  245. public:
  246.     CNonFoodsItem(){};
  247.     CNonFoodsItem(string newName, string newReceiptDate, CMoney newPrice,
  248.            int newAmount, unsigned int newInvoice)
  249.     {
  250.         CGoodsOpen(newName, newReceiptDate, newPrice, newAmount, newInvoice);
  251.     }
  252.     string toString()
  253.     {
  254.         return CGoodsClose::toString();
  255.     }
  256.  
  257.     int increaseAmount(int incr)
  258.     {
  259.         return CGoodsClose::increaseAmount(incr);
  260.     }
  261.  
  262.     int decreaseAmount(int decr)
  263.     {
  264.         return CGoodsClose::decreaseAmount(decr);
  265.     }
  266.  
  267.     CMoney cost()
  268.     {
  269.         return CGoodsClose::cost();
  270.     }
  271.  
  272.     int discount(unsigned int expirationDays)
  273.     {
  274.         return CGoodsClose::discount(expirationDays);
  275.     }
  276.  
  277.     int setPrice(CMoney newPrice)
  278.     {
  279.         return CGoodsClose::setPrice(newPrice);
  280.     }
  281. };
  282.  
  283. int main()
  284. {
  285.     setlocale(0,"Russian");
  286.     CNonFoodsItem * test = new CNonFoodsItem("product", "2019-01-01", CMoney(10,30), 10, 12345);
  287.     //test->setPrice(CMoney(10,0));
  288.     cout<<test->toString()<<endl;
  289.     //test->discount(5);
  290.     //cout<<test->toString()<<endl;
  291.     return 0;
  292. }
Add Comment
Please, Sign In to add comment