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

Kursinhalt

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

Aufgabe

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.

Lösung

java

solution

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 7
toggle bottom row

book
Challenge: Calculating Total Cost with Discounts and Tax

Aufgabe

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.

Lösung

java

solution

Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
War alles klar?

Wie können wir es verbessern?

Danke für Ihr Feedback!

Abschnitt 3. Kapitel 7
Switch to desktopWechseln Sie zum Desktop, um in der realen Welt zu übenFahren Sie dort fort, wo Sie sind, indem Sie eine der folgenden Optionen verwenden
We're sorry to hear that something went wrong. What happened?
some-alt