Advertisement
beinsaduno

E03P!rates

Mar 23rd, 2021
401
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.util.*;
  2.  
  3. public class Demo {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         String command;
  8.         String[] commandParts;
  9.         Map<String, int[]> map = new TreeMap<>();
  10.  
  11.         while (!"Sail".equals(command = scanner.nextLine())) {
  12.             commandParts = command.split("(\\s+)?(\\|{2})(\\s+)?");
  13.             String town = commandParts[0];
  14.             int population = Integer.parseInt(commandParts[1]);
  15.             int gold = Integer.parseInt(commandParts[2]);
  16.  
  17.             map.putIfAbsent(town, new int[2]);
  18.             map.get(town)[0] += population;
  19.             map.get(town)[1] += gold;
  20.         }
  21.  
  22.         while (!"End".equals(command = scanner.nextLine())) {
  23.             commandParts = command.split("(\\s+)?(=>)(\\s+)?");
  24.             String events = commandParts[0];
  25.             String city = commandParts[1];
  26.  
  27.             switch (events) {
  28.                 case "Plunder":
  29.                     int people = Integer.parseInt(commandParts[2]);
  30.                     int gold = Integer.parseInt(commandParts[3]);
  31.  
  32.                     System.out.printf("%s plundered! %d gold stolen, %d citizens killed.%n", city,
  33.                             gold, people);
  34.                     map.get(city)[0] -= people;
  35.                     map.get(city)[1] -= gold;
  36.                     if (map.get(city)[0] == 0 || map.get(city)[1] == 0) {
  37.                         map.remove(city);
  38.                         System.out.printf("%s has been wiped off the map!%n", city);
  39.                     }
  40.                     break;
  41.                 case "Prosper":
  42.                     gold = Integer.parseInt(commandParts[2]);
  43.  
  44.                     if (gold < 0) {
  45.                         System.out.println("Gold added cannot be a negative number!");
  46.                     } else {
  47.                         map.get(city)[1] += gold;
  48.                         System.out.printf("%d gold added to the city treasury. %s now has %d gold.%n",
  49.                                 gold, city, map.get(city)[1]);
  50.                     }
  51.                     break;
  52.                 default:
  53.                     break;
  54.             }
  55.         }
  56.  
  57.         if (map.isEmpty()) {
  58.             System.out.println("Ahoy, Captain! All targets have been plundered and destroyed!");
  59.         } else {
  60.             System.out.printf("Ahoy, Captain! There are %d wealthy settlements to go to:%n", map.size());
  61.             map.entrySet()
  62.                     .stream()
  63.                     .sorted((a, b) -> Integer.compare(b.getValue()[1], a.getValue()[1]))
  64.                     .forEach(e -> System.out.printf("%s -> Population: %d citizens, Gold: %d kg%n",
  65.                             e.getKey(), e.getValue()[0], e.getValue()[1]));
  66.         }
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement