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)
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 foodSelected(index: Int){
val getIndex = food[index]
println("You pick a number $index $getIndex") // Add the index parameter here
}
}
fun main(){
Foods().displayMenu()
print("Pick Menu: ")
val selectMenu = readln().toInt()
Foods().foodSelected(selectMenu)
}
As far as I could understand your question, this could be a possibility of printing the index of the item
Since Lists use 0-based indexing, index+1
could help you print the exact index. I've just put a hint in the right direction for you