Advertisement
asiajv

1. Final Exam 04.04.2020 Problem 3. P!rates

Apr 5th, 2020
851
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.64 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.Comparator;
  5. import java.util.HashMap;
  6. import java.util.List;
  7. import java.util.Map;
  8. import java.util.Map.Entry;
  9. import java.util.Scanner;
  10. import java.util.stream.Stream;
  11.  
  12. public class Main {
  13.  
  14.   static Map<String, List<Integer>> town = new HashMap<>();
  15.  
  16.   public static void main(String[] args) {
  17.     Scanner sc = new Scanner(System.in);
  18.     String command = sc.nextLine();
  19.     while (!"Sail".equals(command)) {
  20.       String[] cups = command.split("[\\|]{2}");
  21.       String nameTown = cups[0];
  22.       int citizens = Integer.parseInt(cups[1]);
  23.       int gold = Integer.parseInt(cups[2]);
  24.       addTown(nameTown, citizens, gold);
  25.       command = sc.nextLine();
  26.     }
  27.     command = sc.nextLine();
  28.     while (!"End".equals(command)) {
  29.       String[] cups2 = command.split("=>");
  30.       switch (cups2[0]) {
  31.         case "Plunder":
  32.           String nameTown = cups2[1];
  33.           int citizens = Integer.parseInt(cups2[2]);
  34.           int gold = Integer.parseInt(cups2[3]);
  35.           System.out.printf("%s plundered! %d gold stolen, %d citizens killed.%n",
  36.               nameTown, citizens, gold);
  37.           int oldCitizens = town.get(nameTown).get(0);
  38.           int oldGold = town.get(nameTown).get(1);
  39.           town.get(nameTown).set(0, oldCitizens - citizens);
  40.           town.get(nameTown).set(1, oldGold - gold);
  41.           if ((oldCitizens - citizens) <= 0 || (oldGold - gold) <= 0) {
  42.             town.get(nameTown).clear();
  43.             town.remove(nameTown);
  44.             System.out.printf("%s has been wiped off the map!", nameTown);
  45.           }
  46.           break;
  47.         case "Prosper":
  48.           nameTown = cups2[1];
  49.           gold = Integer.parseInt(cups2[2]);
  50.           if (gold > 0) {
  51.             oldGold = town.get(nameTown).get(1);
  52.             int totalGold = oldGold + gold;
  53.             town.get(nameTown).set(1, totalGold);
  54.             System.out
  55.                 .printf("%d gold added to the city treasury. %s now has %s gold.", gold, nameTown,
  56.                     totalGold);
  57.           } else {
  58.             System.out.println("Gold added cannot be a negative number!");
  59.           }
  60.           break;
  61.       }
  62.       command = sc.nextLine();
  63.     }
  64.     System.out.println(
  65.         String.format("Ahoy, Captain! There are %d wealthy settlements to go to:", town.size()));
  66.     town
  67.         .entrySet()
  68.         .stream()
  69.         .sorted(Comparator.<Map.Entry<String, List<Integer>>>comparingInt(x -> x.getValue().get(1))
  70.             .reversed()
  71.             .thenComparing(Entry::getKey))
  72. //        .sorted((entryA, entryB) -> {
  73. //          int goldA = entryA.getValue().get(1);
  74. //          int goldB = entryB.getValue().get(1);
  75. //          String nameA = entryA.getKey();
  76. //          String nameB = entryB.getKey();
  77. //          if (goldA != goldB) {
  78. //            return Integer.compare(goldB, goldA);
  79. //          } else {
  80. //            return nameA.compareTo(nameB);
  81. //          }
  82. //        })
  83.         .forEach(entry -> System.out.println(String
  84.             .format("%s -> Population: %d citizens, Gold: %d kg", entry.getKey(),
  85.                 entry.getValue().get(0), entry.getValue().get(1))));
  86.   }
  87.  
  88.   private static void addTown(String name, int citizens, int gold) {
  89.     if (town.containsKey(name)) {
  90.       int oldCit = town.get(name).get(0);
  91.       int oldGold = town.get(name).get(1);
  92.       town.get(name).set(0, oldCit + citizens);
  93.       town.get(name).set(1, oldGold + gold);
  94.     } else {
  95.       List<Integer> citAndgold = new ArrayList<>();
  96.       citAndgold.add(0, citizens);
  97.       citAndgold.add(1, gold);
  98.       town.put(name, citAndgold);
  99.     }
  100.   }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement