Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package ThirdTask;
- import java.util.ArrayList;
- import java.util.LinkedHashMap;
- import java.util.Scanner;
- public class Pirates {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- LinkedHashMap<String, ArrayList<Integer>> targetsMap = new LinkedHashMap<>();
- while (!input.equals("Sail")) {
- String[] targetsArr = input.split("\\|\\|");
- String city = targetsArr[0];
- int population = Integer.parseInt(targetsArr[1]);
- int gold = Integer.parseInt(targetsArr[2]);
- if (population >= 0 && gold >= 0) {
- if (!targetsMap.containsKey(city)) {
- targetsMap.putIfAbsent(city, new ArrayList<>());
- targetsMap.get(city).add(0, population);
- targetsMap.get(city).add(1, gold);
- } else {
- population = targetsMap.get(city).get(0) + population;
- gold = targetsMap.get(city).get(1) + gold;
- targetsMap.get(city).set(0, population);
- targetsMap.get(city).set(1, gold);
- }
- }
- input = scanner.nextLine();
- }
- String newInput = scanner.nextLine();
- while (!newInput.equals("End")) {
- String[] commandArr = newInput.split("=>");
- String command = commandArr[0];
- boolean isValidCommand = command.equals("Plunder") || command.equals("Prosper");
- if (isValidCommand) {
- switch (command) {
- case "Plunder": {
- String town = commandArr[1];
- int peopleKilled = Integer.parseInt(commandArr[2]);
- int stolenGold = Integer.parseInt(commandArr[3]);
- if (targetsMap.containsKey(town)) {
- int leftPeople = targetsMap.get(town).get(0) - peopleKilled;
- targetsMap.get(town).set(0, leftPeople);
- int leftGold = targetsMap.get(town).get(1) - stolenGold;
- targetsMap.get(town).set(1, leftGold);
- System.out.printf("%s plundered! %d gold stolen, %d citizens killed.%n",
- town, stolenGold, peopleKilled);
- if (targetsMap.get(town).get(0) <= 0 || targetsMap.get(town).get(1) <= 0) {
- targetsMap.remove(town);
- System.out.printf("%s has been wiped off the map!%n", town);
- }
- }
- break;
- }
- case "Prosper": {
- String town = commandArr[1];
- int increasedGold = Integer.parseInt(commandArr[2]);
- if (targetsMap.containsKey(town)) {
- if (increasedGold < 0) {
- System.out.printf("Gold added cannot be a negative number!%n");
- } else {
- targetsMap.get(town).set(1, targetsMap.get(town).get(1) + increasedGold);
- System.out.printf("%d gold added to the city treasury. %s now has %d gold.%n",
- increasedGold, town, targetsMap.get(town).get(1));
- }
- break;
- }
- }
- }
- }
- newInput = scanner.nextLine();
- }
- int count = targetsMap.size();
- if (targetsMap.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);
- targetsMap.entrySet().forEach(entry -> {
- System.out.printf("%s -> Population: %d citizens, Gold: %d kg%n",
- entry.getKey(), entry.getValue().get(0), entry.getValue().get(1));
- });
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment