Created
March 21, 2018 15:34
-
-
Save pablitar/bef27d027be8158cc40bfe4b8c424af9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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