#include "stdafx.h"
#include <iostream>
#include "list"
#include <algorithm>
#include <sstream>
#include <string>
using namespace std;
class Car {
public:
enum Type
{
Passenger,
Cargo
};
int Price;
string Model;
Type TypeAuto;
Car(int price, string model, Type type) {
this->Model = model;
this->Price = price;
this->TypeAuto = type;
}
string ToString() {
std::stringstream result;
result << "Model: " << this->Model <<
", Price: " << this->Price <<
", Type: " << this->TypeAuto;
return result.str();
}
};
class FilterCars {
public:
list<Car> Cars;
int AveragePrice;
FilterCars(list<Car> cars, int averagePrice) {
this->Cars = cars;
this->AveragePrice = averagePrice;
}
};
void ShowCars(list<Car> cars) {
for (auto car : cars) {
std::cout << car.ToString() << endl;;
}
}
FilterCars AveragePriceByType(list<Car> cars, Car::Type type) {
list<Car> selectCars;
std::copy_if(cars.begin(), cars.end(), std::back_inserter(selectCars), [type](Car car) {
return car.TypeAuto == type;
});
auto prices = 0;
for (auto car : selectCars) {
prices += car.Price;
}
auto average = prices / selectCars.size();
return FilterCars(selectCars, average);
}
int main()
{
setlocale(LC_ALL, "russian");
auto cars = {
Car(40000, "Tesla", Car::Type::Passenger),
Car(34000, "Chevrolet", Car::Type::Passenger),
Car(324000, "Mercedes ", Car::Type::Passenger),
Car(114000, "Volvo ", Car::Type::Cargo),
Car(222000, "BMW ", Car::Type::Cargo),
};
auto cargo = AveragePriceByType(cars, Car::Type::Cargo);
auto passenger = AveragePriceByType(cars, Car::Type::Passenger);
cout << "Вывод грузовых автомобилей: " << endl;
ShowCars(cargo.Cars);
cout << "Средняя цена грузовых автомобилей: " << cargo.AveragePrice << endl << endl;
cout << "Вывод легковых автомобилей: " << endl;
ShowCars(passenger.Cars);
cout << "Средняя цена легковых автомобилей: " << passenger.AveragePrice << endl << endl;
system("pause");
return 0;
}