Ejemplo n.º 1
0
        public override IEnumerable <Discount> Process(CartContext cart)
        {
            List <Product> products = cart.PurchasedItems.Where(p => !p.AbandonDiscount.Contains(token))
                                      .ToList();
            List <Product> drinks = products.Where(p => p.Tags.Contains(drinkTag))
                                    .OrderByDescending(p => p.Price)
                                    .ToList();
            List <Product> foods = products.Where(p => p.Tags.Contains(foodTag))
                                   .OrderByDescending(p => p.Price)
                                   .ToList();
            int i = 0;

            while (i < foods.Count() && i < drinks.Count())
            {
                yield return(new Discount()
                {
                    Amount = foods[i].Price + drinks[i].Price - price,
                    Products = new Product[] { foods[i], drinks[i] },
                    Rule = this
                });

                foreach (string t in abandonDiscount)
                {
                    foods[i].AbandonDiscount.Add(t);
                    drinks[i].AbandonDiscount.Add(t);
                }

                i++;
            }
        }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            CartContext cart = new CartContext();
            POS         pos  = new POS();

            cart.PurchasedItems.AddRange(LoadProducts(@".\products4.json"));
            pos.ActivedRules.AddRange(LoadRules());

            pos.CheckoutProcess(cart);

            Console.WriteLine($"購買商品:");
            Console.WriteLine($"---------------------------------------------------");
            foreach (var p in cart.PurchasedItems)
            {
                Console.WriteLine($"- {p.Id,02}, [{p.SKU}] {p.Price,8:C}, {p.Name} {p.TagsValue}");
            }
            Console.WriteLine();

            Console.WriteLine($"折扣:");
            Console.WriteLine($"---------------------------------------------------");
            foreach (var d in cart.AppliedDiscounts)
            {
                Console.WriteLine($"- 折抵 {d.Amount,8:C}, {d.Rule.Name} ({d.Rule.Note})");
                foreach (var p in d.Products)
                {
                    Console.WriteLine($"  * 符合: {p.Id, 02}, [{p.SKU}], {p.Name} {p.TagsValue}");
                }
                Console.WriteLine();
            }
            Console.WriteLine();

            Console.WriteLine($"---------------------------------------------------");
            Console.WriteLine($"結帳金額:   {cart.TotalPrice:C}");
        }
Ejemplo n.º 3
0
     public override IEnumerable <Discount> Process(CartContext cart)
     {
         if (cart.TotalPrice > this.MinDiscountPrice)
         {
             yield return new Discount()
                    {
                        Amount   = this.DiscountAmount,
                        Rule     = this,
                        Products = cart.PurchasedItems.ToArray()
                    }
         }
         ;
     }
 }
Ejemplo n.º 4
0
        public bool CheckoutProcess(CartContext cart)
        {
            // reset cart
            cart.AppliedDiscounts.Clear();

            cart.TotalPrice = cart.PurchasedItems.Select(p => p.Price).Sum();

            foreach (var rule in this.ActivedRules)
            {
                var discounts = rule.Process(cart);
                cart.AppliedDiscounts.AddRange(discounts);
                cart.TotalPrice -= discounts.Select(d => d.Amount).Sum();
            }

            return(true);
        }
Ejemplo n.º 5
0
        public override IEnumerable <Discount> Process(CartContext cart)
        {
            List <Product> matched = new List <Product>();

            foreach (var p in cart.PurchasedItems.Where(p => p.Tags.Contains(this.TargetTag)).OrderByDescending(p => p.Price))
            {
                matched.Add(p);
                if (matched.Count == 2)
                {
                    yield return(new Discount()
                    {
                        Amount = matched.Sum(p => p.Price) * this.PercentOff / 100,
                        Products = matched.ToArray(),
                        Rule = this
                    });

                    matched.Clear();
                }
            }
        }
Ejemplo n.º 6
0
        public override IEnumerable <Discount> Process(CartContext cart)
        {
            List <Product> matched = new List <Product>();

            foreach (var p in cart.PurchasedItems.Where(p => p.Tags.Contains(this.TargetTag)))
            {
                matched.Add(p);

                if (matched.Count == this.MinCount)
                {
                    yield return(new Discount()
                    {
                        Amount = this.DiscountAmount,
                        Products = matched.ToArray(),
                        Rule = this
                    });

                    matched.Clear();
                }
            }
        }
Ejemplo n.º 7
0
        public override IEnumerable <Discount> Process(CartContext cart)
        {
            List <Product> matched_products = new List <Product>();

            foreach (var p in cart.PurchasedItems)
            {
                matched_products.Add(p);

                if (matched_products.Count == this.BoxCount)
                {
                    // 符合折扣
                    yield return(new Discount()
                    {
                        Amount = matched_products.Select(p => p.Price).Sum() * this.PercentOff / 100,
                        Products = matched_products.ToArray(),
                        Rule = this,
                    });

                    matched_products.Clear();
                }
            }
        }
Ejemplo n.º 8
0
 public abstract IEnumerable <Discount> Process(CartContext cart);