Skip to content

Instantly share code, notes, and snippets.

@Chrispassold
Created July 4, 2021 17:05
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
Save Chrispassold/462e6015883cd20bfd379c6b0629aab0 to your computer and use it in GitHub Desktop.
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