diff --git a/kotlin/src/main/java/com/baeldung/java/StringUtils.java b/kotlin/src/main/java/com/baeldung/java/StringUtils.java new file mode 100644 index 0000000000..f405924cdf --- /dev/null +++ b/kotlin/src/main/java/com/baeldung/java/StringUtils.java @@ -0,0 +1,7 @@ +package com.baeldung.java; + +public class StringUtils { + public static String toUpperCase(String name) { + return name.toUpperCase(); + } +} diff --git a/kotlin/src/main/kotlin/com/baeldung/kotlin/ItemService.kt b/kotlin/src/main/kotlin/com/baeldung/kotlin/ItemService.kt index 3c0d2eacb1..88de1aa9be 100644 --- a/kotlin/src/main/kotlin/com/baeldung/kotlin/ItemService.kt +++ b/kotlin/src/main/kotlin/com/baeldung/kotlin/ItemService.kt @@ -24,29 +24,41 @@ class ItemManager(val categoryId: String, val dbConnection: String) { fun makeAnalyisOfCategory(catId: String): Unit { val result = if (catId == "100") "Yes" else "No" println(result) - + `object`() } + + fun sum(a: Int, b: Int): Int { + return a + b + } + + fun `object`(): String { + return "this is object" + } + } fun main(args: Array) { val numbers = arrayOf("first", "second", "third", "fourth") - var concat = "" for (n in numbers) { - concat += n + println(n) } - var sum = 0 - for (i in 2..9) { - sum += i + for (i in 2..9 step 2) { + println(i) } + val res = 1.rangeTo(10).map { it * 2 } + println(res) + val firstName = "Tom" val secondName = "Mary" val concatOfNames = "$firstName + $secondName" println("Names: $concatOfNames") + val sum = "four: ${2 + 2}" val itemManager = ItemManager("cat_id", "db://connection") + ItemManager(categoryId = "catId", dbConnection = "db://Connection") val result = "function result: ${itemManager.isFromSpecificCategory("1")}" println(result) @@ -63,4 +75,9 @@ fun main(args: Array) { "Alice" -> println("Hi lady") } + val items = listOf(1, 2, 3, 4) + + + val rwList = mutableListOf(1, 2, 3) + rwList.add(5) } \ No newline at end of file diff --git a/kotlin/src/main/kotlin/com/baeldung/kotlin/MathematicsOperations.kt b/kotlin/src/main/kotlin/com/baeldung/kotlin/MathematicsOperations.kt new file mode 100644 index 0000000000..924f9d2323 --- /dev/null +++ b/kotlin/src/main/kotlin/com/baeldung/kotlin/MathematicsOperations.kt @@ -0,0 +1,7 @@ +package com.baeldung.kotlin + +class MathematicsOperations { + fun addTwoNumbers(a: Int, b: Int): Int { + return a + b + } +} \ No newline at end of file diff --git a/kotlin/src/test/java/com/baeldung/kotlin/JavaCallToKotlinTest.java b/kotlin/src/test/java/com/baeldung/kotlin/JavaCallToKotlinTest.java new file mode 100644 index 0000000000..cfad7cefd4 --- /dev/null +++ b/kotlin/src/test/java/com/baeldung/kotlin/JavaCallToKotlinTest.java @@ -0,0 +1,17 @@ +package com.baeldung.kotlin; + +import org.junit.Test; + +import static org.junit.Assert.assertEquals; + +public class JavaCallToKotlinTest { + @Test + public void givenKotlinClass_whenCallFromJava_shouldProduceResults() { + //when + int res = new MathematicsOperations().addTwoNumbers(2, 4); + + //then + assertEquals(6, res); + + } +} diff --git a/kotlin/src/test/kotlin/com/baeldung/kotlin/KotlinScalaInteroperabilityTest.kt b/kotlin/src/test/kotlin/com/baeldung/kotlin/KotlinScalaInteroperabilityTest.kt new file mode 100644 index 0000000000..f671c3af8b --- /dev/null +++ b/kotlin/src/test/kotlin/com/baeldung/kotlin/KotlinScalaInteroperabilityTest.kt @@ -0,0 +1,20 @@ +package com.baeldung.kotlin + +import com.baeldung.java.StringUtils +import org.junit.Test +import kotlin.test.assertEquals + + +class KotlinScalaInteroperabilityTest { + @Test + fun givenLowercaseString_whenExecuteMethodFromJavaStringUtils_shouldReturnStringUppercase() { + //given + val name = "tom" + + //when + val res = StringUtils.toUpperCase(name) + + //then + assertEquals(res, "TOM") + } +} diff --git a/kotlin/src/test/kotlin/com/baeldung/kotlin/LambdaTest.kt b/kotlin/src/test/kotlin/com/baeldung/kotlin/LambdaTest.kt new file mode 100644 index 0000000000..34217336a0 --- /dev/null +++ b/kotlin/src/test/kotlin/com/baeldung/kotlin/LambdaTest.kt @@ -0,0 +1,19 @@ +package com.baeldung.kotlin + +import org.junit.Test +import kotlin.test.assertEquals + + +class LambdaTest { + @Test + fun givenListOfNumber_whenDoingOperationsUsingLambda_shouldReturnProperResult() { + //given + val listOfNumbers = listOf(1, 2, 3) + + //when + val sum = listOfNumbers.reduce { a, b -> a + b } + + //then + assertEquals(6, sum) + } +} \ No newline at end of file