我正在编写一个出售电影票的程序。用户购买一张票,其中包含电影名称、放映地点和放映开始时间的数据。售出的门票被输入到存档数组中。如果您愿意,您可以显示所有已售出的电影门票。
但在最终数组中仅显示类对象的最后一个数据。为什么?
fun main() {
val film = Film(
"Матрица",
8,
15,
arrayOf("10", "10.0", "10.00", "10-00", "10 00", "12.10", "12-10", "12 10", "19.20", "19-20", "19 20")
)
val tickets = Tickets(0, 0, "")
loop@ while (true) {
do {
println("Выберите ряд (от 1 до ${film.placeRow}):")
tickets.userRow = readln().toInt()
} while (tickets.userRow !in 1..film.placeRow)
do {
println("Выберите место (от 1 до ${film.placeColumn}):")
tickets.userColumn = readln().toInt()
/* if (film.soldTicket.contains(tickets)) {
println("Место занято, выберите ряд и место заново")
continue@loop
}*/
} while (tickets.userColumn !in 1..film.placeColumn)
do {
println("Выберите время 10.00, 12.10, 19.20")
tickets.userTime = readln()
} while (!film.time.contains(tickets.userTime))
film.soldTicket += tickets
println("Купить еще билет? \n 1. Да \n 2. Нет")
if (readln() == "2") break
}
println(film.soldTicket.contentToString())
}
//class Film
class Film(val name:String, var placeRow: Int, var placeColumn: Int, var time: Array<String>) {
var soldTicket: Array<Tickets> = arrayOf()
}
//class Tickets
data class Tickets(var userRow: Int, var userColumn: Int, var userTime: String)
结果:[票证(userRow=1,userColumn=1,userTime=10),票证(userRow=1,userColumn=1,userTime=10)]