Created
February 7, 2021 20:46
-
-
Save mgryszko/c6e42da457555b2493e04ea9d2ca921f 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
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