Advertisement
damesova

Concert [Mimi]

Mar 28th, 2019
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.23 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class _01_Concert {
  4.     public static void main(String[] args) {
  5.         Scanner scanner = new Scanner(System.in);
  6.  
  7.         Map<String, List<String>> bandsMembers = new LinkedHashMap<>();
  8.         Map<String, Integer> bandsTimes = new HashMap<>();
  9.  
  10.         String input = "";
  11.         while (!"start of concert".equals(input = scanner.nextLine())) {
  12.             String[] line = input.split("; ");
  13.             String command = line[0];
  14.             String band = line[1];
  15.             String[] memb = line[2].split(", ");
  16.             if (command.equals("Add")) {
  17.                 bandsMembers.putIfAbsent(band, new ArrayList<>());
  18.                 List<String> members = bandsMembers.get(band);
  19.                 for (int i = 0; i < memb.length; i++) {
  20.                     if (!members.contains(memb[i])) {
  21.                         members.add(memb[i]);
  22.                     }
  23.                 }
  24.  
  25.                 bandsTimes.putIfAbsent(band, 0);
  26.  
  27.             } else if (command.equals("Play")) {
  28.                 int time = Integer.parseInt(line[2]);
  29.                 bandsTimes.putIfAbsent(band, 0);
  30.                 bandsTimes.put(band, bandsTimes.get(band) + time);
  31.             }
  32.         }
  33.         int sum = 0;
  34.  
  35.         for (Map.Entry<String, Integer> entry : bandsTimes.entrySet()) {
  36.             sum += entry.getValue();
  37.         }
  38.         System.out.println("Total time: " + sum);
  39.  
  40.         bandsTimes.entrySet()
  41.                 .stream()
  42.                 .sorted(Map.Entry.<String, Integer>comparingByValue().reversed()
  43.                 ).forEach(e -> {
  44.             System.out.printf("%s -> %d%n", e.getKey(), e.getValue());
  45.         });
  46.  
  47.         String bandListing = scanner.nextLine();
  48.  
  49.         bandsMembers.entrySet()
  50.                 .stream()
  51.                 .sorted((x, y) -> Integer.compare(y.getValue().size(), x.getValue().size()))
  52.                 .forEach(e -> {
  53.                     if (e.getKey().equals(bandListing)) {
  54.                         System.out.println(e.getKey());
  55.  
  56.                         for (int i = 0; i < e.getValue().size(); i++) {
  57.                             System.out.printf("=> %s%n", e.getValue().get(i));
  58.                         }
  59.                     }
  60.                 });
  61.     }
  62. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement