Advertisement
Guest User

Untitled

a guest
Dec 8th, 2020
247
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.87 KB | None | 0 0
  1. package test;
  2.  
  3. import java.util.Comparator;
  4. import java.util.HashMap;
  5. import java.util.Map;
  6. import java.util.Scanner;
  7.  
  8. public class Pirates {
  9.     public static void main(String[] args) {
  10.         Scanner scanner = new Scanner(System.in);
  11.  
  12.         Map<String, City> targetedCities = new HashMap<>();
  13.  
  14.         String input;
  15.         while (!"Sail".equals(input = scanner.nextLine())) {
  16.             String[] targetedCity = input.split("\\|\\|");
  17.  
  18.             String name = targetedCity[0];
  19.             int population = Integer.parseInt(targetedCity[1]);
  20.             int gold = Integer.parseInt(targetedCity[2]);
  21.  
  22.             targetedCities.putIfAbsent(name, new City(name));
  23.             City city = targetedCities.get(name);
  24.             city.setPopulation(city.getPopulation() + population);
  25.             city.setGold(city.getGold() + gold);
  26.         }
  27.  
  28.         while (!"End".equals(input = scanner.nextLine())) {
  29.             String[] events = input.split("=>");
  30.             String town = events[1];
  31.  
  32.             switch (events[0]) {
  33.             case "Plunder":
  34.                 int people = Integer.parseInt(events[2]);
  35.                 int gold = Integer.parseInt(events[3]);
  36.                 targetedCities.computeIfPresent(town, (name, city) -> {
  37.                     city.setPopulation(city.getPopulation() - people);
  38.                     city.setGold(city.getGold() - gold);
  39.                     System.out.printf("%s plundered! %d gold stolen, %d citizens killed.%n", town, gold, people);
  40.                     if (city.getGold() <= 0 || city.getPopulation() <= 0) {
  41.                         System.out.printf("%s has been wiped off the map!%n", town);
  42.                         return null;
  43.                     }
  44.                     return city;
  45.                 });
  46.                 break;
  47.             case "Prosper":
  48.                 gold = Integer.parseInt(events[2]);
  49.                 if (gold > 0) {
  50.                     targetedCities.computeIfPresent(town, (name, city) -> {
  51.                         city.setGold(city.getGold() + gold);
  52.                         System.out.printf("%d gold added to the city treasury. %s now has %d gold.%n",
  53.                                 gold, town, city.getGold());
  54.                         return city;
  55.                     });
  56.                 } else {
  57.                     System.out.println("Gold added cannot be a negative number!");
  58.                 }
  59.                 break;
  60.             }
  61.         }
  62.  
  63.         if (targetedCities.size() > 0) {
  64.             System.out.printf("Ahoy, Captain! There are %d wealthy settlements to go to:%n", targetedCities.size());
  65.             targetedCities.values()
  66.                     .stream()
  67.                     .sorted(Comparator.comparing(City::getGold)
  68.                             .reversed()
  69.                             .thenComparing(City::getName))
  70.                     .forEach(city -> System.out.printf("%s -> Population: %d citizens, Gold: %d kg%n",
  71.                             city.getName(), city.getPopulation(), city.getGold()));
  72.         } else {
  73.             System.out.println("Ahoy, Captain! All targets have been plundered and destroyed!");
  74.         }
  75.     }
  76.  
  77.     static class City {
  78.         String name;
  79.         int population;
  80.         int gold;
  81.  
  82.         public City(String name) {
  83.             this.name = name;
  84.             this.population = 0;
  85.             this.gold = 0;
  86.         }
  87.  
  88.         public String getName() {
  89.             return name;
  90.         }
  91.  
  92.         public void setName(String name) {
  93.             this.name = name;
  94.         }
  95.  
  96.         public int getPopulation() {
  97.             return population;
  98.         }
  99.  
  100.         public void setPopulation(int population) {
  101.             this.population = population;
  102.         }
  103.  
  104.         public int getGold() {
  105.             return gold;
  106.         }
  107.  
  108.         public void setGold(int gold) {
  109.             this.gold = gold;
  110.         }
  111.     }
  112. }
  113.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement