Challenge: Calculating Total Cost with Discounts and Tax
Uppgift
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 theProduct
class). - Apply
reduce()
to sum up all product prices. - Use a ternary operator to check if the
subtotal
exceeds the discount threshold (DISCOUNT_THRESHOLD
) and calculate thediscount
. Ifsubtotal
is greater than the threshold, apply a 10% discount (DISCOUNT_PERCENTAGE
); otherwise, set the discount to 0.0. - Determine the discount amount by multiplying
subtotal
byDISCOUNT_PERCENTAGE
. - Calculate the pre-tax amount (
preTaxAmount
) by subtractingdiscount
fromsubtotal
. - Compute the sales tax (
tax
) by multiplyingpreTaxAmount
by the sales tax rate (SALES_TAX
). - Return the final total, calculated as
preTaxAmount + tax
.
Lösning
solution.java
Var allt tydligt?
Tack för dina kommentarer!
Avsnitt 3. Kapitel 7
single
Fråga AI
Fråga AI
Fråga vad du vill eller prova någon av de föreslagna frågorna för att starta vårt samtal
Awesome!
Completion rate improved to 2.33Awesome!
Completion rate improved to 2.33
Challenge: Calculating Total Cost with Discounts and Tax
Uppgift
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 theProduct
class). - Apply
reduce()
to sum up all product prices. - Use a ternary operator to check if the
subtotal
exceeds the discount threshold (DISCOUNT_THRESHOLD
) and calculate thediscount
. Ifsubtotal
is greater than the threshold, apply a 10% discount (DISCOUNT_PERCENTAGE
); otherwise, set the discount to 0.0. - Determine the discount amount by multiplying
subtotal
byDISCOUNT_PERCENTAGE
. - Calculate the pre-tax amount (
preTaxAmount
) by subtractingdiscount
fromsubtotal
. - Compute the sales tax (
tax
) by multiplyingpreTaxAmount
by the sales tax rate (SALES_TAX
). - Return the final total, calculated as
preTaxAmount + tax
.
Lösning
solution.java
Var allt tydligt?
Tack för dina kommentarer!
Avsnitt 3. Kapitel 7
single
Awesome!
Completion rate improved to 2.33
Challenge: Calculating Total Cost with Discounts and Tax
Svep för att visa menyn
Uppgift
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 theProduct
class). - Apply
reduce()
to sum up all product prices. - Use a ternary operator to check if the
subtotal
exceeds the discount threshold (DISCOUNT_THRESHOLD
) and calculate thediscount
. Ifsubtotal
is greater than the threshold, apply a 10% discount (DISCOUNT_PERCENTAGE
); otherwise, set the discount to 0.0. - Determine the discount amount by multiplying
subtotal
byDISCOUNT_PERCENTAGE
. - Calculate the pre-tax amount (
preTaxAmount
) by subtractingdiscount
fromsubtotal
. - Compute the sales tax (
tax
) by multiplyingpreTaxAmount
by the sales tax rate (SALES_TAX
). - Return the final total, calculated as
preTaxAmount + tax
.
Lösning
solution.java
Var allt tydligt?
Tack för dina kommentarer!