PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Friday, November 4, 2022

[FIXED] How to give discounts based on category and quantity in java 8?

 November 04, 2022     java, java-stream, lambda     No comments   

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)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing