diff --git a/Lesson01/Lesson01/FileManager.cs b/Lesson01/Lesson01/FileManager.cs new file mode 100644 index 0000000..858fe0d --- /dev/null +++ b/Lesson01/Lesson01/FileManager.cs @@ -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 + { + 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(json); + + return file; + } + } +} diff --git a/Lesson01/Lesson01/Joke.cs b/Lesson01/Lesson01/Joke.cs new file mode 100644 index 0000000..ae20edb --- /dev/null +++ b/Lesson01/Lesson01/Joke.cs @@ -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; } + } + + +} diff --git a/Lesson01/Lesson01/Program.cs b/Lesson01/Lesson01/Program.cs index 5559afb..8a55e8b 100644 --- a/Lesson01/Lesson01/Program.cs +++ b/Lesson01/Lesson01/Program.cs @@ -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("https://api.coindesk.com/v1/bpi/currentprice.json"); + + FileManager.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 universities = HttpRequest>($"http://universities.hipolabs.com/search?country={country}"); + + FileManager>.Save(universities); + } + } + #endregion + #region Joke + static void ThreadMakerJoke() + { + Thread thread3 = new Thread(DownloadJokeData); + thread3.Start(); + } + static void DownloadJokeData() + { + Joke joke = HttpRequest("https://official-joke-api.appspot.com/random_joke"); + + Monitor.Enter(locker); + FileManager.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.Load(); + Console.WriteLine($" {joke.punchline}"); + } + catch(Exception ex) + { + Console.WriteLine($"Fayl bo'sh {ex.Message}"); + } + Console.ReadKey(); + } + static void Universities() + { + try + { + var universities = FileManager>.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(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.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(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(json); - - - Console.WriteLine($"Last time Bitcoin was updated: {result.time.updatedISO}"); + T result = JsonConvert.DeserializeObject(json); + return result; - #endregion } } } \ No newline at end of file diff --git a/Lesson01/Lesson01/Root.cs b/Lesson01/Lesson01/Root.cs new file mode 100644 index 0000000..7618550 --- /dev/null +++ b/Lesson01/Lesson01/Root.cs @@ -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 domains { get; set; } + public List web_pages { get; set; } + public string alpha_two_code { get; set; } + public string name { get; set; } + + + } +} diff --git a/Lesson01/Lesson01/Zippotaman.cs b/Lesson01/Lesson01/Zippotaman.cs new file mode 100644 index 0000000..fead91a --- /dev/null +++ b/Lesson01/Lesson01/Zippotaman.cs @@ -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(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 places { get; set; } + } + + +}