diff --git a/kotlin/pom.xml b/kotlin/pom.xml new file mode 100644 index 0000000000..eba248e1b9 --- /dev/null +++ b/kotlin/pom.xml @@ -0,0 +1,65 @@ + + + 4.0.0 + + com.baeldung + kotlin + 1.0-SNAPSHOT + + + + org.jetbrains.kotlin + kotlin-stdlib + ${kotlin-stdlib.version} + + + org.jetbrains.kotlin + kotlin-test-junit + ${kotlin-test-junit.version} + test + + + junit + junit + ${junit.version} + test + + + + + ${project.basedir}/src/main/kotlin + ${project.basedir}/src/test/kotlin + + + kotlin-maven-plugin + org.jetbrains.kotlin + ${kotlin-maven-plugin.version} + + + compile + + compile + + + + + test-compile + + test-compile + + + + + + + + + 4.12 + 1.0.4 + 1.0.4 + 1.0.4 + + + \ No newline at end of file diff --git a/kotlin/src/main/kotlin/com/baeldung/kotlin/Example1.kt b/kotlin/src/main/kotlin/com/baeldung/kotlin/Example1.kt new file mode 100644 index 0000000000..bca1e54a6c --- /dev/null +++ b/kotlin/src/main/kotlin/com/baeldung/kotlin/Example1.kt @@ -0,0 +1,5 @@ +package com.baeldung.kotlin + +fun main(args: Array){ + println("hello word") +} diff --git a/kotlin/src/main/kotlin/com/baeldung/kotlin/Item.kt b/kotlin/src/main/kotlin/com/baeldung/kotlin/Item.kt new file mode 100644 index 0000000000..36994e4994 --- /dev/null +++ b/kotlin/src/main/kotlin/com/baeldung/kotlin/Item.kt @@ -0,0 +1,14 @@ +package com.baeldung.kotlin + +open class Item(val id: String, val name: String = "unknown_name") { + open fun getIdOfItem(): String { + return id + } +} + +class ItemWithCategory(id: String, name: String, val categoryId: String) : Item(id, name) { + override fun getIdOfItem(): String { + return id + name + } +} + diff --git a/kotlin/src/main/kotlin/com/baeldung/kotlin/ItemService.kt b/kotlin/src/main/kotlin/com/baeldung/kotlin/ItemService.kt new file mode 100644 index 0000000000..3c0d2eacb1 --- /dev/null +++ b/kotlin/src/main/kotlin/com/baeldung/kotlin/ItemService.kt @@ -0,0 +1,66 @@ +package com.baeldung.kotlin + +import java.util.* + +class ItemService { + fun findItemNameForId(id: String): Item? { + val itemId = UUID.randomUUID().toString() + return Item(itemId, "name-$itemId") + } +} + +class ItemManager(val categoryId: String, val dbConnection: String) { + var email = "" + + constructor(categoryId: String, dbConnection: String, email: String) + : this(categoryId, dbConnection) { + this.email = email + } + + fun isFromSpecificCategory(catId: String): Boolean { + return categoryId == catId + } + + fun makeAnalyisOfCategory(catId: String): Unit { + val result = if (catId == "100") "Yes" else "No" + println(result) + + } +} + +fun main(args: Array) { + val numbers = arrayOf("first", "second", "third", "fourth") + + var concat = "" + for (n in numbers) { + concat += n + } + + var sum = 0 + for (i in 2..9) { + sum += i + } + + val firstName = "Tom" + val secondName = "Mary" + val concatOfNames = "$firstName + $secondName" + println("Names: $concatOfNames") + + val itemManager = ItemManager("cat_id", "db://connection") + val result = "function result: ${itemManager.isFromSpecificCategory("1")}" + println(result) + + val number = 2 + if (number < 10) { + println("number less that 10") + } else if (number > 10) { + println("number is greater that 10") + } + + val name = "John" + when (name) { + "John" -> println("Hi man") + "Alice" -> println("Hi lady") + } + +} \ No newline at end of file diff --git a/kotlin/src/main/kotlin/com/baeldung/kotlin/ListExtension.kt b/kotlin/src/main/kotlin/com/baeldung/kotlin/ListExtension.kt new file mode 100644 index 0000000000..da1773b7c9 --- /dev/null +++ b/kotlin/src/main/kotlin/com/baeldung/kotlin/ListExtension.kt @@ -0,0 +1,14 @@ +package com.baeldung.kotlin + +import java.util.concurrent.ThreadLocalRandom + +class ListExtension { + fun List.random(): T? { + if (this.isEmpty()) return null + return get(ThreadLocalRandom.current().nextInt(count())) + } + + fun getRandomElementOfList(list: List): T? { + return list.random() + } +} \ No newline at end of file diff --git a/kotlin/src/test/kotlin/com/baeldung/kotlin/ItemServiceTest.kt b/kotlin/src/test/kotlin/com/baeldung/kotlin/ItemServiceTest.kt new file mode 100644 index 0000000000..3d730b1283 --- /dev/null +++ b/kotlin/src/test/kotlin/com/baeldung/kotlin/ItemServiceTest.kt @@ -0,0 +1,20 @@ +package com.baeldung.kotlin + +import org.junit.Test +import kotlin.test.assertNotNull + +class ItemServiceTest { + @Test + fun givenItemId_whenGetForOptionalItem_shouldMakeActionOnNonNullValue() { + //given + val id = "item_id" + val itemService = ItemService() + + //when + val result = itemService.findItemNameForId(id) + + //then + assertNotNull(result?.let { it -> it.id }) + assertNotNull(result!!.id) + } +} \ No newline at end of file diff --git a/kotlin/src/test/kotlin/com/baeldung/kotlin/ListExtensionTest.kt b/kotlin/src/test/kotlin/com/baeldung/kotlin/ListExtensionTest.kt new file mode 100644 index 0000000000..7a496e7437 --- /dev/null +++ b/kotlin/src/test/kotlin/com/baeldung/kotlin/ListExtensionTest.kt @@ -0,0 +1,19 @@ +package com.baeldung.kotlin + +import com.baeldung.kotlin.ListExtension +import org.junit.Test +import kotlin.test.assertTrue + +class ListExtensionTest { + @Test + fun givenList_whenExecuteExtensionFunctionOnList_shouldReturnRandomElementOfList(){ + //given + val elements = listOf("a", "b", "c") + + //when + val result = ListExtension().getRandomElementOfList(elements) + + //then + assertTrue(elements.contains(result)) + } +} \ No newline at end of file diff --git a/pom.xml b/pom.xml index bfbaedefd8..6d26517999 100644 --- a/pom.xml +++ b/pom.xml @@ -68,6 +68,8 @@ jsoup junit5 + kotlin + log-mdc log4j log4j2