From b4facf99eb568b68c04f8a83ddb68b58eb9a6833 Mon Sep 17 00:00:00 2001 From: FirdavsAX Date: Fri, 20 Oct 2023 22:35:18 +0500 Subject: [PATCH 1/5] _ --- Lesson01/Lesson01/Joke.cs | 19 ++++ Lesson01/Lesson01/Program.cs | 156 +++++++++++++++++++++++++++----- Lesson01/Lesson01/Root.cs | 21 +++++ Lesson01/Lesson01/Zippotaman.cs | 31 +++++++ 4 files changed, 205 insertions(+), 22 deletions(-) create mode 100644 Lesson01/Lesson01/Joke.cs create mode 100644 Lesson01/Lesson01/Root.cs create mode 100644 Lesson01/Lesson01/Zippotaman.cs 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..21f584c 100644 --- a/Lesson01/Lesson01/Program.cs +++ b/Lesson01/Lesson01/Program.cs @@ -1,5 +1,6 @@ using Lesson01.Models.CoinDesk; using Newtonsoft.Json; +using System.Reflection.Metadata.Ecma335; using System.Text; namespace Lesson01 @@ -8,46 +9,157 @@ internal class Program { static void Main(string[] args) { - #region Cat facts + ShowMenu(); + Main(args); + } + static void ShowMenu() + { + Console.Clear(); + Console.WriteLine("1.Get informations BitCoin"); + Console.WriteLine("2.Get information Universities in USA"); + Console.WriteLine("3.Get any jokes"); + Console.WriteLine("4.Show zippotaman"); + Menu(); + } + static int ChooseMenu() + { + Console.Write("Choose the menu : "); - //var client = new HttpClient(); + int.TryParse(Console.ReadLine(), out int choose); + Console.Clear(); + return choose; + } + static void Menu() + { + switch (ChooseMenu()) + { + case 1: Bitcoin();break; + case 2: Universities();break; + case 3: Jokes();break; + case 4: ZippotamanShow();break; + default: break; + } + } + static void ZippotamanShow() + { + Zippotaman zippotaman = HttpRequest("https://api.zippopotam.us/us/33162"); - //var webRequest = new HttpRequestMessage(HttpMethod.Get, "https://catfact.ninja/fact") - //{ - // Content = new StringContent("{ 'some': 'value' }", Encoding.UTF8, "application/json") - //}; + Console.WriteLine("Places : "); + foreach (var i in zippotaman.places) + { + Console.WriteLine($"{i.latitude}"); + Console.WriteLine($"{i.longitude}"); + Console.WriteLine($"{i.placename}"); + Console.WriteLine($"{i.state}"); + Console.WriteLine($"{i.stateabbreviation}"); + } + Console.ReadKey(); + } + static void Jokes() + { + Console.Clear(); - //var response = client.Send(webRequest); + Joke joke = HttpRequest("https://official-joke-api.appspot.com/random_joke"); - //using var reader = new StreamReader(response.Content.ReadAsStream()); - //string json = reader.ReadToEnd(); + Console.WriteLine($" {joke.punchline}"); - //FactModel result = JsonSerializer.Deserialize(json); + Console.ReadKey(); + } + static void Universities() + { + Console.WriteLine("Universities in USA : "); - //Console.WriteLine(result?.fact); + List universities = HttpRequest>("http://universities.hipolabs.com/search?country=United+States"); + - #endregion + for(int i = 0; i("https://api.coindesk.com/v1/bpi/currentprice.json"); + + 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, webPath) + { + 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; } + } + + +} From 3b7a1c8f43ea34fb9b82711502cf3c2f1b4e1863 Mon Sep 17 00:00:00 2001 From: FirdavsAX Date: Fri, 20 Oct 2023 22:35:28 +0500 Subject: [PATCH 2/5] - --- Lesson01/Lesson01/Program.cs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lesson01/Lesson01/Program.cs b/Lesson01/Lesson01/Program.cs index 21f584c..9a8e06e 100644 --- a/Lesson01/Lesson01/Program.cs +++ b/Lesson01/Lesson01/Program.cs @@ -10,7 +10,7 @@ internal class Program static void Main(string[] args) { ShowMenu(); - Main(args); + Main(args); } static void ShowMenu() { From 74f37e887843bbfa20a84a1527254c559d44fee0 Mon Sep 17 00:00:00 2001 From: FirdavsAX Date: Sat, 21 Oct 2023 17:51:09 +0500 Subject: [PATCH 3/5] Web requests with Threads --- Lesson01/Lesson01/FileManager.cs | 43 ++++++++++++ Lesson01/Lesson01/Program.cs | 111 +++++++++++++++++++++---------- 2 files changed, 119 insertions(+), 35 deletions(-) create mode 100644 Lesson01/Lesson01/FileManager.cs 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/Program.cs b/Lesson01/Lesson01/Program.cs index 9a8e06e..e0a03d1 100644 --- a/Lesson01/Lesson01/Program.cs +++ b/Lesson01/Lesson01/Program.cs @@ -10,15 +10,58 @@ internal class Program static void Main(string[] args) { ShowMenu(); - Main(args); + Main(args); } + #region BitCoin + static void ThreadMakerBitcoin() + { + Thread thread1 = new Thread(DownloadBitcoinData); + thread1.Start(); + } + static void DownloadBitcoinData() + { + var coinDesk = HttpRequest("https://api.coindesk.com/v1/bpi/currentprice.json"); + + FileManager.Save(coinDesk); + } + #endregion + #region University + static void ThreadMakerUniversities() + { + Thread thread2 = new Thread(DownloadUniversitiesData); + thread2.Start(); + } + static void DownloadUniversitiesData() + { + List universities = HttpRequest>("http://universities.hipolabs.com/search?country=United+States"); + + 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"); + + FileManager.Save(joke); + } + #endregion + static void ShowMenu() { Console.Clear(); - Console.WriteLine("1.Get informations BitCoin"); - Console.WriteLine("2.Get information Universities in USA"); - Console.WriteLine("3.Get any jokes"); - Console.WriteLine("4.Show zippotaman"); + Console.WriteLine("1.Get old informations BitCoin"); + Console.WriteLine("2.Get old information Universities in USA"); + 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() @@ -33,54 +76,52 @@ 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; - case 4: ZippotamanShow();break; default: break; } } - static void ZippotamanShow() + static void Jokes() { - Zippotaman zippotaman = HttpRequest("https://api.zippopotam.us/us/33162"); - - Console.WriteLine("Places : "); - foreach (var i in zippotaman.places) + Console.Clear(); + try { - Console.WriteLine($"{i.latitude}"); - Console.WriteLine($"{i.longitude}"); - Console.WriteLine($"{i.placename}"); - Console.WriteLine($"{i.state}"); - Console.WriteLine($"{i.stateabbreviation}"); + var joke = FileManager.Load(); + Console.WriteLine($" {joke.punchline}"); + } + catch(Exception ex) + { + Console.WriteLine($"Fayl bo'sh {ex.Message}"); } Console.ReadKey(); } - static void Jokes() + static void Universities() { - Console.Clear(); + try + { - Joke joke = HttpRequest("https://official-joke-api.appspot.com/random_joke"); + var universities = FileManager>.Load(); - Console.WriteLine($" {joke.punchline}"); + Console.WriteLine("Universities in USA : "); - Console.ReadKey(); - } - static void Universities() - { - Console.WriteLine("Universities in USA : "); + for (int i = 0; i universities = HttpRequest>("http://universities.hipolabs.com/search?country=United+States"); - + int.TryParse(Console.ReadLine(), out int choose); - for(int i = 0; i("https://api.coindesk.com/v1/bpi/currentprice.json"); + var coinDesk = FileManager.Load(); Console.WriteLine("1.Bitcoin in USD"); Console.WriteLine("2.Bitcoin in EUR"); From 7d769d637ba2d2ed1ad237aa51d65eba0289fcc5 Mon Sep 17 00:00:00 2001 From: FirdavsAX Date: Sat, 21 Oct 2023 18:34:29 +0500 Subject: [PATCH 4/5] Get choose country in universities --- Lesson01/Lesson01/Program.cs | 23 ++++++++++++++++------- 1 file changed, 16 insertions(+), 7 deletions(-) diff --git a/Lesson01/Lesson01/Program.cs b/Lesson01/Lesson01/Program.cs index e0a03d1..a627ce3 100644 --- a/Lesson01/Lesson01/Program.cs +++ b/Lesson01/Lesson01/Program.cs @@ -3,12 +3,14 @@ using System.Reflection.Metadata.Ecma335; using System.Text; + namespace Lesson01 { internal class Program { static void Main(string[] args) { + ShowMenu(); Main(args); } @@ -27,15 +29,23 @@ static void DownloadBitcoinData() #endregion #region University static void ThreadMakerUniversities() - { + { + + Console.WriteLine("enter country : "); + string country = Console.ReadLine(); + Thread thread2 = new Thread(DownloadUniversitiesData); - thread2.Start(); + thread2.Start(country); + } - static void DownloadUniversitiesData() + static void DownloadUniversitiesData(object country) { - List universities = HttpRequest>("http://universities.hipolabs.com/search?country=United+States"); + country = (string)country; + List universities = HttpRequest>($"http://universities.hipolabs.com/search?country={country}"); FileManager>.Save(universities); + + Console.ReadKey(); } #endregion #region Joke @@ -56,7 +66,7 @@ static void ShowMenu() { Console.Clear(); Console.WriteLine("1.Get old informations BitCoin"); - Console.WriteLine("2.Get old information Universities in USA"); + 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"); @@ -103,10 +113,9 @@ static void Universities() { try { - var universities = FileManager>.Load(); - Console.WriteLine("Universities in USA : "); + Console.WriteLine("Universities : "); for (int i = 0; i Date: Sat, 21 Oct 2023 18:59:14 +0500 Subject: [PATCH 5/5] Add locker and monitor --- Lesson01/Lesson01/Program.cs | 24 +++++++++++++++--------- 1 file changed, 15 insertions(+), 9 deletions(-) diff --git a/Lesson01/Lesson01/Program.cs b/Lesson01/Lesson01/Program.cs index a627ce3..8a55e8b 100644 --- a/Lesson01/Lesson01/Program.cs +++ b/Lesson01/Lesson01/Program.cs @@ -1,13 +1,14 @@ 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) { @@ -22,9 +23,11 @@ static void ThreadMakerBitcoin() } static void DownloadBitcoinData() { - var coinDesk = HttpRequest("https://api.coindesk.com/v1/bpi/currentprice.json"); + Monitor.Enter(locker); + var coinDesk = HttpRequest("https://api.coindesk.com/v1/bpi/currentprice.json"); - FileManager.Save(coinDesk); + FileManager.Save(coinDesk); + Monitor.Exit(locker); } #endregion #region University @@ -40,12 +43,13 @@ static void ThreadMakerUniversities() } static void DownloadUniversitiesData(object country) { - country = (string)country; - List universities = HttpRequest>($"http://universities.hipolabs.com/search?country={country}"); - - FileManager>.Save(universities); + lock (locker) + { + country = (string)country; + List universities = HttpRequest>($"http://universities.hipolabs.com/search?country={country}"); - Console.ReadKey(); + FileManager>.Save(universities); + } } #endregion #region Joke @@ -58,7 +62,9 @@ static void DownloadJokeData() { Joke joke = HttpRequest("https://official-joke-api.appspot.com/random_joke"); + Monitor.Enter(locker); FileManager.Save(joke); + Monitor.Exit(locker); } #endregion