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
93 changes: 30 additions & 63 deletions src/main/kotlin/ru/otus/homework/functions.kt
Original file line number Diff line number Diff line change
@@ -1,80 +1,47 @@
package ru.otus.homework

import java.lang.Thread.sleep
import java.sql.Time
import java.time.LocalDate
import java.time.LocalTime

fun main() {
println(calculate(10, 20))
println(calculate(10, 20.5F))
println(calculate(30.1F, 40.2F, 50.3F, 60.4F))

println(calculate(3, 2, ::add))
println(calculate(3, 2, ::subtract))
println(calculate(3, 2) { n1, n2 -> n1 * n2 })
fun main() {
var result1: Int = task_1(1,2, 3, 4, 5, 6, 7, 8, 9, 10, 111 )
println("result1 = $result1")

sign(
lastName = "Иванов",
firstName = "Вася"
)
var result2 : String = task_2("one","two","three","four","five")
println("result2 = $result2")

translate(calculate(1.1F, 2.2F, 3.3F)) {
"In english: ${it.replace("+", "plus").replace("=", "equals")}"
}
println(
calculate(1.1F, 2.2F, 3.3F) {
"%.4f (с точностью до четырех знаков)".format(this)
}
)
result2 = task_2("one","two","three","four","five", separ = '$')
println("result2 = $result2")

val product = 2 by 2
println("Произведение: $product")
var result4 : Int = task_4(::slowFunc)
println("duration = $result4 seconds")
}

infix fun Int.by(other: Int): Int = this * other

fun translate(what: String, translator: (String) -> String) {
println(translator(what))
fun task_1(n1: Int, n2: Int, vararg n3: Int): Int {
var sum = n1 + n2
n3.forEach { sum += it }
return sum
}

fun sign(firstName: String, lastName: String, date: LocalDate = LocalDate.now()) {
println("Работу выполнил: $firstName $lastName, ${date.russian()}")
fun task_2(vararg str: String, separ: Char = ' '): String {
var strRes: String = str.joinToString("$separ")
return strRes
}

internal fun LocalDate.russian(): String {
return "${this.dayOfMonth}.${monthValue}.${year}"
fun task_4(paramFunc: () -> Unit) : Int {
val startTime = LocalTime.now()
paramFunc()
val stopTime = LocalTime.now()
val duration = stopTime.toSecondOfDay() - startTime.toSecondOfDay()
return duration
}

fun what(): String = "Огурцов"

fun calculate(n1: Int, n2: Int): String = "$n1 + $n2 = ${ n1 + n2 } ${ what() }"

fun calculate(n1: Int, n2: Float): String {
fun add(): String {
val s: Float

while (true) {
// Пример блока. Вычисляем, и сразу выходим
val s1 = n1 + n2
s = s1
break
}

return "$n1 + $n2 = $s"
fun slowFunc(): Unit {
for(i in 0 until 10) {
println("Number = $i")
Thread.sleep(1000)
}
return "${ add() } ${ what() }"
}

fun Float.formatWithDot(): String = "%.2f".format(this)

fun calculate(vararg n: Float, format: Float.() -> String = Float::formatWithDot): String {
var sum = 0F
n.forEach { sum += it }
return "${n.joinToString(" + ")} = ${sum.format()}"
}

fun calculate(n1: Int, n2: Int, op: (Int, Int) -> Int): String {
val result = op(n1, n2)
return "Результат операции $n1 и $n2 равен: $result"
}

fun add(a: Int, b: Int): Int = a + b
fun subtract(a: Int, b: Int): Int = a - b
}
13 changes: 10 additions & 3 deletions src/test/kotlin/ru/otus/homework/FunctionsTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ package ru.otus.homework

import org.junit.jupiter.api.Assertions
import org.junit.jupiter.api.Test
import org.junit.jupiter.api.assertThrows

class FunctionsTest {
@Test
fun calculationTest() {
fun task2Test() {

Assertions.assertEquals(
"str1 str2 str3",
task_2("str1", "str2", "str3")
)

Assertions.assertEquals(
"1 + 2 = 3 Огурцов",
calculate(1, 2)
"str1,str2,str3",
task_2("str1", "str2", "str3", separ = ',')
)
}
}