Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.LinkedHashMap;
- import java.util.Scanner;
- public class Pirates {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String inputLines = scanner.nextLine().trim();
- LinkedHashMap<String, Integer> mapCityNameAndPopulation = new LinkedHashMap<>();
- LinkedHashMap<String, Integer> mapCityNameAndGold = new LinkedHashMap<>();
- while (!inputLines.equals("Sail")) {
- String[] tokens = inputLines.split("[|]{2}");
- String cityName = tokens[0].trim();
- int cityPopulation = Integer.parseInt(tokens[1].trim());
- int cityGold = Integer.parseInt(tokens[2].trim());
- mapCityNameAndPopulation.putIfAbsent(cityName, 0);
- mapCityNameAndGold.putIfAbsent(cityName, 0);
- int cityCurrentPopulation = mapCityNameAndPopulation.get(cityName);
- int cityCurrentGold = mapCityNameAndGold.get(cityName);
- mapCityNameAndPopulation.put(cityName, cityCurrentPopulation + cityPopulation);
- mapCityNameAndGold.put(cityName, cityCurrentGold + cityGold);
- inputLines = scanner.nextLine().trim();
- }
- String events = scanner.nextLine().trim();
- while (!events.equals("End")) {
- String[] tokens = events.split("=>");
- String command = tokens[0].trim();
- switch (command) {
- case "Plunder": {
- String cityName = tokens[1].trim();
- int cityPopulation = Integer.parseInt(tokens[2].trim());
- int cityGold = Integer.parseInt(tokens[3].trim());
- System.out.println(String.format("%s plundered! %d gold stolen, %d citizens killed.",
- cityName, cityGold, cityPopulation));
- int cityCurrentPopulation = mapCityNameAndPopulation.get(cityName);
- int cityCurrentGold = mapCityNameAndGold.get(cityName);
- int cityPopulationAfterAttack = cityCurrentPopulation - cityPopulation;
- int cityGoldAfterAttack = cityCurrentGold - cityGold;
- mapCityNameAndPopulation.put(cityName, cityPopulationAfterAttack);
- mapCityNameAndGold.put(cityName, cityGoldAfterAttack);
- if (cityPopulationAfterAttack <= 0 || cityGoldAfterAttack <= 0) {
- mapCityNameAndPopulation.remove(cityName);
- mapCityNameAndGold.remove(cityName);
- System.out.println(String.format("%s has been wiped off the map!", cityName));
- }
- }
- break;
- case "Prosper": {
- String cityName = tokens[1].trim();
- int cityGold = Integer.parseInt(tokens[2].trim());
- if (cityGold < 0) {
- System.out.println("Gold added cannot be a negative number!");
- events = scanner.nextLine();
- continue;
- }
- int cityCurrentGold = mapCityNameAndGold.get(cityName);
- mapCityNameAndGold.put(cityName, cityCurrentGold + cityGold);
- System.out.println(String.format("%d gold added to the city treasury. %s now has %d gold.",
- cityGold, cityName, mapCityNameAndGold.get(cityName)));
- }
- break;
- default:
- break;
- }
- events = scanner.nextLine().trim();
- }
- if (mapCityNameAndPopulation.size() > 0) {
- System.out.println(String.format("Ahoy, Captain! There are %d wealthy settlements to go to:",
- mapCityNameAndPopulation.size()));
- mapCityNameAndGold
- .entrySet()
- .stream()
- .sorted((cg1, cg2) -> {
- if (cg2.getValue() - cg1.getValue() == 0) {
- return cg1.getKey().compareTo(cg2.getKey());
- }
- return cg2.getValue() - cg1.getValue();
- })
- .forEach(cg -> System.out.println(String.format("%s -> Population: %d citizens, Gold: %d kg",
- cg.getKey(), mapCityNameAndPopulation.get(cg.getKey()), cg.getValue())));
- } else {
- System.out.println("Ahoy, Captain! All targets have been plundered and destroyed!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement