Didart

P!rates

Oct 23rd, 2021
137
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.81 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Pirates {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         List<City> targetedCities = new ArrayList<>();
  8.  
  9.         String input = scanner.nextLine();
  10.         while (!input.equals("Sail")) {
  11.             String[] targetedCity = input.split("\\|\\|");
  12.  
  13.             String name = targetedCity[0];
  14.             int population = Integer.parseInt(targetedCity[1]);
  15.             int gold = Integer.parseInt(targetedCity[2]);
  16.  
  17.             City city = new City(name, population, gold);
  18.             if (!targetedCities.contains(city)) {
  19.                 targetedCities.add(city);
  20.             } else {
  21.                 city = targetedCities.stream().filter(c -> c.getName().equals(name)).findFirst().orElse(city);
  22.                 city.setPopulation(city.getPopulation() + population);
  23.                 city.setGold(city.getGold() + gold);
  24.             }
  25.             input = scanner.nextLine();
  26.         }
  27.  
  28.         input = scanner.nextLine();
  29.         while (!input.equals("End")) {
  30.             String[] events = input.split("=>");
  31.             String town = events[1];
  32.  
  33.             switch (events[0]) {
  34.                 case "Plunder":
  35.                     int people = Integer.parseInt(events[2]);
  36.                     int gold = Integer.parseInt(events[3]);
  37.                     for (City c : targetedCities) {
  38.                         if (c.getName().equals(town)) {
  39.  
  40.                             int afterAttackPeople = c.getPopulation() - people;
  41.                             int afterAttackGold = c.getGold() - gold;
  42.  
  43.                             c.setGold(afterAttackGold);
  44.                             c.setPopulation(afterAttackPeople);
  45.  
  46.                             System.out.printf("%s plundered! %d gold stolen, %d citizens killed.\n", town, gold, people);
  47.  
  48.                             if (afterAttackGold <= 0 || afterAttackPeople <= 0) {
  49.                                 System.out.printf("%s has been wiped off the map!\n", town);
  50.                             }
  51.                         }
  52.                     }
  53.                     targetedCities.removeIf(city -> city.getGold() <= 0 || city.getPopulation() <= 0);
  54.  
  55.                     break;
  56.                 case "Prosper":
  57.                     gold = Integer.parseInt(events[2]);
  58.                     if (gold > 0) {
  59.                         for (City c : targetedCities) {
  60.                             if (c.getName().equals(town)) {
  61.                                 c.setGold(c.getGold() + gold);
  62.                                 System.out.printf("%d gold added to the city treasury. %s now has %d gold.\n", gold, town, c.getGold());
  63.                             }
  64.                         }
  65.                     } else {
  66.                         System.out.println("Gold added cannot be a negative number!");
  67.  
  68.                     }
  69.                     break;
  70.             }
  71.             input = scanner.nextLine();
  72.         }
  73.         if (targetedCities.size() > 0) {
  74.             System.out.printf("Ahoy, Captain! There are %d wealthy settlements to go to:\n", targetedCities.size());
  75.             targetedCities
  76.                     .stream()
  77.                     .sorted(Comparator.comparing(City::getGold)
  78.                             .reversed()
  79.                             .thenComparing(City::getName))
  80.                     .forEach(city -> System.out.printf("%s -> Population: %d citizens, Gold: %d kg\n", city.getName(), city.getPopulation(), city.getGold()));
  81.         } else {
  82.             System.out.println("Ahoy, Captain! All targets have been plundered and destroyed!");
  83.         }
  84.     }
  85.  
  86.     static class City {
  87.         String name;
  88.         int population;
  89.         int gold;
  90.  
  91.         public City(String name, int population, int gold) {
  92.             this.name = name;
  93.             this.population = population;
  94.             this.gold = gold;
  95.         }
  96.  
  97.         public String getName() {
  98.             return name;
  99.         }
  100.  
  101.         public void setName(String name) {
  102.             this.name = name;
  103.         }
  104.  
  105.         public int getPopulation() {
  106.             return population;
  107.         }
  108.  
  109.         public void setPopulation(int population) {
  110.             this.population = population;
  111.         }
  112.  
  113.         public int getGold() {
  114.             return gold;
  115.         }
  116.  
  117.         public void setGold(int gold) {
  118.             this.gold = gold;
  119.         }
  120.  
  121.         @Override
  122.         public boolean equals(Object o) {
  123.             if (this == o) {
  124.                 return true;
  125.             }
  126.             if (o == null || getClass() != o.getClass()) {
  127.                 return false;
  128.             }
  129.             City city = (City) o;
  130.             return name.equals(city.name);
  131.         }
  132.  
  133.         @Override
  134.         public int hashCode() {
  135.             return Objects.hash(name);
  136.         }
  137.     }
  138. }
  139.  
  140.  
Advertisement
Add Comment
Please, Sign In to add comment