private static void InvalidCommand(string command, ActionDict actions) { if (command != "help") { Console.WriteLine($"{((command.Length > 0) ? $"Invalid input: {command}. " : "")}Valid inputs are:"); } actions.Help(); }
private static Action <string> SelectorGenerator(Func <IEnumerable <string> > inFunc, Action <string> outFunc, string type) { return(new Action <string>(selection => { IEnumerable <string> vals = inFunc(); ActionDict actions = new ActionDict(); foreach (string val in vals) { actions.Add(val.ToLower(), s => outFunc(val)); } if (selection == null) { actions.Help(); selection = prompt($"Select a {type} from the above list or cancel").ToLower().Trim(); } do { try { actions[selection](null); break; } catch (KeyNotFoundException) { if (selection == "cancel") { break; } else { InvalidCommand(selection, actions); } } catch (InvalidOperationException e) { Console.WriteLine(e.Message); } catch (PizzaBoxException e) { Console.WriteLine(e.Message); } catch (NotImplementedException) { Console.WriteLine($"The functionality of {selection} is not available yet. Please try again in the future."); } selection = prompt($"Select a {type} or cancel").ToLower().Trim(); } while (true); })); }
static void Main(string[] _args) { context = ContextDBBacked.Context; context.GetToppings(); ActionDict actions = new ActionDict(); #region required foreach (var command in new string[] { "login", "signin" }) { actions.Add(command, Login); } foreach (var command in new string[] { "ls", "locs", "locations" }) { actions.Add(command, Locations); } foreach (var command in new string[] { "loc", "location" }) { actions.Add(command, SelectorGenerator(Location, context.SetStore, "store")); } foreach (var command in new string[] { "new", "order" }) { actions.Add(command, Order); } actions.Add("history", History); foreach (var command in new string[] { "logout", "signout" }) { actions.Add(command, context.Logout); } foreach (var command in new string[] { "quit", "exit", "end" }) { actions.Add(command, Quit); } #endregion #region optional actions.Add("register", Register); actions.Add("sales", Sales); actions.Add("users", Users); actions.Add("inventory", SelectorGenerator(Inventory, s => { Console.WriteLine(context.GetInventory(s)); }, "topping")); #endregion Console.WriteLine("Welcome to Revature Pizza Portal"); Console.WriteLine("Valid commands are listed below."); actions.Help(); string input; while (!finished && (input = prompt( #region uglyPrompt $"{((context.LoggedIn!=null)?context.LoggedIn.Username:"******")}{((context.Location!=null)?$"@{context.Location.Name}":"")}" #endregion )) != null) { string[] sep = input.Split(null, 2); sep[0] = sep[0].ToLower(); try { actions[sep[0]](sep.ElementAtOrDefault(1)); } catch (KeyNotFoundException) { InvalidCommand(sep[0], actions); } catch (InvalidOperationException e) { Console.WriteLine(e.Message); } catch (PizzaBoxException e) { Console.WriteLine(e.Message); } catch (NotImplementedException) { Console.WriteLine($"The functionality of {sep[0]} is not available yet. Please try again in the future."); } } }