Exemple #1
0
        /// <summary>
        /// Function to show input form and output to user
        /// </summary>
        public static void ShowForm()
        {
            var         myJsonString = File.ReadAllText("Items.json");
            List <Item> items        = JsonConvert.DeserializeObject <List <Item> >(myJsonString);

            Console.WriteLine("Following items are available for purchase - \n");
            foreach (Item item in items)
            {
                Console.WriteLine("Item Name: {0}- {1}{2} {3} ", item.Description, item.Price, item.PriceUnit, item.Size);
            }
            Console.WriteLine("Please enter the quantity of each item you would like to buy following by space. Enter 0 for item not needed. \n");
            Console.Write("Items- ");

            string[] quantities = Console.ReadLine().Trim().Split(' ');

            if (quantities.Count() == 0)
            {
                Console.WriteLine("Values entered for item quantity is not correct. Please enter again ");
                Console.Write("Items- ");
            }

            // chec if input is valid and calculate total price
            if (Helper.CheckInteger(quantities))
            {
                for (int i = 0; i < items.Count; i++)
                {
                    if (i < quantities.Length)
                    {
                        items[i].Quantiity = Convert.ToInt32(quantities[i]);
                    }
                }
                CalculatePrice   calculateAmount = new CalculatePrice();
                PriceCalculation totoalAmount    = calculateAmount.CalculateTotalPrice(items);

                Console.WriteLine("Subtotal: \u00A3{0}", Math.Round(totoalAmount.Subtotoal, 2));

                foreach (ItemDiscount item in totoalAmount.ItemDiscount)
                {
                    if (!String.IsNullOrEmpty(item.DiscountPercentage.ToString()) && item.DiscountAmount > 0)
                    {
                        Console.WriteLine("{0}- {1}% off: -\u00A3{2}", item.Description, item.DiscountPercentage, Math.Round(item.DiscountAmount, 2));
                    }
                }
                Console.WriteLine("Total discount: \u00A3{0}", Math.Round(totoalAmount.TotalDiscount, 2));
                if (totoalAmount.TotalDiscount == 0)
                {
                    Console.WriteLine("(No offers available)");
                }
                Console.WriteLine("Total: \u00A3{0}", Math.Round((totoalAmount.Subtotoal - totoalAmount.TotalDiscount), 2));

                Console.WriteLine("\nPress any key to start again.");
            }
            // if input is not valid, ask user to enter again
            else
            {
                Console.WriteLine("Values entered for item quantity is not correct. Please enter again ");
                Console.Write("Items- ");
            }
            ConsoleKey readKey = Console.ReadKey().Key;

            if (readKey == ConsoleKey.Escape)
            {
                Environment.Exit(0);
            }
            else
            {
                ShowForm();
            }
        }
        /// <summary>
        /// Function to calculate price of input items.
        /// </summary>
        /// <param name="items"></param>
        /// <returns></returns>
        public PriceCalculation CalculateTotalPrice(List <Item> items)
        {
            PriceCalculation price = new PriceCalculation();

            price.ItemDiscount = new List <ItemDiscount>();
            try
            {
                // calculate direct percentage disocunt
                foreach (Item item in items)
                {
                    //calculate price of each item
                    double itemprice;
                    double discount = 0;
                    if (String.IsNullOrEmpty(item.DiscountPercentage.ToString()))
                    {
                        if (item.PriceUnit == Cuurency.p.ToString())
                        {
                            itemprice = ((double)((Convert.ToInt32(item.Quantiity) * Convert.ToInt32(item.Price))) / 100);
                        }
                        else
                        {
                            itemprice = (Convert.ToInt32(item.Quantiity) * Convert.ToDouble(item.Price));
                        }
                    }

                    else
                    {
                        if (item.PriceUnit == Cuurency.p.ToString())
                        {
                            itemprice = ((double)(Convert.ToInt32(item.Quantiity) * Convert.ToInt32(item.Price)) / 100);
                            discount  = (itemprice * (Convert.ToInt32(item.DiscountPercentage))) / 100;
                        }
                        else
                        {
                            itemprice = (Convert.ToInt32(item.Quantiity) * Convert.ToDouble(item.Price));
                            discount  = (itemprice * (Convert.ToInt32(item.DiscountPercentage))) / 100;
                        }
                    }
                    price.ItemDiscount.Add(new ItemDiscount
                    {
                        ItemCode           = item.ItemCode,
                        Description        = item.Description,
                        DiscountPercentage = item.DiscountPercentage,
                        DiscountAmount     = discount,
                        ItemPrice          = itemprice
                    });
                    price.Subtotoal     = price.Subtotoal + itemprice;
                    price.TotalDiscount = price.TotalDiscount + discount;
                }

                // calculate for extended discount items
                // example: Buy 2 cans of Bean and get a loaf of bread for half price
                // This will be applied if there is no original discount on second item such as bread
                foreach (Item item in items)
                {
                    foreach (FurtherDiscount furtherDiscount in item.FurtherDiscount)
                    {
                        if (!String.IsNullOrEmpty(furtherDiscount.OnItemPurchased))
                        {
                            if (Convert.ToInt32(furtherDiscount.OnItemPurchased) > 0 && Convert.ToInt32(furtherDiscount.OnItemPurchased) <= item.Quantiity)
                            {
                                string discountedItem = furtherDiscount.DiscountedItemCode;
                                // apply extended discount only if there is no orginal discount on item
                                if ((String.IsNullOrEmpty(items.Find(x => x.ItemCode == discountedItem).DiscountPercentage)))
                                {
                                    // update discount details for item
                                    price.ItemDiscount.Find(x => x.ItemCode == discountedItem).DiscountPercentage = furtherDiscount.DiscountPercentage;

                                    price.ItemDiscount.Find(x => x.ItemCode == discountedItem).DiscountAmount =
                                        ((double)price.ItemDiscount.Find(x => x.ItemCode == discountedItem).ItemPrice) *
                                        (Convert.ToInt32(furtherDiscount.DiscountPercentage)) / 100;

                                    // update total discount
                                    price.TotalDiscount = price.TotalDiscount + ((double)price.ItemDiscount.Find(x => x.ItemCode == discountedItem).ItemPrice) *
                                                          (100 - Convert.ToInt32(furtherDiscount.DiscountPercentage)) / 100;
                                }
                            }
                        }
                    }
                }

                return(price);
            }
            catch (Exception ex)
            {
                // write exception to logger and throw exception
                return(price);
            }
        }