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
2 changes: 1 addition & 1 deletion build.gradle
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
plugins {
id 'org.jetbrains.kotlin.jvm' version '2.2.10'
id 'org.jetbrains.kotlin.jvm' version '2.0.21'
}

kotlin {
Expand Down
40 changes: 17 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,26 @@ 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 AbstractDecorator(protected val decoratedCoffee: Coffee) : Coffee {
protected abstract val additionalCost: Int
protected abstract val additionalDescription: String

class SugarDecorator(private val coffee: Coffee) : Coffee {
override fun cost(): Int {
TODO("Not yet implemented")
}
final override fun cost() = decoratedCoffee.cost() + additionalCost
final override fun description() = "${decoratedCoffee.description()}, $additionalDescription"
}

override fun description(): String {
TODO("Not yet implemented")
}
class MilkDecorator(coffee: Coffee) : AbstractDecorator(coffee) {
override val additionalCost = 50
override val additionalDescription = "молоко"
}

class VanillaDecorator(private val coffee: Coffee) : Coffee {
override fun cost(): Int {
TODO("Not yet implemented")
}
class SugarDecorator(decoratedCoffee: Coffee) : AbstractDecorator(decoratedCoffee) {
override val additionalCost = 20
override val additionalDescription = "сахар"
}

override fun description(): String {
TODO("Not yet implemented")
}
class VanillaDecorator(decoratedCoffee: Coffee) : AbstractDecorator(decoratedCoffee) {
override val additionalCost = 70
override val additionalDescription = "ваниль"
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package ru.otus.homework.homework


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
} else {
System.err.println("Property ${property.name} cannot be empty")
}
}
}
61 changes: 59 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.reflect.KProperty

/**
* Профиль пользователя
*/
Expand Down Expand Up @@ -37,7 +39,9 @@ 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,57 @@ 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 {
val vetoable = VetoableEmail()
val nonEmptyStringDelegate = NonEmptyStringDelegate()
override var fullName: String by nonEmptyStringDelegate
override var email: String by vetoable

init {
this.fullName = fullName
this.email = email
}

}

/**
* Реализация [UserProfile.Logging].
*/
private class ProfileImplementationWithLogging(
val userProfile: UserProfile
) : UserProfile.Logging, UserProfile by userProfile {
private val log = mutableListOf<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
}

class VetoableEmail {
var email: String = ""
operator fun setValue(thisRef: Any?, property: KProperty<*>, value: String) {
if (value.matches(emailRegex))
email = value
else
System.err.println("veto placed on invalid email $value")
}

operator fun getValue(hisRef: Any?, property: KProperty<*>): String {
return this.email
}
}
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")
}
}