Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 26 additions & 24 deletions src/main/kotlin/ru/otus/homework/homework/Coffee.kt
Original file line number Diff line number Diff line change
Expand Up @@ -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")
}
}
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())
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
}
30 changes: 28 additions & 2 deletions src/main/kotlin/ru/otus/homework/homework/UserProfile.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

package ru.otus.homework.homework

import kotlin.properties.Delegates

/**
* Профиль пользователя
*/
Expand Down Expand Up @@ -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))
}
}
}
Expand All @@ -50,4 +52,28 @@ private val emailRegex = Regex("^[A-Za-z](.*)([@])(.+)(\\.)(.+)")
/**
* Реализация простого [UserProfile].
*/
private class ProfileImplementation(override var fullName: String, override var email: String): UserProfile
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<String> = mutableListOf()

override var fullName: String by Delegates.vetoable(pi.fullName) { property, oldValue, newValue ->
pi.fullName = newValue
if (pi.fullName != newValue) return@vetoable false
Copy link
Author

@evgenysalnikov evgenysalnikov Sep 7, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

тут можно использовать рефлексию и сделать метод вроде

private fun changeField(property: KProperty<*>, oldValue: String, newValue: String) : Boolean {
        if (property is KMutableProperty1<*, *>) {
            val mutableKProperty = property as KMutableProperty1<ProfileImplementation, String>
            mutableKProperty.set(pi, newValue)

            val kProperty = property as KProperty1<ProfileImplementation, *>
            if (kProperty.get(pi) != newValue) return false
            log.add("Changing `${property.name}` from '$oldValue' to '${newValue}'")
            return true
        } else return 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<String> = log
}
2 changes: 1 addition & 1 deletion src/main/kotlin/ru/otus/homework/homework/WithLogging.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ interface WithLogging {
* Текущие записи журнала
*/
fun getLog(): List<String>
}
}
3 changes: 2 additions & 1 deletion src/main/kotlin/ru/otus/homework/homework/processList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@ inline fun processList(list: List<Int>, action: (Int) -> Unit) {

fun skipThreeAndPrint(list: List<Int>) {
processList(list) {
if (it == 3) return
if (list.indexOf(it) == 2) return@processList
println("Processing $it")
}

}