Last active
February 7, 2021 20:36
-
-
Save mgryszko/ccff403658fe52b467bf093fddc085b6 to your computer and use it in GitHub Desktop.
This file contains 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
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