-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
62 lines (54 loc) · 2.12 KB
/
Program.cs
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Threading.Tasks;
using System.Globalization;
using System.Linq;
using System.Text.RegularExpressions;
using System.Collections.Generic;
namespace CityFinder
{
public class Program
{
static async Task Main(string[] args)
{
// Set up api request
List<String> worldCountries = GetCountryCodes();
while (true)
{
Console.Write($"Please enter 2-letter ISO country code (https://www.countrycode.org/) \n-> ");
// Get user input
string countryCode = Console.ReadLine().ToUpper();
if (!worldCountries.Contains(countryCode))
{
Console.WriteLine($"Country code is incorrect. Try again.");
continue;
}
Console.Write($"Please enter alphanumeric zip/postal code\n-> ");
string zipCode = Console.ReadLine();
if (!Regex.IsMatch(zipCode, "^[a-zA-Z0-9]*$"))
{
Console.WriteLine($"Your input contained invalid characters");
continue;
}
var response = await CityProcessor.FindCity(countryCode, zipCode);
Console.WriteLine($"{response.CityEn}");
Console.WriteLine($"Press any key to try again.");
Console.ReadLine();
Console.Clear();
continue;
}
}
private static List<String> GetCountryCodes()
{
List<CultureInfo> worldCountries = CultureInfo.GetCultures(CultureTypes.SpecificCultures).ToList<CultureInfo>();
List<String> listOfCountryCodes = new List<string>();
foreach (var country in worldCountries)
{
listOfCountryCodes.Add(new RegionInfo(country.LCID).TwoLetterISORegionName);
}
listOfCountryCodes = listOfCountryCodes.Distinct().ToList();
listOfCountryCodes.Sort();
listOfCountryCodes.RemoveAll(code => code.Length > 2);
return listOfCountryCodes;
}
}
}