desislava_topuzakova

03. Pirates

Nov 24th, 2023
151
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.69 KB | None | 0 0
  1. package examPreparation;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.Map;
  5. import java.util.Scanner;
  6.  
  7. public class FirstTask {
  8. public static void main(String[] args) {
  9. Scanner scanner = new Scanner(System.in);
  10. //1. СЪХРАНЯВАНЕ НА ДАННИ ЗА ГРАДОВЕТЕ
  11. //град -> население
  12. Map<String, Integer> townPopulation = new LinkedHashMap<>();
  13. //град -> злато
  14. Map<String, Integer> townGold = new LinkedHashMap<>();
  15.  
  16. String input = scanner.nextLine();
  17. while (!input.equals("Sail")) {
  18. //input = "Tortuga||345000||1250".split("||") -> ["Tortuga", "345000", "1250"]
  19. String town = input.split("\\|\\|")[0]; //"Tortuga"
  20. int population = Integer.parseInt(input.split("\\|\\|")[1]); //"345000" -> 345000
  21. int gold = Integer.parseInt(input.split("\\|\\|")[2]); //"1250" -> 1250
  22.  
  23. //не е записан града
  24. if (!townPopulation.containsKey(town) && !townGold.containsKey(town)) {
  25. townPopulation.put(town, population);
  26. townGold.put(town, gold);
  27. } else {
  28. //имаме такъв град
  29. townPopulation.put(town, townPopulation.get(town) + population);
  30. townGold.put(town, townGold.get(town) + gold);
  31.  
  32. }
  33.  
  34. input = scanner.nextLine();
  35. }
  36.  
  37. //2. ОБРАБОТКА НА ДАННИТЕ ЗА ГРАДОВЕТЕ
  38. String command = scanner.nextLine();
  39. while (!command.equals("End")) {
  40. //command = "Plunder=>{town}=>{people}=>{gold}".split("=>") -> ["Plunder","{town}", "{people}", {gold}"]
  41. //command = "Prosper=>{town}=>{gold}".split("=>") -> ["Prosper", "{town}", {gold}"]
  42. String[] commandParts = command.split("=>");
  43. String commandName = commandParts[0]; //име на команда -> "Plunder" или "Prosper"
  44. String town = commandParts[1]; //град
  45.  
  46. switch (commandName) {
  47. case "Plunder":
  48. //плячкосваме / нападаме
  49. int killedPeople = Integer.parseInt(commandParts[2]); //убити хора
  50. int stealGold = Integer.parseInt(commandParts[3]); //откраднато злато
  51. System.out.printf("%s plundered! %d gold stolen, %d citizens killed.%n", town, stealGold, killedPeople);
  52. townPopulation.put(town, townPopulation.get(town) - killedPeople);
  53. townGold.put(town, townGold.get(town) - stealGold);
  54.  
  55. //проверка дали града ще съществува
  56. if (townPopulation.get(town) <= 0 || townGold.get(town) <= 0) {
  57. //изчезва града, ако няма или хора, или злато
  58. townPopulation.remove(town);
  59. townGold.remove(town);
  60. System.out.printf("%s has been wiped off the map!%n", town);
  61. }
  62. break;
  63.  
  64. case "Prosper":
  65. //икономически прогрес
  66. int earnedGold = Integer.parseInt(commandParts[2]); //заработено злато
  67. if (earnedGold <= 0) {
  68. System.out.println("Gold added cannot be a negative number!");
  69. } else {
  70. townGold.put(town, townGold.get(town) + earnedGold);
  71. System.out.printf("%d gold added to the city treasury. %s now has %d gold.%n",
  72. earnedGold, town, townGold.get(town));
  73. }
  74. break;
  75. }
  76.  
  77.  
  78. command = scanner.nextLine();
  79. }
  80.  
  81. //3. ОТПЕЧАТВАНЕ НА ГРАДОВЕТЕ
  82. if (townPopulation.size() > 0) {
  83. //имаме налични градове
  84. System.out.printf("Ahoy, Captain! There are %d wealthy settlements to go to:%n", townGold.size());
  85. townGold.entrySet().forEach(entry -> {
  86. //entry
  87. //key -> град
  88. //value -> злато
  89. String town = entry.getKey();
  90. int gold = entry.getValue();
  91. int population = townPopulation.get(town);
  92. System.out.printf("%s -> Population: %d citizens, Gold: %d kg%n", town, population, gold);
  93. });
  94. } else {
  95. System.out.println("Ahoy, Captain! All targets have been plundered and destroyed!");
  96. }
  97. }
  98. }
  99.  
Advertisement
Add Comment
Please, Sign In to add comment