Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package AdvancedCSharp_11Oct_2015;
- import java.util.*;
- import java.util.LinkedHashMap;
- import java.util.Scanner;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- public class _04SrabskoUnleashed {
- public static void main(String[] args) {
- Scanner inputScanner = new Scanner(System.in);
- LinkedHashMap<String, LinkedHashMap<String, Integer>> srabskoStatistics = new LinkedHashMap<>();
- String inputLine = inputScanner.nextLine();
- while (!inputLine.equals("End")) {
- Pattern srabskoPattern = Pattern.compile("([\\w\\s]+)\\s@([a-zA-z\\s]+)\\s(\\d+)\\s(\\d+)");
- Matcher srabskoMatcher = srabskoPattern.matcher(inputLine);
- if (srabskoMatcher.find()) {
- String venue = srabskoMatcher.group(2);
- String singer = srabskoMatcher.group(1);
- int ticketPrice = Integer.parseInt(srabskoMatcher.group(3));
- int ticketCount = Integer.parseInt(srabskoMatcher.group(4));
- if (!srabskoStatistics.containsKey(venue)) {
- srabskoStatistics.put(venue, new LinkedHashMap<>());
- }
- if (!srabskoStatistics.get(venue).containsKey(singer)) {
- srabskoStatistics.get(venue).put(singer, ticketCount * ticketPrice);
- } else {
- int oldValue = srabskoStatistics.get(venue).get(singer);
- srabskoStatistics.get(venue).put(singer, oldValue + ticketCount * ticketPrice);
- }
- }
- inputLine = inputScanner.nextLine();
- }
- StringBuilder output = new StringBuilder();
- for (String venue : srabskoStatistics.keySet()) {
- output.append(String.format("%s%n", venue));
- TreeMap<String, Integer> orderedSrabsko = sortByValue(srabskoStatistics.get(venue));
- for (String singer : orderedSrabsko.keySet()) {
- output.append(String.format("# %s -> %d%n", singer, orderedSrabsko.get(singer)));
- }
- }
- System.out.print(output.toString().trim());
- }
- public static TreeMap<String, Integer> sortByValue(LinkedHashMap<String, Integer> map) {
- ValueComparator valueComparator = new ValueComparator(map);
- TreeMap<String, Integer> sortedMap = new TreeMap<>(valueComparator);
- sortedMap.putAll(map);
- return sortedMap;
- }
- }
- class ValueComparator implements Comparator<String> {
- private Map<String, Integer> map;
- ValueComparator(Map<String, Integer> map) {
- this.map = map;
- }
- @Override
- public int compare(String key1, String key2) {
- int firstValue = map.get(key1);
- int secondValue = map.get(key2);
- if (firstValue == secondValue) {
- return key1.compareTo(key2);
- }
- return map.get(key2).compareTo(map.get(key1));
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement