Esempio n. 1
0
        internal int Run()
        {
            m_consoleWriter.WriteLine("Prompt_PleaseWaitLoading");
            loadCategories();

            try
            {
                char key = AskQuestionsForASingleKeyStroke(new[] { "Prompt_IntroLine?OrQuit" }, new[] { '?' });
                if (key == '?')
                {
                    while (key != 'q')
                    {
                        JokeRequestBuilder jokeRequestBuilder = new JokeRequestBuilder();

                        key = AskQuestionsForASingleKeyStroke(new[] { "Prompt_CIsForCategories", "Prompt_RIsForRandomJokes", "Prompt_QIsToQuit" }, new[] { 'c', 'r', 'q' });

                        switch (key)
                        {
                        case 'c':
                            displayCategories();
                            break;

                        case 'r':
                            key = AskQuestionsForASingleKeyStroke(new[] { "Question_RandomName" }, new[] { 'y', 'n' });
                            switch (key)
                            {
                            case 'y':
                                GetNames();
                                break;

                            case 'n':
                                jokeRequestBuilder.FirstName = AskForAName(new[] { "Question_FirstName", "PRESS_ENTER" });
                                jokeRequestBuilder.LastName  = AskForAName(new[] { "Question_LastName", "PRESS_ENTER" });
                                break;
                            }

                            key = AskQuestionsForASingleKeyStroke(new[] { "Question_WantACategory" }, new[] { 'y', 'n' });
                            if (key == 'y')
                            {
                                jokeRequestBuilder.Category = AskForACategory(new[] { "Question_EnterACategory", "PRESS_ENTER" });
                            }

                            jokeRequestBuilder.JokeCount = AskQuestionsForANumber(new[] { "Question_HowManyJokes" });

                            DisplayJokes(GetRandomJokes(jokeRequestBuilder.ToJokeRequest()));
                            break;

                        case 'q':
                            break;
                        }
                    }
                }
            }
            catch (ApplicationException appex)
            {
                m_consoleWriter.WriteLine("ExpectedExceptionMessage");
                m_consoleWriter.WriteLine(appex.Message);
                return(1);
            }
            catch (Exception ex)
            {
                m_consoleWriter.WriteLine("GenericExceptionMessage");

                Exception e = ex;
                while (e != null)
                {
                    m_consoleWriter.WriteLine(e.Message);
                    m_consoleWriter.WriteLine(e.StackTrace);
                    e = e.InnerException;
                }
                return(2);
            }
            return(0);
        }
Esempio n. 2
0
        internal int Run()
        {
            _presentationBehavior.LoadingInformation();
            if (_categories == null)
            {
                _categories = _jokeProvider.GetCategories();
            }

            try
            {
                var key = ConsoleResults.Question;
                if (key == ConsoleResults.Question)
                {
                    var jokeRequestBuilder = new JokeRequestBuilder();
                    // initialize joke counter to 1 to by deafult display atleast 1 joke
                    jokeRequestBuilder.WithJokeCount(1);

                    while (key != ConsoleResults.Q)
                    {
                        key = _presentationBehavior.MakeChoose();
                        switch (key)
                        {
                        case ConsoleResults.C:
                            _presentationBehavior.WriteCategories(_categories);
                            key = _presentationBehavior.WannaSpecifyCategory();
                            if (key == ConsoleResults.Y)
                            {
                                var category = _presentationBehavior.EnterCategory(_categories);
                                jokeRequestBuilder.WithCategory(category);
                            }
                            break;

                        case ConsoleResults.R:
                            var keyValuePairs = _jokeProvider.GetNames(1);
                            jokeRequestBuilder.WithName(
                                keyValuePairs[0].Key, keyValuePairs[0].Value);
                            _presentationBehavior.SetRandomName(keyValuePairs[0].Key, keyValuePairs[0].Value);
                            break;

                        case ConsoleResults.J:
                            var buildRequest = jokeRequestBuilder.BuildRequest();
                            var jokes        = _jokeProvider.GetJokes(buildRequest);
                            _presentationBehavior.WriteJokes(jokes);
                            break;

                        case ConsoleResults.A:
                            jokeRequestBuilder.WithJokeCount(_presentationBehavior.HowManyJokes());
                            break;

                        case ConsoleResults.N:
                            jokeRequestBuilder.WithName(
                                _presentationBehavior.InputName(),
                                _presentationBehavior.InputLastName());
                            break;

                        case ConsoleResults.Q:
                            break;
                        }
                    }
                }
            }
            catch (ApplicationException applicationException)
            {
                return(_presentationBehavior.ExpectedExceptionMessage(applicationException));
            }
            catch (Exception ex)
            {
                return(_presentationBehavior.Exception(ex));
            }
            return(0);
        }