Challenge: Calculating Total Cost with Discounts and Tax
Tâche
Swipe to start coding
Implement a shopping cart system that calculates the total order cost, applying a discount and sales tax based on predefined conditions.
- Use
map()to get the total price of each product (there is a dedicated methodgetTotalPrice()in theProductclass). - Apply
reduce()to sum up all product prices. - Use a ternary operator to check if the
subtotalexceeds the discount threshold (DISCOUNT_THRESHOLD) and calculate thediscount. Ifsubtotalis greater than the threshold, apply a 10% discount (DISCOUNT_PERCENTAGE); otherwise, set the discount to 0.0. - Determine the discount amount by multiplying
subtotalbyDISCOUNT_PERCENTAGE. - Calculate the pre-tax amount (
preTaxAmount) by subtractingdiscountfromsubtotal. - Compute the sales tax (
tax) by multiplyingpreTaxAmountby the sales tax rate (SALES_TAX). - Return the final total, calculated as
preTaxAmount + tax.
Solution
solution.java
Tout était clair ?
Merci pour vos commentaires !
Section 3. Chapitre 7
single
Demandez à l'IA
Demandez à l'IA
Posez n'importe quelle question ou essayez l'une des questions suggérées pour commencer notre discussion
Awesome!
Completion rate improved to 2.33
Challenge: Calculating Total Cost with Discounts and Tax
Glissez pour afficher le menu
Tâche
Swipe to start coding
Implement a shopping cart system that calculates the total order cost, applying a discount and sales tax based on predefined conditions.
- Use
map()to get the total price of each product (there is a dedicated methodgetTotalPrice()in theProductclass). - Apply
reduce()to sum up all product prices. - Use a ternary operator to check if the
subtotalexceeds the discount threshold (DISCOUNT_THRESHOLD) and calculate thediscount. Ifsubtotalis greater than the threshold, apply a 10% discount (DISCOUNT_PERCENTAGE); otherwise, set the discount to 0.0. - Determine the discount amount by multiplying
subtotalbyDISCOUNT_PERCENTAGE. - Calculate the pre-tax amount (
preTaxAmount) by subtractingdiscountfromsubtotal. - Compute the sales tax (
tax) by multiplyingpreTaxAmountby the sales tax rate (SALES_TAX). - Return the final total, calculated as
preTaxAmount + tax.
Solution
solution.java
Tout était clair ?
Merci pour vos commentaires !
Section 3. Chapitre 7
single