In: Computer Science
explain line by line what it does
Description: This function applies discount percentage on an item
price unless the item price is less than the whole sale
price. For example, a product at $10 with a wholesale price of $5 and a discount
of 10% returns $9.
function applyDiscount(productPrice : Double,
wholesalePrice : Double,
discount : Double)
Return Double
Create variable discountedPrice as double
discountedPrice = productPrice * (1–discount)
if (discountedPrice < wholesalePrice)
discountedPrice = wholesalePrice
end if
return discountedPrice
End function
// function that applies discount on the productPrice by the
given discount percent and returns the price after discount
function applyDiscount(productPrice : Double, wholesalePrice :
Double, discount : Double)
Return Double // specifying the return type
Create variable discountedPrice as double // creates a variable discountedPrice of double type
// calculates discountedPrice where productPrice is the price of
product
// and discount is the discount percent expressed as double (i.e if
discount percent is 10 then discount = 0.1)
discountedPrice = productPrice * (1–discount)
// after calculating the discountedPrice check if
discountedPrice < wholesalePrice , then set discountedPrice to
wholesalePrice else does nothing
if (discountedPrice < wholesalePrice)
discountedPrice = wholesalePrice
end if
return discountedPrice // returns the discountedPrice
End function // end of function applyDiscount
Eg: Using the above given example:
productPrice = 10
wholesalePrice = 5
discount = 0.1 (10%)
discountedPrice = productPrice * (1–discount)
= 10(1-0.1)
= 10 * 0.9
= 9
Since discountedPrice > wholesalePrice (9 > 5) , so discountedPrice will be 9 which will be returned , if suppose discount was 0.6 i.e 60%
discountedPrice = productPrice * (1–discount)
= 10(1-0.6)
= 10 * 0.4
= 4
In this case discountedPrice < wholesalePrice (4 < 5) , so discountedPrice = wholesalePrice = 5 and this will be returned