From 031a559f9d18cca724bad6c1d8a584c878b96e5c Mon Sep 17 00:00:00 2001 From: ArtemRem Date: Tue, 28 Oct 2025 22:39:26 +0400 Subject: [PATCH] hw - kotlin6 --- .../ru/otus/homework/homework/Coffee.kt | 24 +++------- .../homework/NonEmptyStringDelegate.kt | 17 +++++-- .../ru/otus/homework/homework/UserProfile.kt | 47 +++++++++++++++++-- .../ru/otus/homework/homework/processList.kt | 3 +- 4 files changed, 63 insertions(+), 28 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..8a2d023 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() = coffee.cost() + 50 - override fun description(): String { - TODO("Not yet implemented") - } + override fun description() = coffee.description() + ", молоко" } class SugarDecorator(private val coffee: Coffee) : Coffee { - override fun cost(): Int { - TODO("Not yet implemented") - } + override fun cost() = coffee.cost() + 20 - override fun description(): String { - TODO("Not yet implemented") - } + override fun description() = coffee.description() + ", сахар" } class VanillaDecorator(private val coffee: Coffee) : Coffee { - override fun cost(): Int { - TODO("Not yet implemented") - } + override fun cost() = coffee.cost() + 70 - override fun description(): String { - TODO("Not yet implemented") - } + override fun description() = 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..09e5a18 100644 --- a/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt +++ b/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt @@ -1,16 +1,23 @@ package ru.otus.homework.homework +import kotlin.properties.ReadWriteProperty 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") +class NonEmptyStringDelegate(var value: String=""): ReadWriteProperty { + + override fun getValue(thisRef: Any?, property: KProperty<*>): String { + return value } - operator fun setValue(thisRef: Any?, property: KProperty<*>, newValue: String) { - TODO("Implement `setValue` function") + override fun setValue(thisRef: Any?, property: KProperty<*>, newValue: String) { + if (newValue.isNotBlank()) { + this.value = newValue + } else { + System.err.println("Property ${property.name} cannot be empty") + + } } } \ 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..a6018bb 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 + /** * Профиль пользователя */ @@ -27,7 +29,7 @@ interface UserProfile { /** * Создает профиль пользователя */ - fun create(fullName: String, email: String): UserProfile { + 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) @@ -37,7 +39,7 @@ interface UserProfile { * Creates user profile with logging */ fun createWithLogging(fullName: String, email: String): UserProfile.Logging { - TODO("Implement `createWithLogging` function") + return LoggingProfileImplementation(fullName, email) } } } @@ -50,4 +52,43 @@ 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) { _, _, newValue -> + newValue.isNotBlank() && emailRegex.matches(newValue) + } +} + +private class LoggingProfileImplementation( + fullName: String, + email: String +) : UserProfile.Logging { + private val log = mutableListOf() + private val profile = ProfileImplementation(fullName, email) + private val oldValue = fullName + + override var fullName: String + get() = profile.fullName + set(value) { + + if (value != oldValue) { + log.add("Changing `fullName` from '$oldValue' to '$value'") + profile.fullName = value + } + } + + override var email: String + get() = profile.email + set(value) { + val oldValue = profile.email + if (value != oldValue) { + log.add("Changing `email` from '$oldValue' to '$value'") + profile.email = value + } + } + init{ + profile.fullName = fullName + } + + override fun getLog(): List = log.toList() +} diff --git a/src/main/kotlin/ru/otus/homework/homework/processList.kt b/src/main/kotlin/ru/otus/homework/homework/processList.kt index 6d8ab43..e8c0263 100644 --- a/src/main/kotlin/ru/otus/homework/homework/processList.kt +++ b/src/main/kotlin/ru/otus/homework/homework/processList.kt @@ -8,7 +8,6 @@ inline fun processList(list: List, action: (Int) -> Unit) { fun skipThreeAndPrint(list: List) { processList(list) { - if (it == 3) return - println("Processing $it") + if (it != 3) (print("Processing $it\n")) } }