Beispiel #1
0
        //Helper method to verify if all the ingredients are available for a particular beverage
        private IEnumerable <MissingIngredients> CheckMissingIngredients(MachineVM model)
        {
            List <Ingredient>          itemCount = model.availableItems;
            IList <MissingIngredients> result    = new List <MissingIngredients>();

            try
            {
                foreach (var beverage in model.beverages)
                {
                    MissingIngredients product         = new MissingIngredients();
                    List <string>      prodIngredients = new List <string>();

                    foreach (var item in beverage.ingredients)
                    {
                        if (!itemCount.Any(o => o.name == item.name))
                        {
                            if (string.IsNullOrEmpty(product.beverageName))
                            {
                                product.beverageName = beverage.name;
                                prodIngredients.Add(item.name);
                            }
                            else
                            {
                                prodIngredients.Add(item.name);
                            }
                        }
                    }
                    if (product.beverageName != null)
                    {
                        product.ingredients = prodIngredients;
                        result.Add(product);
                    }
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return(result);
        }
Beispiel #2
0
        //Main method to process the inputs of current test case and return result accordingly
        public void ProcessBeverage(MachineVM model)
        {
            if (model != null)
            {
                List <Ingredient> itemCount = AvailableIngredient.Ingredients;
                int freeOutlets             = model.outlet;

                //Check if All ingredients are present for the given beverages;
                IEnumerable <MissingIngredients> impossibleBeverages = CheckMissingIngredients(model);

                //Check if it is possible to make each given item through the coffee machine
                //Print result accordingly
                foreach (var beverage in model.beverages)
                {
                    if (impossibleBeverages != null && impossibleBeverages.Any(b => b.beverageName == beverage.name))
                    {
                        MissingIngredients item = impossibleBeverages.Where(b => b.beverageName == beverage.name).First();
                        Console.WriteLine(beverage.name + " cannot be prepared because " + item.ingredients.First() + " is not available");
                    }
                    else
                    {
                        //Check if coffee machine outlets are available
                        if (freeOutlets > 0)
                        {
                            bool canPrepare  = true;
                            bool printResult = false;
                            foreach (var item in beverage.ingredients)
                            {
                                if (itemCount.Any(o => o.name == item.name))
                                {
                                    var currentItem = itemCount.FirstOrDefault(o => o.name == item.name);

                                    if (currentItem.quantity >= item.quantity)
                                    {
                                        currentItem.quantity -= item.quantity;
                                    }
                                    else
                                    {
                                        canPrepare = false;
                                        if (!printResult)
                                        {
                                            Console.WriteLine(beverage.name + " cannot be prepared because " + item.name + " is not sufficient");
                                            printResult = true;
                                        }

                                        _repo.UpdateLowIngredientIndicator(currentItem);
                                    }
                                }
                            }
                            if (canPrepare)
                            {
                                freeOutlets--;
                                Console.WriteLine(beverage.name + " is prepared");
                            }
                        }
                        else
                        {
                            Console.WriteLine(beverage.name + " cannot be prepared as free outlet is not available");
                        }
                    }
                }
            }
            else
            {
                Console.WriteLine("Invalid input data");
                Console.ReadLine();
                return;
            }
            Console.WriteLine();

            _repo.ShowLowIngredientIndicator();
            Console.WriteLine();
            Console.WriteLine("Press Enter for next test case...\n\n\n");
            Console.ReadLine();
        }