jeudi 20 août 2015

OOP Design on shopping cart and discounts application

My application needs to have the ability to place different types of discounts with different types of rules to a shopping cart.

I have finished writing the application, but I know there are major flaws in the design I have come up with, from my understanding the Decorator Pattern would solve most of those flaws, but I'm having some trouble on applying it.

Here is in short the code I believe to be flawed

public interface IDiscount
{
    decimal Value { get; }
    Cart Cart { get; set; }

    void ApplyDiscount();
}

public class SimpleDiscount : IDiscount
{

    public void ApplyDiscount ()
    {
        Cart.DiscountAmount += Value;
        Cart.Discounts.Add (this);
    }

}
public class ItemDiscountDiscount : IDiscount
{

    public void ApplyDiscount ()
    {
        if(Cart.HasItem(this.Item)){
            Cart.DiscountAmount += Value;
            Cart.Discounts.Add (this);
        }
    }

}


public class Cart
{   
    internal decimal TotalValue() {
        return this.CartLines.Sum ((cl) => (cl.Price));
    }

    internal decimal FinalValue(){
        return TotalValue - DiscountAmount;
    }

    internal void AddDiscount(IDicount discount){
        discount.Cart = this;
        discount.ApplyDiscount ();
    }

}

Aucun commentaire:

Enregistrer un commentaire