Issue
I have a data set like this.
name: tea, pricePerUnit: 5.3, quantity: 2, category: drinks
name: coffee, pricePerUnit: 3.5, quantity: 2, category: drinks
name: cheese, pricePerUnit: 8, quantity: 2, category: food
Based on category and the priceUnit I have to give some discount like
If the client buys more than 3 items of the product within the same category, we are giving him 10% discount for all product in this category.
So, I have implemented this method but It has a bug.
public BigDecimal calculateTotalWithDiscountAndCategory(ShoppingCart shoppingCart){
        return  shoppingCart.getProducts()
                .stream()
                .collect(Collectors.groupingBy(Product::getCategory))
                .entrySet()
                .stream()
                .map( v -> v.getValue()
                        .stream()
                        .map(product ->{
                            if(product.getQuantity() > 3){
                                BigDecimal currentTotal = product.getPricePerUnit().multiply(BigDecimal.valueOf(product.getQuantity()));
                                return currentTotal.multiply(BigDecimal.valueOf(0.9));
                            }else {
                                return product.getPricePerUnit().multiply(BigDecimal.valueOf(product.getQuantity()));
                            }
                                }
                        )
                        .reduce(BigDecimal::add)
                        .orElse(BigDecimal.ZERO)
                ).reduce(BigDecimal::add)
                .orElse(BigDecimal.ZERO);
    }
If I executed this, Every time no discount calculating because it is taking care of one object which quantity < 2.
But correct one would be, As there are 4 drinks discounts should apply to both drinks.
How can I modify this code?
Solution
You need to aggregate the quantity before applying the discount. Here's one way to do that:
public BigDecimal calculateTotalWithDiscountAndCategory(ShoppingCart shoppingCart){
    Map<Category, List<Product>> products = shoppingCart.getProducts()
            .stream()
            .collect(Collectors.groupingBy(Product::getCategory));
    
    return products.values()
            .stream()
            .map(this::categoryTotal)
            .reduce(BigDecimal.ZERO, BigDecimal::add);
}
private BigDecimal categoryTotal(List<Product> products) {
    BigDecimal sum = products.stream()
            .map(Product::getPricePerUnit)
            .reduce(BigDecimal.ZERO, BigDecimal::add);
    int quantity = products.stream()
            .mapToInt(Product::getQuantity)
            .sum();
    if (quantity > 3) {
        sum = sum.multiply(new BigDecimal("0.9"));
    }
    return sum;
}
A more efficient implementation might aggregate the quantity and price in the initial pass so you don't have to iterate each category twice, but for that you'll need a mutable container or a clunky merge function. I'll leave that as an exercise to the reader.
Answered By - shmosel Answer Checked By - Senaida (PHPFixing Volunteer)
 
 Posts
Posts
 
 
0 Comments:
Post a Comment
Note: Only a member of this blog may post a comment.