Skip to content

Instantly share code, notes, and snippets.

@pablitar
Created March 21, 2018 15:34
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pablitar/bef27d027be8158cc40bfe4b8c424af9 to your computer and use it in GitHub Desktop.
Save pablitar/bef27d027be8158cc40bfe4b8c424af9 to your computer and use it in GitHub Desktop.
package holascala
object ConsolaDeCafe {
abstract class ConPrecio {
def precio: Double
}
class Cafe extends ConPrecio {
def precio: Double = 10
}
trait Leche extends ConPrecio {
abstract override def precio: Double = super.precio + cantidadLeche * 20
def porcentajeLeche: Double
def cantidadLeche: Double = porcentajeLeche * tamañoRecipiente
def tamañoRecipiente: Double
}
trait Chocolate extends ConPrecio {
abstract override def precio: Double = super.precio + 5
}
trait Tamaño extends ConPrecio {
abstract override def precio: Double = super.precio * multiplicador
def multiplicador: Double
def tamañoRecipiente: Double
}
trait Chico extends Tamaño {
val multiplicador = 0.75
val tamañoRecipiente: Double = 0.150
}
trait Mediano extends Tamaño {
val multiplicador = 1.00
val tamañoRecipiente: Double = 0.200
}
trait Descuento extends ConPrecio {
abstract override def precio: Double = aplicarDescuento(super.precio)
def descuento: Double
def aplicarDescuento(valor: Double) = valor * (1 - descuento)
}
trait DescuentoEstudiante extends Descuento {
val descuento = 0.1
}
trait HappyHour extends Descuento {
val descuento = 0.2
}
def promo1 =
new Cafe with Leche with Chico with DescuentoEstudiante {
val porcentajeLeche = 0.1
}
def promo2 =
new Cafe with Leche with Mediano {
val porcentajeLeche = 0.9
}
def promo3 =
new Cafe with Leche with Chocolate with Mediano with DescuentoEstudiante {
val porcentajeLeche = 0.44
}
}
object ConsolaApp extends App {
println(ConsolaDeCafe.promo3.precio)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment