From 75710a214c080417e2922ab8374f2fce3a3d8f5f Mon Sep 17 00:00:00 2001 From: Dmitriy Bulygin Date: Wed, 27 Aug 2025 16:36:41 +0300 Subject: [PATCH] added tasks solutions --- .../ru/otus/homework/homework/Coffee.kt | 36 +++++------- .../homework/NonEmptyStringDelegate.kt | 18 ++++-- .../ru/otus/homework/homework/UserProfile.kt | 58 ++++++++++++++++++- .../ru/otus/homework/homework/processList.kt | 12 +++- 4 files changed, 92 insertions(+), 32 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..42e435d 100644 --- a/src/main/kotlin/ru/otus/homework/homework/Coffee.kt +++ b/src/main/kotlin/ru/otus/homework/homework/Coffee.kt @@ -20,32 +20,22 @@ class SimpleCoffee : Coffee { override fun description() = "Простой кофе" } -class MilkDecorator(private val coffee: Coffee) : Coffee { - override fun cost(): Int { - TODO("Not yet implemented") - } - - override fun description(): String { - TODO("Not yet implemented") - } +abstract class DecoratorCoffee(private val coffee: Coffee): Coffee{ + override fun cost() = coffee.cost() + override fun description() = coffee.description() } -class SugarDecorator(private val coffee: Coffee) : Coffee { - override fun cost(): Int { - TODO("Not yet implemented") - } - - override fun description(): String { - TODO("Not yet implemented") - } +class MilkDecorator(private val coffee: Coffee) : DecoratorCoffee(coffee) { + override fun cost() = super.cost() + 50 + override fun description() = super.description() + ", молоко" } -class VanillaDecorator(private val coffee: Coffee) : Coffee { - override fun cost(): Int { - TODO("Not yet implemented") - } +class SugarDecorator(private val coffee: Coffee) : DecoratorCoffee(coffee) { + override fun cost() = super.cost() + 20 + override fun description() = super.description() + ", сахар" +} - override fun description(): String { - TODO("Not yet implemented") - } +class VanillaDecorator(private val coffee: Coffee) : DecoratorCoffee(coffee) { + override fun cost() = super.cost() + 70 + override fun description() = super.description() + ", ваниль" } \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt b/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt index 568f368..dab6789 100644 --- a/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt +++ b/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt @@ -6,11 +6,21 @@ import kotlin.reflect.KProperty * Delegate that allows to set non-empty string value */ class NonEmptyStringDelegate() { - operator fun getValue(thisRef: Any?, property: KProperty<*>): String { - TODO("Implement `getValue` function") + private var value: String = "" + + // если модифицровать конструктор NonEmptyStringDelegate, было бы проще.. + fun init(initValue: String) { + value = initValue } - operator fun setValue(thisRef: Any?, property: KProperty<*>, newValue: String) { - TODO("Implement `setValue` function") + operator fun getValue(thisRef: Any?, property: KProperty<*>): String { + return value + } + operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) { + if (value.isNotBlank()) { + this.value = value + } else { + println("empty fullname") + } } } \ 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..1bb3c9a 100644 --- a/src/main/kotlin/ru/otus/homework/homework/UserProfile.kt +++ b/src/main/kotlin/ru/otus/homework/homework/UserProfile.kt @@ -1,6 +1,7 @@ @file:Suppress("RemoveRedundantQualifierName") package ru.otus.homework.homework +import kotlin.properties.Delegates /** * Профиль пользователя @@ -27,6 +28,7 @@ interface UserProfile { /** * Создает профиль пользователя */ + //override var email: String by Delegates.vetoable() fun create(fullName: String, email: String): UserProfile { require(fullName.isNotBlank()) { "Full name should not be empty" } require(email.isNotBlank() && emailRegex.matches(email)) { "Invalid email" } @@ -37,7 +39,7 @@ interface UserProfile { * Creates user profile with logging */ fun createWithLogging(fullName: String, email: String): UserProfile.Logging { - TODO("Implement `createWithLogging` function") + return LoggingProfile(fullName, email) } } } @@ -50,4 +52,56 @@ 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(initFullNAme: String, initEmail: String): UserProfile { + override var email: String by Delegates.vetoable(initEmail) { _, old, new -> + when { + !emailRegex.matches(new) -> { + false + } + + else -> true + } + } + + // если бы можно было переделать конструктор NonEmptyStringDelegate(), + // было бы проще с передачей значения + private val fullNameDelegate = NonEmptyStringDelegate().apply { + init(initFullNAme) + } + override var fullName: String by fullNameDelegate +} + +private class LoggingProfile(initFullName: String, initEmail: String) : UserProfile.Logging { + private val logs = mutableListOf() + + override fun getLog(): List = logs.toList() + + private val fullNameDelegate = NonEmptyStringDelegate().apply { + init(initFullName) + } + override var fullName: String + get() = fullNameDelegate.getValue(this, ::fullName) + set(value) { + val oldValue = fullName + fullNameDelegate.setValue(this, ::fullName, value) + if (fullName != oldValue) { + addLog("Changing `fullName` from '$oldValue' to '$fullName'") + } else { + addLog("Bad FullName: $fullName") + } + } + + override var email: String by Delegates.vetoable(initEmail) { _, old, new -> + val isValid = emailRegex.matches(new) + if (isValid) { + addLog("Changing `email` from '$old' to '$new'") + } else { + addLog("Bad email: $new") + } + isValid + } + + private fun addLog(message: String) { + logs.add(message) + } +} \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/homework/homework/processList.kt b/src/main/kotlin/ru/otus/homework/homework/processList.kt index 6d8ab43..4192822 100644 --- a/src/main/kotlin/ru/otus/homework/homework/processList.kt +++ b/src/main/kotlin/ru/otus/homework/homework/processList.kt @@ -7,8 +7,14 @@ inline fun processList(list: List, action: (Int) -> Unit) { } fun skipThreeAndPrint(list: List) { + var counter = 1 processList(list) { - if (it == 3) return - println("Processing $it") + if (counter != 3) + /* тут пришлось помучиться, + * т.к. тесты НЕ проходили, если использовать функцию println() + * пришлось переключиться на print(...\n) + */ + print("Processing $it\n") + counter ++ } -} +} \ No newline at end of file