Exemplo n.º 1
0
        /// <summary>Runs an asyncronous Main for the program.</summary>
        /// <param><c>args</c> is the flags used when the program was started.</param>
        /// <returns>A Task<int> representing if the program ran successfully or not.</returns>
        static async Task <int> AsyncMain(string[] args)
        {
            DisplayFullMenu();
            while (true)
            {
                input = GetTextualInput(null, new[] { "c", "h", "j", "x" }, "That is not a recognized command.");
                switch (input)
                {
                case "c":
                    // User wants to see the categories available
                    if (categories == null)
                    {
                        categories = await JokeFeed.GetCategories(client, printer);
                    }
                    DisplayCategories(categories);
                    break;

                case "h":
                    // User wants to see the menu
                    DisplayFullMenu();
                    break;

                case "j":
                    // User wants to hear some jokes
                    await DisplayJoke();

                    break;

                case "x":
                    // User wants to leave
                    return(0);
                }
            }
        }
Exemplo n.º 2
0
        /// <summary>Runs the user through the vaious steps to display a joke.</summary>
        /// <returns>A Task<int> to say the joke was successfully displayed.</returns>
        public static async Task <int> DisplayJoke()
        {
            do
            {
                input = GetTextualInput("Want to use a random name? y/n", new[] { "y", "n" }, "Sorry, I don't understand.");
                if (input == "y")
                {
                    // User wants to use a random name
                    name = await NameFeed.GetRandomName(client, printer);
                }
                else
                {
                    input = GetTextualInput("Want to choose your name? y/n", new[] { "y", "n" }, "Sorry, I don't understand.");
                    if (input == "y")
                    {
                        // User wants to use a custom name
                        printer.Value("Want would you like your first name to be?").PrintToConsole();
                        string firstName = Console.ReadLine();
                        printer.Value("Want would you like your last name to be?").PrintToConsole();
                        string lastName = Console.ReadLine();
                        name = Tuple.Create(firstName, lastName);
                    }
                    else
                    {
                        // User wants to use default name
                        printer.Value("The name used will therefore be Chuck Norris.").PrintToConsole();
                        break;
                    }
                }
            } while (name == null);
            if (name != null)
            {
                printer.Value($"The name is {name.Item1} {name.Item2}").PrintToConsole();
            }


            input = GetTextualInput("Want to specify a category? y/n", new[] { "y", "n" }, "Sorry, I don't understand.");
            if (input == "y")
            {
                // Get categories to remind user and / or verfication
                if (categories == null)
                {
                    categories = await JokeFeed.GetCategories(client, printer);
                }
                do
                {
                    category = GetTextualInput("Enter a category. Enter c to view categories.", categories.Union(new [] { "c" }).ToArray(), "That doesn't seem to be a category.");

                    // User requested to view the categories instead of typing a category
                    if (category[0] == 'c' && category.Length == 1)
                    {
                        DisplayCategories(categories);
                        // Reset input for user to try again
                        category = null;
                    }
                } while (category == null);
            }

            // Ask user how many jokes they want
            int n = GetNumericInput("How many jokes do you want? (1-9)", 1, 9, "That doesn't seem to be a number from 1-9.");

            printer.Value("Please wait while I get your jokes...").PrintToConsole();

            //Get jokes!
            printer.PrintArrayToConsole(await JokeFeed.GetRandomJokes(client, printer, name?.Item1, name?.Item2, category, n));

            // Reset variables for subsequent runs
            name       = null;
            category   = null;
            categories = null;

            printer.Value("Good stuff! Feel free to go again. Remember to enter h to see the menu.").PrintToConsole();

            return(0);
        }