RGAlex

O3. Pirates / FinalExam - O5

Mar 16th, 2021 (edited)
225
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.50 KB | None | 0 0
  1. package FinalExam.O5;
  2.  
  3. import java.util.Map;
  4. import java.util.Scanner;
  5. import java.util.TreeMap;
  6.  
  7. public class O3Pirates {
  8.     public static void main(String[] args) {
  9.  
  10.         Scanner scanner = new Scanner(System.in);
  11.         String input;
  12.         Map<String, int[]> towns = new TreeMap<>();
  13.  
  14.         while (!"Sail".equals(input = scanner.nextLine())) {
  15.             String[] town = input.split("\\|\\|");
  16.             towns.putIfAbsent(town[0], new int[2]);
  17.             towns.get(town[0])[0] += Integer.parseInt(town[1]);
  18.             towns.get(town[0])[1] += Integer.parseInt(town[2]);
  19.         }
  20.         String text = scanner.nextLine();
  21.         while (!"End".equals(text)) {
  22.             String[] command = text.split("=>");
  23.             switch (command[0]) {
  24.                 case "Plunder":
  25.                     System.out.printf("%s plundered! %d gold stolen, %d citizens killed.%n", command[1],
  26.                             Integer.parseInt(command[3]), Integer.parseInt(command[2]));
  27.                     towns.get(command[1])[0] -= Integer.parseInt(command[2]);
  28.                     towns.get(command[1])[1] -= Integer.parseInt(command[3]);
  29.                     if (towns.get(command[1])[0] == 0 || towns.get(command[1])[1] == 0) {
  30.                         towns.remove(command[1]);
  31.                         System.out.printf("%s has been wiped off the map!%n", command[1]);
  32.                     }
  33.                     break;
  34.                 case "Prosper":
  35.                     if (Integer.parseInt(command[2]) < 0) {
  36.                         System.out.println("Gold added cannot be a negative number!");
  37.                     } else {
  38.                         towns.get(command[1])[1] += Integer.parseInt(command[2]);
  39.                         System.out.printf("%d gold added to the city treasury. %s now has %d gold.%n", Integer.parseInt(command[2]), command[1], towns.get(command[1])[1]);
  40.                     }
  41.                     break;
  42.             }
  43.             text = scanner.nextLine();
  44.         }
  45.         if (towns.isEmpty()) {
  46.             System.out.println("Ahoy, Captain! All targets have been plundered and destroyed!");
  47.         } else {
  48.             System.out.printf("Ahoy, Captain! There are %d wealthy settlements to go to:%n", towns.size());
  49.             towns.entrySet().stream().sorted((one, two) -> Integer.compare(two.getValue()[1], one.getValue()[1])).forEach(e -> System.out.printf("%s -> Population: %d citizens, Gold: %d kg%n", e.getKey(), e.getValue()[0], e.getValue()[1]));
  50.         }
  51.     }
  52. }
  53.  
  54.  
Advertisement
Add Comment
Please, Sign In to add comment