Skip to content

Instantly share code, notes, and snippets.

@mgryszko
Last active February 10, 2021 15:55
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 mgryszko/176149b1eb3ecc7ef08a313ba00c937d to your computer and use it in GitHub Desktop.
Save mgryszko/176149b1eb3ecc7ef08a313ba00c937d to your computer and use it in GitHub Desktop.
case class Pricing(val quantity: Int, val unitPrice: UnitPrice, val tierPrice: TierPrice)
case class UnitPrice(val price: BigDecimal)
case class TierPrice(val price: BigDecimal, val quantity: Int)
def total(croissants: Pricing, baguettes: Pricing): BigDecimal = {
val croissantTotal = total(croissants)
val baguetteTotal = total(baguettes)
croissantTotal + baguetteTotal
}
private def total(pricing: Pricing): BigDecimal = {
val tiers = pricing.quantity / pricing.tierPrice.quantity
val regularPricedArticles = pricing.quantity % pricing.tierPrice.quantity
tiers * pricing.tierPrice.price + regularPricedArticles * pricing.unitPrice.price
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment