Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import java.util.*;
- public class Ex072419_01_Concert {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String command = scanner.nextLine();
- Map<String, List<String>> bands = new LinkedHashMap<>();
- Map<String, Integer> time = new LinkedHashMap<>();
- while (!command.equals("start of concert")) {
- String[] tokens = command.split("; ");
- command = tokens[0];
- String bandName = tokens[1];
- List<String> members = bands.get(bandName);
- switch (command) {
- case "Play":
- int currTime = Integer.parseInt(tokens[2]);
- bands.putIfAbsent(bandName, new ArrayList<>());
- time.putIfAbsent(bandName, 0);
- int totalTime = time.get(bandName) + currTime;
- time.put(bandName, totalTime);
- break;
- case "Add":
- String[] bandMembers = tokens[2].split(", ");
- bands.putIfAbsent(bandName, new ArrayList<>());
- members = bands.get(bandName);
- for (String currMember : bandMembers) {
- if (members.contains(currMember)) {
- continue;
- }
- members.add(currMember);
- }
- time.putIfAbsent(bandName, 0);
- break;
- }
- command = scanner.nextLine();
- }
- int totalTime = time.values().stream().mapToInt(value -> value).sum();
- System.out.println("Total time: "+ totalTime);
- time
- .entrySet()
- .stream()
- .sorted((f, s) -> {
- int result = s.getValue().compareTo(f.getValue());
- if (result==0){
- result = f.getKey().compareTo(s.getKey());
- }
- return result;
- })
- .forEach( band -> System.out.printf("%s -> %d%n",band.getKey(), band.getValue()));
- command = scanner.nextLine();
- System.out.println(command);
- for (String member : bands.get(command)) {
- System.out.println("=> "+ member);
- }
- }
- }
Add Comment
Please, Sign In to add comment