Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class piratesProblem {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- Map<String, List<Integer>> collection = new TreeMap<>();
- String input = scanner.nextLine();
- while (!input.equals("Sail")){
- String [] tokens = input.split("\\|\\|");
- String city = tokens[0];
- int population =Integer.parseInt(tokens[1].trim());
- int gold = Integer.parseInt(tokens[2].trim());
- if (!collection.containsKey(city)){
- List<Integer> listFodAdd = new ArrayList<>();
- collection.put(city,listFodAdd);
- collection.get(city).add(0,population);
- collection.get(city).add(1,gold);
- }else {
- int oldPopulation = collection.get(city).get(0);
- int oldGold = collection.get(city).get(1);
- collection.get(city).set(0,oldPopulation+population);
- collection.get(city).set(1,oldGold+gold);
- }
- input = scanner.nextLine();
- }
- String newInput = scanner.nextLine();
- while (!newInput.equals("End")){
- String [] newTokens = newInput.split("=>");
- String command = newTokens[0];
- String town = newTokens[1];
- switch (command){
- case "Plunder":
- int people = Integer.parseInt(newTokens[2].trim());
- int gold = Integer.parseInt(newTokens[3].trim());
- int oldPopulation = collection.get(town).get(0);
- int oldGold = collection.get(town).get(1);
- collection.get(town).set(0,oldPopulation - people);
- collection.get(town).set(1,oldGold - gold);
- if (collection.get(town).get(0) <= 0 || collection.get(town).get(1) <= 0){
- collection.remove(town);
- System.out.println(String.format("%s has been wiped off the map!",town));
- }else {
- System.out.println(String.format("%s plundered! %d gold stolen," +
- " %d citizens killed.",town,gold,
- people));
- }
- break;
- case "Prosper":
- int goldForAdd = Integer.parseInt(newTokens[2]);
- if (goldForAdd < 0 ){
- System.out.println("Gold added cannot be a negative number!");
- }else {
- int oldGoldcount = collection.get(town).get(1);
- collection.get(town).set(1,goldForAdd+oldGoldcount);
- System.out.println(String.format("%d gold added to the city treasury." +
- " %s now has %d gold.",goldForAdd,town,collection.get(town).get(1)));
- }
- break;
- }
- newInput = scanner.nextLine();
- }
- if (collection.size() > 0) {
- System.out.println(String.format("Ahoy, Captain! There are %d wealthy settlements to go to:",
- collection.size()));
- collection
- .entrySet()
- .stream()
- .sorted((a,b)->Integer.compare(b.getValue().get(1),a.getValue().get(1)))
- .forEach(e ->{
- String nameOfSity = e.getKey();
- System.out.println(String.format("%s -> Population: %d citizens," +
- " Gold: %d kg",nameOfSity,e.getValue().get(0),e.getValue().get(1)));
- });
- }else{
- System.out.println("Ahoy, Captain! All targets have been plundered and destroyed!");
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement