public decimal ComputePrice(PizzaOrder order)
        {
            decimal nonDiscounted   = order.Pizzas.Sum(p => p.Price);
            decimal discountedValue = _discountPolicy(order);

            return(nonDiscounted - discountedValue);
        }
        public static decimal BuyOneGetOneFree(PizzaOrder order)
        {
            var pizzas = order.Pizzas;

            if (pizzas.Count < 2)
            {
                return(0m);
            }
            return(pizzas.Min(p => p.Price));
        }
        public decimal ApplyDiscount(PizzaOrder order)
        {
            var pizzas = order.Pizzas;

            if (pizzas.Count < 2)
            {
                return(0m);
            }
            return(pizzas.Min(p => p.Price));
        }
        public decimal ApplyDiscount(PizzaOrder order)
        {
            var discountPolicies = new List <IDiscountPolicy>()
            {
                new BuyOneGetOneFree(),
                new FivePercentOffMoreThanFiftyDollars(),
                new FiveDollarsOffStuffedCrust()
            };

            var bestDiscount =
                discountPolicies.Max(policy => policy.ApplyDiscount(order));

            return(bestDiscount);
        }
        public decimal ComputePrice(PizzaOrder order)
        {
            decimal total = order.Pizzas.Sum(p => p.Price);

            decimal[] discounts = new[] {
                _discountPolicyMethods.BuyOneGetOneFree(order),
                _discountPolicyMethods.FivePercentOffMoreThanFiftyDollars(order),
                _discountPolicyMethods.FiveDollarsOffStuffedCrust(order),
            };

            decimal bestDiscount = discounts.Max(discount => discount);

            total = total - bestDiscount;
            return(total);
        }
예제 #6
0
        public void Five_dollars_off_stuffed_crust()
        {
            var pizzaOrderingSystem = new PizzaOrderingSystemInterfaces(new BestDiscount());
            var order = new PizzaOrder()
            {
                Pizzas = new List <Pizza>()
                {
                    new Pizza()
                    {
                        Crust = Crust.Stuffed,
                        Price = 10.00m,
                        Size  = Size.Large
                    }
                }
            };
            var price = pizzaOrderingSystem.ComputePrice(order);

            Assert.Equal(price, 5.00m);
        }
예제 #7
0
        public void Get_a_pizza_no_discount()
        {
            var pizzaOrderingSystem = new PizzaOrderingSystemInterfaces(new BestDiscount());
            var order = new PizzaOrder()
            {
                Pizzas = new List <Pizza>()
                {
                    new Pizza()
                    {
                        Crust = Crust.Regular,
                        Price = 10.00m,
                        Size  = Size.Large
                    }
                }
            };
            var price = pizzaOrderingSystem.ComputePrice(order);

            Assert.Equal(price, 10.00m);
        }
        public void Five_dollars_off_stuffed_crust()
        {
            var pizzaOrderingSystem = new PizzaOrderingSystem(DiscountPolicyDelegates.DiscountAllThePizzas());
            var order = new PizzaOrder()
            {
                Pizzas = new List <Pizza>()
                {
                    new Pizza()
                    {
                        Crust = Crust.Stuffed,
                        Price = 10.00m,
                        Size  = Size.Large
                    }
                }
            };
            var price = pizzaOrderingSystem.ComputePrice(order);

            Assert.Equal(price, 5.00m);
        }
        public void Get_a_pizza_no_discount()
        {
            var pizzaOrderingSystem = new PizzaOrderingSystem(DiscountPolicyDelegates.DiscountAllThePizzas());
            var order = new PizzaOrder()
            {
                Pizzas = new List <Pizza>()
                {
                    new Pizza()
                    {
                        Crust = Crust.Regular,
                        Price = 10.00m,
                        Size  = Size.Large
                    }
                }
            };
            var price = pizzaOrderingSystem.ComputePrice(order);

            Assert.Equal(price, 10.00m);
        }
예제 #10
0
        public void Five_percent_off_more_than_50_policy()
        {
            var pizzaOrderingSystem = new PizzaOrderingSystemInterfaces(new BestDiscount());
            var order = new PizzaOrder();

            for (int i = 0; i < 6; i++)
            {
                var pizza = new Pizza()
                {
                    Crust = Crust.Regular,
                    Price = 10.00m,
                    Size  = Size.Large
                };
                order.Pizzas.Add(pizza);
            }
            var price = pizzaOrderingSystem.ComputePrice(order);

            Assert.Equal(6, order.Pizzas.Count);
            Assert.Equal(50.00m, price);
        }
예제 #11
0
        public void Buy_one_get_one_policy()
        {
            var pizzaOrderingSystem = new PizzaOrderingSystemInterfaces(new BestDiscount());
            var order = new PizzaOrder();

            for (int i = 0; i < 2; i++)
            {
                var pizza = new Pizza()
                {
                    Crust = Crust.Regular,
                    Price = 10.00m,
                    Size  = Size.Large
                };
                order.Pizzas.Add(pizza);
            }
            var price = pizzaOrderingSystem.ComputePrice(order);

            Assert.Equal(2, order.Pizzas.Count);
            Assert.Equal(10.00m, price);
        }
        public void Best_discount_for_big_order()
        {
            var pizzaOrderingSystem = new PizzaOrderingSystem(
                DiscountPolicyDelegates.DiscountAllThePizzas());
            var order = new PizzaOrder();

            // Buy one get one
            for (int i = 0; i < 2; i++)
            {
                var pizza = new Pizza()
                {
                    Crust = Crust.Regular,
                    Price = 10.00m,
                    Size  = Size.Large
                };
                order.Pizzas.Add(pizza);
            }
            // Over 50 5% off
            for (int i = 0; i < 6; i++)
            {
                var pizza = new Pizza()
                {
                    Crust = Crust.Regular,
                    Price = 10.00m,
                    Size  = Size.Large
                };
                order.Pizzas.Add(pizza);
            }
            // Stuffed crust
            var stuffedCrust = new Pizza()
            {
                Crust = Crust.Stuffed,
                Price = 10.00m,
                Size  = Size.Large
            };

            order.Pizzas.Add(stuffedCrust);
            var price = pizzaOrderingSystem.ComputePrice(order);

            Assert.Equal(80.00m, price);
        }
 public static decimal FiveDollarsOffStuffedCrust(PizzaOrder order)
 {
     return(order.Pizzas.Sum(p => p.Crust == Crust.Stuffed ? 5m : 0m));
 }
        public static decimal FivePercentOffMoreThanFiftyDollars(PizzaOrder order)
        {
            decimal nonDiscounted = order.Pizzas.Sum(p => p.Price);

            return(nonDiscounted >= 50 ? nonDiscounted * 0.05m : 0M);
        }
 public decimal ApplyDiscount(PizzaOrder order)
 {
     return(order.Pizzas.Sum(p => p.Crust == Crust.Stuffed ? 5m : 0m));
 }
        public decimal ApplyDiscount(PizzaOrder order)
        {
            decimal nonDiscounted = order.Pizzas.Sum(p => p.Price);

            return(nonDiscounted >= 50 ? nonDiscounted * 0.05m : 0M);
        }