private void ExecuteCommand(string command) { string[] commandAndArgs = command.Split(' '); string commandName = commandAndArgs[0]; if (!CommandsHelper.IsCommand(commandName)) { return; } switch (commandName) { case CommandsHelper.GOTO_PREV_QUESTION: { int curStateInt = (int)this.CurrentState; if (curStateInt != 1) { this.CurrentState = (QuestionaryState)curStateInt - 1; } } break; case CommandsHelper.GOTO_QUESTION: { int questionNumber; if (int.TryParse(commandAndArgs[1], out questionNumber)) { if (Enum.IsDefined(typeof(QuestionaryState), questionNumber)) { this.CurrentState = (QuestionaryState)questionNumber; } else { Console.WriteLine("Неверный номер вопроса, введите номер от 1 до 5"); } } } break; case CommandsHelper.RESTART_PROFILE: { this.CurrentState = QuestionaryState.Start; } break; default: { } break; } }
private static bool AskQuestion(string text, Func <string, bool> checkAnswer, out string answer) { answer = String.Empty; bool isCommand = false; do { Console.WriteLine(text); answer = Console.ReadLine(); if (CommandsHelper.IsCommand(answer.Split(' ')[0])) { isCommand = true; } }while (!checkAnswer(answer) && !isCommand); return(!isCommand); }
static void Main(string[] args) { Console.Title = "Questionnaire"; Console.WriteLine("Select an action. Type -help to see all available commands."); Questionary questionary = null; while (true) { string input = Console.ReadLine(); if (!CommandsHelper.IsCommand(input)) { continue; } switch (input) { case CommandsHelper.HELP: CommandsHelper.PrintAvailableCommands(); break; case CommandsHelper.NEW_PROFILE: questionary = Questionary.StartQuestioning(); Console.WriteLine("Select an action. Type -help to see all available commands."); break; case CommandsHelper.EXIT: Environment.Exit(0); break; default: CommandsHelper.ExecuteCommand(input, questionary); break; } } }