コード例 #1
0
        // Apply the promotions on the the sales invoice.
        public float ApplyPromotion(PromotionOffer promotions, Dictionary <string, int> SalesInvoice)
        {
            // Single promotion
            if (promotions.ProductOffer.Count == 1)
            {
                var key = promotions.ProductOffer.ElementAt(0).Key;
                if (SalesInvoice.ContainsKey(key))
                {
                    int quantityInvoice   = SalesInvoice[key];
                    int offerQuantity     = promotions.ProductOffer[key];
                    int offerscount       = quantityInvoice / offerQuantity;
                    int remainingQuantity = quantityInvoice % offerQuantity;
                    SalesInvoice[key] = remainingQuantity;
                    return(offerscount * promotions.OfferPrice);
                }
            }
            else
            {   // Combo offer for 2 products
                var key1 = promotions.ProductOffer.ElementAt(0).Key;
                var key2 = promotions.ProductOffer.ElementAt(1).Key;;
                if (SalesInvoice.ContainsKey(key1) && SalesInvoice.ContainsKey(key2))
                {
                    int quantityInvoice1   = SalesInvoice[key1];
                    int offerQuantity1     = promotions.ProductOffer[key1];
                    int offerscount1       = quantityInvoice1 / offerQuantity1;
                    int remainingQuantity1 = quantityInvoice1 % offerQuantity1;

                    int quantityInvoice2   = SalesInvoice[key2];
                    int offerQuantity2     = promotions.ProductOffer[key2];
                    int offerscount2       = quantityInvoice2 / offerQuantity2;
                    int remainingQuantity2 = quantityInvoice2 % offerQuantity2;

                    if (offerscount1 <= offerscount2) // combo offer with least offer counts.
                    {
                        SalesInvoice[key1] = remainingQuantity1;
                        SalesInvoice[key2] = remainingQuantity2 + (offerQuantity2 * (offerscount2 - offerscount1));
                        return(offerscount1 * promotions.OfferPrice);
                    }
                    else
                    {
                        SalesInvoice[key1] = remainingQuantity1 + (offerQuantity1 * (offerscount1 - offerscount2));
                        SalesInvoice[key2] = remainingQuantity2;
                        return(offerscount2 * promotions.OfferPrice);
                    }
                }
            }
            return(0);
        }
コード例 #2
0
        public Promotions()
        {
            promotionsList = new List <PromotionOffer>();
            PromotionOffer A = new PromotionOffer();

            A.ProductOffer.Add("A", 3);
            A.OfferPrice = 130;
            promotionsList.Add(A);

            PromotionOffer B = new PromotionOffer();

            B.ProductOffer.Add("B", 2);
            B.OfferPrice = 45;
            promotionsList.Add(B);

            PromotionOffer CandD = new PromotionOffer();

            CandD.ProductOffer.Add("C", 1);
            CandD.ProductOffer.Add("D", 1);
            CandD.OfferPrice = 30;
            promotionsList.Add(CandD);
        }