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 @@ -5,12 +5,17 @@ import kotlin.reflect.KProperty
/**
* Delegate that allows to set non-empty string value
*/
class NonEmptyStringDelegate() {
class NonEmptyStringDelegate( ) {

private var strValue = ""

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

operator fun setValue(thisRef: Any?, property: KProperty<*>, newValue: String) {
TODO("Implement `setValue` function")
operator fun setValue(thisRef: Any?, property: KProperty<*>, fullName: String) {
if( fullName.isNotBlank() ) {
strValue = fullName
}
}
}
46 changes: 36 additions & 10 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 ru.otus.homework.homework.UserProfile
import kotlin.properties.Delegates.vetoable

/**
* Профиль пользователя
*/
Expand All @@ -27,27 +30,50 @@ interface 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)
}

/**
* Creates user profile with logging
*/
fun createWithLogging(fullName: String, email: String): UserProfile.Logging {
TODO("Implement `createWithLogging` function")
return ProfileWithLoggingImplementation( fullName , email )
}
}
}

/**
* Проверка емейла на корректность
*/
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 email: String by vetoable(email) {_, _, newValue -> newValue.isNotBlank() && Regex("^[A-Za-z](.*)([@])(.+)(\\.)(.+)").matches(newValue)}
override var fullName: String by NonEmptyStringDelegate( )
init { fullName = _fullName }
}

private class ProfileWithLoggingImplementation( val personFullName: String, val personEmail: String ): UserProfile.Logging {

private var profileLog = mutableListOf<String>()
private var userProfile = ProfileImplementation( personFullName, personEmail )

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

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

override fun getLog(): List<String>{
return profileLog.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")
}
}