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
27 changes: 6 additions & 21 deletions src/main/kotlin/ru/otus/homework/homework/Coffee.kt
Original file line number Diff line number Diff line change
Expand Up @@ -21,31 +21,16 @@ class SimpleCoffee : Coffee {
}

class MilkDecorator(private val coffee: Coffee) : Coffee {
override fun cost(): Int {
TODO("Not yet implemented")
}

override fun description(): String {
TODO("Not yet implemented")
}
override fun cost(): Int = coffee.cost() + 50
override fun description(): String = "${coffee.description()}, молоко"
}

class SugarDecorator(private val coffee: Coffee) : Coffee {
override fun cost(): Int {
TODO("Not yet implemented")
}

override fun description(): String {
TODO("Not yet implemented")
}
override fun cost(): Int = coffee.cost() + 20
override fun description(): String = "${coffee.description()}, сахар"
}

class VanillaDecorator(private val coffee: Coffee) : Coffee {
override fun cost(): Int {
TODO("Not yet implemented")
}

override fun description(): String {
TODO("Not yet implemented")
}
override fun cost(): Int = coffee.cost() + 70
override fun description(): String = "${coffee.description()}, ваниль"
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,16 @@ import kotlin.reflect.KProperty
/**
* Delegate that allows to set non-empty string value
*/
class NonEmptyStringDelegate() {
class NonEmptyStringDelegate(nameParam: String = "") {
private var _name: String = nameParam

operator fun getValue(thisRef: Any?, property: KProperty<*>): String {
TODO("Implement `getValue` function")
return _name
}

operator fun setValue(thisRef: Any?, property: KProperty<*>, newValue: String) {
TODO("Implement `setValue` function")
if (newValue.isNotBlank()) {
_name = newValue
}
}
}
46 changes: 44 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,33 @@ interface UserProfile {
* Creates user profile with logging
*/
fun createWithLogging(fullName: String, email: String): UserProfile.Logging {
TODO("Implement `createWithLogging` function")
val baseProfile = create(fullName = fullName, email = email)
val logs = mutableListOf<String>()

return object : UserProfile.Logging, UserProfile by baseProfile {
override fun getLog(): List<String> = logs

override var fullName: String
get() = baseProfile.fullName
set(value) {
val old = baseProfile.fullName
baseProfile.fullName = value
if (old != baseProfile.fullName) {
logs += "Changing `fullName` from '$old' to '${baseProfile.fullName}'"
}
}

override var email: String
get() = baseProfile.email
set(value) {
val old = baseProfile.email
baseProfile.email = value
if (old != baseProfile.email) {
logs += "Changing `email` from '$old' to '${baseProfile.email}'"
}
}
}

}
}
}
Expand All @@ -50,4 +78,18 @@ private val emailRegex = Regex("^[A-Za-z](.*)([@])(.+)(\\.)(.+)")
/**
* Реализация простого [UserProfile].
*/
private class ProfileImplementation(override var fullName: String, override var email: String): UserProfile
//private class ProfileImplementation(override var fullName: String, override var email: String): UserProfile

private class ProfileImplementation(fullNameParam: String, emailParam: String) : UserProfile {

private val delegate = NonEmptyStringDelegate(fullNameParam)

override var fullName: String
get() = delegate.getValue(this, ::fullName)
set(fullNameParam) = delegate.setValue(this, ::fullName, fullNameParam)

override var email: String by Delegates.vetoable(emailParam) { property, oldValue, newValue ->
emailRegex.matches(newValue)
}

}
2 changes: 1 addition & 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,7 @@ inline fun processList(list: List<Int>, action: (Int) -> Unit) {

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