Skip to content

Instantly share code, notes, and snippets.

@mgryszko
Last active February 7, 2021 20:36
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/ccff403658fe52b467bf093fddc085b6 to your computer and use it in GitHub Desktop.
Save mgryszko/ccff403658fe52b467bf093fddc085b6 to your computer and use it in GitHub Desktop.
private def total(quantity: Int, tierPrice: TierPrice, unitPrice: UnitPrice): BigDecimal = {
// initial value
var total = BigDecimal(0)
// iteration 1 - tier price
val tiers = quantity / tierPrice.quantity
val tierPriceAmount = tiers * tierPrice.price
// accumulator to pass to the iteration 2
val remainingQuantityAfterTierPrice = quantity % tierPrice.quantity
total = total + tierPriceAmount
// iteration 2 - unit price
val unitPriceAmount = remainingQuantityAfterTierPrice * unitPrice.price
// accumulator to return from folding
val remainingQuantityAfterUnitPrice = 0
total = total + unitPriceAmount
// remaining quanity can be ignored, we return only the total part of the accumulator
total
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment