Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.company;
- import java.util.*;
- import java.util.regex.Matcher;
- import java.util.regex.Pattern;
- import java.util.stream.Collectors;
- public class Main {
- public static void main(String[] args) {
- Scanner scanner = new Scanner(System.in);
- String input = scanner.nextLine();
- LinkedHashMap<String, List<String>> bandAndNames = new LinkedHashMap<>();
- LinkedHashMap<String, Integer> playTime = new LinkedHashMap<>();
- int totalTime = 0;
- while (!input.equals("start of concert")) {
- String[] tokens = input.split("; ");
- String command = tokens[0];
- String band = tokens[1];
- if (command.equals("Add")) {
- List<String> members = Arrays.stream(tokens[2].split(", ")).collect(Collectors.toList());
- if (!bandAndNames.containsKey(band)) {
- bandAndNames.put(band, members);
- } else {
- for (String member : members) {
- if (!bandAndNames.get(band).contains(member)) {
- bandAndNames.get(band).add(member);
- }
- }
- }
- } else if (command.equals("Play")) {
- int time = Integer.parseInt(tokens[2]);
- totalTime += time;
- if (!playTime.containsKey(band)) {
- playTime.put(band, time);
- } else {
- Integer currentTime = playTime.get(band);
- playTime.put(band, currentTime + time);
- }
- }
- input = scanner.nextLine();
- }
- String band = scanner.nextLine();
- System.out.printf("Total time: %d%n", totalTime);
- playTime.entrySet()
- .stream()
- .sorted((f, s) -> {
- int result = s.getValue() - f.getValue();// тук е минус, но е същото като Compare
- if (result == 0) {
- result = f.getKey().compareTo(s.getKey());// тук не може минус, защото са String-ове
- }
- return result;
- })
- .forEach(entry -> {
- System.out.println(entry.getKey() + " -> " + entry.getValue());
- });
- System.out.println(band);
- for (String name : bandAndNames.get(band)) {
- System.out.println("=> " + name);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement