Leon Versteeg 3 tahun lalu
melakukan
cae7e8e2f8

+ 28 - 0
src/com/virtualprogrammers/learning/java/BoringJavaCode.java

@@ -0,0 +1,28 @@
+package com.virtualprogrammers.learning.java;
+
+import java.math.BigDecimal;
+import java.util.Random;
+
+public class BoringJavaCode {
+    public static void main(String[] args) {
+        Object result;
+
+        Integer random = new Random().nextInt(3);
+        if (random == 1) {
+            result = new BigDecimal(30);
+        } else {
+            result = "hello";
+        }
+
+        System.out.println("result is " + result);
+        if (result instanceof BigDecimal) {
+            result = ((BigDecimal) result).add(new BigDecimal(47));
+        } else {
+            String tempResult = (String) result;
+            result = tempResult.toUpperCase();
+        }
+
+        System.out.println("result is " + result);
+
+    }
+}

+ 77 - 0
src/com/virtualprogrammers/learning/kotlin/Customer.kt

@@ -0,0 +1,77 @@
+package com.virtualprogrammers.learning.kotlin
+
+data class Customer(
+    val name: String,
+    val address: String,
+    var age: Int
+) {
+    constructor(name: String, age: Int) : this(name, "", age) {
+        println("second constructor")
+    }
+
+    init {
+        println("init block")
+    }
+}
+
+class AlternativeCustomer(val name: String, val age: Int) {
+    var address: String
+
+    init {
+        address = ""
+    }
+
+    constructor(name: String, address: String, age: Int) : this(name, age) {
+        this.address = address
+    }
+}
+
+class AnotherAlternativeCustomer(val name: String, val address: String = "", var age: Int) {
+    var approved: Boolean = false
+        set(value) {
+            if (age >= 21) {
+                field = value
+            } else {
+                println("no can do")
+            }
+        }
+
+    val nextAge
+        get() = age + 1
+
+    operator fun component1() = name
+    operator fun component2() = age
+    
+    fun upperCaseName() = name.toUpperCase()
+
+    override fun toString() = "$name $address $age"
+
+    companion object {
+        fun getInstance() = AnotherAlternativeCustomer("micky", "address", 22)
+    }
+}
+
+fun main() {
+    val customer = Customer("leon", "waterlelie", 45)
+    customer.age = 20
+    val customer2 = Customer("John", 49)
+    println("${customer.name} is ${customer.age}")
+    println("${customer2.name} is ${customer2.age}")
+
+    val customer3 = AnotherAlternativeCustomer("leon", "waterlelie", 20)
+    customer3.approved = true
+    println("${customer3} == ${customer3.name} is ${customer3.age}")
+    customer3.nextAge
+    println("${customer3.name.uppercase()} is ${customer3.nextAge}")
+
+    val customer4 = AnotherAlternativeCustomer.getInstance()
+    println(customer4)
+
+
+    val customer5 = Customer("Sally", 29)
+    println(customer5)
+    val customer6 = customer5.copy(name = "Diana")
+    println(customer6)
+
+    val (name, address, age) = customer6
+}

+ 28 - 0
src/com/virtualprogrammers/learning/kotlin/ExploringCasting.kt

@@ -0,0 +1,28 @@
+package com.virtualprogrammers.learning.kotlin
+
+import java.math.BigDecimal
+import java.util.*
+
+fun main() {
+    var result: Any
+
+    val random = Random().nextInt(3)
+
+    if (random == 1) {
+        result = BigDecimal(30)
+    } else {
+        result = "hello";
+    }
+
+    println("result is $result")
+
+    if (result is BigDecimal) {
+        result = result.add(BigDecimal(47));
+    } else {
+        val tempResult = result as String
+        result = tempResult.toUpperCase()
+    }
+
+    println("result is $result");
+
+}

+ 28 - 0
src/com/virtualprogrammers/learning/kotlin/ExploringExceptions.kt

@@ -0,0 +1,28 @@
+package com.virtualprogrammers.learning.kotlin
+
+import java.io.FileInputStream
+
+@Throws(InterruptedException::class)
+fun divide(a: Int, b: Int): Double {
+    Thread.sleep(1000)
+    return (a.toDouble() / b)
+}
+
+fun printFile() {
+    val input = FileInputStream("file.txt")
+
+    input.use {
+        // an exception could be thrown
+    }
+}
+
+fun main() {
+
+    val result = try {
+        divide(5, 23)
+    } catch (e: Exception) {
+        println(e)
+        0
+    }
+    println(result)
+}

+ 29 - 0
src/com/virtualprogrammers/learning/kotlin/ExploringFunctions.kt

@@ -0,0 +1,29 @@
+package com.virtualprogrammers.learning.kotlin
+
+// top level functions. these are static and public
+private fun printAString(value: String) {
+    println(value)
+}
+
+fun main() {
+    printAString("Leon")
+    println(addTwoNumbers(17.3, 9.6))
+    println(addTwoNumbers(two = 5.6))
+    println(printSameMaths(two = 17.3, one = 9.6))
+}
+
+fun addTwoNumbers(one: Double = 6.2, two: Double = 3.9) = one + two
+
+fun printSameMaths(one: Double, two: Double) {
+    println("one + two is ${one + two}")
+    println("one - two is ${one - two}")
+
+    fun functionWithinFunction(a: String) {
+        println(a)
+    }
+    functionWithinFunction("hello")
+}
+
+fun methodTakesALambda(input: String, action: (String) -> Int) {
+    println(action(input))
+}

+ 11 - 0
src/com/virtualprogrammers/learning/kotlin/ExploringNull.kt

@@ -0,0 +1,11 @@
+package com.virtualprogrammers.learning.kotlin
+
+fun main() {
+    var name: String? = null
+
+
+
+    println(name?.toUpperCase())
+
+    var address = null
+}

+ 20 - 0
src/com/virtualprogrammers/learning/kotlin/ExploringReflection.kt

@@ -0,0 +1,20 @@
+package com.virtualprogrammers.learning.kotlin
+
+import kotlin.math.roundToInt
+import kotlin.math.sqrt
+import kotlin.reflect.full.declaredFunctions
+
+fun isPrime(a: Int): Boolean {
+    val maxNumberToCheck = sqrt(a.toDouble()).roundToInt()
+    for (i in 2..maxNumberToCheck) if (a % i == 0) return false
+    return true
+}
+
+fun main() {
+    val myList = listOf(14, 15, 16, 17, 18, 19, 20)
+    val primeNumbers = myList.filter(::isPrime)
+    println(primeNumbers)
+
+    val functions = myList::class.declaredFunctions
+    println(functions)
+}

+ 47 - 0
src/com/virtualprogrammers/learning/kotlin/ExploringTesting.kt

@@ -0,0 +1,47 @@
+package com.virtualprogrammers.learning.kotlin
+
+import org.junit.jupiter.api.Assertions.assertThrows
+import org.junit.jupiter.api.Test
+import java.util.*
+import kotlin.test.assertEquals
+
+class AgeCalculation() {
+    fun getAge(dob: Calendar): Int {
+        val today = Calendar.getInstance()
+        if (dob.timeInMillis > today.timeInMillis) throw IllegalArgumentException()
+        val years = today.get(Calendar.YEAR) - dob.get(Calendar.YEAR)
+        return if (dob.get(Calendar.DAY_OF_YEAR) > today.get(Calendar.DAY_OF_YEAR))
+            years - 1
+        else
+            years
+    }
+}
+
+class AgeCalculationTests() {
+
+    private val ageCalculation = AgeCalculation()
+
+    @Test
+    fun checkAgeWhenBornToday() {
+        assertEquals(0, ageCalculation.getAge(Calendar.getInstance()))
+    }
+
+    @Test
+    fun checkAgeWhenBorn1000DaysAgo() {
+        val date = Calendar.getInstance()
+        date.add(Calendar.DAY_OF_YEAR, -1000)
+        assertEquals(2, ageCalculation.getAge(date))
+    }
+
+    @Test
+    fun testForException() {
+        val date = Calendar.getInstance()
+        date.add(Calendar.DAY_OF_YEAR, 10)
+
+        assertThrows(IllegalArgumentException::class.java) {
+            ageCalculation.getAge(date)
+        }
+
+    }
+
+}

+ 48 - 0
src/com/virtualprogrammers/learning/kotlin/ExploringVariables.kt

@@ -0,0 +1,48 @@
+package com.virtualprogrammers.learning.kotlin
+
+import java.math.BigDecimal
+import kotlin.math.roundToInt
+
+fun main(args: Array<String>) {
+
+    val myDouble = 21.4
+
+    println("is my double a double? ${myDouble is Double}")
+    println("my double is a ${myDouble::class.qualifiedName}")
+
+    println("my double java class is ${myDouble.javaClass}")
+
+    val myInteger = myDouble.roundToInt()
+    println("my myInteger is a ${myInteger::class.qualifiedName}")
+
+    val anotherInteger: Int = 17
+
+    val myFloat: Float = 13.6f
+    val result = myFloat + anotherInteger
+    println(result)
+
+    val bd = BigDecimal(17)
+    val bd2: BigDecimal
+    println("hello")
+    bd2 = bd.add(BigDecimal(30))
+
+
+    var name = "Leon"
+    val surname = "Versteeg"
+
+    name = "Leon 2"
+
+    println("Hello $name ${surname.toUpperCase()}")
+    println("amount \$name chars is ${surname.length}")
+    println("bla is \$about10")
+
+    val store = """
+        it was a dark story night.
+        and bla bla
+    """.trimIndent()
+    println(store)
+
+    // void == Unit
+    val myUnit: Unit
+
+}