Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Вивчайте Challenge: Calculating Total Cost with Discounts and Tax | Terminal Operations in the Stream API
Stream API
course content

Зміст курсу

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

Завдання

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.

Рішення

java

solution

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 7
toggle bottom row

book
Challenge: Calculating Total Cost with Discounts and Tax

Завдання

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.

Рішення

java

solution

Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
Все було зрозуміло?

Як ми можемо покращити це?

Дякуємо за ваш відгук!

Секція 3. Розділ 7
Switch to desktopПерейдіть на комп'ютер для реальної практикиПродовжуйте з того місця, де ви зупинились, використовуючи один з наведених нижче варіантів
We're sorry to hear that something went wrong. What happened?
some-alt