Linked Questions

Popular Questions

How to get spesific index number from listof Kotlin

Asked by At
data class foodInfo(
    val name: String,
    val price: Int
)

class Foods {
    private val food = mutableListOf(
        foodInfo("Pizza", 50000),
        foodInfo("Burger", 40000),
        foodInfo("Fried Rice", 40000),
        foodInfo("Noodle", 15000),
        foodInfo("Salad", 5000)
    )

    fun displayMenu() {
        food.forEachIndexed { index, foodInfo ->
            println("${index + 1}. ${foodInfo.name} =  ${foodInfo.price}/porsi")
        }
    }

    fun foodSelected(index: Int){
       val getIndex = food[index]
       println("You pick a number $getIndex")
    }
}

fun main(){
    Foods().displayMenu()
    print("Pick Menu: ")
    
    val selectMenu = readln().toInt()
    Foods().foodSelected(selectMenu)

}

How to get spesific index number, name and price from food listof ?

I already tried but i got full object like this foodInfo(name=Pizza, price=50000)

Related Questions