plamenavylcheva

PiratesWithArrayList

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