Skip to content

Instantly share code, notes, and snippets.

@mgryszko
Created February 7, 2021 20:46
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/c6e42da457555b2493e04ea9d2ca921f to your computer and use it in GitHub Desktop.
Save mgryszko/c6e42da457555b2493e04ea9d2ca921f to your computer and use it in GitHub Desktop.
type Quantity = Int
type Amount = BigDecimal
private def total(quantity: Quantity, tierPrice: TierPrice, unitPrice: UnitPrice): Amount = {
val priceByTier: Quantity => (Quantity, Amount) = { quantity =>
val tiers = quantity / tierPrice.quantity
val tierPriceAmount = tiers * tierPrice.price
val remainingQuantityAfterTierPrice = quantity % tierPrice.quantity
(remainingQuantityAfterTierPrice, tierPriceAmount)
}
val priceByUnit: Quantity => (Quantity, Amount) = { quantity =>
val unitPriceAmount = quantity * unitPrice.price
val remainingQuantityAfterUnitPrice = 0
(remainingQuantityAfterUnitPrice, unitPriceAmount)
}
var total = BigDecimal(0)
val (remainingQuantityAfterTierPrice, tierPriceAmount) = priceByTier(quantity)
total = total + tierPriceAmount
val (remainingQuantityAfterUnitPrice, unitPriceAmount) = priceByUnit(remainingQuantityAfterTierPrice)
total + unitPriceAmount
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment