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

            cart.PurchasedItems.AddRange(LoadProducts(@"..\..\..\products5.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.º 2
0
        private static void PrintReceipt(CartContext 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)
        {
            foreach (var purchasedItem in cart.PurchasedItems.OrderByDescending(z => z.Price))
            {
                var matchOffer = _specialOffer.FirstOrDefault(m => m.Tags.Any(tag => purchasedItem.Tags.Contains(tag)));

                foreach (var tag in purchasedItem.Tags)
                {
                    if (matchOffer != null && matchOffer.ProductQueue.TryGetValue(tag, out var queue))
                    {
                        queue.Enqueue(purchasedItem);

                        if (matchOffer.ProductQueue.All(z => z.Value.Count > 0))
                        {
                            var products = matchOffer.ProductQueue.Select(x => x.Value.Dequeue()).ToList();
                            yield return(new Discount()
                            {
                                Amount = products.Sum(x => x.Price) - matchOffer.Amount,
                                Products = products.ToArray(),
                                Rule = this
                            });
                        }
                    }
                }
            }
        }
Ejemplo n.º 4
0
        public override IEnumerable <Discount> Process(CartContext cart)
        {
            List <Product> purchased_items = new List <Product>(cart.GetVisiblePurchasedItems(this.ExclusiveTag));

            foreach (var d in this._discount_table)
            {
                var drinks = purchased_items.Where(p => p.Tags.Contains(d.drink)).OrderByDescending(p => p.Price).ToArray();
                var foods  = purchased_items.Where(p => p.Tags.Contains(d.food)).OrderByDescending(p => p.Price).ToArray();

                if (drinks.Count() == 0)
                {
                    continue;
                }
                if (foods.Count() == 0)
                {
                    continue;
                }

                for (int i = 0; true; i++)
                {
                    if (drinks.Length <= i)
                    {
                        break;
                    }
                    if (foods.Length <= i)
                    {
                        break;
                    }

                    if (purchased_items.Contains(drinks[i]) == false)
                    {
                        break;
                    }
                    if (purchased_items.Contains(foods[i]) == false)
                    {
                        break;
                    }


                    purchased_items.Remove(drinks[i]);
                    purchased_items.Remove(foods[i]);
                    yield return(new Discount()
                    {
                        Rule = this,
                        Products = new Product[] { drinks[i], foods[i] },
                        Amount = drinks[i].Price + foods[i].Price - d.price
                    });
                }
            }
        }
Ejemplo n.º 5
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.º 6
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.º 7
0
        public override IEnumerable <Discount> Process(CartContext cart)
        {
            List <Product> matched = new List <Product>();

            foreach (var p in cart.GetVisiblePurchasedItems(this.ExclusiveTag).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.º 8
0
        private static void Main(string[] args)
        {
            // 購物車 Context。
            var cart = new CartContext();
            // POS。
            var pos = new Pos();

            // 購物車加入購買商品。
            cart.PurchasedItems.AddRange(LoadProducts());
            // POS 加入啟用的折扣規則。
            pos.ActivatedRules.AddRange(LoadRules());

            // 對購物車 Context 進行結帳。
            pos.CheckoutProcess(cart);

            // 列印收據。
            PrintReceipt(cart);

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

            foreach (var p in cart.GetVisiblePurchasedItems(this.ExclusiveTag).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.º 10
0
        public override IEnumerable <Discount> Process(CartContext cart)
        {
            List <Product> matched_products = new List <Product>();

            foreach (var p in cart.GetVisiblePurchasedItems(this.ExclusiveTag))
            {
                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.º 11
0
        public override IEnumerable <Discount> Process(CartContext cart)
        {
            List <Product> matched = new List <Product>();

            foreach (var sku in cart.PurchasedItems.Where(p => p.Tags.Contains(this.TargetTag)).Select(p => p.SKU).Distinct())
            {
                matched.Clear();
                foreach (var p in cart.PurchasedItems.Where(p => p.SKU == sku))
                {
                    matched.Add(p);
                    if (matched.Count == 2)
                    {
                        yield return(new Discount()
                        {
                            Products = matched.ToArray(),
                            Amount = this.DiscountAmount,
                            Rule = this
                        });

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