Download as pdf or txt
Download as pdf or txt
You are on page 1of 7

Assignment-3(1)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
using System.Text;
using System.Threading.Tasks;
using Newtonsoft.Json;

namespace ConsoleApp1
{
class Program
{
static string[] results = new string[50];
static List<string> jokesList = null;
static char key;
static Tuple<string, string> names;
static ConsolePrinter printer = new ConsolePrinter();

static void Main(string[] args)


{
printer.Value("Press ? to get instructions.").ToString();
if (Console.ReadLine() == "?")
{
while (true)
{
jokesList = new List<string>();
printer.Value("Press c to get categories").ToString();
printer.Value("Press r to get random
jokes").ToString();
GetEnteredKey(Console.ReadKey());
if (key == 'c')
{
getCategories();
PrintResults();
}
else if (key == 'r')
{
Console.WriteLine();
printer.Value("Want to use a random name?
y/n").ToString();
GetEnteredKey(Console.ReadKey());
if (key == 'y')
GetNames();
if(key == 'n' || key == 'y')
{
Console.WriteLine();
printer.Value("Want to specify a category?
y/n").ToString();
GetEnteredKey(Console.ReadKey()); //DT: Read
the last entered key to get proper result.
if (key == 'y')
{
Console.WriteLine();
printer.Value("How many jokes do you want?
(1-9)").ToString();
int n = Int32.Parse(Console.ReadLine());
if (n <= 0 || n > 9)
{
printer.Value("Invalid Input!").ToString();
}
else
{
printer.Value("Choose from following
categories:").ToString();
getCategories();
PrintResults();
printer.Value("Enter a category;").ToString();
string jokeCategory = Console.ReadLine();
if (results[0].Contains(jokeCategory))
{
for (int i = 0; i < n; i++)
{
GetRandomJokes(jokeCategory, n);
PrintResults();
}
}
else
{
printer.Value("Invalid Input!").ToString();
}
}
}
else
{
Console.WriteLine();
printer.Value("How many jokes do you want?
(1-9)").ToString();
int n = Int32.Parse(Console.ReadLine());
if(n<=0 || n>9)
{
printer.Value("Invalid Input!").ToString();
}
else
{
for (int i = 0; i < n; i++)
{
GetRandomJokes(null, n);
PrintResults();
}
}
}
}
else
printer.Value("Invalid Input!").ToString();
}
else
{
//DT: Invalid input entered by user.
printer.Value("Invalid Input!").ToString();
}
names = null;
}
}

private static void PrintResults()


{
Console.WriteLine();
printer.Value("[" + string.Join(",", results) + "]").ToString();
}

private static void GetEnteredKey(ConsoleKeyInfo


consoleKeyInfo)
{
switch (consoleKeyInfo.Key)
{
case ConsoleKey.C:
key = 'c';
break;
case ConsoleKey.D0:
key = '0';
break;
case ConsoleKey.D1:
key = '1';
break;
case ConsoleKey.D3:
key = '3';
break;
case ConsoleKey.D4:
key = '4';
break;
case ConsoleKey.D5:
key = '5';
break;
case ConsoleKey.D6:
key = '6';
break;
case ConsoleKey.D7:
key = '7';
break;
case ConsoleKey.D8:
key = '8';
break;
case ConsoleKey.D9:
key = '9';
break;
case ConsoleKey.R:
key = 'r';
break;
case ConsoleKey.N:
key = 'n';
break;
case ConsoleKey.Y:
key = 'y';
break;
}
}

private static void GetRandomJokes(string category, int


number)
{
new JsonFeed("https://api.chucknorris.io", number);
string[] tempResult = new string[50];

//DT: Get the unique joke by performing linear search


operation on already printed jokes.
tempResult = JsonFeed.GetRandomJokes(names?.Item1,
names?.Item2, category);
if(jokesList.Count > 0)
{
while(jokesList.Contains(tempResult[0]))
{
tempResult =
JsonFeed.GetRandomJokes(names?.Item1, names?.Item2,
category);
}
}
jokesList.Add(tempResult[0]);
results = tempResult;
}

private static void getCategories()


{
new JsonFeed("https://api.chucknorris.io", 0);
results = JsonFeed.GetCategories();
}

private static void GetNames()


{
new JsonFeed("https://www.names.privserv.com/api/", 0);
dynamic result = JsonFeed.Getnames();
names = Tuple.Create(result.name.ToString(),
result.surname.ToString());
}
}
}

You might also like