From f8e66a1bddfcfe49c4891d5bf04b45fc5383246c Mon Sep 17 00:00:00 2001 From: Gena Date: Thu, 28 Aug 2025 22:51:10 +0300 Subject: [PATCH] =?UTF-8?q?=D0=94.=D0=97.=20=D0=BF=D0=BE=20=D1=82=D0=B5?= =?UTF-8?q?=D0=BC=D0=B5=20Kotlin-6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ru/otus/homework/homework/Coffee.kt | 12 +++--- .../homework/NonEmptyStringDelegate.kt | 8 +++- .../ru/otus/homework/homework/UserProfile.kt | 43 ++++++++++++++----- .../ru/otus/homework/homework/processList.kt | 5 +-- 4 files changed, 46 insertions(+), 22 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..1769f08 100644 --- a/src/main/kotlin/ru/otus/homework/homework/Coffee.kt +++ b/src/main/kotlin/ru/otus/homework/homework/Coffee.kt @@ -22,30 +22,30 @@ class SimpleCoffee : Coffee { class MilkDecorator(private val coffee: Coffee) : 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 { 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 { 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 diff --git a/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt b/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt index 568f368..522f565 100644 --- a/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt +++ b/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt @@ -6,11 +6,15 @@ import kotlin.reflect.KProperty * Delegate that allows to set non-empty string value */ 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.isEmpty() && !newValue.none({ it != ' ' })) { + this.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..23fc14b 100644 --- a/src/main/kotlin/ru/otus/homework/homework/UserProfile.kt +++ b/src/main/kotlin/ru/otus/homework/homework/UserProfile.kt @@ -2,6 +2,9 @@ package ru.otus.homework.homework +import java.util.LinkedList +import kotlin.properties.Delegates.vetoable + /** * Профиль пользователя */ @@ -24,30 +27,50 @@ interface UserProfile { interface Logging : UserProfile, WithLogging companion object { + /** * Создает профиль пользователя */ fun create(fullName: String, email: String): UserProfile { - require(fullName.isNotBlank()) { "Full name should not be empty" } - require(email.isNotBlank() && emailRegex.matches(email)) { "Invalid email" } return ProfileImplementation(fullName, email) } - /** * Creates user profile with logging */ fun createWithLogging(fullName: String, email: String): UserProfile.Logging { - TODO("Implement `createWithLogging` function") + return ProfileWithLoggingImplementation(create(fullName, email)) } } } -/** - * Проверка емейла на корректность - */ -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(fullNameStr: String, email: String): UserProfile { + override var email: String by vetoable(email) {_, _, newValue -> newValue.isNotBlank() && Regex("^[A-Za-z](.*)([@])(.+)(\\.)(.+)").matches(newValue)} + override var fullName: String by NonEmptyStringDelegate() + + init { + fullName = fullNameStr + } +} + +private class ProfileWithLoggingImplementation(private val profile: UserProfile): UserProfile.Logging, UserProfile by profile { + private val log = LinkedList() + + 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 diff --git a/src/main/kotlin/ru/otus/homework/homework/processList.kt b/src/main/kotlin/ru/otus/homework/homework/processList.kt index 6d8ab43..f3103dc 100644 --- a/src/main/kotlin/ru/otus/homework/homework/processList.kt +++ b/src/main/kotlin/ru/otus/homework/homework/processList.kt @@ -7,8 +7,5 @@ inline fun processList(list: List, action: (Int) -> Unit) { } fun skipThreeAndPrint(list: List) { - processList(list) { - if (it == 3) return - println("Processing $it") - } + processList(list, {if (it != 3) print("Processing $it\n")}) }