Beispiel #1
0
        static void Main(string[] args)
        {
            ConsoleWriter writer = null;

            try                                                   //BugFix #2b
            {
                HttpClient httpClient = new HttpClient();

                JokeRepository jokeRepository = new JokeRepository(httpClient);
                NameRepository nameRepository = new NameRepository(httpClient);

                writer = new ConsoleWriter();
                IReader reader = new ConsoleReader(writer);

                UserQuestions    userQuestions    = new UserQuestions(reader, writer);
                UserInteractions userInteractions = new UserInteractions(writer, userQuestions);

                JokeService jokeService = new JokeService();

                ProgramRunner programRunner = new ProgramRunner(userInteractions, jokeRepository, nameRepository, jokeService);

                programRunner.Run();
            }
            catch (Exception)
            {
                if (writer != null)
                {
                    writer.WriteLine("Something went terribly wrong.");
                    writer.WriteLine("Are you sure you're connected to the internet?");
                }
            }
        }
Beispiel #2
0
        private static void Main()
        {
            UserInterface.PrintStartupScreen();

            char userSelection;

            while ((userSelection = Console.ReadKey().KeyChar) != 'q')
            {
                switch (userSelection)
                {
                case 'c':
                    UserInterface.PrintJokeCategories(JokeService.GetCategories());
                    break;

                case 'r':
                    UserInterface.UserJokeParameters jokeParameters = UserInterface.GetJokeParametersFromUser();
                    List <string> jokes = JokeService.GetRandomJokes(jokeParameters.FirstName, jokeParameters.LastName,
                                                                     jokeParameters.Category, jokeParameters.NumberOfJokes);
                    UserInterface.PrintJokes(jokes);
                    break;

                case '?':
                    UserInterface.PrintUsageInstructions();
                    break;

                default:
                    Console.WriteLine("The input selected is not recognized. Please try again. If you need help press '?'");
                    break;
                }

                UserInterface.PrintEmptyLines(2);
                Console.WriteLine("Execution Finished. Please make another selection.");
                UserInterface.PrintUsageInstructions();
            }
        }
Beispiel #3
0
        /// <summary>
        ///     Prompts the user for all required joke parameters
        /// </summary>
        /// <returns>The joke parameters chosen by the user</returns>
        public static UserJokeParameters GetJokeParametersFromUser()
        {
            UserJokeParameters jokeParameters = new UserJokeParameters();

            Console.Write(Environment.NewLine + "Would you like to substitute a random name in the jokes (y/N)? ");
            if (Console.ReadKey().KeyChar == 'y')
            {
                Tuple <string, string> randomName = NameService.GetRandomName();
                jokeParameters.FirstName = randomName?.Item1;
                jokeParameters.LastName  = randomName?.Item2;
            }

            Console.Write(Environment.NewLine + "Would you like to specify a category (y/N)? ");
            if (Console.ReadKey().KeyChar == 'y')
            {
                while (jokeParameters.Category == null)
                {
                    List <string> categories = JokeService.GetCategories();
                    PrintJokeCategories(categories);
                    PrintEmptyLines(1);
                    Console.Write("Which category would you like? ");
                    string chosenCategory = Console.ReadLine();
                    if (!categories.Contains(chosenCategory))
                    {
                        Console.WriteLine("Invalid input. Chosen category is not in the list of available categories.");
                    }
                    else
                    {
                        jokeParameters.Category = chosenCategory;
                    }
                }
            }

            Console.Write(Environment.NewLine + "How many jokes would you like to be generated? ");
            while (jokeParameters.NumberOfJokes < 1 || jokeParameters.NumberOfJokes > 9)
            {
                try
                {
                    jokeParameters.NumberOfJokes = int.Parse(Console.ReadLine());
                    if (jokeParameters.NumberOfJokes < 1 || jokeParameters.NumberOfJokes > 9)
                    {
                        Console.WriteLine("Invalid input. Number of jokes must be an integer from 1 - 9.");
                    }
                }
                catch (Exception e) when(e is ArgumentNullException || e is FormatException)
                {
                    Console.WriteLine("Invalid input. The entered value is null or is not an integer.");
                }
                catch (Exception e) when(e is OverflowException)
                {
                    Console.WriteLine("Invalid input. The entered value is out of the acceptable range. Please choose a number between 1 and " + int.MaxValue);
                }
            }

            return(jokeParameters);
        }