-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathmain.kt
More file actions
157 lines (125 loc) · 5.67 KB
/
main.kt
File metadata and controls
157 lines (125 loc) · 5.67 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
package com.example.hw2
data class City(val title: String)
data class Product(val name: String, val price: Double)
data class Order(val id: Int, val products: List<Product>, val isDelivered: Boolean)
data class Customer(val name: String, val city: City, val orders: List<Order>)
data class Shop(val name: String, val customers: List<Customer>)
fun main() {
val city1 = City("Ulsk")
val city2 = City("Piter")
val city3 = City("Msc")
val product1 = Product("Bread", 10.0)
val product2 = Product("Butter", 77.0)
val product3 = Product("Beer", 90.0)
val product4 = Product("Apple", 19.0)
val product5 = Product("Orange", 2.0)
val product6 = Product("Milk", 17.0)
val product7 = Product("Honey", 34.0)
val product8 = Product("Sugar", 30.0)
val product9 = Product("Egg", 1.0)
val product10 = Product("Brain", 100500.0)
val customer1 = Customer("Bob", city1, listOf(
Order(1, listOf(product1,product2), true),
Order(2, listOf(product3,product4), false))
)
val customer2 = Customer("Mike", city2, listOf(
Order(4, listOf(product6,product7), true),
Order(5, listOf(product8,product1), true),
Order(6, listOf(product9,product2), true),
Order(3, listOf(product3,product5), true)
))
val customer3 = Customer("Liza", city3, listOf(
Order(7, listOf(product9,product10), false),
Order(8, listOf(product1,product4), false),
Order(9, listOf(product3,product6), false))
)
val shop = Shop("Test", listOf(customer1,customer2,customer3))
println(shop.getSetOfCustomers() == setOf(customer1,customer2,customer3))
println(shop.getCitiesCustomersAreFrom() == setOf(city1,city2,city3))
println(shop.getCustomersFrom(city1) == listOf(customer1))
println(shop.hasCustomerFrom(city2))
println(shop.countCustomersFrom(city1) == 1)
println(shop.findAnyCustomerFrom(city1) == customer1)
println(shop.findAnyCustomerFrom(City("Urupinsk")) == null)
println(customer3.getOrderedProducts() == setOf(product1,product3,product4,product6,product9,product10))
println(shop.getCustomersSortedByNumberOfOrders() == listOf(customer1,customer3,customer2))
println(shop.groupCustomersByCity() == mapOf(city1.title to setOf(customer1), city2.title to setOf(customer2), city3.title to setOf(customer3)))
println(shop.getCustomersWithMoreUndeliveredOrdersThanDelivered() == setOf(customer3))
println(customer1.getMostExpensiveDeliveredProduct() == product2)
println(shop.getNumberOfTimesProductWasOrdered(product1) == 3)
}
//Пример как может выглядеть реализация (в сложном кейсе)
fun Shop.example(): Int? = customers.map { it.orders }.flatten().find { it.id == 3 }?.id
//TODO
//Преобразовать список клиентов в сет
fun Shop.getSetOfCustomers(): Set<Customer> = customers.toSet()
// Вернуть сет городов в которых проживают клиенты
fun Shop.getCitiesCustomersAreFrom(): Set<City> {
return customers.map {
it.city
}.toSet()
}
// Вернуть список клиентов из представленного города
fun Shop.getCustomersFrom(city: City): List<Customer> {
return customers.filter {
it.city == city
}
}
// Вернуть true если хоть один клиент из выбранного города
fun Shop.hasCustomerFrom(city: City): Boolean {
return customers.any {
it.city == city
}
}
// Вернуть количество клиентов из выбранного города
fun Shop.countCustomersFrom(city: City): Int {
return customers.count {
it.city == city
}
}
// Вернуть клиента из выбранного города или null, если нет таких
fun Shop.findAnyCustomerFrom(city: City): Customer? {
return customers.firstOrNull {
it.city == city
}
}
// Вернуть сет всех продуктов заказанных клиентом
fun Customer.getOrderedProducts(): Set<Product> {
return orders.flatMap {
it.products
}.toSet()
}
// Отсортировать клиентов по количеству заказов от меньшего к большему
fun Shop.getCustomersSortedByNumberOfOrders(): List<Customer> {
return customers.sortedBy {
it.orders.count()
}
}
// Вернуть словарь в котором названия городов являются ключами, а значениями - сет клиентов, проживающих в этом городе
fun Shop.groupCustomersByCity(): Map<String, Set<Customer>> {
return customers.groupBy {
it.city.title
}.mapValues {
it.value.toSet()
}
}
// Вернуть сет клиентов, у которых не доставленных заказов больше чем заказанных
fun Shop.getCustomersWithMoreUndeliveredOrdersThanDelivered(): Set<Customer> {
return customers.filter { it ->
it.orders.count { it.isDelivered } < it.orders.count { !it.isDelivered }
}.toSet()
}
// Вернуть наиболее дорогой продукт из всех доставленных
fun Customer.getMostExpensiveDeliveredProduct(): Product? {
return orders.filter { it.isDelivered }.flatMap { it ->
it.products.sortedBy { it.price }
}.lastOrNull()
}
// Вернуть число - сколько раз был заказан выбранный продукт
fun Shop.getNumberOfTimesProductWasOrdered(product: Product): Int {
return customers.flatMap { it ->
it.orders.flatMap {
it.products
}
}.count { it == product }
}