Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Calculadora controller
import java.lang.Exception
class CalculadoraController {
fun executarOperacao(text: String): Int {
if(text.contains('*')){
val split = text.split('*')
if(split.size > 2)
throw Exception("Multiplicação inválida")
return multiplicar(split[0].toInt(), split[1].toInt())
}
if(text.contains('+')){
val split = text.split('+')
if(split.size > 2)
throw Exception("Soma inválida")
return soma(split[0].toInt(), split[1].toInt())
}
if(text.contains('-')){
val split = text.split('-')
if(split.size > 2)
throw Exception("Subtração inválida")
return subtrair(split[0].toInt(), split[1].toInt())
}
throw Exception("Operação inválida")
}
private fun soma(n1: Int, n2: Int): Int {
return n1 + n2
}
private fun subtrair(n1: Int, n2: Int): Int {
return n1 - n2
}
private fun multiplicar(n1: Int, n2: Int): Int {
return n1 * n2
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment