kalinikov

13. Сръбско Unleashed

Dec 19th, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.11 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.regex.Matcher;
  3. import java.util.regex.Pattern;
  4.  
  5. public class SerbianUnleashed {
  6. public static void main(String[] args) {
  7. Scanner scanner = new Scanner(System.in);
  8.  
  9. String line = scanner.nextLine();
  10.  
  11. Map<String, Map<String, Long>> venuesWithSingersWithIncome = new LinkedHashMap<>();
  12.  
  13. //String regex2 = "^(?<singer>[\\w\\s]+)\\s@(?<venue>[\\w\\s]+)\\s(?<price>\\d+)\\s(?<count>\\d+)$";
  14.  
  15.  
  16. while (!line.equals("End")) {
  17.  
  18. //if (inputIsValid(line)) {
  19. String regex =
  20. "(?<singer>[A-Za-z0-9 ]+ ?){1,3} @(?<venue>[A-Za-z0-9 ]+ ?){1,3} (?<price>\\d+) (?<count>\\d+)";
  21. Pattern pattern = Pattern.compile(regex);
  22. Matcher matcher = pattern.matcher(line);
  23.  
  24. if (matcher.find()) {
  25. String singer = matcher.group("singer");
  26. String venue = matcher.group("venue");
  27. int price = Integer.parseInt(matcher.group("price"));
  28. int count = Integer.parseInt(matcher.group("count"));
  29. long totalIncome = price * count;
  30.  
  31. if (!venuesWithSingersWithIncome.containsKey(venue)) {
  32. venuesWithSingersWithIncome.put(venue, new LinkedHashMap<>());
  33. venuesWithSingersWithIncome.get(venue).put(singer, totalIncome);
  34. } else if (!venuesWithSingersWithIncome.get(venue).containsKey(singer)) {
  35. venuesWithSingersWithIncome.get(venue).put(singer, totalIncome);
  36. } else {
  37. venuesWithSingersWithIncome.get(venue)
  38. .put(singer, venuesWithSingersWithIncome.get(venue).get(singer) + totalIncome);
  39. }
  40.  
  41. /*venuesWithSingers.putIfAbsent(venue, new ArrayList<>());
  42. venuesWithSingers.get(venue).add(singer);
  43. singersWithIncome.putIfAbsent(singer, totalIncome);
  44. singersWithIncome.put(singer, singersWithIncome.get(singer) + totalIncome);*/
  45. //}
  46. }
  47.  
  48. line = scanner.nextLine();
  49. }
  50.  
  51. //#{2*space}{singer}{space}->{space}{total money}
  52.  
  53. venuesWithSingersWithIncome.forEach((key, value) -> {
  54. System.out.println(key);
  55. value.entrySet()
  56. .stream()
  57. .sorted((f, s) -> {
  58. long result = s.getValue() - f.getValue();
  59. return (int) result;
  60. })
  61. .forEach(e -> {
  62. System.out.printf("# %s -> %d%n", e.getKey(), e.getValue());
  63. });
  64. });
  65.  
  66. }
  67.  
  68. /*private static boolean inputIsValid(String line) {
  69. boolean isValid = false;
  70. String regex = "(?<singer>[A-Za-z0-9 ]+ ?){1,3} @(?<venue>[A-Za-z0-9 ]+ ?){1,3} (?<price>\\d+) (?<count>\\d+)";
  71. Pattern pattern = Pattern.compile(regex);
  72. Matcher matcher = pattern.matcher(line);
  73.  
  74. if (matcher.find()) {
  75. isValid = true;
  76. }
  77.  
  78. return isValid;
  79. }*/
  80. }
Add Comment
Please, Sign In to add comment