Skip to content

Commit be89df4

Browse files
add when and smartcast
1 parent 28d3473 commit be89df4

File tree

6 files changed

+134
-9
lines changed

6 files changed

+134
-9
lines changed

README.md

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,6 +135,33 @@ class Game(val name: String,
135135

136136
Example: [05-classes.kt](./examples/05-classes.kt)
137137

138+
## Lists
139+
140+
There is some ways to create lists in Kotlin
141+
142+
````kotlin
143+
var digimons = mutableListOf("Agumon", "Tailmon", "Angemon")
144+
var digimons = listOf("Agumon", "Tailmon", "Angemon")
145+
val digimonPower: MutableMap<Int, Int> = mutableMapOf(0 to 50, 1 to 50, 2 to 100)
146+
````
147+
148+
## When
149+
150+
When is similar Java Switch
151+
152+
``` kotlin
153+
fun getPrice(console: Console): Int =
154+
when (console) {
155+
Console.GBA -> 1000
156+
Console.PLAYSTATION -> 5299
157+
Console.SWITCH -> 2500
158+
Console.XBOX -> 4200
159+
Console.PC -> 5000
160+
}
161+
```
162+
163+
Example: [06-when.k](./examples/06-when.kt)
164+
138165
## References
139166

140167
- [Learn the Kotlin programming language](https://developer.android.com/kotlin/learn?gclsrc=aw.ds&gclid=CjwKCAjw9e6SBhB2EiwA5myr9tk-mZhoAytl5-3nJeQ0lgYnyIGcs5GFh9-aN1tDvkwvcrFEAJZdLhoC0lAQAvD_BwE)

examples/04-enums.kts

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,17 @@
1-
enum class Console {
2-
GBA,
3-
PLAYSTATION,
4-
XBOX,
5-
SWITCH,
6-
PC
1+
enum class Console(val year: Int) {
2+
GBA(2003),
3+
PLAYSTATION(2000),
4+
XBOX(2005),
5+
SWITCH(2017),
6+
PC(1946)
77
}
88

9-
class Game(val name: String,
10-
val console: Console) {
9+
class Game(
10+
val name: String,
11+
val console: Console
12+
) {
1113
fun play(): String {
12-
return "You're playing $name on $console"
14+
return "You're playing $name on $console (realeased at ${console.year})"
1315
}
1416
}
1517

examples/06-when.kt

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
enum class Console(val year: Int) {
2+
GBA(2003),
3+
PLAYSTATION(2000),
4+
XBOX(2005),
5+
SWITCH(2017),
6+
PC(1946)
7+
}
8+
9+
fun getPrice(console: Console): Int =
10+
when (console) {
11+
Console.GBA -> 1000
12+
Console.PLAYSTATION -> 5299
13+
Console.SWITCH -> 2500
14+
Console.XBOX -> 4200
15+
Console.PC -> 5000
16+
}
17+
18+
fun haveDiscount(console: Console): String {
19+
return when (console) {
20+
Console.GBA, Console.SWITCH, Console.PC -> "have discount"
21+
Console.PLAYSTATION, Console.XBOX -> "not have discount"
22+
}
23+
}
24+
25+
fun main() {
26+
val price = getPrice(Console.PLAYSTATION)
27+
println("Console price is R$$price and this console ${haveDiscount(Console.PLAYSTATION)}")
28+
29+
val pricePC = getPrice(Console.PC)
30+
println("Console price is R$$pricePC and this console ${haveDiscount(Console.PC)}")
31+
}

examples/07-smartcast.kt

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
interface Expression
2+
class Number(val value: Int) : Expression
3+
class Sum(val left: Expression, val right: Expression) : Expression
4+
5+
/*
6+
fun exam(expression: Expression): Int {
7+
if (expression is Number) {
8+
// There is a smart cast here
9+
return expression.value
10+
}
11+
12+
if (expression is Sum) {
13+
// There is a smart cast here
14+
return exam(expression.left) + exam(expression.right)
15+
}
16+
17+
throw IllegalArgumentException("Expression is invalid")
18+
}*/
19+
20+
fun exam(expression: Expression): Int =
21+
when (expression) {
22+
is Number -> {
23+
print("Expression with number: ${expression.value}")
24+
expression.value // Last expression in block will be returned as a result
25+
}
26+
is Sum -> exam(expression.left) + exam(expression.right)
27+
else -> throw IllegalArgumentException("Expression is invalid")
28+
}
29+
30+
fun main() {
31+
// ((1+2) + 4)
32+
val result = exam(Sum(Sum(Number(1), Number(2)), Number(4)))
33+
println("Result: $result")
34+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package com.jjeanjacques.digimon.controller
2+
3+
import com.jjeanjacques.digimon.controller.dto.DigimonPowerDTO
4+
import org.springframework.web.bind.annotation.GetMapping
5+
import org.springframework.web.bind.annotation.RequestMapping
6+
import org.springframework.web.bind.annotation.RestController
7+
import org.springframework.http.ResponseEntity
8+
9+
@RestController
10+
@RequestMapping("/digimon")
11+
class DigimonController {
12+
13+
@GetMapping
14+
fun listAll(): ResponseEntity<Any> {
15+
val response = ArrayList<DigimonPowerDTO>()
16+
17+
var digimons = listOf("Agumon", "Tailmon", "Angemon")
18+
val digimonPower: MutableMap<Int, Int> = mutableMapOf(0 to 50, 1 to 50, 2 to 100)
19+
20+
var i: Int = 0
21+
digimons.forEach {
22+
val digimonPowerDTO = DigimonPowerDTO(it, digimonPower[i]!!)
23+
response.add(digimonPowerDTO)
24+
i++
25+
}
26+
return ResponseEntity.ok(response)
27+
}
28+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.jjeanjacques.digimon.controller.dto
2+
3+
data class DigimonPowerDTO(val name: String, val power: Int)

0 commit comments

Comments
 (0)