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
24 changes: 6 additions & 18 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,19 @@ class SimpleCoffee : Coffee {
}

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

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

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

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

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

override fun description(): String {
TODO("Not yet implemented")
}
override fun description() = coffee.description() + ", ваниль"
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,23 @@
package ru.otus.homework.homework

import kotlin.properties.ReadWriteProperty
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(var value: String=""): ReadWriteProperty<Any?, String> {

override fun getValue(thisRef: Any?, property: KProperty<*>): String {
return value
}

operator fun setValue(thisRef: Any?, property: KProperty<*>, newValue: String) {
TODO("Implement `setValue` function")
override fun setValue(thisRef: Any?, property: KProperty<*>, newValue: String) {
if (newValue.isNotBlank()) {
this.value = newValue
} else {
System.err.println("Property ${property.name} cannot be empty")

}
}
}
47 changes: 44 additions & 3 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 All @@ -27,7 +29,7 @@ interface UserProfile {
/**
* Создает профиль пользователя
*/
fun create(fullName: String, email: String): UserProfile {
fun create(fullName: String, email: String ): UserProfile {
require(fullName.isNotBlank()) { "Full name should not be empty" }
require(email.isNotBlank() && emailRegex.matches(email)) { "Invalid email" }
return ProfileImplementation(fullName, email)
Expand All @@ -37,7 +39,7 @@ interface UserProfile {
* Creates user profile with logging
*/
fun createWithLogging(fullName: String, email: String): UserProfile.Logging {
TODO("Implement `createWithLogging` function")
return LoggingProfileImplementation(fullName, email)
}
}
}
Expand All @@ -50,4 +52,43 @@ 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) { _, _, newValue ->
newValue.isNotBlank() && emailRegex.matches(newValue)
}
}

private class LoggingProfileImplementation(
fullName: String,
email: String
) : UserProfile.Logging {
private val log = mutableListOf<String>()
private val profile = ProfileImplementation(fullName, email)
private val oldValue = fullName

override var fullName: String
get() = profile.fullName
set(value) {

if (value != oldValue) {
log.add("Changing `fullName` from '$oldValue' to '$value'")
profile.fullName = value
}
}

override var email: String
get() = profile.email
set(value) {
val oldValue = profile.email
if (value != oldValue) {
log.add("Changing `email` from '$oldValue' to '$value'")
profile.email = value
}
}
init{
profile.fullName = fullName
}

override fun getLog(): List<String> = log.toList()
}
3 changes: 1 addition & 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,6 @@ 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) (print("Processing $it\n"))
}
}