#include <algorithm>
#include <iostream>
#include <string>
#include <vector>
using namespace std;
class Tovar
{
public:
Tovar(const string& name, const string& shop, double prices[])
{
SetName(name);
SetShop(shop);
for (int i = 0; i < 6; i++)
{
prices_[i] = prices[i];
}
}
void SetName(const string& name)
{
name_ = name;
}
const string& GetName() const
{
return name_;
}
void SetShop(const string& shop)
{
shop_ = shop;
}
const string& GetShop() const
{
return shop_;
}
void SetPrices(const vector<double>& prices)
{
for (int i = 0; i < 6 && i < prices.size(); i++)
{
prices_[i] = prices[i];
}
}
vector<double> GetPrices() const
{
vector<double> result;
for (int i = 0; i < 6; i++)
{
result.push_back(prices_[i]);
}
return result;
}
bool operator < (const Tovar& other)
{
return name_ < other.name_;
}
double AveragePrice() const
{
double summ = 0;
for (int i = 0; i < 6; i++)
{
summ += prices_[i];
}
return summ / 6;
}
void Print() const
{
cout << "Название: " << name_ << "\n"
<< "Магазин: " << shop_ << "\n"
<< "Средняя цена: " << AveragePrice() << "\n"
<< "Цены: ";
for (int i = 0; i < 6; i++)
{
cout << prices_[i] << ", ";
}
cout << "\b\b\n";
}
private:
string name_;
string shop_;
double prices_[6];
};
void PrintByShop(const vector<Tovar>& tovars, const string& shop)
{
for (int i = 0; i < tovars.size(); i++)
{
if (tovars[i].GetShop() == shop)
{
tovars[i].Print();
cout << "\n";
}
}
}
Tovar InputTovar()
{
string name, shop;
double prices[6];
cout << "Введите название\n";
cin >> name;
cout << "Введите магазин\n";
cin >> shop;
cout << "Введите цены\n";
for (int i = 0; i < 6; i++)
{
cin >> prices[i];
}
Tovar tovar(name, shop, prices);
return tovar;
}
void PrintTovars(const vector<Tovar>& tovars)
{
for (int i = 0; i < tovars.size(); i++)
{
tovars[i].Print();
cout << "\n";
}
}
int main()
{
vector<Tovar> tovars;
cout << "Введите список товаров\n";
bool finished = false;
while (!finished)
{
int action;
cout << "Выберите действие:\n"
"1) Добавить товар\n"
"2) Закончить ввод\n";
cin >> action;
switch (action)
{
case 1:
tovars.push_back(InputTovar());
break;
case 2:
finished = true;
break;
default:
cout << "Вы выбрали несуществующее действие, попробуйте ещё раз\n";
continue;
}
}
sort(tovars.begin(), tovars.end());
finished = false;
while (!finished)
{
cout << "Выберите действие: \n"
"0) Выйти\n"
"1) Поиск по названию магазина\n"
"2) Вывести товары, отсортированные по названию\n";
int action;
cin >> action;
switch (action)
{
case 0:
finished = true;
break;
case 1:
{
cout << "Введите название магазина\n";
string shop;
cin >> shop;
PrintByShop(tovars, shop);
break;
}
case 2:
PrintTovars(tovars);
break;
default:
cout << "Вы выбрали несуществующее действие, попробуйте ещё раз\n";
continue;
}
}
return 0;
}