Загрузка данных


data class Product(
    val id: Int,
    val name: String,
    val price: Double,
    val category: String = "general"
)

data class Order(
    val orderId: Int,
    val customerName: String,
    val items: List<Product>
)

fun Order.totalPrice(): Double = items.sumOf { it.price }

fun main() {
    val products = listOf(
        Product(1, "Хлеб", 45.0, "food"),
        Product(2, "Молоко", 80.0, "food"),
        Product(3, "Наушники", 3500.0, "electronics"),
        Product(4, "Ручка", 30.0)
    )

    val order1 = Order(101, "Алексей", listOf(products[0], products[2]))
    val order2 = Order(102, "Мария", listOf(products[1], products[3], products[2]))

    for (order in listOf(order1, order2)) {
        println("Заказ #${order.orderId}")
        println("Клиент: ${order.customerName}")
        order.items.forEach { println("  $it") }
        println("Итого: ${order.totalPrice()} руб.\n")
    }

    val original = products[2]
    val discounted = original.copy(price = original.price * 0.8)
    println("Исходный товар:    $original")
    println("Со скидкой 20%:    $discounted")
}