From 6ab345a122525c754aa69ef8767e1b336b3b464b Mon Sep 17 00:00:00 2001 From: maxdgladkov Date: Thu, 9 Apr 2026 22:29:55 +0300 Subject: [PATCH 1/3] processList done --- src/main/kotlin/ru/otus/homework/homework/processList.kt | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/main/kotlin/ru/otus/homework/homework/processList.kt b/src/main/kotlin/ru/otus/homework/homework/processList.kt index 6d8ab43..fa9940e 100644 --- a/src/main/kotlin/ru/otus/homework/homework/processList.kt +++ b/src/main/kotlin/ru/otus/homework/homework/processList.kt @@ -8,7 +8,7 @@ inline fun processList(list: List, action: (Int) -> Unit) { fun skipThreeAndPrint(list: List) { processList(list) { - if (it == 3) return + if (it == 3) return@processList println("Processing $it") } } From 954b0bff09b462b7b171219654eb1b3b3ec022af Mon Sep 17 00:00:00 2001 From: maxdgladkov Date: Thu, 9 Apr 2026 22:30:03 +0300 Subject: [PATCH 2/3] coffee done --- .../ru/otus/homework/homework/Coffee.kt | 24 +++++-------------- 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/src/main/kotlin/ru/otus/homework/homework/Coffee.kt b/src/main/kotlin/ru/otus/homework/homework/Coffee.kt index c73f420..813f05d 100644 --- a/src/main/kotlin/ru/otus/homework/homework/Coffee.kt +++ b/src/main/kotlin/ru/otus/homework/homework/Coffee.kt @@ -21,31 +21,19 @@ class SimpleCoffee : Coffee { } class MilkDecorator(private val coffee: Coffee) : Coffee { - override fun cost(): Int { - TODO("Not yet implemented") - } + override fun cost(): Int = coffee.cost() + 50 - override fun description(): String { - TODO("Not yet implemented") - } + override fun description(): String = coffee.description() + ", молоко" } class SugarDecorator(private val coffee: Coffee) : Coffee { - override fun cost(): Int { - TODO("Not yet implemented") - } + override fun cost(): Int = coffee.cost() + 20 - override fun description(): String { - TODO("Not yet implemented") - } + override fun description(): String = coffee.description() + ", сахар" } class VanillaDecorator(private val coffee: Coffee) : Coffee { - override fun cost(): Int { - TODO("Not yet implemented") - } + override fun cost(): Int = coffee.cost() + 70 - override fun description(): String { - TODO("Not yet implemented") - } + override fun description(): String = coffee.description() + ", ваниль" } \ No newline at end of file From b131f54263c37f41c865aa18637096ec4e2f8199 Mon Sep 17 00:00:00 2001 From: maxdgladkov Date: Fri, 10 Apr 2026 00:19:19 +0300 Subject: [PATCH 3/3] user profile done --- .../homework/NonEmptyStringDelegate.kt | 6 ++-- .../ru/otus/homework/homework/UserProfile.kt | 32 +++++++++++++++++-- 2 files changed, 33 insertions(+), 5 deletions(-) diff --git a/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt b/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt index 568f368..8dab1c5 100644 --- a/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt +++ b/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt @@ -5,12 +5,12 @@ import kotlin.reflect.KProperty /** * Delegate that allows to set non-empty string value */ -class NonEmptyStringDelegate() { +class NonEmptyStringDelegate(private var value: String = "") { operator fun getValue(thisRef: Any?, property: KProperty<*>): String { - TODO("Implement `getValue` function") + return value } operator fun setValue(thisRef: Any?, property: KProperty<*>, newValue: String) { - TODO("Implement `setValue` function") + if (newValue.isNotBlank()) value = newValue } } \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/homework/homework/UserProfile.kt b/src/main/kotlin/ru/otus/homework/homework/UserProfile.kt index f0fab82..f4572bf 100644 --- a/src/main/kotlin/ru/otus/homework/homework/UserProfile.kt +++ b/src/main/kotlin/ru/otus/homework/homework/UserProfile.kt @@ -1,6 +1,8 @@ @file:Suppress("RemoveRedundantQualifierName") package ru.otus.homework.homework +import kotlin.properties.Delegates.observable +import kotlin.properties.Delegates.vetoable /** * Профиль пользователя @@ -37,7 +39,7 @@ interface UserProfile { * Creates user profile with logging */ fun createWithLogging(fullName: String, email: String): UserProfile.Logging { - TODO("Implement `createWithLogging` function") + return ProfileImplementationWithLogging(create(fullName, email)) } } } @@ -50,4 +52,30 @@ private val emailRegex = Regex("^[A-Za-z](.*)([@])(.+)(\\.)(.+)") /** * Реализация простого [UserProfile]. */ -private class ProfileImplementation(override var fullName: String, override var email: String): UserProfile \ No newline at end of file +private class ProfileImplementation( + fullName: String, + email: String +) : UserProfile { + override var fullName: String by NonEmptyStringDelegate(fullName) + override var email: String by vetoable(email) {_, _, new -> new.isNotBlank() && emailRegex.matches(new)} +} + +private class ProfileImplementationWithLogging(private val userProfile: UserProfile) : UserProfile by userProfile, UserProfile.Logging { + private val log: MutableList = mutableListOf() + + override var fullName: String + get() = userProfile.fullName + set(value) { + log.add("Changing `fullName` from '${userProfile.fullName}' to '$value'") + userProfile.fullName = value + } + + override var email: String + get() = userProfile.email + set(value) { + log.add("Changing `email` from '${userProfile.email}' to '$value'") + userProfile.email = value + } + + override fun getLog(): List = log +} \ No newline at end of file