Пример #1
0
        /// <summary>
        /// Orders the drink.
        /// </summary>
        /// <param name="type">The type.</param>
        /// <param name="addOn">The add on.</param>
        /// <returns>IDrink implementation</returns>
        public static IDrink OrderDrink(DrinkType type, DrinkAddOn addOn)
        {
            IDrink drink = null;

            switch (type)
            {
            case DrinkType.Expresso:
                drink = new Expresso();
                break;

            case DrinkType.Tea:
                drink = new Tea();
                break;

            case DrinkType.IceTea:
                drink = new IceTea();
                break;

            default:
                break;
            }

            if (drink != null)
            {
                if (type != DrinkType.IceTea)
                {
                    if (addOn.HasMilk)
                    {
                        drink = new MilkDecorator(drink);
                    }
                    else
                    {
                        drink.Description += " without milk";
                    }
                }

                if (addOn.HasSugar)
                {
                    drink = new SugarDecorator(drink);
                }
                else
                {
                    drink.Description += " without sugar";
                }

                if (addOn.HasChocolate && type == DrinkType.Expresso)
                {
                    drink = new ChocolateDecorator(drink);
                }
            }

            drink.Description = $"We are preparing the following drink for you: {drink.Description}";
            return(drink);
        }
Пример #2
0
        public static IDrink OrderDrink(string type, bool hasMilk, bool hasSugar)
        {
            DrinkType  drinkType;
            IDrink     drink = null;
            DrinkAddOn addOn = new DrinkAddOn();

            addOn.HasMilk  = hasMilk;
            addOn.HasSugar = hasSugar;
            if (Enum.TryParse(type, out drinkType))
            {
                try
                {
                    drink = DrinkFactory.OrderDrink(drinkType, addOn);
                    Console.WriteLine(drink.Description);
                }
                catch (Exception ex)
                {
                    Console.WriteLine("We are unable to prepare your drink.");
                    System.IO.File.WriteAllText(@"c:\Error.txt", ex.ToString());
                }
            }

            return(drink);
        }