Ejemplo n.º 1
0
        /// <summary>
        /// Creates the correct IDrink from EDrinks.
        /// Logs with <see cref="IOutputter"/> if an incomaptible topping is detected
        /// Throws an exception if <paramref name="type"/> value is not supported
        /// </summary>
        /// <param name="type">
        /// Drink type
        /// </param>
        /// <param name="hasSugar">
        /// Should drink have sugar?
        /// </param>
        /// <param name="hasMilk">
        /// Should drink have milk? Defaults to false
        /// </param>
        /// <param name="hasChocolate">
        /// Should drink have chocolate? Defaults to false
        /// </param>
        /// <returns></returns>
        public IDrink BuildDrink(EDrinks type, bool hasSugar, bool hasMilk = false, bool hasChocolate = false)
        {
            IDrink newDrink = null;

            switch (type)
            {
            case EDrinks.Expresso:
                newDrink = new Expresso(hasSugar);
                break;

            case EDrinks.HotTea:
                newDrink = new HotTea(hasSugar);
                break;

            case EDrinks.IceTea:
                newDrink = new IceTea(hasSugar);
                break;

            default:
                throw new ArgumentException($"Unknown drink type: {type}");
            }

            //I tried to make it as simple as possible to add new interfaces, following the expected pattern
            if (hasChocolate)
            {
                IChocolateDrink chocoDrink = newDrink as IChocolateDrink;
                if (chocoDrink == null)
                {
                    Logger.WriteToConsole($"Drink type: {newDrink.Description} does not accept chocholate");
                }
                chocoDrink.HasChocolate = hasChocolate;
            }

            if (hasMilk)
            {
                IMilkDrink milkDrink = newDrink as IMilkDrink;
                if (milkDrink == null)
                {
                    Logger.WriteToConsole($"Drink type: {newDrink.Description} does not accept milk");
                }
                milkDrink.HasMilk = hasMilk;
            }

            return(newDrink);
        }
Ejemplo n.º 2
0
        /// <summary>
        /// Check <paramref name="drink"/> for possible interfaces and take their values for the toppings
        /// Uses <see cref="IOutputter"/> to write to console
        /// </summary>
        /// <param name="drink">
        /// Drink being read for prepare
        /// </param>
        public void Prepare(IDrink drink)
        {
            string message = $"We are preparing the following drink for you: {drink.Description}";

            var milk = drink as IMilkDrink;

            if (milk == null || !milk.HasMilk)
            {
                message += " without milk";
            }
            else
            {
                message += " with milk";
            }

            if (drink.HasSugar)
            {
                message += " with sugar";
            }
            else
            {
                message += " without sugar";
            }

            var chocolate = drink as IChocolateDrink;

            if (chocolate == null || !chocolate.HasChocolate)
            {
                message += " without chocolate";
            }
            else
            {
                message += " with chocolate";
            }

            Outputter.WriteToConsole(message);
        }