vendredi 22 décembre 2017

Java design pattern - How to apply offers on product and shopping cart

I am working on a shopping cart application which will output price of cart. I have 3 classes for this Cart, Purchase, Product

public class Product {

    private int id;
    private String name;
    private double price;
    Offer offer;
// getter setter

    public double getPrice(int quantity) {
        return offer.getPrice....
    }

}

public class Purchase {

    Product product;
    int quantity;
// getter setter

    public double getPrice() {
        return product.getPrice(quantity);
    }

}

public class Cart {

    List<Purchase> purchase = new ArrayList<Purchase>();
    Offer offer;
    Integer loyalityPoints;
//getter setter

    public double getTotal(){
        double total = 0;
        for (Purchase purchase : purchase) {
            total += purchase.getPrice();
        }

        double finalPrice = offer.getPrice(total,....;
        return finalPrice;
    }


}

As shown above individual product can have offer and cart can also have offer.

Initially I thought of having offer factory. OfferPrice can be abstract class & its child could be buyonegetoneprice, buytwogetoneprice, 50precentoffprice but then input for buyonegetoneprice will be qunatity and price and input for 50precentoffprice is only price.

This means 2 different method but implementor of OfferPrice is concerned with only one implementation.

Also how could offer on cart look like? offer on cart can be based on customer loyalityPoints or 50percentoff or something else.

How to design these offers for cart and individual product in a way that could be extensible?

Aucun commentaire:

Enregistrer un commentaire