Challenge: Calculating Total Cost with Discounts and Tax
Oppgave
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.
- Use
map()
to get the total price of each product (there is a dedicated methodgetTotalPrice()
in theProduct
class). - Apply
reduce()
to sum up all product prices. - Use a ternary operator to check if the
subtotal
exceeds the discount threshold (DISCOUNT_THRESHOLD
) and calculate thediscount
. Ifsubtotal
is greater than the threshold, apply a 10% discount (DISCOUNT_PERCENTAGE
); otherwise, set the discount to 0.0. - Determine the discount amount by multiplying
subtotal
byDISCOUNT_PERCENTAGE
. - Calculate the pre-tax amount (
preTaxAmount
) by subtractingdiscount
fromsubtotal
. - Compute the sales tax (
tax
) by multiplyingpreTaxAmount
by the sales tax rate (SALES_TAX
). - Return the final total, calculated as
preTaxAmount + tax
.
Løsning
solution
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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);
Alt var klart?
Takk for tilbakemeldingene dine!
Seksjon 3. Kapittel 7
99
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
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(___, ___);
Spør AI
Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår