Ejemplo n.º 1
0
        static async Task Main(string[] args)
        {
            ConsolePrinter printer = new ConsolePrinter();
            ChuckNorrisController controller = new ChuckNorrisController();

            var donePlaying = false;
            printer.Value("").Print();//new line
            printer.Value("Lets get some Chuck Norris jokes!").Print();
            while (!donePlaying)
            {
                //igor - get the joke (no questions asked) or customize your jokes
                printer.Value("Press 1 - Get one Chuck Norris joke").Print();
                printer.Value("Press 2 - Make customized Chuck Norris joke/s").Print();
                printer.Value("Press x - Exit").Print();
                
                char mainSelectionKey = GetEnteredKey(Console.ReadKey());

                string categorySelected = null;
                int numOfJokes = 0;
                Tuple<string, string> names = null;

                //get just one joke
                if (mainSelectionKey == '1')
                {
                    numOfJokes = 1;
                }
                //customized jokes
                if (mainSelectionKey == '2')
                {
                    printer.Value("Want to use a random name? y/n").Print();
                    char nameSelectionKey = GetEnteredKey(Console.ReadKey());
                    if (nameSelectionKey == 'y')
                        names = await controller.GetNames().ConfigureAwait(false);

                    printer.Value("Want to specify a category? y/n").Print();
                    char categorySelectionKey = GetEnteredKey(Console.ReadKey());
                    if (categorySelectionKey == 'y')
                    {
                        //present categories and check input
                        categorySelected = await GetCategorySelection(controller, printer).ConfigureAwait(false);
                    }

                    numOfJokes = GetNumberOfJokes(printer);

                }
                if (mainSelectionKey == '1' || mainSelectionKey == '2')
                {
                    printer.Value("Asking Chuck for permission to publish jokes.").Print();
                    var jokes = await controller.GetRandomJokes(names, categorySelected, numOfJokes).ConfigureAwait(false);
                    printer.PrintResultsPerLine(jokes);
                    printer.Value("").Print();
                }
                if (mainSelectionKey == 'x')
                {
                    printer.Value("Thanks for playing").Print();
                    donePlaying = true;
                }
            }
        }
Ejemplo n.º 2
0
        private static async Task<string> GetCategorySelection(ChuckNorrisController controller, ConsolePrinter printer)
        {
            bool validCategory = false;
            string rc = "";
            while (!validCategory)
            {      
                var categories = await controller.GetCategories().ConfigureAwait(false);
                printer.PrintResults(categories);
                printer.Value("Enter a category:").Print();
                string categoryInput = Console.ReadLine();
                if (controller.CachedCategories.Contains(categoryInput))
                {
                    validCategory = true;
                    rc = categoryInput;
                }
                else
                {
                    printer.Value("Invalid catergory entered. Please enter a category from this list below:").Print();
                }
            }
            return rc;

        }