data class Product(
val id: Int,
val name: String,
val price: Double,
val category: String,
val rating: Double
)
fun main() {
val products = listOf(
Product(1, "Ноутбук", 85000.0, "electronics", 4.8),
Product(2, "Смартфон", 45000.0, "electronics", 4.6),
Product(3, "Наушники", 5000.0, "electronics", 4.2),
Product(4, "Яблоки", 150.0, "food", 4.9),
Product(5, "Хлеб", 50.0, "food", 4.0),
Product(6, "Куртка", 7000.0, "clothes", 4.5),
Product(7, "Джинсы", 3500.0, "clothes", 4.3),
Product(8, "Футболка", 1200.0, "clothes", 4.7)
)
shopReport(products)
}
fun shopReport(products: List<Product>) {
println("1. Товары по убыванию цены:")
products.sortedByDescending { it.price }.forEach { println(" ${it.name}:${it.price} руб.") }
println("\n2. Товары с рейтингом >= 4.5:")
products.filter { it.rating >= 4.5 }.forEach { println(" ${it.name} (рейтинг: ${it.rating})") }
val grouped = products.groupBy { it.category }
println("\n3. Группировка по категориям:")
grouped.forEach { (category, list) ->
println(" $category:${list.map { it.name }}")
}
println("\n4. Средняя цена по категориям:")
grouped.mapValues { (_, list) -> list.map { it.price }.average() }
.forEach { (cat, avg) -> println(" $cat:${String.format("%.2f", avg)} руб.") }
println("\n5. Самый дорогой товар в каждой категории:")
grouped.mapValues { (_, list) -> list.maxByOrNull { it.price } }
.forEach { (cat, prod) -> println(" $cat: ${prod?.name} (${prod?.price} руб.)") }
println("\n6. Всего товаров: ${products.size}, Общая стоимость: ${products.sumOf { it.price }} руб.")
}