Skip to content

Instantly share code, notes, and snippets.

@frehn
Last active September 13, 2019 09:17
  • 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 frehn/661f525ca7361359f69c800203939eb1 to your computer and use it in GitHub Desktop.
DUs in Scala
sealed trait Customer
case class RegisteredCustomer(id : String) extends Customer
case class EligibleRegisteredCustomer(id : String) extends Customer
case class Guest(id: String) extends Customer
def calculateTotal(customer: Customer)(spend: Double) = {
val discount = customer match {
case EligibleRegisteredCustomer(_) if spend >= 100.0 => spend * 0.1
case _ => 0.0
}
spend - discount
}
val john = EligibleRegisteredCustomer("John")
val assertJohn = (calculateTotal (john) (100.0)) == 90.0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment