Advertisement
Ivelin_1936

Population Counter

May 22nd, 2018
104
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.76 KB | None | 0 0
  1. import java.io.BufferedReader;
  2. import java.io.IOException;
  3. import java.io.InputStreamReader;
  4. import java.util.HashMap;
  5.  
  6. public class PopulationCounter {
  7.     public static void main(String[] args) throws IOException {
  8.         BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
  9.  
  10.         HashMap<String, String> cityCountry = new HashMap<>();
  11.         HashMap<String, Long> countryTotalPop = new HashMap<>();
  12.         HashMap<String, Long> cityPop = new HashMap<>();
  13.  
  14.         String input = reader.readLine();
  15.  
  16.         while (!input.equals("report"))
  17.         {
  18.             String[] inputArr = input.split("\\|") ;
  19.             String country = inputArr[1];
  20.             String city = inputArr[0];
  21.             long population = Long.parseLong(inputArr[2]);
  22.  
  23.             AddCityCountry(cityCountry, country, city);
  24.             AddCountryAndTotalPopulation(countryTotalPop, country, population);
  25.             AddCityPop(cityPop, city, population);
  26.  
  27.             input = reader.readLine();
  28.         }
  29.  
  30.         PrintResult(cityCountry, countryTotalPop, cityPop);
  31.  
  32.     }
  33.  
  34.     private static void PrintResult(HashMap<String, String> cityCountry, HashMap<String, Long> countryTotalPop, HashMap<String, Long> cityPop) {
  35.         countryTotalPop.entrySet().stream().sorted((a,b) -> b.getValue().compareTo(a.getValue())).forEach(cP -> {
  36.             System.out.println(String.format("%s (total population: %d)", cP.getKey(), cP.getValue()));
  37.  
  38.             cityPop.entrySet().stream().sorted((x,y) -> y.getValue().compareTo(x.getValue())).forEach(a -> {
  39.                 if (cityCountry.get(a.getKey()).equals(cP.getKey())) {
  40.                     System.out.println(String.format("=>%s: %d", a.getKey(), a.getValue()));
  41.                 }
  42.             });
  43.  
  44.         });
  45.     }
  46.  
  47.     private static void AddCityCountry(HashMap<String, String> cityCountry, String country, String city) {
  48.         if (!cityCountry.containsKey(city)) {
  49.             cityCountry.put(city, country);
  50.         }
  51.     }
  52.  
  53.     private static void AddCityPop(HashMap<String, Long> cityPop, String city, long population)
  54.     {
  55.         if (!cityPop.containsKey(city)) {
  56.             cityPop.put(city, population);
  57.         } else {
  58.             long value = cityPop.get(city);
  59.             value += population;
  60.             cityPop.replace(city, population);
  61.         }
  62.  
  63.     }
  64.  
  65.     private static void AddCountryAndTotalPopulation(HashMap<String, Long> countryTotalPop, String country, long population)
  66.     {
  67.         if (countryTotalPop.containsKey(country))
  68.         {
  69.             long value = countryTotalPop.get(country);
  70.             value += population;
  71.             countryTotalPop.replace(country, value);
  72.         } else {
  73.             countryTotalPop.put(country, population);
  74.         }
  75.     }
  76. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement