Advertisement
NadezhdaGeorgieva

Pirates

Dec 6th, 2020
977
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.57 KB | None | 0 0
  1. package bg.softuni.javafundamentals;
  2.  
  3. import java.util.HashMap;
  4. import java.util.Scanner;
  5.  
  6. public class Fin01_20Apr_Pirates {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.         HashMap<String, Integer> citiesAndPopulation = new HashMap<>();
  10.         HashMap<String, Integer> citiesAndGold = new HashMap<>();
  11.         String input = scanner.nextLine();
  12.  
  13.         while (!"Sail".equals(input)){
  14.             String[] tokens = input.split("\\|\\|");
  15.             String city = tokens[0];
  16.             int population = Integer.parseInt(tokens[1].trim());
  17.             int gold = Integer.parseInt(tokens[2].trim());
  18.  
  19.             Integer currentPopulation = citiesAndPopulation.get(city);
  20.             if (currentPopulation == null){
  21.                 currentPopulation = 0;
  22.             }
  23.             citiesAndPopulation.put(city, currentPopulation + population);
  24.  
  25.             Integer currentGold = citiesAndGold.get(city);
  26.             if (currentGold == null){
  27.                 currentGold = 0;
  28.             }
  29.             citiesAndGold.put(city, currentGold + gold);
  30.  
  31.             input = scanner.nextLine();
  32.         }
  33.         input = scanner.nextLine();
  34.         while (!"End".equals(input)){
  35.             String[]tokens = input.split("=>");
  36.             String command = tokens[0];
  37.             String town = tokens[1];
  38.             switch(command){
  39.                 case "Plunder":
  40.                     int people = Integer.parseInt(tokens[2].trim());
  41.                     int gold = Integer.parseInt(tokens[3].trim());
  42.                     citiesAndPopulation.put(town, citiesAndPopulation.get(town) - people);
  43.                     citiesAndGold.put(town, citiesAndGold.get(town) - gold);
  44.                     System.out.printf("%s plundered! %d gold stolen, %d citizens killed.%n", town, gold, people);
  45.                     if (citiesAndPopulation.get(town) <= 0 || citiesAndGold.get(town) <= 0){
  46.                         citiesAndPopulation.remove(town);
  47.                         citiesAndGold.remove(town);
  48.                         System.out.printf("%s has been wiped off the map!%n", town);
  49.                     }
  50.                     break;
  51.                 case "Prosper":
  52.                     gold = Integer.parseInt(tokens[2]);
  53.                     if (gold < 0){
  54.                         System.out.println("Gold added cannot be a negative number!");
  55.                         break;
  56.                     }
  57.                     citiesAndGold.put(town, citiesAndGold.get(town) + gold);
  58.                     System.out.printf("%d gold added to the city treasury. %s now has %d gold.%n", gold, town, citiesAndGold.get(town));
  59.                     break;
  60.             }
  61.  
  62.             input = scanner.nextLine();
  63.         }
  64.         if (citiesAndGold.isEmpty()){
  65.             System.out.println("Ahoy, Captain! All targets have been plundered and destroyed!");
  66.         }else{
  67.             System.out.printf("Ahoy, Captain! There are %d wealthy settlements to go to:%n", citiesAndGold.size());
  68.             citiesAndGold.entrySet()
  69.                     .stream()
  70.                     .sorted((f, s) -> {
  71.                         int result = s.getValue().compareTo(f.getValue());
  72.                         if (result == 0){
  73.                             result = f.getKey().compareTo(s.getKey());
  74.                         }
  75.                         return result;
  76.                     }).forEach(city -> System.out.printf("%s -> Population: %d citizens, Gold: %d kg%n",
  77.                     city.getKey(), citiesAndPopulation.get(city.getKey()), city.getValue()));
  78.         }
  79.     }
  80. }
  81.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement