//Let's create a an overloaded alternative method for the updated version public static Drink OrderDrink(DrinkMenu option) { Drink drink = new Drink(); if (option.type == AvailableDrinks.Expresso) { drink = new Expresso(); } else if (option.type == AvailableDrinks.Tea) { drink = new Tea(); } else if (option.type == AvailableDrinks.IceTea) { drink = new IceTea(); } try { drink.Prepare(); //Remove type as it is not used drink.HasMilk = option.hasMilk; drink.HasSugar = option.hasSugar; drink.HasChocolate = option.hasChocolate; return(drink); } catch (Exception ex) { Console.WriteLine("We are unable to prepare your drink: " + ex.ToString()); System.IO.File.WriteAllText(@"c:\Error.txt", ex.ToString()); } //Let's return nothing if something fails return(null); }
// add a class comment to notify library consumers there is a improved version /// <summary> /// This method has been deprecated, please use OrderDrink(DrinkMenu option) /// </summary> /// <param name="type"></param> /// <param name="hasMilk"></param> /// <param name="hasSugar"></param> /// <returns></returns> public static Drink OrderDrink(string type, bool hasMilk, bool hasSugar) { Drink drink = new Drink(); if (type == "Expresso") { drink = new Expresso(); } else if (type == "HotTea") { drink = new Tea(); } else if (type == "IceTea") { drink = new IceTea(); } try { drink.HasMilk = hasMilk; drink.HasSugar = hasSugar; drink.Prepare(); //Remove parameter as it was not used in the method } catch (Exception ex) { Console.WriteLine("We are unable to prepare your drink."); System.IO.File.WriteAllText(@"c:\Error.txt", ex.ToString()); } return(drink); }
public Drink OrderDrink(string type, bool hasMilk, bool hasSugar, bool hasMocha = false) { Drink drink ; switch (type) { case "Expresso": drink = new Expresso { HasMilk = hasMilk, HasSugar = hasSugar, HasMocha = hasMocha }; break; case "IceTea": drink = new IceTea(); drink.HasMilk = hasMilk; drink.HasSugar = hasSugar; break; case "Tea": drink = new Tea(); drink.HasMilk = hasMilk; drink.HasSugar = hasSugar; break; default: throw new Exception("Wrong drink type"); } drink.Prepare(type); return drink; }
/// <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); }
public static Drink OrderDrink(Drink type, bool hasMilk, bool hasSugar) { // Set field to be used later for the Preparation method. HasMilk = hasMilk; HasSugar = hasSugar; /// For compering and returning variable. Drink drink = type; if (drink is Expresso) { drink = new Expresso(); // Adding topping to just Expresso. Other drinks are nullify drink.getChocolateTopping(); } else if (drink is Tea) { drink = new Tea(); } else if (drink is IceTea) { drink = new IceTea(); } try { double drinkCost = drink.Cost(); // Check is has mill and that is not an "Ice Tea" if (hasMilk && !(drink is IceTea)) { drinkCost += MilkCost; } if (hasSugar) { drinkCost += SugarCost; } // Sets the new price to the Drink. drink.setCost(drinkCost); // Here could be a bit confusing as will print the name of the class, // whicj will print "IceTea" rather tha "Ice Tea" Prepare(drink.ToString()); } catch (Exception ex) { Console.WriteLine("We are unable to prepare your drink."); System.IO.File.WriteAllText(@"c:\Error.txt", ex.ToString()); } return(drink); }
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); }
public static Drink OrderDrink(DrinkType drinkType, ITopping topping) { Drink drink = null; switch (drinkType) { case DrinkType.Expresso: drink = new Expresso(topping.HasSugar, topping.HasMilk, topping.HasChocolate); break; case DrinkType.Tea: drink = new Tea(topping.HasSugar); break; case DrinkType.IceTea: drink = new IceTea(topping.HasSugar); break; default: throw new NotSupportedException(); } return(drink); }
//Discovered this a while ago on a Java project. //Same in C#. Works well when creating objects like this. public static IDrink GetDrink(string type) { IDrink drink = null; switch (type) { case "Expresso": drink = new Expresso(); break; case "HotTea": drink = new Tea(); break; case "IceTea": drink = new IceTea(); break; default: drink = new Drink(); break; } return(drink); }