Advertisement
SIRAKOV4444

Untitled

Apr 1st, 2020
155
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.86 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. public class Concert {
  5. public static void main(String[] args) {
  6. Scanner sc = new Scanner(System.in);
  7.  
  8. Map<String, Map<Integer, List<String>>> myBands = new LinkedHashMap<>();
  9.  
  10. String input = sc.nextLine();
  11. while (!"start of concert".equals(input)) {
  12. String[] data = input.split("; ");
  13. int playTime = 0;
  14. String bandName = data[1];
  15. if ("Add".equals(data[0])) {
  16. if (!myBands.containsKey(bandName)) {
  17. myBands.put(bandName, new TreeMap<>());
  18. myBands.get(bandName).put(playTime, new ArrayList<>());
  19. } else {
  20. for (Map.Entry<Integer, List<String>> kvp : myBands.get(bandName).entrySet()) {
  21. playTime = kvp.getKey();
  22. }
  23. }
  24. String[] bandMembers = data[2].split(", ");
  25. for (String bandMember : bandMembers) {
  26. if (!myBands.get(bandName).get(playTime).contains(bandMember)) {
  27. myBands.get(bandName).get(playTime).add(bandMember);
  28. }
  29. }
  30. } else if ("Play".equals(data[0])) {
  31. playTime = Integer.parseInt(data[2]);
  32. if (!myBands.containsKey(bandName)) {
  33. myBands.put(bandName, new LinkedHashMap<>());
  34. myBands.get(bandName).put(playTime, new ArrayList<>());
  35. } else {
  36. int oldPlayTime = 0;
  37. List<String> helpList = new ArrayList<>();
  38. for (Map.Entry<Integer, List<String>> kvp : myBands.get(bandName).entrySet()) {
  39. oldPlayTime = kvp.getKey();
  40. helpList.addAll(kvp.getValue());
  41. }
  42. myBands.get(bandName).remove(oldPlayTime);
  43. myBands.get(bandName).put(oldPlayTime + playTime, new ArrayList<>());
  44. for (String s : helpList) {
  45. myBands.get(bandName).get(oldPlayTime + playTime).add(s);
  46. }
  47. }
  48.  
  49. }
  50. input = sc.nextLine();
  51. }
  52. myBands = myBands.entrySet()
  53. .stream()
  54. .sorted((a,b) -> {
  55. int firstKey = 0;
  56. int secKey = 0;
  57. for (Integer keyA : a.getValue().keySet()) {
  58. firstKey = keyA;
  59. }
  60. for (Integer keyB : b.getValue().keySet()) {
  61. secKey = keyB;
  62. }
  63. int sort = Integer.compare(secKey, firstKey);
  64. if(sort == 0) {
  65. sort = a.getKey().compareTo(b.getKey());
  66. }
  67. return sort;
  68. }).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue, (a,b) -> a, LinkedHashMap::new));
  69. int totalTime = 0;
  70. for (Map.Entry<String, Map<Integer, List<String>>> outerLoop : myBands.entrySet()) {
  71. for (Integer time : outerLoop.getValue().keySet()) {
  72. totalTime += time;
  73. }
  74. }
  75. System.out.printf("Total time: %d%n", totalTime);
  76. for (Map.Entry<String, Map<Integer, List<String>>> outerLoop : myBands.entrySet()) {
  77. System.out.printf("%s -> ", outerLoop.getKey());
  78. for (Integer time : outerLoop.getValue().keySet()) {
  79. System.out.printf("%d%n", time);
  80. }
  81. }
  82. input = sc.nextLine();
  83. System.out.println(input);
  84. myBands.get(input).forEach((key, value) -> {
  85. for (String s : value) {
  86. System.out.printf("=> %s%n", s);
  87. }
  88. });
  89. }
  90. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement