예제 #1
0
    static void Main(string[] args)
    {
        List<Country> countryCollection = new List<Country>();

        while (true)
        {
            string[] currentLine = Console.ReadLine().Split('|');

            if (currentLine[0] == "report")
            {
                break;
            }

            string countryName = currentLine[1];
            string cityName = currentLine[0];
            long population = long.Parse(currentLine[2]);

            City currentCity = new City(cityName, population);
            Country currentCountry = new Country(countryName);

            if (!countryCollection.ContainsCountry(currentCountry))
            {
                currentCountry.Cities.Add(currentCity);
                countryCollection.Add(currentCountry);
            }
            else
            {
                if (!countryCollection.Country(currentCountry).Cities.ContainsCity(currentCity))
                {
                    countryCollection.Country(currentCountry).Cities.Add(currentCity);
                }
                else
                {
                    countryCollection.Country(currentCountry).Cities.City(currentCity).Population += population;
                }
            }
        }

        var sortedCountries = countryCollection.OrderByDescending(x => x.TotalPopulation());

        foreach (Country currentCountry in sortedCountries)
        {
            Console.WriteLine("{0} (total population: {1})", currentCountry.Name, currentCountry.TotalPopulation());

            var sortedCities = currentCountry.Cities.OrderByDescending(x => x.Population).ThenBy(x => x.Name);

            foreach (City currentCity in sortedCities)
            {
                Console.WriteLine("=>{0}: {1}", currentCity.Name, currentCity.Population);
            }
        }
    }