diff --git a/src/main/kotlin/ru/otus/homework/homework/Coffee.kt b/src/main/kotlin/ru/otus/homework/homework/Coffee.kt index c73f420..7ec4fa7 100644 --- a/src/main/kotlin/ru/otus/homework/homework/Coffee.kt +++ b/src/main/kotlin/ru/otus/homework/homework/Coffee.kt @@ -20,32 +20,23 @@ 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 CoffeeDecorator(private val coffee: Coffee) : Coffee { + protected val base: Coffee = coffee + override fun cost() = base.cost() + override fun description() = base.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(coffee: Coffee) : CoffeeDecorator(coffee) { + override fun cost(): Int = base.cost() + 50 + override fun description(): String = base.description() + ", молоко" } -class VanillaDecorator(private val coffee: Coffee) : Coffee { - override fun cost(): Int { - TODO("Not yet implemented") - } +class SugarDecorator(coffee: Coffee) : CoffeeDecorator(coffee) { + override fun cost(): Int = base.cost() + 20 + override fun description(): String = base.description() + ", сахар" +} - override fun description(): String { - TODO("Not yet implemented") - } +class VanillaDecorator(coffee: Coffee) : CoffeeDecorator(coffee) { + override fun cost(): Int = base.cost() + 70 + override fun description(): String = base.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..0e6301d 100644 --- a/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt +++ b/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt @@ -5,12 +5,15 @@ 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(initialValue: String ="") { + private var value = initialValue + + operator fun getValue(thisRef: Any?, property: KProperty<*>): String = value operator fun setValue(thisRef: Any?, property: KProperty<*>, newValue: String) { - TODO("Implement `setValue` function") + if (newValue.isBlank()) { + return + } + 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..c7ccc5d 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,8 +39,27 @@ interface UserProfile { * Creates user profile with logging */ fun createWithLogging(fullName: String, email: String): UserProfile.Logging { - TODO("Implement `createWithLogging` function") + val profile = ProfileImplementation(fullName, email) + + return object : UserProfile.Logging { + private val log = mutableListOf() + 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.toList() + } } + } } @@ -50,4 +71,11 @@ 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) { _, _, new -> + new.isNotBlank() + } + override var email: String by Delegates.vetoable(email) { _, _, new -> + emailRegex.matches(new) + } +} \ 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..4aeeb69 100644 --- a/src/main/kotlin/ru/otus/homework/homework/processList.kt +++ b/src/main/kotlin/ru/otus/homework/homework/processList.kt @@ -1,14 +1,14 @@ package ru.otus.homework.homework -inline fun processList(list: List, action: (Int) -> Unit) { - for (item in list) { - action(item) +inline fun processList(list: List, action: (Int,Int) -> Unit) { + for ((index, item) in list.withIndex()) { + action(index, item) } } fun skipThreeAndPrint(list: List) { - processList(list) { - if (it == 3) return - println("Processing $it") + processList(list) { index, value -> + if (index == 2) return@processList + println("Processing $value") } -} +} \ No newline at end of file