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)
}
如何从食品列表中获取具体的索引号、名称和价格?
我已经尝试过,但我得到了像这样的完整对象 foodInfo(name=Pizza, Price=50000)