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

Contenido del Curso

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

Tarea

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.

Solución

java

solution

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 7
toggle bottom row

book
Challenge: Calculating Total Cost with Discounts and Tax

Tarea

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.

Solución

java

solution

Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 3. Capítulo 7
Switch to desktopCambia al escritorio para practicar en el mundo realContinúe desde donde se encuentra utilizando una de las siguientes opciones
We're sorry to hear that something went wrong. What happened?
some-alt