I need to show different seat arrangement till the user is making choices and register it's previous choices as well in the displayed seat arrangement. My code is:
package cinema
fun main() {
println("Enter the number of rows:")
val row = readLine()!!.toInt()
println("Enter the number of seats in each row")
val seats = readLine()!!.toInt()
var price = 0
val totalSeats = row*seats
var rowNumberUpdate = 0
var seatNumberUpdate = 0
var a = true
fun seatDisplay(){
var newSeatCount = 1
println("Cinema: ")
print(" ")
while(newSeatCount <=seats){
print(" $newSeatCount")
newSeatCount = 1
}
print("\n")
for(i in 1..row){
print(i)
for(j in 1..seats){
if(i == rowNumberUpdate && j==seatNumberUpdate) print(" B") else print(" S")
}
println()
}
}
fun priceDisplay(){
println("Enter a row number: ")
val rowNumber = readln().toInt()
rowNumberUpdate = rowNumber
println("Enter a seat number in that row: ")
val seatNumber = readln().toInt()
seatNumberUpdate = seatNumber
if(totalSeats<=60){
price = 10
} else {
if(row%2==0){
if(rowNumber<=row/2) price = 10 else price = 8
} else {
if(rowNumber<=row/2) price = 10 else price = 8
}
}
println("Ticket price: $$price")
}
while(a){
println("1. Show the seats")
println("2. Buy a ticket")
println("0. Exit")
val optionsInput = readln().toInt()
when(optionsInput){
1 -> seatDisplay()
2 -> priceDisplay()
0 -> a = false
}
}
}
Problem with this code is, everytime user is making choice it is showing the seat arrangement as per the latest choice. It doesn't hold the value of previous choices.
You can see this in output attached as an image. Please zoom the image for clear visibility.
Hoping to get some help from the community.
CodePudding user response:
You store the selection of the user in variables rowNumberUpdate
and seatNumberUpdate
that can hold singular values. If you intend to memorize all choices, you have to change these variables to be some kind of collection (e.g. a list) and add every choice to that collection:
val seatUpdate = mutableListOf<Pair<Int, Int>>()
and in seatDisplay
:
if(seatUpdate.contains(Pair(i,j))) print(" B") else print(" S")
and in priceDisplay
:
seatUpdate.add(Pair(rowNumber, seatNumber))