Home > Net >  how can i compare a var in class enum with a var in fun main for make a when statements
how can i compare a var in class enum with a var in fun main for make a when statements

Time:09-22

I am trying make a menu using enum in kotlin, but i don't know how reference for compare the value of getOption in ENUM with var option in main fun. How can i refence getOption in my fun main for compare?

fun main() {

     val clienteFundo = fundo()
     val clienteDados = cliente()
     var option: Int = 1;

     when(option){
          option = Menu.SALDO.getOption -> 
          println("test");
     }
}



enum class Menu(val optionName: String, val getOption: Int){
    SALDO("Saldo", 1),
    SACAR("Sacar", 2),
    DEPOSITAR("Depositar", 3),
    EXTRATO("Extrato", 4),
    SAIR("Sair", 5)
}



enum class Menu: GetMenu{
    SALDO{
         override fun getMenu() = 1
     },
    SACAR{
        override fun getMenu() = 2
    },
    DEPOSITAR{
         override fun getMenu() = 3
    },
    EXTRATO{
        override fun getMenu() = 4
    },
    SAIR{
        override fun getMenu() = 5
    }

}

CodePudding user response:

The following should work:

fun main() {

     val clienteFundo = fundo()
     val clienteDados = cliente()
     var option: Int = 1;

     when(option){
          Menu.SALDO.getOption -> println("test")
     }
}
  • Related