diff --git a/src/main/kotlin/ru/otus/homework/homework/Coffee.kt b/src/main/kotlin/ru/otus/homework/homework/Coffee.kt index c73f420..4a0811d 100644 --- a/src/main/kotlin/ru/otus/homework/homework/Coffee.kt +++ b/src/main/kotlin/ru/otus/homework/homework/Coffee.kt @@ -15,37 +15,39 @@ interface Coffee { fun description(): String } -class SimpleCoffee : Coffee { - override fun cost() = 200 - override fun description() = "Простой кофе" -} +sealed class CoffeeIngredient : Coffee { + class Milk : CoffeeIngredient() { + override fun cost(): Int = 50 + override fun description(): String = "молоко" + } -class MilkDecorator(private val coffee: Coffee) : Coffee { - override fun cost(): Int { - TODO("Not yet implemented") + class Sugar : CoffeeIngredient() { + override fun cost(): Int = 20 + override fun description(): String = "сахар" } - override fun description(): String { - TODO("Not yet implemented") + class Vanilla : CoffeeIngredient() { + override fun cost(): Int = 70 + override fun description(): String = "ваниль" } } -class SugarDecorator(private val coffee: Coffee) : Coffee { - override fun cost(): Int { - TODO("Not yet implemented") - } +class CoffeeDecorator(private val coffee: Coffee, private val ingredient: Coffee) : Coffee { + override fun cost(): Int = coffee.cost() + ingredient.cost() - override fun description(): String { - TODO("Not yet implemented") - } + override fun description(): String = "${coffee.description()}, ${ingredient.description()}" } -class VanillaDecorator(private val coffee: Coffee) : Coffee { - override fun cost(): Int { - TODO("Not yet implemented") - } +class SimpleCoffee : Coffee { + override fun cost() = 200 + override fun description() = "Простой кофе" +} - override fun description(): String { - TODO("Not yet implemented") - } -} \ No newline at end of file +class MilkDecorator(private val coffee: Coffee) : + Coffee by CoffeeDecorator(coffee, CoffeeIngredient.Milk()) + +class SugarDecorator(private val coffee: Coffee) : + Coffee by CoffeeDecorator(coffee, CoffeeIngredient.Sugar()) + +class VanillaDecorator(private val coffee: Coffee) : + Coffee by CoffeeDecorator(coffee, CoffeeIngredient.Vanilla()) diff --git a/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt b/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt index 568f368..4bd308d 100644 --- a/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt +++ b/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt @@ -5,12 +5,10 @@ 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(private var initialValue: String = "") { + operator fun getValue(thisRef: Any?, property: KProperty<*>): String = initialValue operator fun setValue(thisRef: Any?, property: KProperty<*>, newValue: String) { - TODO("Implement `setValue` function") + if (newValue.isNotBlank()) initialValue = 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..82b47b7 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 + /** * Профиль пользователя */ @@ -37,7 +39,7 @@ interface UserProfile { * Creates user profile with logging */ fun createWithLogging(fullName: String, email: String): UserProfile.Logging { - TODO("Implement `createWithLogging` function") + return ProfileWithLoggingImplementation(ProfileImplementation(fullName, email)) } } } @@ -50,4 +52,28 @@ 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(initialFullName: String, initialEmail: String) : UserProfile { + override var email: String by Delegates.vetoable(initialEmail) { _, _, newValue -> + return@vetoable newValue.isNotBlank() && emailRegex.matches(newValue) + } + override var fullName: String by NonEmptyStringDelegate(initialFullName) +} + +private class ProfileWithLoggingImplementation(private val pi: ProfileImplementation) : + UserProfile.Logging { + private val log: MutableList = mutableListOf() + + override var fullName: String by Delegates.vetoable(pi.fullName) { property, oldValue, newValue -> + pi.fullName = newValue + if (pi.fullName != newValue) return@vetoable false + log.add("Changing `${property.name}` from '$oldValue' to '${newValue}'") + } + + override var email: String by Delegates.vetoable(pi.email) { property, oldValue, newValue -> + pi.email = newValue + if (pi.email != newValue) return@vetoable false + log.add("Changing `${property.name}` from '$oldValue' to '${newValue}'") + } + + override fun getLog(): List = log +} \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/homework/homework/WithLogging.kt b/src/main/kotlin/ru/otus/homework/homework/WithLogging.kt index 4a41138..6e55ad0 100644 --- a/src/main/kotlin/ru/otus/homework/homework/WithLogging.kt +++ b/src/main/kotlin/ru/otus/homework/homework/WithLogging.kt @@ -8,4 +8,4 @@ interface WithLogging { * Текущие записи журнала */ fun getLog(): List -} \ 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..9c6ea00 100644 --- a/src/main/kotlin/ru/otus/homework/homework/processList.kt +++ b/src/main/kotlin/ru/otus/homework/homework/processList.kt @@ -8,7 +8,8 @@ inline fun processList(list: List, action: (Int) -> Unit) { fun skipThreeAndPrint(list: List) { processList(list) { - if (it == 3) return + if (list.indexOf(it) == 2) return@processList println("Processing $it") } + }