Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Kotlin: I am unable to hold previous value of a function when calling it multiple times

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 enter image description hereimage. Please zoom the image for clear visibility.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Hoping to get some help from the community.

>Solution :

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))
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading