Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*
- fun main() {
- val input = Scanner(System.`in`)
- val purchases = mutableMapOf<String, MutableMap<String, Int>>()
- println("Введите покупки (нажмите Enter для завершения):")
- while (true) {
- val line = input.nextLine().trim()
- if (line.isEmpty()) break // остановить ввод, если введена пустая строка
- if (line.isBlank()) continue // игнорируем пустые строки
- val parts = line.split(" ")
- if (parts.size != 3) continue // пропускаем некорректные строки
- val customer = parts[0]
- val product = parts[1]
- val quantity = parts[2].toIntOrNull() ?: continue // игнорируем некорректные количества
- // Обновляем информацию о покупках
- purchases.computeIfAbsent(customer) { mutableMapOf() }
- .merge(product, quantity) { oldQuantity, newQuantity -> oldQuantity + newQuantity }
- }
- // Сортируем покупателей в лексикографическом порядке
- val sortedCustomers = purchases.keys.sorted()
- for (customer in sortedCustomers) {
- println("$customer:")
- val products = purchases[customer]?.toList()?.sortedBy { it.first } ?: emptyList()
- for ((product, quantity) in products) {
- println("$product $quantity")
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement