Advertisement
meteor4o

JF-Exams-Concert

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