plamenavylcheva

Pirates

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