borovaneca

PopulationCounter

Feb 21st, 2023
60
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.86 KB | None | 0 0
  1. package Advance.SetsAndMaps.Exercise;
  2.  
  3. import java.util.*;
  4. import java.util.regex.Matcher;
  5. import java.util.regex.Pattern;
  6.  
  7. public class PopulationCounter {
  8.     public static void main(String[] args) {
  9.         Scanner scanner = new Scanner(System.in);
  10.  
  11.  
  12.         LinkedHashMap<String, LinkedHashMap<String, Integer>> countryMap = new LinkedHashMap<>();
  13.         TreeMap<String, Integer> populationCounter = new TreeMap<>();
  14.         String regex = "(?<city>[A-Za-z]+[\\s]?[A-Za-z]*)\\|(?<country>[A-Za-z\\s]*)\\|(?<population>[\\d]+)";
  15.         Pattern pattern = Pattern.compile(regex);
  16.  
  17.         String input = scanner.nextLine();
  18.  
  19.         while (!"report".equals(input)) {
  20.             Matcher matcher = pattern.matcher(input);
  21.             while (matcher.find()) {
  22.                 String country = matcher.group("country");
  23.                 String city = matcher.group("city");
  24.                 int population = Integer.parseInt(matcher.group("population"));
  25.  
  26.                 if (!countryMap.containsKey(country)) {
  27.                     countryMap.put(country, new LinkedHashMap<>());
  28.                     countryMap.get(country).put(city, population);
  29.                     populationCounter.put(country, population);
  30.                 } else {
  31.                     if (!countryMap.get(country).containsKey(city)) {
  32.                         countryMap.get(country).put(city, population);
  33.                         populationCounter.put(country, populationCounter.get(country) + population);
  34.                     } else {
  35.                         countryMap.get(country).put(city, population);
  36.                         populationCounter.put(country, populationCounter.get(country) + population);
  37.                     }
  38.                 }
  39.             }
  40.  
  41.             input = scanner.nextLine();
  42.         }
  43.  
  44.         List<Integer> descendingList = new ArrayList<>();
  45.         for (Map.Entry<String, Integer> item : populationCounter.entrySet()) {
  46.             descendingList.add(item.getValue());
  47.         }
  48.         descendingList.sort(Collections.reverseOrder());
  49.         boolean printed = false;
  50.  
  51.         for (int i = 0; i < descendingList.size(); i++) {
  52.             printed = false;
  53.  
  54.             for (Map.Entry<String, Integer> populationEntry : populationCounter.entrySet()) {
  55.                 String countryName = populationEntry.getKey();
  56.                 int countryPopulation = populationEntry.getValue();
  57.                 if (countryPopulation == descendingList.get(i)) {
  58.                     System.out.printf("%s (total population: %d)%n", countryName, countryPopulation);
  59.                     for (Map.Entry<String, LinkedHashMap<String, Integer>> countryEntry : countryMap.entrySet()) {
  60.                         if (countryName.equals(countryEntry.getKey())) {
  61.                             List<Integer> cityList = new ArrayList<>();
  62.                             for (Map.Entry<String, Integer> citySet : countryEntry.getValue().entrySet()) {
  63.                                 cityList.add(citySet.getValue());
  64.                             }
  65.                             cityList.sort(Collections.reverseOrder());
  66.                             for (int j = 0; j < cityList.size(); j++) {
  67.                                 for (Map.Entry<String, Integer> printCity : countryEntry.getValue().entrySet()) {
  68.                                     if (printCity.getValue() == cityList.get(j)) {
  69.                                         System.out.printf("=>%s %d%n", printCity.getKey(), printCity.getValue());
  70.                                 }
  71.  
  72.                             }
  73.                         }
  74.                             printed = true;
  75.  
  76.                     }
  77.                         if (printed) {
  78.                             break;
  79.                         }
  80.                 }
  81.             }
  82.                 if (printed) {
  83.                     break;
  84.                 }
  85.         }
  86.  
  87.     }
  88.  
  89.  
  90. }
  91. }
  92.  
Advertisement
Add Comment
Please, Sign In to add comment