Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.ArrayList;
- import java.util.Comparator;
- import java.util.HashMap;
- import java.util.List;
- import java.util.Map;
- import java.util.Map.Entry;
- import java.util.Scanner;
- import java.util.stream.Stream;
- public class Main {
- static Map<String, List<Integer>> town = new HashMap<>();
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- String command = sc.nextLine();
- while (!"Sail".equals(command)) {
- String[] cups = command.split("[\\|]{2}");
- String nameTown = cups[0];
- int citizens = Integer.parseInt(cups[1]);
- int gold = Integer.parseInt(cups[2]);
- addTown(nameTown, citizens, gold);
- command = sc.nextLine();
- }
- command = sc.nextLine();
- while (!"End".equals(command)) {
- String[] cups2 = command.split("=>");
- switch (cups2[0]) {
- case "Plunder":
- String nameTown = cups2[1];
- int citizens = Integer.parseInt(cups2[2]);
- int gold = Integer.parseInt(cups2[3]);
- System.out.printf("%s plundered! %d gold stolen, %d citizens killed.%n",
- nameTown, citizens, gold);
- int oldCitizens = town.get(nameTown).get(0);
- int oldGold = town.get(nameTown).get(1);
- town.get(nameTown).set(0, oldCitizens - citizens);
- town.get(nameTown).set(1, oldGold - gold);
- if ((oldCitizens - citizens) <= 0 || (oldGold - gold) <= 0) {
- town.get(nameTown).clear();
- town.remove(nameTown);
- System.out.printf("%s has been wiped off the map!", nameTown);
- }
- break;
- case "Prosper":
- nameTown = cups2[1];
- gold = Integer.parseInt(cups2[2]);
- if (gold > 0) {
- oldGold = town.get(nameTown).get(1);
- int totalGold = oldGold + gold;
- town.get(nameTown).set(1, totalGold);
- System.out
- .printf("%d gold added to the city treasury. %s now has %s gold.", gold, nameTown,
- totalGold);
- } else {
- System.out.println("Gold added cannot be a negative number!");
- }
- break;
- }
- command = sc.nextLine();
- }
- System.out.println(
- String.format("Ahoy, Captain! There are %d wealthy settlements to go to:", town.size()));
- town
- .entrySet()
- .stream()
- .sorted(Comparator.<Map.Entry<String, List<Integer>>>comparingInt(x -> x.getValue().get(1))
- .reversed()
- .thenComparing(Entry::getKey))
- // .sorted((entryA, entryB) -> {
- // int goldA = entryA.getValue().get(1);
- // int goldB = entryB.getValue().get(1);
- // String nameA = entryA.getKey();
- // String nameB = entryB.getKey();
- // if (goldA != goldB) {
- // return Integer.compare(goldB, goldA);
- // } else {
- // return nameA.compareTo(nameB);
- // }
- // })
- .forEach(entry -> System.out.println(String
- .format("%s -> Population: %d citizens, Gold: %d kg", entry.getKey(),
- entry.getValue().get(0), entry.getValue().get(1))));
- }
- private static void addTown(String name, int citizens, int gold) {
- if (town.containsKey(name)) {
- int oldCit = town.get(name).get(0);
- int oldGold = town.get(name).get(1);
- town.get(name).set(0, oldCit + citizens);
- town.get(name).set(1, oldGold + gold);
- } else {
- List<Integer> citAndgold = new ArrayList<>();
- citAndgold.add(0, citizens);
- citAndgold.add(1, gold);
- town.put(name, citAndgold);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement