Skip to content
Open

DZ#2 #80

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
81 changes: 19 additions & 62 deletions src/main/kotlin/ru/otus/homework/functions.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,78 +3,35 @@ package ru.otus.homework
import java.time.LocalDate

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 })
println(sumInts(1, 2, 3, 4))

sign(
lastName = "Иванов",
firstName = "Вася"
)
println(joinStrings("Hello", "World"))
println(joinStrings("Hello", "World", separator = ','))

translate(calculate(1.1F, 2.2F, 3.3F)) {
"In english: ${it.replace("+", "plus").replace("=", "equals")}"
val time = measureExecutionTime {
for (i in 1..1000000) { }
}
println(
calculate(1.1F, 2.2F, 3.3F) {
"%.4f (с точностью до четырех знаков)".format(this)
}
)

val product = 2 by 2
println("Произведение: $product")
}

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

fun translate(what: String, translator: (String) -> String) {
println(translator(what))
}
println("Execution time: $time ms")

fun sign(firstName: String, lastName: String, date: LocalDate = LocalDate.now()) {
println("Работу выполнил: $firstName $lastName, ${date.russian()}")
}

internal fun LocalDate.russian(): String {
return "${this.dayOfMonth}.${monthValue}.${year}"
}

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 sumInts(n1: Int, n2: Int, vararg others: Int): Int {
var sum = n1 + n2
for (n in others) {
sum += n
}
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()}"
return sum
}

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

fun add(a: Int, b: Int): Int = a + b
fun subtract(a: Int, b: Int): Int = a - b
fun measureExecutionTime(block: () -> Unit): Long {
val start = System.currentTimeMillis()
block()
val end = System.currentTimeMillis()
return end - start
}
18 changes: 15 additions & 3 deletions src/test/kotlin/ru/otus/homework/FunctionsTest.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,26 @@

package ru.otus.homework

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

class FunctionsTest {

@Test
fun calculationTest() {
fun joinStringsTest() {

val str1 = "str1"
val str2 = "str2"
val str3 = "str3"

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

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