Last active
November 7, 2019 20:48
-
-
Save JonasWanke/cca53173806e118e18284cbb26e455dc to your computer and use it in GitHub Desktop.
MobileDev: Kotlin-Workshop — student after
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
fun main() { | |
val john = Student(age = 18, name = "John Doe") | |
john.age = john.age + 1 | |
println("${john.name}, ${john.age}, ${john.age.ageGroup}") | |
val card = StudentCard(123, john) | |
println("$john, $card") | |
val students = List(5) { index -> Student("Student", index) } | |
.map { StudentCard(it.age, it) } | |
.filterIndexed { index, _ -> index % 2 == 0 } | |
.drop(1) | |
println(students) | |
} | |
/** | |
* _This_ `class` represents a __single__ [Student]. | |
*/ | |
class Student( | |
val name: String, | |
age: Int = 19 | |
) { | |
var age: Int = age | |
set(value) { | |
require(value > age) { "age must increase" } | |
field = value | |
} | |
} | |
data class StudentCard( | |
val id: Int, | |
val student: Student | |
) | |
val Int.ageGroup: String | |
get() = when (this) { | |
in 0 until 13 -> "Child" | |
13 -> "Sad life" | |
in 14 until 20 -> "Teenager" | |
else -> "Adult" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment