commit
32ad05a43e
65
kotlin/pom.xml
Normal file
65
kotlin/pom.xml
Normal file
@ -0,0 +1,65 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>kotlin</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-stdlib</artifactId>
|
||||||
|
<version>${kotlin-stdlib.version}</version>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<artifactId>kotlin-test-junit</artifactId>
|
||||||
|
<version>${kotlin-test-junit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<build>
|
||||||
|
<sourceDirectory>${project.basedir}/src/main/kotlin</sourceDirectory>
|
||||||
|
<testSourceDirectory>${project.basedir}/src/test/kotlin</testSourceDirectory>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<artifactId>kotlin-maven-plugin</artifactId>
|
||||||
|
<groupId>org.jetbrains.kotlin</groupId>
|
||||||
|
<version>${kotlin-maven-plugin.version}</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>compile</id>
|
||||||
|
<goals>
|
||||||
|
<goal>compile</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
|
||||||
|
<execution>
|
||||||
|
<id>test-compile</id>
|
||||||
|
<goals>
|
||||||
|
<goal>test-compile</goal>
|
||||||
|
</goals>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<junit.version>4.12</junit.version>
|
||||||
|
<kotlin-test-junit.version>1.0.4</kotlin-test-junit.version>
|
||||||
|
<kotlin-stdlib.version>1.0.4</kotlin-stdlib.version>
|
||||||
|
<kotlin-maven-plugin.version>1.0.4</kotlin-maven-plugin.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
</project>
|
5
kotlin/src/main/kotlin/com/baeldung/kotlin/Example1.kt
Normal file
5
kotlin/src/main/kotlin/com/baeldung/kotlin/Example1.kt
Normal file
@ -0,0 +1,5 @@
|
|||||||
|
package com.baeldung.kotlin
|
||||||
|
|
||||||
|
fun main(args: Array<String>){
|
||||||
|
println("hello word")
|
||||||
|
}
|
14
kotlin/src/main/kotlin/com/baeldung/kotlin/Item.kt
Normal file
14
kotlin/src/main/kotlin/com/baeldung/kotlin/Item.kt
Normal file
@ -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
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
66
kotlin/src/main/kotlin/com/baeldung/kotlin/ItemService.kt
Normal file
66
kotlin/src/main/kotlin/com/baeldung/kotlin/ItemService.kt
Normal file
@ -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<String>) {
|
||||||
|
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")
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
14
kotlin/src/main/kotlin/com/baeldung/kotlin/ListExtension.kt
Normal file
14
kotlin/src/main/kotlin/com/baeldung/kotlin/ListExtension.kt
Normal file
@ -0,0 +1,14 @@
|
|||||||
|
package com.baeldung.kotlin
|
||||||
|
|
||||||
|
import java.util.concurrent.ThreadLocalRandom
|
||||||
|
|
||||||
|
class ListExtension {
|
||||||
|
fun <T> List<T>.random(): T? {
|
||||||
|
if (this.isEmpty()) return null
|
||||||
|
return get(ThreadLocalRandom.current().nextInt(count()))
|
||||||
|
}
|
||||||
|
|
||||||
|
fun <T> getRandomElementOfList(list: List<T>): T? {
|
||||||
|
return list.random()
|
||||||
|
}
|
||||||
|
}
|
@ -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)
|
||||||
|
}
|
||||||
|
}
|
@ -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))
|
||||||
|
}
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user