From 28f619867b75d8977056bf74d8617b5e09e18118 Mon Sep 17 00:00:00 2001 From: Vp-Ma Date: Fri, 12 Sep 2025 10:38:17 +0300 Subject: [PATCH 1/3] Homework 5 implementation. Task 1. --- src/main/kotlin/ru/otus/homework/homework/processList.kt | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/main/kotlin/ru/otus/homework/homework/processList.kt b/src/main/kotlin/ru/otus/homework/homework/processList.kt index 6d8ab43..c9b07f8 100644 --- a/src/main/kotlin/ru/otus/homework/homework/processList.kt +++ b/src/main/kotlin/ru/otus/homework/homework/processList.kt @@ -8,7 +8,6 @@ inline fun processList(list: List, action: (Int) -> Unit) { fun skipThreeAndPrint(list: List) { processList(list) { - if (it == 3) return - println("Processing $it") + if( it !=3) print("Processing $it\n") } } From cfdb39326ff04b99f2794dc97d67e1eede2bbfb4 Mon Sep 17 00:00:00 2001 From: Vp-Ma Date: Fri, 12 Sep 2025 10:49:09 +0300 Subject: [PATCH 2/3] Homework 5 implementation. Task 2. --- src/main/kotlin/ru/otus/homework/homework/Coffee.kt | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/main/kotlin/ru/otus/homework/homework/Coffee.kt b/src/main/kotlin/ru/otus/homework/homework/Coffee.kt index c73f420..ed17804 100644 --- a/src/main/kotlin/ru/otus/homework/homework/Coffee.kt +++ b/src/main/kotlin/ru/otus/homework/homework/Coffee.kt @@ -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()}, ваниль" } } \ No newline at end of file From b7c54453cc781e7225aeb4d0c8992238a9b863d7 Mon Sep 17 00:00:00 2001 From: Vp-Ma Date: Fri, 10 Oct 2025 14:33:51 +0300 Subject: [PATCH 3/3] Homework 5 implementation. Task 3. --- .../homework/NonEmptyStringDelegate.kt | 13 ++++-- .../ru/otus/homework/homework/UserProfile.kt | 46 +++++++++++++++---- 2 files changed, 45 insertions(+), 14 deletions(-) diff --git a/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt b/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt index 568f368..578d5c8 100644 --- a/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt +++ b/src/main/kotlin/ru/otus/homework/homework/NonEmptyStringDelegate.kt @@ -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 + } } } \ No newline at end of file diff --git a/src/main/kotlin/ru/otus/homework/homework/UserProfile.kt b/src/main/kotlin/ru/otus/homework/homework/UserProfile.kt index f0fab82..eb80488 100644 --- a/src/main/kotlin/ru/otus/homework/homework/UserProfile.kt +++ b/src/main/kotlin/ru/otus/homework/homework/UserProfile.kt @@ -2,6 +2,9 @@ package ru.otus.homework.homework +import ru.otus.homework.homework.UserProfile +import kotlin.properties.Delegates.vetoable + /** * Профиль пользователя */ @@ -27,9 +30,8 @@ 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) } @@ -37,17 +39,41 @@ interface UserProfile { * 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 \ No newline at end of file +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() + 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{ + return profileLog.toList() + } +} \ No newline at end of file