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

book
Challenge: Calculating Total Cost with Discounts and Tax

Tarefa

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.

Solução

java

solution

package com.example;

import java.util.Arrays;
import java.util.List;

public class Main {
public static void main(String[] args) {
ShoppingCart cart = new ShoppingCart(Arrays.asList(
new Product("Laptop", 1200.0, 1),
new Product("Wireless Headphones", 150.0, 2),
new Product("Smartphone", 800.0, 1),
new Product("Charging Cable", 25.0, 3),
new Product("Tablet", 600.0, 1),
new Product("Mechanical Keyboard", 200.0, 1),
new Product("Gaming Mouse", 100.0, 1)
));

System.out.println("Subtotal: $" + cart.calculateSubtotal());
System.out.println("Total (after tax & discount): $" + cart.calculateTotal());
}
}

class ShoppingCart {
private List<Product> products;
private static final double SALES_TAX = 0.08; // 8% sales tax
private static final double DISCOUNT_THRESHOLD = 1000.0;
private static final double DISCOUNT_PERCENTAGE = 0.10; // 10% discount

public ShoppingCart(List<Product> products) {
this.products = products;
}

public double calculateSubtotal() {
return products.stream()
.map(Product::getTotalPrice)
.reduce(0.0, Double::sum);
Tudo estava claro?

Como podemos melhorá-lo?

Obrigado pelo seu feedback!

Seção 3. Capítulo 7
package com.example;

import java.util.Arrays;
import java.util.List;

public class Main {
public static void main(String[] args) {
ShoppingCart cart = new ShoppingCart(Arrays.asList(
new Product("Laptop", 1200.0, 1),
new Product("Wireless Headphones", 150.0, 2),
new Product("Smartphone", 800.0, 1),
new Product("Charging Cable", 25.0, 3),
new Product("Tablet", 600.0, 1),
new Product("Mechanical Keyboard", 200.0, 1),
new Product("Gaming Mouse", 100.0, 1)
));

System.out.println("Subtotal: $" + cart.calculateSubtotal());
System.out.println("Total (after tax & discount): $" + cart.calculateTotal());
}
}

class ShoppingCart {
private List<Product> products;
private static final double SALES_TAX = 0.08; // 8% sales tax
private static final double DISCOUNT_THRESHOLD = 1000.0;
private static final double DISCOUNT_PERCENTAGE = 0.10; // 10% discount

public ShoppingCart(List<Product> products) {
this.products = products;
}

public double calculateSubtotal() {
return products.stream()
.map(___)
.reduce(___, ___);
}

public double calculateTotal() {
double subtotal = calculateSubtotal();
double discount = (subtotal > ___) ? subtotal * ___ : 0.0;
double preTaxAmount = ___;
double tax = ___
return ___;
}
}

class Product {
private String name;
private double price;
private int quantity;

public Product(String name, double price, int quantity) {
this.name = name;
this.price = price;
this.quantity = quantity;
}

public double getTotalPrice() {
toggle bottom row
some-alt