/// <summary> /// Returns the dish counter and, optionally, increments it. /// </summary> /// <param name="dishType">Dish type counter.</param> /// <param name="incrementCounter">True if it will increments the counter, or false if it just returns the counter.</param> /// <returns>Returns an integer with the counter.</returns> public int DishCounter(Enums.DishTypes dishType, bool incrementCounter = false) { int dishCounter = 0; switch (dishType) { case Enums.DishTypes.Entree: dishCounter = incrementCounter ? ++EntreeCount : EntreeCount; break; case Enums.DishTypes.Side: dishCounter = incrementCounter ? ++SideCount : SideCount; break; case Enums.DishTypes.Drink: dishCounter = incrementCounter ? ++DrinkCount : DrinkCount; break; case Enums.DishTypes.Dessert: dishCounter = incrementCounter ? ++DessertCount : DessertCount; break; case Enums.DishTypes.Error: break; default: break; } return(dishCounter); }
/// <summary> /// Writes the counter of each dish processed and validated in the order. /// </summary> /// <param name="order">The order being processed</param> /// <param name="dishType">The dish which will be writed.</param> private static void WriteCounter(IOrder order, Enums.DishTypes dishType) { try { switch (dishType) { case Enums.DishTypes.Entree: if (order.EntreeCount > 0) { order.SetOutputString(order.Entree); } break; case Enums.DishTypes.Side: if (order.SideCount > 0) { order.SetOutputString(order.Side + Helpers.FormatCounterOutput(order.SideCount)); } break; case Enums.DishTypes.Drink: if (order.DrinkCount > 0) { order.SetOutputString(order.Drink + Helpers.FormatCounterOutput(order.DrinkCount)); } break; case Enums.DishTypes.Dessert: if (order.DessertCount > 0) { order.SetOutputString(order.Dessert); } break; case Enums.DishTypes.Error: break; default: break; } } catch (Exception ex) { Console.WriteLine("{0}\r\n{1}", ex.Message, ex.StackTrace); } }
/// <summary> /// Process dishes in the order list. /// It will validate multiple dishes rule and increment the dish's counter. /// </summary> /// <param name="order">The order wich will be processed.</param> /// <param name="inputString">Input string with order info.</param> /// <returns>Returns true if process completes ok, or false if not.</returns> public static bool ProcessDishes(IOrder order, string inputString) { try { if (order == null) { return(false); } Enums.DishTypes dishType = GetDishType(inputString); if (dishType == Enums.DishTypes.Error) { order.SetError(); return(false); } if (OrderValidation.ValidateMultipleDishes(order, dishType)) { // If dish allows multiple choices, increment counter order.DishCounter(dishType, true); } else { // If dish doesn't allow multiple choices, but counter is 0 AND // is not dessert on morning, increment counter if (order.DishCounter(dishType) == 0 && OrderValidation.ValidateMorningDessertRule(order, dishType)) { order.DishCounter(dishType, true); } else { // Otherwise, set error in the order order.SetError(); return(false); } } return(true); } catch (Exception) { order.SetError(); return(false); } }
/// <summary> /// Validate if order does NOT have a dessert on morning - rule #4. /// </summary> /// <param name="order">The order being processed.</param> /// <param name="dishType">The dish to validate.</param> /// <returns>Returns true if there is no dessert on morning, or false if yes.</returns> public static bool ValidateMorningDessertRule(IOrder order, Enums.DishTypes dishType) { try { if (order == null) { return(false); } if (order.TimeOfDay == Enums.TimeOfDay.Morning && dishType == Enums.DishTypes.Dessert) { return(false); } return(true); } catch (Exception) { return(false); } }
/// <summary> /// Validates multiple dishes rules #7 & #8 /// These rules allows multiple coffees at morning and multiple potatoes at night. /// </summary> /// <param name="order">The order being processed.</param> /// <param name="dishType">The dish type to validate.</param> /// <returns>Return true if dish is in one of the rules, and false if not. </returns> public static bool ValidateMultipleDishes(IOrder order, Enums.DishTypes dishType) { try { if (order == null) { return(false); } if (order.TimeOfDay == Enums.TimeOfDay.Morning && dishType == Enums.DishTypes.Drink || order.TimeOfDay == Enums.TimeOfDay.Night && dishType == Enums.DishTypes.Side) { return(true); } return(false); } catch { return(false); } }