IvaAnd

Ex072419_01_Concert

Aug 8th, 2020
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.35 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class Ex072419_01_Concert {
  4. public static void main(String[] args) {
  5. Scanner scanner = new Scanner(System.in);
  6.  
  7. String command = scanner.nextLine();
  8.  
  9. Map<String, List<String>> bands = new LinkedHashMap<>();
  10. Map<String, Integer> time = new LinkedHashMap<>();
  11.  
  12. while (!command.equals("start of concert")) {
  13.  
  14. String[] tokens = command.split("; ");
  15. command = tokens[0];
  16. String bandName = tokens[1];
  17.  
  18. List<String> members = bands.get(bandName);
  19. switch (command) {
  20. case "Play":
  21. int currTime = Integer.parseInt(tokens[2]);
  22. bands.putIfAbsent(bandName, new ArrayList<>());
  23. time.putIfAbsent(bandName, 0);
  24. int totalTime = time.get(bandName) + currTime;
  25. time.put(bandName, totalTime);
  26. break;
  27. case "Add":
  28. String[] bandMembers = tokens[2].split(", ");
  29. bands.putIfAbsent(bandName, new ArrayList<>());
  30. members = bands.get(bandName);
  31. for (String currMember : bandMembers) {
  32. if (members.contains(currMember)) {
  33. continue;
  34. }
  35. members.add(currMember);
  36. }
  37. time.putIfAbsent(bandName, 0);
  38.  
  39. break;
  40. }
  41. command = scanner.nextLine();
  42. }
  43. int totalTime = time.values().stream().mapToInt(value -> value).sum();
  44. System.out.println("Total time: "+ totalTime);
  45.  
  46. time
  47. .entrySet()
  48. .stream()
  49. .sorted((f, s) -> {
  50. int result = s.getValue().compareTo(f.getValue());
  51. if (result==0){
  52. result = f.getKey().compareTo(s.getKey());
  53. }
  54. return result;
  55. })
  56. .forEach( band -> System.out.printf("%s -> %d%n",band.getKey(), band.getValue()));
  57.  
  58. command = scanner.nextLine();
  59. System.out.println(command);
  60.  
  61. for (String member : bands.get(command)) {
  62. System.out.println("=> "+ member);
  63.  
  64. }
  65.  
  66. }
  67. }
  68.  
  69.  
  70.  
Add Comment
Please, Sign In to add comment