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
39 changes: 16 additions & 23 deletions src/main/kotlin/ru/otus/homework/homework/Coffee.kt
Original file line number Diff line number Diff line change
Expand Up @@ -20,32 +20,25 @@ 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 Decorator(private val coffee: Coffee): Coffee {
abstract val extCost: Int
abstract val extDescription: String

class SugarDecorator(private val coffee: Coffee) : Coffee {
override fun cost(): Int {
TODO("Not yet implemented")
}
override fun cost() = coffee.cost() + extCost
override fun description() = coffee.description() + ", " + extDescription
}

override fun description(): String {
TODO("Not yet implemented")
}
class MilkDecorator(coffee: Coffee) : Decorator(coffee) {
override val extCost: Int = 50
override val extDescription: String = "молоко"
}

class VanillaDecorator(private val coffee: Coffee) : Coffee {
override fun cost(): Int {
TODO("Not yet implemented")
}
class SugarDecorator(coffee: Coffee) : Decorator(coffee) {
override val extCost: Int = 20
override val extDescription: String = "сахар"
}

override fun description(): String {
TODO("Not yet implemented")
}
class VanillaDecorator(coffee: Coffee) : Decorator(coffee) {
override val extCost: Int = 70
override val extDescription: String = "ваниль"
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,14 @@ import kotlin.reflect.KProperty
/**
* Delegate that allows to set non-empty string value
*/
class NonEmptyStringDelegate() {
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.isNullOrBlank()) {
value = newValue
}
}
}
32 changes: 30 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.vetoable

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

override var fullName: String by NonEmptyStringDelegate(fullName)

override var email: String by vetoable(email) { _, _, new -> new.isNotBlank() && emailRegex.matches(new)}
}

private class ProfileImplementationWithLogging(private val profile: UserProfile): UserProfile.Logging, UserProfile by profile {

private val log: MutableList<String> = 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<String> = log
}
6 changes: 3 additions & 3 deletions 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
println("Processing $it")
if (it == 3) return@processList
print("Processing $it\n")
}
}
}