Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Apprendre Challenge: Calculating Total Cost with Discounts and Tax | Terminal Operations in the Stream API
Stream API
course content

Contenu du cours

Stream API

Stream API

1. Fundamentals and Functional Capabilities of Stream API
4. Practical Applications of Stream API

book
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.

  1. Use map() to get the total price of each product (there is a dedicated method getTotalPrice() in the Product class).
  2. Apply reduce() to sum up all product prices.
  3. Use a ternary operator to check if the subtotal exceeds the discount threshold (DISCOUNT_THRESHOLD) and calculate the discount. If subtotal is greater than the threshold, apply a 10% discount (DISCOUNT_PERCENTAGE); otherwise, set the discount to 0.0.
  4. Determine the discount amount by multiplying subtotal by DISCOUNT_PERCENTAGE.
  5. Calculate the pre-tax amount (preTaxAmount) by subtracting discount from subtotal.
  6. Compute the sales tax (tax) by multiplying preTaxAmount by the sales tax rate (SALES_TAX).
  7. Return the final total, calculated as preTaxAmount + tax.

Solution

java

solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 7
toggle bottom row

book
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.

  1. Use map() to get the total price of each product (there is a dedicated method getTotalPrice() in the Product class).
  2. Apply reduce() to sum up all product prices.
  3. Use a ternary operator to check if the subtotal exceeds the discount threshold (DISCOUNT_THRESHOLD) and calculate the discount. If subtotal is greater than the threshold, apply a 10% discount (DISCOUNT_PERCENTAGE); otherwise, set the discount to 0.0.
  4. Determine the discount amount by multiplying subtotal by DISCOUNT_PERCENTAGE.
  5. Calculate the pre-tax amount (preTaxAmount) by subtracting discount from subtotal.
  6. Compute the sales tax (tax) by multiplying preTaxAmount by the sales tax rate (SALES_TAX).
  7. Return the final total, calculated as preTaxAmount + tax.

Solution

java

solution

Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
Tout était clair ?

Comment pouvons-nous l'améliorer ?

Merci pour vos commentaires !

Section 3. Chapitre 7
Switch to desktopPassez à un bureau pour une pratique réelleContinuez d'où vous êtes en utilisant l'une des options ci-dessous
We're sorry to hear that something went wrong. What happened?
some-alt