-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathProgram.cs
More file actions
44 lines (37 loc) · 1.95 KB
/
Program.cs
File metadata and controls
44 lines (37 loc) · 1.95 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.Net.Http;
using System.Threading.Tasks;
using Newtonsoft.Json;
class Program
{
static async Task Main(string[] args)
{
// Запитати користувача про категорію
Console.WriteLine("Введiть категорiю (animal, career, celebrity, dev, explicit, fashion, food, history, money, movie, music, political, religion, science, sport, travel):");
string category = Console.ReadLine();
// Перевірити, чи введена категорія є в списку доступних категорій
string[] categories = { "animal", "career", "celebrity", "dev", "explicit", "fashion", "food", "history", "money", "movie", "music", "political", "religion", "science", "sport", "travel" };
if (!Array.Exists(categories, x => x == category))
{
Console.WriteLine("Неправильна категорiя!");
return;
}
// Викликати API та отримати випадковий анекдот вибраної категорії
HttpClient client = new HttpClient();
string apiUrl = $"https://api.chucknorris.io/jokes/random?category={category}";
HttpResponseMessage response = await client.GetAsync(apiUrl);
string responseBody = await response.Content.ReadAsStringAsync();
// Розпакувати JSON в об'єкт типу Joke
Joke joke = JsonConvert.DeserializeObject<Joke>(responseBody);
// Вивести категорію, дату та анекдот в консоль
Console.WriteLine($"Категорiя: {category}");
Console.WriteLine($"Дата створення: {joke.created_at}");
Console.WriteLine($"Анекдот: {joke.value}");
Console.ReadLine();
}
}
class Joke
{
public string value { get; set; }
public string created_at { get; set; }
}