/// <summary> /// Creates output string based on customer order. Stops string construction when any errors found. /// </summary> /// <param name="menu">User selected menu</param> /// <param name="customerOrder">User selected dishes</param> private void SetResult(Menu menu, List<Dish> customerOrder = null) { var output = ""; if (menu.HasError) { Result = menu.ErrorMessage; return; } if (customerOrder == null || !customerOrder.Any()) { Result = "error"; return; } foreach (var dish in customerOrder.OrderBy(d => d.Priority)) { var multiple = ""; if (dish.QuantityOrdered > 1) { multiple = string.Format("(x{0})", dish.QuantityOrdered); } output += string.Format("{0}{1}, {2}", dish.Name, multiple, dish.ErrorMessage); if (dish.HasError) { break; } } Result = string.Format("{0}", output.Trim().TrimEnd(',')); }
/// <summary> /// Hardcoded list of dishes for each menu type /// </summary> private void PopulateMenus() { var morningMenu = new Menu { Name = "morning", Type = MenuType.Morning}; morningMenu.Dishes.Add(new Dish { Name = "eggs", Value = "1", Type = DishType.Entrée, Priority = 1, IsMultipleAllowed = false}); morningMenu.Dishes.Add(new Dish { Name = "toast", Value = "2", Type = DishType.Side, Priority = 2, IsMultipleAllowed = false }); morningMenu.Dishes.Add(new Dish { Name = "coffee", Value = "3", Type = DishType.Drink, Priority = 3, IsMultipleAllowed = true }); Menus.Add(morningMenu); var nightMenu = new Menu { Name = "night", Type = MenuType.Night }; nightMenu.Dishes.Add(new Dish { Name = "steak", Value = "1", Type = DishType.Entrée, Priority = 1, IsMultipleAllowed = false }); nightMenu.Dishes.Add(new Dish { Name = "potato", Value = "2", Type = DishType.Side, Priority = 2, IsMultipleAllowed = true }); nightMenu.Dishes.Add(new Dish { Name = "wine", Value = "3", Type = DishType.Drink, Priority = 3, IsMultipleAllowed = false }); nightMenu.Dishes.Add(new Dish { Name = "cake", Value = "4", Type = DishType.Desert, Priority = 4, IsMultipleAllowed = false }); Menus.Add(nightMenu); }
/// <summary> /// Creates customer order based on user inputs and enforces quantity and availability rules /// </summary> /// <param name="args">list of command line arguments</param> private void TakeOrder(string[] args) { var _menuService = new MenuService(); Menu selectedMenu = null; var customerOrder = new List<Dish>(); args = ParseArgs(args); if (args.Any()) { selectedMenu = _menuService.Menus.FirstOrDefault(m => m.Name == args[0]); if (selectedMenu == null) { selectedMenu = new Menu {HasError = true, ErrorMessage = "error"}; SetResult(selectedMenu); return; } Dish selectedDish; for (int i = 1; i < args.Count(); i++) { selectedDish = selectedMenu.Dishes.FirstOrDefault(d => d.Value == args[i]); if (selectedDish == null) { selectedDish = customerOrder.Last(); selectedDish.HasError = true; selectedDish.ErrorMessage = "error"; break; } if (selectedDish.QuantityOrdered == 0) { selectedDish.QuantityOrdered++; customerOrder.Add(selectedDish); } else if (selectedDish.IsMultipleAllowed) { selectedDish.QuantityOrdered++; } else { selectedDish.HasError = true; selectedDish.ErrorMessage = "error"; } } } else { selectedMenu = new Menu {HasError = true, ErrorMessage = "error"}; } SetResult(selectedMenu, customerOrder); }