Advertisement
Guest User

Srabsko

a guest
Oct 24th, 2015
234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.91 KB | None | 0 0
  1. package AdvancedCSharp_11Oct_2015;
  2.  
  3. import java.util.*;
  4. import java.util.LinkedHashMap;
  5. import java.util.Scanner;
  6. import java.util.regex.Matcher;
  7. import java.util.regex.Pattern;
  8.  
  9. public class _04SrabskoUnleashed {
  10. public static void main(String[] args) {
  11. Scanner inputScanner = new Scanner(System.in);
  12.  
  13. LinkedHashMap<String, LinkedHashMap<String, Integer>> srabskoStatistics = new LinkedHashMap<>();
  14. String inputLine = inputScanner.nextLine();
  15. while (!inputLine.equals("End")) {
  16.  
  17. Pattern srabskoPattern = Pattern.compile("([\\w\\s]+)\\s@([a-zA-z\\s]+)\\s(\\d+)\\s(\\d+)");
  18. Matcher srabskoMatcher = srabskoPattern.matcher(inputLine);
  19. if (srabskoMatcher.find()) {
  20. String venue = srabskoMatcher.group(2);
  21. String singer = srabskoMatcher.group(1);
  22. int ticketPrice = Integer.parseInt(srabskoMatcher.group(3));
  23. int ticketCount = Integer.parseInt(srabskoMatcher.group(4));
  24.  
  25. if (!srabskoStatistics.containsKey(venue)) {
  26. srabskoStatistics.put(venue, new LinkedHashMap<>());
  27. }
  28.  
  29. if (!srabskoStatistics.get(venue).containsKey(singer)) {
  30. srabskoStatistics.get(venue).put(singer, ticketCount * ticketPrice);
  31. } else {
  32. int oldValue = srabskoStatistics.get(venue).get(singer);
  33. srabskoStatistics.get(venue).put(singer, oldValue + ticketCount * ticketPrice);
  34. }
  35. }
  36.  
  37. inputLine = inputScanner.nextLine();
  38. }
  39.  
  40. StringBuilder output = new StringBuilder();
  41. for (String venue : srabskoStatistics.keySet()) {
  42. output.append(String.format("%s%n", venue));
  43. TreeMap<String, Integer> orderedSrabsko = sortByValue(srabskoStatistics.get(venue));
  44. for (String singer : orderedSrabsko.keySet()) {
  45. output.append(String.format("# %s -> %d%n", singer, orderedSrabsko.get(singer)));
  46. }
  47. }
  48.  
  49. System.out.print(output.toString().trim());
  50. }
  51.  
  52. public static TreeMap<String, Integer> sortByValue(LinkedHashMap<String, Integer> map) {
  53. ValueComparator valueComparator = new ValueComparator(map);
  54. TreeMap<String, Integer> sortedMap = new TreeMap<>(valueComparator);
  55. sortedMap.putAll(map);
  56. return sortedMap;
  57. }
  58. }
  59.  
  60. class ValueComparator implements Comparator<String> {
  61.  
  62. private Map<String, Integer> map;
  63.  
  64. ValueComparator(Map<String, Integer> map) {
  65. this.map = map;
  66. }
  67.  
  68. @Override
  69. public int compare(String key1, String key2) {
  70. int firstValue = map.get(key1);
  71. int secondValue = map.get(key2);
  72.  
  73. if (firstValue == secondValue) {
  74. return key1.compareTo(key2);
  75. }
  76.  
  77. return map.get(key2).compareTo(map.get(key1));
  78. }
  79. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement