Skip to content

Татьяна Белова homework 2#8

Open
TatianaBelova wants to merge 1 commit intoMadBrains:masterfrom
TatianaBelova:homework
Open

Татьяна Белова homework 2#8
TatianaBelova wants to merge 1 commit intoMadBrains:masterfrom
TatianaBelova:homework

Conversation

@TatianaBelova
Copy link
Copy Markdown

No description provided.

@TatianaBelova TatianaBelova changed the title Tatiana Belova Android homework Татьяна Белова Android homework Feb 23, 2022
@TatianaBelova TatianaBelova changed the title Татьяна Белова Android homework Татьяна Белова homework 2 Feb 23, 2022

// Вернуть список клиентов из представленного города
fun Shop.getCustomersFrom(city: City): List<Customer> = listOf()
fun Shop.getCustomersFrom(city: City): List<Customer> = customers.filter { it.city == city }.toList()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Последний .toList() тебе просто копию результата делает - не нужно тут это

// Вернуть словарь в котором названия городов являются ключами, а значениями - сет клиентов, проживающих в этом городе
fun Shop.groupCustomersByCity(): Map<String, Set<Customer>> = mapOf()
fun Shop.groupCustomersByCity(): Map<String, Set<Customer>> =
customers.associate { it.city.title to this.getCustomersFrom(it.city).toSet() }
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Хорошо, только каждый раз еще поиск по всем клиентам. Можно вот так customers.groupBy { it.city.title }.mapValues { it.value.toSet() }

// Вернуть сет клиентов, у которых не доставленных заказов больше чем заказанных
fun Shop.getCustomersWithMoreUndeliveredOrdersThanDelivered(): Set<Customer> = setOf()
fun Shop.getCustomersWithMoreUndeliveredOrdersThanDelivered(): Set<Customer> = customers.filter { customer ->
customer.orders.filter { !it.isDelivered }.count() > customer.orders.filter { it.isDelivered }.count() }.toSet()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{ !it.isDelivered } - можно в count передавать, тогда фильтр не нужен будет

// Вернуть наиболее дорогой продукт из всех доставленных
fun Customer.getMostExpensiveDeliveredProduct(): Product? = null
fun Customer.getMostExpensiveDeliveredProduct(): Product? =
orders.filter { order -> order.isDelivered }.flatMap { it.products }.sortedBy { it.price }.reversed().firstOrNull()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Накрутила в конце) Можно было просто maxByOrNull { it.price }

// Вернуть число - сколько раз был заказан выбранный продукт
fun Shop.getNumberOfTimesProductWasOrdered(product: Product): Int = 0 No newline at end of file
fun Shop.getNumberOfTimesProductWasOrdered(product: Product): Int =
customers.flatMap { it.orders }.flatMap { it.products }.filter { it == product }.count()
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Тоже просто .count { it == product } в конце

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants