From feb1fbf2829f827dd77ff817d3eae3b7470f0544 Mon Sep 17 00:00:00 2001 From: Josh Creek Date: Sun, 23 May 2021 21:38:49 +0100 Subject: [PATCH] feat(*): Handle getting the user's input country code --- CityFinder/Program.cs | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) diff --git a/CityFinder/Program.cs b/CityFinder/Program.cs index e639879..4b300a2 100644 --- a/CityFinder/Program.cs +++ b/CityFinder/Program.cs @@ -42,6 +42,7 @@ namespace CityFinder { InitialSetup(); + string countryInput = GetUserCountryInput(); /// /// This method sets up the json config file and sets the user-agent header on the HttpClient. /// @@ -57,6 +58,44 @@ namespace CityFinder client.DefaultRequestHeaders.Add("User-Agent", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/90.0.4430.212 Safari/537.36"); } + /// + /// This method gets the user's input country code safely. + /// + /// Returns a valid country code. + private static string GetUserCountryInput() + { + Console.WriteLine("Do you know the valid country code you need?"); + string userResponse = Console.ReadLine(); + if (userResponse.ToLower() != "y") + { + ListCountryCodes(); + } + + bool IsValidCountryInput = false; + string countryInput = string.Empty; + + while (!IsValidCountryInput) + { + Console.WriteLine("Enter a country code:"); + countryInput = Console.ReadLine(); + + try + { + // Check that it's a valid country code by initialising a RegionInfo object + RegionInfo regionInfo = new RegionInfo(countryInput); + IsValidCountryInput = true; + } + catch (Exception) + { + Console.WriteLine($"{countryInput} is not a valid country code. Please press any key then find your desired country code from this table."); + Console.ReadKey(); + ListCountryCodes(); + } + } + + return countryInput; + } + /// /// This method displays a table of valid country codes. ///