Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions Lesson01/Lesson01/FileManager.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lesson01
{
internal class FileManager<T>
{
static string path = Directory.GetCurrentDirectory() + $"\\{typeof(T)}Data";
public static void Save(T file)
{

using FileStream fs = new(path, FileMode.OpenOrCreate);
using StreamWriter sw = new(fs);

string json = JsonConvert.SerializeObject(file);

sw.WriteLine(json);
}
public static T Load()
{
string json ="";
try
{
using FileStream fs = new(path, FileMode.Open);
using StreamReader sr = new(fs);

json = sr.ReadLine();
}
catch(Exception e)
{
Console.WriteLine("File not exist , file be create");
}

T file = JsonConvert.DeserializeObject<T>(json);

return file;
}
}
}
19 changes: 19 additions & 0 deletions Lesson01/Lesson01/Joke.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lesson01
{

internal class Joke
{
public string type { get; set; }
public string setup { get; set; }
public string punchline { get; set; }
public int id { get; set; }
}


}
214 changes: 191 additions & 23 deletions Lesson01/Lesson01/Program.cs
Original file line number Diff line number Diff line change
@@ -1,53 +1,221 @@
using Lesson01.Models.CoinDesk;
using Newtonsoft.Json;
using System.Reflection.Metadata.Ecma335;
using System.Security.Cryptography;
using System.Text;

namespace Lesson01
{
internal class Program
{
static object locker = new object();

static void Main(string[] args)
{
#region Cat facts

//var client = new HttpClient();
ShowMenu();
Main(args);
}
#region BitCoin
static void ThreadMakerBitcoin()
{
Thread thread1 = new Thread(DownloadBitcoinData);
thread1.Start();
}
static void DownloadBitcoinData()
{
Monitor.Enter(locker);
var coinDesk = HttpRequest<CoinDesk>("https://api.coindesk.com/v1/bpi/currentprice.json");

FileManager<CoinDesk>.Save(coinDesk);
Monitor.Exit(locker);
}
#endregion
#region University
static void ThreadMakerUniversities()
{

Console.WriteLine("enter country : ");
string country = Console.ReadLine();

Thread thread2 = new Thread(DownloadUniversitiesData);
thread2.Start(country);

}
static void DownloadUniversitiesData(object country)
{
lock (locker)
{
country = (string)country;
List<University> universities = HttpRequest<List<University>>($"http://universities.hipolabs.com/search?country={country}");

FileManager<List<University>>.Save(universities);
}
}
#endregion
#region Joke
static void ThreadMakerJoke()
{
Thread thread3 = new Thread(DownloadJokeData);
thread3.Start();
}
static void DownloadJokeData()
{
Joke joke = HttpRequest<Joke>("https://official-joke-api.appspot.com/random_joke");

Monitor.Enter(locker);
FileManager<Joke>.Save(joke);
Monitor.Exit(locker);
}
#endregion

static void ShowMenu()
{
Console.Clear();
Console.WriteLine("1.Get old informations BitCoin");
Console.WriteLine("2.Get old information Universities");
Console.WriteLine("3.Get old any jokes");
Console.WriteLine("4.Update informations Bitcoin");
Console.WriteLine("5.Update information Universities in USA");
Console.WriteLine("6.Update any jokes");

Menu();
}
static int ChooseMenu()
{
Console.Write("Choose the menu : ");

int.TryParse(Console.ReadLine(), out int choose);
Console.Clear();
return choose;
}
static void Menu()
{
switch (ChooseMenu())
{
case 4: ThreadMakerBitcoin();break;
case 5: ThreadMakerUniversities();break;;
case 6: ThreadMakerJoke();break;
case 1: Bitcoin();break;
case 2: Universities();break;
case 3: Jokes();break;
default: break;
}
}
static void Jokes()
{
Console.Clear();
try
{
var joke = FileManager<Joke>.Load();
Console.WriteLine($" {joke.punchline}");
}
catch(Exception ex)
{
Console.WriteLine($"Fayl bo'sh {ex.Message}");
}
Console.ReadKey();
}
static void Universities()
{
try
{
var universities = FileManager<List<University>>.Load();

//var webRequest = new HttpRequestMessage(HttpMethod.Get, "https://catfact.ninja/fact")
//{
// Content = new StringContent("{ 'some': 'value' }", Encoding.UTF8, "application/json")
//};
Console.WriteLine("Universities : ");

//var response = client.Send(webRequest);
for (int i = 0; i<universities.Count; i++)
{
Console.WriteLine((i+1) + universities[i].name);
}
Console.WriteLine("Choose the University ID number");

//using var reader = new StreamReader(response.Content.ReadAsStream());
//string json = reader.ReadToEnd();
int.TryParse(Console.ReadLine(), out int choose);

//FactModel result = JsonSerializer.Deserialize<FactModel>(json);
GetInformationUniverity(universities[--choose]);
}
catch(Exception ex)
{
Console.WriteLine("Nimadir xato" + ex.Message);
}

//Console.WriteLine(result?.fact);
}
static void GetInformationUniverity(University university)
{
Console.WriteLine($"Name : {university.name}");
Console.WriteLine($"Name : {university.country}");
Console.WriteLine($"Name : {university.alpha_two_code}");
Console.WriteLine($"Name : {university.stateprovince}");

#endregion
Console.WriteLine("Web pages : ");
foreach(var i in university.web_pages)
{
Console.WriteLine(i);
}

Console.WriteLine("Domains : ");
foreach (var i in university.domains)
{
Console.WriteLine(i);
}
Console.ReadKey();
}
static void Bitcoin()
{
var coinDesk = FileManager<CoinDesk>.Load();

#region CoinDesk
Console.WriteLine("1.Bitcoin in USD");
Console.WriteLine("2.Bitcoin in EUR");
Console.WriteLine("3.Bitcoin in GBP");

switch (ChooseMenu())
{
case 1: BitcoinUSD(coinDesk.bpi); break;
case 2: BitcoinEUR(coinDesk.bpi); break;
case 3: BitcoinGBP(coinDesk.bpi); break;
default:return;
}
Console.ReadKey();
}
static void BitcoinEUR(Bpi bpi)
{
Console.WriteLine($"Code : {bpi.EUR.code}");
Console.WriteLine($"Rate : {bpi.EUR.rate}");
Console.WriteLine($"Description {bpi.EUR.description}");
Console.WriteLine($"Symbol : {bpi.EUR.symbol}");
Console.WriteLine($"Rate float : {bpi.EUR.rate_float}");
}
static void BitcoinUSD(Bpi bpi)
{
Console.WriteLine($"Code : {bpi.USD.code}");
Console.WriteLine($"Rate : {bpi.USD.rate}");
Console.WriteLine($"Description {bpi.USD.description}");
Console.WriteLine($"Symbol : {bpi.USD.symbol}");
Console.WriteLine($"Rate float : {bpi.USD.rate_float}");
}
static void BitcoinGBP(Bpi bpi)
{
Console.WriteLine($"Code : {bpi.GBP.code}");
Console.WriteLine($"Rate : {bpi.GBP.rate}");
Console.WriteLine($"Description {bpi.GBP.description}");
Console.WriteLine($"Symbol : {bpi.GBP.symbol}");
Console.WriteLine($"Rate float : {bpi.GBP.rate_float}");
}
static T HttpRequest<T>(string webPath)
{
var client = new HttpClient();

var webRequest = new HttpRequestMessage(HttpMethod.Get, "https://api.coindesk.com/v1/bpi/currentprice.json")
var webrequest = new HttpRequestMessage(HttpMethod.Get, webPath)
{
Content = new StringContent("", Encoding.UTF8, "application/json")
Content = new StringContent("", Encoding.UTF8, "application/json")
};
var response = client.Send(webrequest);

var response = client.Send(webRequest);

using var reader = new StreamReader(response.Content.ReadAsStream());
string json = reader.ReadToEnd();

CoinDesk result = JsonConvert.DeserializeObject<CoinDesk>(json);


Console.WriteLine($"Last time Bitcoin was updated: {result.time.updatedISO}");
T result = JsonConvert.DeserializeObject<T>(json);
return result;

#endregion
}
}
}
21 changes: 21 additions & 0 deletions Lesson01/Lesson01/Root.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lesson01
{
public class University
{
public string stateprovince { get; set; }
public string country { get; set; }
public List<string> domains { get; set; }
public List<string> web_pages { get; set; }
public string alpha_two_code { get; set; }
public string name { get; set; }


}
}
31 changes: 31 additions & 0 deletions Lesson01/Lesson01/Zippotaman.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Lesson01
{

// Root myDeserializedClass = JsonConvert.DeserializeObject<Root>(myJsonResponse);
public class Place
{
public string placename { get; set; }
public string longitude { get; set; }
public string state { get; set; }

public string stateabbreviation { get; set; }
public string latitude { get; set; }
}

public class Zippotaman
{
public string postcode { get; set; }
public string country { get; set; }

public string countryabbreviation { get; set; }
public List<Place> places { get; set; }
}


}