Last active
December 27, 2021 15:32
-
-
Save arsalankhan994/f575ba806ffc228ee2a48c742b793fe0 to your computer and use it in GitHub Desktop.
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() { | |
/* | |
when -> with Enum value | |
*/ | |
val state = State.values().random() | |
printState(state) | |
/* | |
when -> with Int value | |
*/ | |
val integerList = listOf(1,2,3,4) | |
printIntValue(integerList) | |
/* | |
when -> with String value | |
*/ | |
val stringList = listOf("Erselan","Khan","Android","KotlinVsJava") | |
printStringValue(stringList) | |
} | |
private fun printStringValue(stringList: List<String>) { | |
when (stringList.random()) { | |
stringList[0] -> { | |
println("String value: ${stringList[0]}") | |
} | |
stringList[1] -> { | |
println("String value: ${stringList[1]}") | |
} | |
stringList[2] -> { | |
println("String value: ${stringList[2]}") | |
} | |
stringList[3] -> { | |
println("String value: ${stringList[3]}") | |
} | |
else -> { | |
println("Some Default String") | |
} | |
} | |
} | |
private fun printIntValue(integerList: List<Int>) { | |
when (integerList.random()) { | |
1 -> { | |
println("Int value: ${integerList[0]}") | |
} | |
2 -> { | |
println("Int value: ${integerList[1]}") | |
} | |
3 -> { | |
println("Int value: ${integerList[2]}") | |
} | |
4 -> { | |
println("Int value: ${integerList[3]}") | |
} | |
else -> { | |
println("Some Default Int") | |
} | |
} | |
} | |
private fun printState(state: State) { | |
when (state) { | |
State.NOT_STARTED -> { | |
println("State: $state") | |
} | |
State.STARTED -> { | |
println("State: $state") | |
} | |
State.IN_PROGRESS -> { | |
println("State: $state") | |
} | |
State.COMPLETED -> { | |
println("State: $state") | |
} | |
State.CANCELLED -> { | |
println("State: $state") | |
} | |
else -> { | |
println("No State") | |
} | |
} | |
} | |
enum class State { | |
NOT_STARTED, STARTED, IN_PROGRESS, COMPLETED, CANCELLED, | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment