From 3af70058a3d9468d0db2d061669c8b17681d1f80 Mon Sep 17 00:00:00 2001 From: Karavaev Date: Sat, 27 Sep 2025 16:27:45 +0300 Subject: [PATCH 1/4] Homework_Kotlin#6_task1 --- src/main/kotlin/ru/otus/homework/homework/processList.kt | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/kotlin/ru/otus/homework/homework/processList.kt b/src/main/kotlin/ru/otus/homework/homework/processList.kt index 6d8ab43..e817ed2 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 - println("Processing $it") + if (it == 3) return@processList + print("Processing $it\n") } -} +} \ No newline at end of file From fa0b9a23756f0170f028ae95eb94ac1d683d7768 Mon Sep 17 00:00:00 2001 From: Karavaev Date: Sat, 27 Sep 2025 16:37:56 +0300 Subject: [PATCH 2/4] Homework_Kotlin#6_task2_main --- .../kotlin/ru/otus/homework/homework/Coffee.kt | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 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..5500ee1 100644 --- a/src/main/kotlin/ru/otus/homework/homework/Coffee.kt +++ b/src/main/kotlin/ru/otus/homework/homework/Coffee.kt @@ -20,32 +20,32 @@ class SimpleCoffee : Coffee { override fun description() = "Простой кофе" } -class MilkDecorator(private val coffee: Coffee) : Coffee { +class MilkDecorator(private val coffee: Coffee) : Coffee by coffee { override fun cost(): Int { - TODO("Not yet implemented") + return coffee.cost() + 50 } override fun description(): String { - TODO("Not yet implemented") + return coffee.description() + ", молоко" } } -class SugarDecorator(private val coffee: Coffee) : Coffee { +class SugarDecorator(private val coffee: Coffee) : Coffee by coffee { override fun cost(): Int { - TODO("Not yet implemented") + return coffee.cost() + 20 } override fun description(): String { - TODO("Not yet implemented") + return coffee.description() + ", сахар" } } -class VanillaDecorator(private val coffee: Coffee) : Coffee { +class VanillaDecorator(private val coffee: Coffee) : Coffee by coffee { override fun cost(): Int { - TODO("Not yet implemented") + return coffee.cost() + 70 } override fun description(): String { - TODO("Not yet implemented") + return coffee.description() + ", ваниль" } } \ No newline at end of file From 553f663b5bbff170f3301e84ac257a7a0e14cb4b Mon Sep 17 00:00:00 2001 From: Karavaev Date: Sat, 27 Sep 2025 16:41:35 +0300 Subject: [PATCH 3/4] Homework_Kotlin#6_task2_optional --- .../ru/otus/homework/homework/Coffee.kt | 39 ++++++++----------- 1 file changed, 16 insertions(+), 23 deletions(-) diff --git a/src/main/kotlin/ru/otus/homework/homework/Coffee.kt b/src/main/kotlin/ru/otus/homework/homework/Coffee.kt index 5500ee1..4e3a0df 100644 --- a/src/main/kotlin/ru/otus/homework/homework/Coffee.kt +++ b/src/main/kotlin/ru/otus/homework/homework/Coffee.kt @@ -20,32 +20,25 @@ class SimpleCoffee : Coffee { override fun description() = "Простой кофе" } -class MilkDecorator(private val coffee: Coffee) : Coffee by coffee { - override fun cost(): Int { - return coffee.cost() + 50 - } - - override fun description(): String { - return coffee.description() + ", молоко" - } -} +abstract class Decorator(private val coffee: Coffee): Coffee { + abstract val extCost: Int + abstract val extDescription: String -class SugarDecorator(private val coffee: Coffee) : Coffee by coffee { - override fun cost(): Int { - return coffee.cost() + 20 - } + override fun cost() = coffee.cost() + extCost + override fun description() = coffee.description() + ", " + extDescription +} - override fun description(): String { - return coffee.description() + ", сахар" - } +class MilkDecorator(coffee: Coffee) : Decorator(coffee) { + override val extCost: Int = 50 + override val extDescription: String = "молоко" } -class VanillaDecorator(private val coffee: Coffee) : Coffee by coffee { - override fun cost(): Int { - return coffee.cost() + 70 - } +class SugarDecorator(coffee: Coffee) : Decorator(coffee) { + override val extCost: Int = 20 + override val extDescription: String = "сахар" +} - override fun description(): String { - return coffee.description() + ", ваниль" - } +class VanillaDecorator(coffee: Coffee) : Decorator(coffee) { + override val extCost: Int = 70 + override val extDescription: String = "ваниль" } \ No newline at end of file From b00561abef0fbb1a371b6641fa07af807c6b2164 Mon Sep 17 00:00:00 2001 From: Karavaev Date: Sat, 27 Sep 2025 16:47:16 +0300 Subject: [PATCH 4/4] Homework_Kotlin#6_task3 --- .../homework/NonEmptyStringDelegate.kt | 8 +++-- .../ru/otus/homework/homework/UserProfile.kt | 32 +++++++++++++++++-- 2 files changed, 35 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..bbc050b 100644 --- a/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt +++ b/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt @@ -5,12 +5,14 @@ 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.isNullOrBlank()) { + 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..d841e71 100644 --- a/src/main/kotlin/ru/otus/homework/homework/UserProfile.kt +++ b/src/main/kotlin/ru/otus/homework/homework/UserProfile.kt @@ -2,6 +2,8 @@ package ru.otus.homework.homework +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 profile: UserProfile): UserProfile.Logging, UserProfile by profile { + + private val log: MutableList = mutableListOf() + + override var fullName: String + get() = profile.fullName + set(value) { + log.add("Changing `fullName` from '${profile.fullName}' to '$value'") + profile.fullName = value + } + + override var email: String + get() = profile.email + set(value) { + log.add("Changing `email` from '${profile.email}' to '$value'") + profile.email = value + } + + override fun getLog(): List = log +} \ No newline at end of file