Advertisement
Ifrail

Less9 Task4

Jan 23rd, 2020
142
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.33 KB | None | 0 0
  1. /*Опасные покупки - 2*/
  2.  
  3. /*
  4. Добавьте функцию printCheck, которая будет печатать список товаров и их вес.
  5. void printCheck(ShopingCart cart)
  6.  
  7. Для правильной работы программы реализуйте конструктор копирования для класса ShopingCart.
  8. */
  9.  
  10. #include <iostream>
  11. #include <vector>
  12. #include <string>
  13.  
  14. using namespace std;
  15.  
  16.  
  17. class Goods {
  18.     string _name;
  19.     int _weightByOne;
  20.     int _count;
  21.  
  22. public:
  23.     Goods(string name, int count, int weight) {
  24.         _name = name;
  25.         _weightByOne = weight;
  26.         _count = count;
  27.     }
  28.  
  29.     int getWeight() {
  30.         return _count * _weightByOne;
  31.     }
  32.  
  33.     string getName() {
  34.         return _name;
  35.     }
  36.  
  37.     int getCount() {
  38.         return _count;
  39.     }
  40. };
  41.  
  42. class ShopingCart {
  43.    
  44.     vector<Goods*> _goods;
  45. public:
  46.     ShopingCart() {}
  47.  
  48.     ShopingCart(const ShopingCart& cart) {
  49.         for (int i = 0; i < cart._goods.size(); i++) {
  50.             _goods.push_back(new Goods(
  51.                 cart._goods[i]->getName(),
  52.                 cart._goods[i]->getCount(),
  53.                 cart._goods[i]->getWeight()
  54.             ));
  55.         }
  56.     }
  57.  
  58.     ~ShopingCart() {
  59.         for (int i = 0; i < _goods.size(); i++) {
  60.             delete _goods[i];
  61.         }
  62.     }
  63.  
  64.     void addGoods(Goods* goods) {
  65.         _goods.push_back(goods);
  66.     }
  67.  
  68.     vector<Goods*> allGoods() {
  69.         return _goods;
  70.     }
  71.  
  72.     int getWeight() {
  73.         int currenWeight = 0;
  74.         for (int i = 0; i < _goods.size(); i++) {
  75.             currenWeight += _goods[i]->getWeight();
  76.         }
  77.         return currenWeight;
  78.     }
  79.  
  80. };
  81.  
  82. void printCheck(ShopingCart cart) {
  83.     vector<Goods*> goods = cart.allGoods();
  84.     for (int i = 0; i < goods.size(); i++) {
  85.         cout << goods[i]->getName() + ": weight " << goods[i]->getWeight() << endl;
  86.     }
  87. }
  88.  
  89. int main() {
  90.     ShopingCart cart;
  91.  
  92.     int goodsCount;
  93.     cin >> goodsCount;
  94.  
  95.     for (int i = 0; i < goodsCount; i++) {
  96.         string name;
  97.         int weight, count;
  98.         cin >> name >> count >> weight;
  99.         cart.addGoods(new Goods(name, weight, count));
  100.     }
  101.  
  102.     printCheck(cart);
  103.     cout << cart.getWeight() << endl;
  104. }
  105.  
  106. /*
  107. Тест 1:
  108. Ввод:
  109. 5
  110. milk 5 10
  111. chocolate 2 1
  112. coffe 2 2
  113. potato 15 2
  114. tomato 5 2
  115.  
  116. Вывод:
  117. milk: weight 500
  118. chocolate: weight 2
  119. coffe: weight 8
  120. potato: weight 60
  121. tomato: weight 20
  122. 96
  123.  
  124. Тест 2:
  125. Ввод:
  126. 3
  127. water 1 10
  128. cd-disk 10 1
  129. boots 4 3
  130.  
  131. Вывод:
  132. water: weight 100
  133. cd-disk: weight 10
  134. boots: weight 36
  135. 32
  136.  
  137. */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement