예제 #1
0
        private static async Task ProcessMonarchs()
        {
            var streamTask = client.GetStreamAsync("https://gist.githubusercontent.com/christianpanton/10d65ccef9f29de3acd49d97ed423736/raw/b09563bc0c4b318132c7a738e679d4f984ef0048/kings");
            var monarchs   = await JsonSerializer.DeserializeAsync <List <Monarch> >(await streamTask);

            foreach (var monarch in monarchs)
            {
                monarch.period = await CalculateNumberOfYears(monarch);
            }

            MonarchCollection collection     = new  MonarchCollection(monarchs);
            Monarch           longestMonarch = collection.GetLongestRuledMonarch();

            Console.WriteLine($"How many monarchs are there in the list?: {collection.CountItems()}");
            Console.WriteLine($"Which monarch ruled the longest (and for how long)?: {longestMonarch.name}, {(longestMonarch.period)} years");
            Console.WriteLine($"Which house ruled the longest (and for how long)?: {longestMonarch.house}, {(longestMonarch.period)} years");
            Console.WriteLine($"What was the most common first name?: {collection.GetMostCommonName()}");
        }
예제 #2
0
        private static async Task <int> CalculateNumberOfYears(Monarch monarch)
        {
            bool hasDash = monarch.years.Contains('-');

            if (hasDash)
            {
                var year = monarch.years.Split('-');
                if (String.IsNullOrEmpty(year[1]))
                {
                    return(DateTime.Now.Year - Int32.Parse(year[0]));
                }
                else
                {
                    return(Int32.Parse(year[1]) - Int32.Parse(year[0]));
                }
            }
            else
            {
                return(1);
            }
        }