Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ThirdTask;
- import java.util.LinkedHashMap;
- import java.util.Map;
- import java.util.Scanner;
- public class pirat {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- Map<String, Integer> populationMap = new LinkedHashMap<>();
- Map<String, Integer> goldMap = new LinkedHashMap<>();
- while (!input.equals("Sail")) {
- String[] tokens = input.split("\\|{2}");
- String city = tokens[0];
- int population = Integer.parseInt(tokens[1]);
- int gold = Integer.parseInt(tokens[2]);
- if (populationMap.containsKey(city)) {
- int newPopulation = populationMap.get(city) + population;
- int newGold = goldMap.get(city) + gold;
- populationMap.put(city, newPopulation);
- goldMap.put(city, newGold);
- }
- populationMap.putIfAbsent(city, population);
- goldMap.putIfAbsent(city, gold);
- input = scanner.nextLine();
- }
- String inputLine = scanner.nextLine();
- while (!inputLine.equals("End")) {
- String[] tokens = inputLine.split("=>");
- String command = tokens[0];
- String town = tokens[1];
- if (populationMap.containsKey(town) && goldMap.containsKey(town)) {
- switch (command) {
- case "Plunder": {
- int people = Integer.parseInt(tokens[2]);
- int gold = Integer.parseInt(tokens[3]);
- int currentPopulation = populationMap.get(town);
- currentPopulation -= people;
- int currentGold = goldMap.get(town);
- currentGold -= gold;
- populationMap.put(town, currentPopulation);
- goldMap.put(town, currentGold);
- System.out.printf("%s plundered! %s gold stolen, %s citizens killed.%n",
- town, gold, people);
- if (currentGold <= 0 || currentPopulation <= 0) {
- populationMap.remove(town);
- goldMap.remove(town);
- System.out.printf("%s has been wiped off the map!%n", town);
- }
- break;
- }
- case "Prosper": {
- int gold = Integer.parseInt(tokens[2]);
- if (gold < 0) {
- System.out.println("Gold added cannot be a negative number!");
- } else {
- goldMap.put(town, goldMap.get(town) + gold);
- System.out.printf(
- "%d gold added to the city treasury. %s now has %d gold.%n",
- gold, town, goldMap.get(town));
- }
- break;
- }
- }
- }
- inputLine = scanner.nextLine();
- }
- int count = populationMap.size();
- if (populationMap.isEmpty()) {
- System.out.println("Ahoy, Captain! All targets have been plundered and destroyed!");
- } else {
- System.out.printf("Ahoy, Captain! There are %d wealthy settlements to go to:%n", count);
- populationMap.entrySet().forEach(entry -> {
- System.out.printf("%s -> Population: %d citizens, Gold: %d kg%n"
- , entry.getKey(), entry.getValue(), goldMap.get(entry.getKey()));
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment