borovaneca

P!rates

Jul 21st, 2023
96
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.94 KB | None | 0 0
  1. package PracticeFinalExam;
  2.  
  3. import java.util.LinkedHashMap;
  4. import java.util.Scanner;
  5.  
  6. public class Pirates {
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         LinkedHashMap<String, LinkedHashMap<String, Integer>> cities = new LinkedHashMap<>();
  11.  
  12.         String sail;
  13.         while (!"Sail".equals(sail = scanner.nextLine())) {
  14.             String[] data = sail.split("\\|\\|");
  15.             String cityName = data[0];
  16.             int population = Integer.parseInt(data[1]);
  17.             int gold = Integer.parseInt(data[2]);
  18.  
  19.             if (!cities.containsKey(cityName)) {
  20.                 cities.put(cityName, new LinkedHashMap<>());
  21.                 cities.get(cityName).put("population", population);
  22.                 cities.get(cityName).put("gold", gold);
  23.             } else {
  24.                 cities.get(cityName).put("population", cities.get(cityName).get("population") + population);
  25.                 cities.get(cityName).put("gold", cities.get(cityName).get("gold") + gold);
  26.             }
  27.         }
  28.  
  29.         String end;
  30.         while (!"End".equals(end = scanner.nextLine())) {
  31.             String[] tokens = end.split("=>");
  32.             String command = tokens[0];
  33.             String cityName = tokens[1];
  34.  
  35.             if (command.equals("Plunder")) {
  36.                 int people = Integer.parseInt(tokens[2]);
  37.                 int gold = Integer.parseInt(tokens[3]);
  38.  
  39.                 System.out.printf("%s plundered! %d gold stolen, %d citizens killed.\n", cityName, gold, people);
  40.                 cities.get(cityName).put("population", cities.get(cityName).get("population") - people);
  41.                 cities.get(cityName).put("gold", cities.get(cityName).get("gold") - gold);
  42.                 if (cities.get(cityName).get("population") <= 0 || cities.get(cityName).get("gold") <= 0) {
  43.                     cities.remove(cityName);
  44.                     System.out.println(cityName + " has been wiped off the map!");
  45.                 }
  46.             } else if (command.equals("Prosper")) {
  47.                 int gold = Integer.parseInt(tokens[2]);
  48.                 if (gold < 0) {
  49.                     System.out.println("Gold added cannot be a negative number!");
  50.                 } else {
  51.                     cities.get(cityName).put("gold", cities.get(cityName).get("gold") + gold);
  52.                     System.out.printf("%d gold added to the city treasury. %s now has %d gold.\n", gold, cityName, cities.get(cityName).get("gold"));
  53.                 }
  54.             }
  55.         }
  56.  
  57.         if (cities.isEmpty()) {
  58.             System.out.println("Ahoy, Captain! All targets have been plundered and destroyed!");
  59.         } else {
  60.             System.out.printf("Ahoy, Captain! There are %d wealthy settlements to go to:\n", cities.size());
  61.             cities.forEach((k, v) -> System.out.printf("%s -> Population: %d citizens, Gold: %d kg\n", k, v.get("population"), v.get("gold")));
  62.         }
  63.     }
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment