Beispiel #1
0
        static void Main(string[] args)
        {
            while (true)
            {
                Jokes knockKnock = new KnockKnockJokes();
                Jokes oneLiner   = new OneLinerJokes();
                Jokes dadJokes   = new DadJokes();

                Console.WriteLine("Welcome to the Joke Generator! What type of joke would you like to hear?");
                Console.WriteLine("[1] Knock-Knock Joke");
                Console.WriteLine("[2] One-Liner");
                Console.WriteLine("[3] Dad Joke");
                Console.WriteLine("[4] Any Type");
                Console.WriteLine("Please select a joke type >>> ");
                string userChoice = Console.ReadLine();
                Console.WriteLine();

                switch (userChoice)
                {
                case "1":
                    knockKnock.TellJoke(GetRandomIndexValue(knockKnock.JokeList.Count));
                    Console.WriteLine();
                    ReturnToMenu();
                    break;

                case "2":
                    oneLiner.TellJoke(GetRandomIndexValue(oneLiner.JokeList.Count));
                    Console.WriteLine();
                    ReturnToMenu();
                    break;

                case "3":
                    dadJokes.TellJoke(GetRandomIndexValue(dadJokes.JokeList.Count));
                    Console.WriteLine();
                    ReturnToMenu();
                    break;

                case "4":
                    Console.WriteLine(TellRandomJokeOfAnyType(knockKnock.JokeList, oneLiner.JokeList, dadJokes.JokeList));
                    Console.WriteLine();
                    ReturnToMenu();
                    break;

                default:
                    Console.WriteLine("Selection was invalid, so you get a random joke!");
                    Console.WriteLine();
                    Console.WriteLine(TellRandomJokeOfAnyType(knockKnock.JokeList, oneLiner.JokeList, dadJokes.JokeList));
                    Console.WriteLine();
                    ReturnToMenu();
                    break;
                }
            }
Beispiel #2
0
        /*
         * GroupDadJokesByLength(DadJokes dadJokes) is called by SearchDadJokes to
         * sort the resulting Dad Jokes by length and display them by grouped length beginning with short jokes first.
         * Note: during  my testing I was not able to find jokes with a length under 20 so they
         * all came out under Long Jokes
         */
        private void GroupDadJokesByLength(DadJokes dadJokes)
        {
            IList <DadJoke> shortJokes  = new List <DadJoke>();
            IList <DadJoke> mediumJokes = new List <DadJoke>();
            IList <DadJoke> longJokes   = new List <DadJoke>();

            foreach (DadJoke dadJoke in dadJokes.Results)
            {
                int wordCount = CountWords(dadJoke.Joke);

                if (wordCount < _settings.ShortJokeLimit)
                {
                    shortJokes.Add(dadJoke);
                }
                else if (wordCount > _settings.ShortJokeLimit && wordCount < _settings.MediumJokeLimit)
                {
                    mediumJokes.Add(dadJoke);
                }
                else
                {
                    longJokes.Add(dadJoke);
                }
            }

            if (shortJokes.Count != 0)
            {
                Console.WriteLine("*******************************************************");
                Console.WriteLine("Short jokes:");
                FormatAndDisplayDadJokes(shortJokes);
            }
            if (mediumJokes.Count != 0)
            {
                Console.WriteLine("*******************************************************");
                Console.WriteLine("Medium jokes:");
                FormatAndDisplayDadJokes(mediumJokes);
            }
            if (longJokes.Count != 0)
            {
                Console.WriteLine("*******************************************************");
                Console.WriteLine("Long jokes:");
                FormatAndDisplayDadJokes(longJokes);
            }
        }
Beispiel #3
0
        /* Function: SearchDadJokes(HttpClient, string, DadJokesSettings)
         * This function is called when Option 2 is selected from the UI. It
         * builds a query with parameters in order to use the /search endpoint of the API.
         * It hits the API and returns a "page" of DadJokes given the limit set in the _settings.
         * It then binds the response to the DadJokes class and returns that for us to use in later functions.
         * */
        public async Task SearchDadJokes()
        {
            try
            {
                using (HttpClient client = new HttpClient())
                {
                    InitializeClient(client);

                    string responseBody = "";
                    // Build the parameterized query then spawns a thread to return the responseBody as a string in an asyhonchronous operation.
                    var builder = new UriBuilder(_settings.BaseURL + "/search");
                    var query   = HttpUtility.ParseQueryString(builder.Query);
                    query.Add("term", _settings.SearchTerm);
                    query.Add("limit", _settings.JokesPerPage);
                    builder.Query = query.ToString();
                    string url = builder.ToString();
                    responseBody = await client.GetStringAsync(url);

                    // Bind the responseBody to our DadJokes object
                    DadJokes dadJokes = JsonConvert.DeserializeObject <DadJokes>(responseBody);
                    if (dadJokes.Status.Equals("200") && dadJokes.Results.Count != 0)
                    {
                        GroupDadJokesByLength(dadJokes);
                    }
                    else if (dadJokes.Results.Count != 0)
                    {
                        Console.WriteLine("Your search terms resulted in no jokes.");
                    }

                    Console.WriteLine("Press any key to exit.");
                }
            }
            catch (Exception e)
            {
                Console.WriteLine("Exception: " + e);
                Console.WriteLine("Press any key to exit.");
            }
        }
Beispiel #4
0
        internal static StringBuilder SetSearchContent(DadJokes jokes, string searchTerm)
        {
            StringBuilder allContents        = new StringBuilder();
            StringBuilder shortJokesContent  = new StringBuilder();
            StringBuilder mediumJokesContent = new StringBuilder();
            StringBuilder LongJokesContent   = new StringBuilder();

            int shortJokesCount  = 0;
            int mediumJokesCount = 0;
            int longJokesCount   = 0;

            shortJokesContent.AppendLine("Short jokes (<10 words):");
            shortJokesContent.AppendLine("------------------------------");
            mediumJokesContent.AppendLine("Medium jokes (<20 words):");
            mediumJokesContent.AppendLine("------------------------------");
            LongJokesContent.AppendLine("Long jokes (>= 20 words):");
            LongJokesContent.AppendLine("------------------------------");

            foreach (DadJokeItem joke in jokes.Results)
            {
                int numberOfWords = WordCount(joke.Joke);

                if (numberOfWords < 10)
                {
                    shortJokesContent.AppendLine(FormatJoke(joke.Joke, searchTerm));
                    shortJokesCount++;
                }
                else if (numberOfWords < 20)
                {
                    mediumJokesContent.AppendLine(FormatJoke(joke.Joke, searchTerm));
                    mediumJokesCount++;
                }
                else if (numberOfWords >= 20)
                {
                    LongJokesContent.AppendLine(FormatJoke(joke.Joke, searchTerm));
                    longJokesCount++;
                }
            }

            if (shortJokesCount != 0)
            {
                allContents.Append(shortJokesContent);
            }
            else
            {
                shortJokesContent.AppendLine("***Your search term resulted in no short jokes.***");
                allContents.Append(shortJokesContent);
            }

            allContents.AppendLine();

            if (mediumJokesCount != 0)
            {
                allContents.Append(mediumJokesContent);
            }
            else
            {
                mediumJokesContent.AppendLine("***Your search term resulted in no medium jokes.***");
                allContents.Append(mediumJokesContent);
            }

            allContents.AppendLine();

            if (longJokesCount != 0)
            {
                allContents.Append(LongJokesContent);
            }
            else
            {
                LongJokesContent.AppendLine("***Your search term resulted in no long jokes.***");
                allContents.Append(LongJokesContent);
            }

            return(allContents);
        }