G2A Many GEOs
SHARE
TWEET

03. P!rates

a guest Apr 4th, 2020 147 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. import java.util.LinkedHashMap;
  2. import java.util.Scanner;
  3.  
  4. public class Pirates {
  5.     public static void main(String[] args) {
  6.         Scanner scanner = new Scanner(System.in);
  7.  
  8.         String inputLines = scanner.nextLine().trim();
  9.  
  10.         LinkedHashMap<String, Integer> mapCityNameAndPopulation = new LinkedHashMap<>();
  11.         LinkedHashMap<String, Integer> mapCityNameAndGold = new LinkedHashMap<>();
  12.  
  13.         while (!inputLines.equals("Sail")) {
  14.             String[] tokens = inputLines.split("[|]{2}");
  15.  
  16.             String cityName = tokens[0].trim();
  17.             int cityPopulation = Integer.parseInt(tokens[1].trim());
  18.             int cityGold = Integer.parseInt(tokens[2].trim());
  19.  
  20.             mapCityNameAndPopulation.putIfAbsent(cityName, 0);
  21.             mapCityNameAndGold.putIfAbsent(cityName, 0);
  22.  
  23.             int cityCurrentPopulation = mapCityNameAndPopulation.get(cityName);
  24.             int cityCurrentGold = mapCityNameAndGold.get(cityName);
  25.  
  26.             mapCityNameAndPopulation.put(cityName, cityCurrentPopulation + cityPopulation);
  27.             mapCityNameAndGold.put(cityName, cityCurrentGold + cityGold);
  28.  
  29.             inputLines = scanner.nextLine().trim();
  30.         }
  31.  
  32.         String events = scanner.nextLine().trim();
  33.  
  34.         while (!events.equals("End")) {
  35.             String[] tokens = events.split("=>");
  36.  
  37.             String command = tokens[0].trim();
  38.  
  39.             switch (command) {
  40.                 case "Plunder": {
  41.                     String cityName = tokens[1].trim();
  42.                     int cityPopulation = Integer.parseInt(tokens[2].trim());
  43.                     int cityGold = Integer.parseInt(tokens[3].trim());
  44.                     System.out.println(String.format("%s plundered! %d gold stolen, %d citizens killed.",
  45.                             cityName, cityGold, cityPopulation));
  46.  
  47.                     int cityCurrentPopulation = mapCityNameAndPopulation.get(cityName);
  48.                     int cityCurrentGold = mapCityNameAndGold.get(cityName);
  49.  
  50.                     int cityPopulationAfterAttack = cityCurrentPopulation - cityPopulation;
  51.                     int cityGoldAfterAttack = cityCurrentGold - cityGold;
  52.  
  53.                     mapCityNameAndPopulation.put(cityName, cityPopulationAfterAttack);
  54.                     mapCityNameAndGold.put(cityName, cityGoldAfterAttack);
  55.  
  56.                     if (cityPopulationAfterAttack <= 0 || cityGoldAfterAttack <= 0) {
  57.                         mapCityNameAndPopulation.remove(cityName);
  58.                         mapCityNameAndGold.remove(cityName);
  59.                         System.out.println(String.format("%s has been wiped off the map!", cityName));
  60.                     }
  61.                 }
  62.                 break;
  63.                 case "Prosper": {
  64.                     String cityName = tokens[1].trim();
  65.                     int cityGold = Integer.parseInt(tokens[2].trim());
  66.  
  67.                     if (cityGold < 0) {
  68.                         System.out.println("Gold added cannot be a negative number!");
  69.                         events = scanner.nextLine();
  70.                         continue;
  71.                     }
  72.  
  73.                     int cityCurrentGold = mapCityNameAndGold.get(cityName);
  74.                     mapCityNameAndGold.put(cityName, cityCurrentGold + cityGold);
  75.  
  76.                     System.out.println(String.format("%d gold added to the city treasury. %s now has %d gold.",
  77.                             cityGold, cityName, mapCityNameAndGold.get(cityName)));
  78.                 }
  79.                 break;
  80.                 default:
  81.                     break;
  82.             }
  83.  
  84.             events = scanner.nextLine().trim();
  85.         }
  86.  
  87.         if (mapCityNameAndPopulation.size() > 0) {
  88.             System.out.println(String.format("Ahoy, Captain! There are %d wealthy settlements to go to:",
  89.                     mapCityNameAndPopulation.size()));
  90.             mapCityNameAndGold
  91.                     .entrySet()
  92.                     .stream()
  93.                     .sorted((cg1, cg2) -> {
  94.                         if (cg2.getValue() - cg1.getValue() == 0) {
  95.                             return cg1.getKey().compareTo(cg2.getKey());
  96.                         }
  97.                         return cg2.getValue() - cg1.getValue();
  98.                     })
  99.                     .forEach(cg -> System.out.println(String.format("%s -> Population: %d citizens, Gold: %d kg",
  100.                             cg.getKey(), mapCityNameAndPopulation.get(cg.getKey()), cg.getValue())));
  101.         } else {
  102.             System.out.println("Ahoy, Captain! All targets have been plundered and destroyed!");
  103.         }
  104.     }
  105. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top