Skip to content

Instantly share code, notes, and snippets.

@marconvcm
Last active November 16, 2018 23:09
Show Gist options
  • Save marconvcm/74e8737d5a39993352d6f5bdf5993bff to your computer and use it in GitHub Desktop.
Save marconvcm/74e8737d5a39993352d6f5bdf5993bff to your computer and use it in GitHub Desktop.
package curso.android.trinopolo
fun main(args: Array<String>) {
val combs = listOf(
Gasolina(),
GasolinaPodium(),
Disel(),
Nuclear(Gasolina(), Disel())
)
println("Selecione o carro: N / 1")
val formula1 = when(readLine()) {
"1" -> true
else -> false
}
for (combustivel in combs) {
val carro: Carro = if(formula1) {
CarroFormula1(combustivel)
} else {
CarroNormal(combustivel)
}
var count = 0
while(carro.tanque > 0) {
carro.acelerar()
count = count + 1
}
println("""
|Carro ${carro} acelerou $count
|vezes com o combustivel $combustivel""")
}
}
interface Combustivel {
val fatorDeQueima: Float
}
interface Carro {
val combustivel: Combustivel
var tanque: Float
fun acelerar()
}
class Gasolina() : Combustivel {
override val fatorDeQueima: Float = 1.0f
}
class GasolinaPodium() : Combustivel {
override val fatorDeQueima: Float = 0.5f
}
class Disel() : Combustivel {
override val fatorDeQueima: Float = 0.2f
}
class Nuclear(val a: Combustivel, val b: Combustivel) : Combustivel {
override val fatorDeQueima: Float
get() = a.fatorDeQueima + b.fatorDeQueima
}
class CarroFormula1(override val combustivel: Combustivel) : Carro {
private val consumo = 10f
override var tanque: Float = 100f
override fun acelerar() {
val combustivelConsumido = consumo * combustivel.fatorDeQueima
tanque = tanque - combustivelConsumido
}
}
class CarroNormal(override val combustivel: Combustivel): Carro {
private val consumo = 5f
override var tanque: Float = 100f
override fun acelerar() {
val combustivelConsumido = consumo * combustivel.fatorDeQueima
tanque = tanque - combustivelConsumido
}
}
Criar um programa para simular um Carro
interface Carro {
val combustivel: Combustivel
var tanque: Float
fun acelerar()
}
interface Combustivel {
val fatorDeQueima: Float
}
O sistema suporta 3 tipos de combustivel
Gasolina = fatorDeQueima por acelerada = 1
Gasolina Podium = fatorDeQueima por acelerada = 0.5
Diesil = fatorDeQueima por acelerada = 0.2
Nuclear = (Gasolina + Diesil)
O sistema tem 2 tipos de Carro - CarroNormal, CarroFormula1
Todos os carros tem 100 unidades de combustivel no tanque
CarroFormula1 consome 10 unidades X fatorDeQueima
CarroNormal consome 5 unidades X fatorDeQueima
Voce deve mostrar quantas acelaradas o carro pode dar antes
do combustivel acabar
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment