diff --git a/src/main/kotlin/ru/otus/homework/homework/Coffee.kt b/src/main/kotlin/ru/otus/homework/homework/Coffee.kt index c73f420..4c88207 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..395c1c3 100644 --- a/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt +++ b/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt @@ -6,11 +6,16 @@ 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.isNotBlank()) { + 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..e80f810 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 + /** * Профиль пользователя */ @@ -37,7 +40,8 @@ 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 +54,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 Delegates.vetoable(fullName) { _, _, newValue -> + newValue.isNotBlank()} + + override var email : String by Delegates.vetoable(email) { _, _, newValue -> + newValue.isNotBlank() && emailRegex.matches(newValue)} +} + +private class ProfileImplementationWithLogging(private val userProfile: UserProfile) : UserProfile.Logging, UserProfile by userProfile { + private val log = LinkedList() + + 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 diff --git a/src/main/kotlin/ru/otus/homework/homework/processList.kt b/src/main/kotlin/ru/otus/homework/homework/processList.kt index 6d8ab43..a3564a0 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") } }