public static Drink OrderDrink(string type, bool hasMilk, bool hasSugar, bool hasChocolate = false)//Adding hasChocolate as optional parameter to avoid regression { Drink drink = null; type = RemoveSpaceIfAny(type); Enum.TryParse(type, out DrinkType drinkType); switch (drinkType) { case DrinkType.Expresso: drink = new Expresso(); break; case DrinkType.HotTea: drink = new HotTea(); break; case DrinkType.IceTea: drink = new IceTea(); break; } try { if (hasMilk) { drink.Toppings.Add(new Milk()); } if (hasSugar) { drink.Toppings.Add(new Sugar()); } if (hasChocolate) { drink.Toppings.Add(new Chocolate()); } drink.Prepare(); } catch (Exception ex) { Console.WriteLine("We are unable to prepare your drink."); //Warning: I Add to change to d:\ as root is readonly on my machine. System.IO.File.WriteAllText(@"c:\Error.txt", ex.ToString()); } return(drink); }
/// <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); }
public Drink OrderDrink(Enumerations.DrinkType type, bool hasMilk, bool hasSugar, bool isChocolatey = false) { Drink drink; switch (type) { case Enumerations.DrinkType.Expresso: drink = new Expresso { HasMilk = hasMilk, HasSugar = hasSugar, ChocolateTopping = isChocolatey }; break; case Enumerations.DrinkType.IceTea: drink = new IceTea { HasMilk = hasMilk, HasSugar = hasSugar, }; break; case Enumerations.DrinkType.HotTea: drink = new HotTea { HasMilk = hasMilk, HasSugar = hasSugar, }; break; default: throw new ApplicationException("Unable to prepare drink. Unrecognised drink type"); } drink.Prepare(drink);; return(drink); }