Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*Опасные покупки - 2*/
- /*
- Добавьте функцию printCheck, которая будет печатать список товаров и их вес.
- void printCheck(ShopingCart cart)
- Для правильной работы программы реализуйте конструктор копирования для класса ShopingCart.
- */
- #include <iostream>
- #include <vector>
- #include <string>
- using namespace std;
- class Goods {
- string _name;
- int _weightByOne;
- int _count;
- public:
- Goods(string name, int count, int weight) {
- _name = name;
- _weightByOne = weight;
- _count = count;
- }
- int getWeight() {
- return _count * _weightByOne;
- }
- string getName() {
- return _name;
- }
- int getCount() {
- return _count;
- }
- };
- class ShopingCart {
- vector<Goods*> _goods;
- public:
- ShopingCart() {}
- ShopingCart(const ShopingCart& cart) {
- for (int i = 0; i < cart._goods.size(); i++) {
- _goods.push_back(new Goods(
- cart._goods[i]->getName(),
- cart._goods[i]->getCount(),
- cart._goods[i]->getWeight()
- ));
- }
- }
- ~ShopingCart() {
- for (int i = 0; i < _goods.size(); i++) {
- delete _goods[i];
- }
- }
- void addGoods(Goods* goods) {
- _goods.push_back(goods);
- }
- vector<Goods*> allGoods() {
- return _goods;
- }
- int getWeight() {
- int currenWeight = 0;
- for (int i = 0; i < _goods.size(); i++) {
- currenWeight += _goods[i]->getWeight();
- }
- return currenWeight;
- }
- };
- void printCheck(ShopingCart cart) {
- vector<Goods*> goods = cart.allGoods();
- for (int i = 0; i < goods.size(); i++) {
- cout << goods[i]->getName() + ": weight " << goods[i]->getWeight() << endl;
- }
- }
- int main() {
- ShopingCart cart;
- int goodsCount;
- cin >> goodsCount;
- for (int i = 0; i < goodsCount; i++) {
- string name;
- int weight, count;
- cin >> name >> count >> weight;
- cart.addGoods(new Goods(name, weight, count));
- }
- printCheck(cart);
- cout << cart.getWeight() << endl;
- }
- /*
- Тест 1:
- Ввод:
- 5
- milk 5 10
- chocolate 2 1
- coffe 2 2
- potato 15 2
- tomato 5 2
- Вывод:
- milk: weight 500
- chocolate: weight 2
- coffe: weight 8
- potato: weight 60
- tomato: weight 20
- 96
- Тест 2:
- Ввод:
- 3
- water 1 10
- cd-disk 10 1
- boots 4 3
- Вывод:
- water: weight 100
- cd-disk: weight 10
- boots: weight 36
- 32
- */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement