public int CalculateTotalPriceApplyingUnitPrice(List <ProductToBuy> productsListToBuy)
        {
            int total = 0;

            for (int j = 0; j < productsListToBuy.Count; j++)
            {
                ProductToBuy product = productsListToBuy[j];
                if (product.ProductQuantity > 0)
                {
                    total = total + Convert.ToInt32(this.UnitPrice[product.ProuductName].Value) * product.ProductQuantity;
                }
            }
            return(total);
        }
        public int CalculateTotalPriceApplyingPromotion(List <ProductToBuy> productsListToBuy)
        {
            int total = 0;

            for (int i = 0; i < ActivePromotions.Count; i++)
            {
                dynamic currentPromotion = ActivePromotions[i];
                bool    isSatisfied      = true;
                for (int j = 0; j < productsListToBuy.Count; j++)
                {
                    ProductToBuy product = productsListToBuy[j];

                    int currentPromotionQuantity = Convert.ToInt32(currentPromotion[product.ProuductName].Value);
                    if (currentPromotionQuantity <= product.ProductQuantity)
                    {
                        isSatisfied = isSatisfied & true;
                    }
                    else
                    {
                        isSatisfied = false;
                    }
                    if (!isSatisfied)
                    {
                        break;
                    }
                }
                if (isSatisfied)
                {
                    total += currentPromotion["Price"].Value;
                    Console.WriteLine(currentPromotion["Message"].Value);
                    for (int j = 0; j < productsListToBuy.Count; j++)
                    {
                        ProductToBuy product = productsListToBuy[j];
                        int          currentPromotionQuantity = Convert.ToInt32(currentPromotion[product.ProuductName].Value);
                        product.ProductQuantity = product.ProductQuantity - currentPromotionQuantity;

                        Console.WriteLine("Remaining " + product.ProuductName + "-" + product.ProductQuantity);
                    }
                    i = i - 1;
                }
            }
            return(total);
        }
Beispiel #3
0
        static void Main(string[] args)
        {
            JSONParser          parser = new JSONParser();
            dynamic             promotionEngineModel = parser.ParseJSON();
            PriceCalculation    priceCalculation     = new PriceCalculation();
            List <ProductToBuy> NoOfProduct          = new List <ProductToBuy>();

            priceCalculation.UnitPrice        = promotionEngineModel.UnitPrice;
            priceCalculation.ActivePromotions = promotionEngineModel.ActivePromotions;
            string continueKey = "y";

            do
            {
                NoOfProduct = new List <ProductToBuy>();
                foreach (PropertyDescriptor prop in TypeDescriptor.GetProperties(promotionEngineModel.UnitPrice))
                {
                    Console.WriteLine("No of Product- " + prop.Name + " you want yo buy.");
                    int n;
                    while (!int.TryParse(Console.ReadLine(), out n))
                    {
                        Console.Clear();
                        Console.WriteLine("You entered an invalid number");
                        Console.WriteLine("No of Product- " + prop.Name + " you want yo buy.");
                    }
                    ProductToBuy productToBuy = new ProductToBuy();
                    productToBuy.ProuductName    = prop.Name;
                    productToBuy.ProductQuantity = n;

                    NoOfProduct.Add(productToBuy);
                }

                int total = priceCalculation.GetTotalPrice(NoOfProduct);
                Console.WriteLine("================================================================================");
                Console.WriteLine("Total price====" + total);

                Console.WriteLine("Press y to continue. Any key to exit");

                continueKey = Console.ReadLine().ToLower();
            } while (continueKey == "y");
        }