Advertisement
Guest User

pirates

a guest
Apr 4th, 2020
177
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.89 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class piratesProblem {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         Map<String, List<Integer>> collection = new TreeMap<>();
  8.  
  9.         String input = scanner.nextLine();
  10.  
  11.         while (!input.equals("Sail")){
  12.             String [] tokens = input.split("\\|\\|");
  13.             String city = tokens[0];
  14.             int population =Integer.parseInt(tokens[1].trim());
  15.             int gold = Integer.parseInt(tokens[2].trim());
  16.  
  17.             if (!collection.containsKey(city)){
  18.                 List<Integer> listFodAdd = new ArrayList<>();
  19.                 collection.put(city,listFodAdd);
  20.                 collection.get(city).add(0,population);
  21.                 collection.get(city).add(1,gold);
  22.             }else {
  23.                 int oldPopulation = collection.get(city).get(0);
  24.                 int oldGold = collection.get(city).get(1);
  25.  
  26.                 collection.get(city).set(0,oldPopulation+population);
  27.                 collection.get(city).set(1,oldGold+gold);
  28.  
  29.             }
  30.  
  31.             input = scanner.nextLine();
  32.         }
  33.  
  34.  
  35.         String newInput = scanner.nextLine();
  36.  
  37.         while (!newInput.equals("End")){
  38.             String [] newTokens = newInput.split("=>");
  39.             String command = newTokens[0];
  40.             String town = newTokens[1];
  41.  
  42.             switch (command){
  43.                 case "Plunder":
  44.                     int people = Integer.parseInt(newTokens[2].trim());
  45.                     int gold = Integer.parseInt(newTokens[3].trim());
  46.  
  47.                     int oldPopulation = collection.get(town).get(0);
  48.                     int oldGold = collection.get(town).get(1);
  49.  
  50.                     collection.get(town).set(0,oldPopulation - people);
  51.                     collection.get(town).set(1,oldGold - gold);
  52.  
  53.                     if (collection.get(town).get(0) <= 0 || collection.get(town).get(1) <= 0){
  54.  
  55.                         collection.remove(town);
  56.                         System.out.println(String.format("%s has been wiped off the map!",town));
  57.                     }else {
  58.                         System.out.println(String.format("%s plundered! %d gold stolen," +
  59.                                 " %d citizens killed.",town,gold,
  60.                                 people));
  61.                     }
  62.  
  63.                     break;
  64.                 case "Prosper":
  65.                     int goldForAdd = Integer.parseInt(newTokens[2]);
  66.  
  67.                     if (goldForAdd < 0 ){
  68.                         System.out.println("Gold added cannot be a negative number!");
  69.                     }else {
  70.                         int oldGoldcount = collection.get(town).get(1);
  71.                         collection.get(town).set(1,goldForAdd+oldGoldcount);
  72.                         System.out.println(String.format("%d gold added to the city treasury." +
  73.                                 " %s now has %d gold.",goldForAdd,town,collection.get(town).get(1)));
  74.                     }
  75.  
  76.                     break;
  77.             }
  78.  
  79.  
  80.             newInput = scanner.nextLine();
  81.         }
  82.  
  83.         if (collection.size() > 0) {
  84.             System.out.println(String.format("Ahoy, Captain! There are %d wealthy settlements to go to:",
  85.                     collection.size()));
  86.  
  87.             collection
  88.                     .entrySet()
  89.                     .stream()
  90.                     .sorted((a,b)->Integer.compare(b.getValue().get(1),a.getValue().get(1)))
  91.                     .forEach(e ->{
  92.                         String nameOfSity = e.getKey();
  93.  
  94.                         System.out.println(String.format("%s -> Population: %d citizens," +
  95.                                 " Gold: %d kg",nameOfSity,e.getValue().get(0),e.getValue().get(1)));
  96.  
  97.                     });
  98.  
  99.  
  100.  
  101.         }else{
  102.             System.out.println("Ahoy, Captain! All targets have been plundered and destroyed!");
  103.         }
  104.  
  105.     }
  106. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement