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

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

override fun description(): String {
TODO("Not yet implemented")
return coffee.description() + ", молоко"
}
}

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

override fun description(): String {
TODO("Not yet implemented")
return coffee.description() + ", сахар"
}
}

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

override fun description(): String {
TODO("Not yet implemented")
return coffee.description() + ", ваниль"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,16 @@ import kotlin.reflect.KProperty
* Delegate that allows to set non-empty string value
*/
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.isNotBlank()) {
this.value = newValue
}
}
}
34 changes: 32 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,9 @@

package ru.otus.homework.homework

import java.util.LinkedList
import kotlin.properties.Delegates

/**
* Профиль пользователя
*/
Expand Down Expand Up @@ -37,7 +40,8 @@ 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 +54,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 Delegates.vetoable(fullName) { _, _, newValue ->
newValue.isNotBlank()}

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

private class ProfileImplementationWithLogging(private val userProfile: UserProfile) : UserProfile.Logging, UserProfile by userProfile {
private val log = LinkedList<String>()

override var fullName: String
get() = userProfile.fullName
set(value) {
log.add("Changing `fullName` from '${userProfile.fullName}' to '$value'")
userProfile.fullName = value
}

override var email: String
get() = userProfile.email
set(value) {
log.add("Changing `email` from '${userProfile.email}' to '$value'")
userProfile.email = value
}

override fun getLog(): List<String> = log
}
4 changes: 2 additions & 2 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")
}
}