Exemple #1
0
        private static void GetAllCountries(string filePath)
        {
            CsvReader      reader    = new CsvReader(filePath);
            List <Country> countries = reader.ReadAllCountriesToList();

            // This is an example code that inserts and then immediately removes the country Lilliput, for studying purposes.
            Country lilliput      = new Country("Lilliput", "LIL", "Somewhere", 2_000_000);
            int     lilliputIndex = countries.FindIndex(x => x.Population < 2_000_000);

            countries.Insert(lilliputIndex, lilliput);
            countries.RemoveAt(lilliputIndex);
            //

            int maxToDisplay = AskForMaxToDisplay();

            for (int i = 0; i < countries.Count; i++)
            {
                if (i > 0 && (i % maxToDisplay == 0))
                {
                    Console.WriteLine("Hit return to continue, or anything else to quit");
                    if (Console.ReadLine() != "")
                    {
                        break;
                    }
                }

                Country country = countries[i];
                Console.WriteLine($"{i + 1}: {PopulationFormatter.FormatPopulation(country.Population).PadLeft(15)}: {country.Name}");
            }
        }
Exemple #2
0
        private static void GetFirstNCountries(string filePath)
        {
            CsvReader reader = new CsvReader(filePath);

            Country[] countries = reader.ReadFirstNCountries();

            foreach (Country country in countries)
            {
                Console.WriteLine($"{PopulationFormatter.FormatPopulation(country.Population).PadLeft(15)}: {country.Name}");
            }
        }
Exemple #3
0
        private static void GetCountryByCode(string filePath)
        {
            CsvReader reader = new CsvReader(filePath);
            Dictionary <string, Country> countries = reader.ReadAllCountriesToDictionary();

            Console.WriteLine("Which country code do you want to look up? ");
            string userInput = Console.ReadLine();

            bool gotCountry = countries.TryGetValue(userInput.ToUpper(), out Country country);

            if (!gotCountry)
            {
                Console.WriteLine($"Sorry, couldn't find country with code {userInput}");
            }
            else
            {
                Console.WriteLine($"{country.Name} has population {PopulationFormatter.FormatPopulation(country.Population)}");
            }
        }
Exemple #4
0
        private static void GetAllCountriesInReverseOrder(string filePath)
        {
            CsvReader      reader    = new CsvReader(filePath);
            List <Country> countries = reader.ReadAllCountriesToList();

            int maxToDisplay = AskForMaxToDisplay();

            for (int i = countries.Count - 1; i >= 0; i--)
            {
                int displayIndex = countries.Count - 1 - i;
                if (displayIndex > 0 && (displayIndex % maxToDisplay == 0))
                {
                    Console.WriteLine("Hit return to continue, or anything else to quit");
                    if (Console.ReadLine() != "")
                    {
                        break;
                    }
                }

                Country country = countries[i];
                Console.WriteLine($"{displayIndex + 1}: {PopulationFormatter.FormatPopulation(country.Population).PadLeft(15)}: {country.Name}");
            }
        }