Advertisement
Guest User

Untitled

a guest
Dec 8th, 2020
338
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.69 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.  
  54.                 targetedCities.removeIf(city -> city.getGold() <= 0 || city.getPopulation() <= 0);
  55.  
  56.                 break;
  57.             case "Prosper":
  58.                 gold = Integer.parseInt(events[2]);
  59.                 if (gold > 0) {
  60.                     for (City c : targetedCities) {
  61.                         if (c.getName().equals(town)) {
  62.                             c.setGold(c.getGold() + gold);
  63.                             System.out.printf("%d gold added to the city treasury. %s now has %d gold.\n", gold, town, c.getGold());
  64.                         }
  65.                     }
  66.                 } else {
  67.                     System.out.println("Gold added cannot be a negative number!");
  68.  
  69.                 }
  70.                 break;
  71.             }
  72.             input = scanner.nextLine();
  73.         }
  74.         if (targetedCities.size() > 0) {
  75.             System.out.printf("Ahoy, Captain! There are %d wealthy settlements to go to:\n", targetedCities.size());
  76.             targetedCities
  77.                     .stream()
  78.                     .sorted(Comparator.comparing(City::getGold)
  79.                             .reversed()
  80.                             .thenComparing(City::getName))
  81.                     .forEach(city -> System.out.printf("%s -> Population: %d citizens, Gold: %d kg\n", city.getName(), city.getPopulation(), city.getGold()));
  82.         } else {
  83.             System.out.println("Ahoy, Captain! All targets have been plundered and destroyed!");
  84.         }
  85.  
  86.     }
  87.  
  88.     static class City {
  89.         String name;
  90.         int population;
  91.         int gold;
  92.  
  93.         public City(String name, int population, int gold) {
  94.             this.name = name;
  95.             this.population = population;
  96.             this.gold = gold;
  97.         }
  98.  
  99.         public String getName() {
  100.             return name;
  101.         }
  102.  
  103.         public void setName(String name) {
  104.             this.name = name;
  105.         }
  106.  
  107.         public int getPopulation() {
  108.             return population;
  109.         }
  110.  
  111.         public void setPopulation(int population) {
  112.             this.population = population;
  113.         }
  114.  
  115.         public int getGold() {
  116.             return gold;
  117.         }
  118.  
  119.         public void setGold(int gold) {
  120.             this.gold = gold;
  121.         }
  122.  
  123.         @Override
  124.         public boolean equals(Object o) {
  125.             if (this == o) {
  126.                 return true;
  127.             }
  128.             if (o == null || getClass() != o.getClass()) {
  129.                 return false;
  130.             }
  131.             City city = (City) o;
  132.             return name.equals(city.name);
  133.         }
  134.  
  135.         @Override
  136.         public int hashCode() {
  137.             return Objects.hash(name);
  138.         }
  139.     }
  140. }
  141.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement