Advertisement
IrinaIgnatova

ExamPrep - Concert

Aug 1st, 2019
138
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.57 KB | None | 0 0
  1.  
  2.  
  3. package com.company;
  4.  
  5.  
  6. import java.util.*;
  7. import java.util.regex.Matcher;
  8. import java.util.regex.Pattern;
  9. import java.util.stream.Collectors;
  10.  
  11. public class Main {
  12.  
  13.     public static void main(String[] args) {
  14.  
  15.         Scanner scanner = new Scanner(System.in);
  16.  
  17.         String input = scanner.nextLine();
  18.  
  19.         LinkedHashMap<String, List<String>> bandAndNames = new LinkedHashMap<>();
  20.         LinkedHashMap<String, Integer> playTime = new LinkedHashMap<>();
  21.  
  22.         int totalTime = 0;
  23.  
  24.         while (!input.equals("start of concert")) {
  25.             String[] tokens = input.split("; ");
  26.             String command = tokens[0];
  27.             String band = tokens[1];
  28.  
  29.             if (command.equals("Add")) {
  30.  
  31.                 List<String> members = Arrays.stream(tokens[2].split(", ")).collect(Collectors.toList());
  32.  
  33.                 if (!bandAndNames.containsKey(band)) {
  34.  
  35.                     bandAndNames.put(band, members);
  36.  
  37.                 } else {
  38.                     for (String member : members) {
  39.                         if (!bandAndNames.get(band).contains(member)) {
  40.                             bandAndNames.get(band).add(member);
  41.                         }
  42.                     }
  43.                 }
  44.             } else if (command.equals("Play")) {
  45.                 int time = Integer.parseInt(tokens[2]);
  46.                 totalTime += time;
  47.  
  48.                 if (!playTime.containsKey(band)) {
  49.                     playTime.put(band, time);
  50.                 } else {
  51.                     Integer currentTime = playTime.get(band);
  52.                     playTime.put(band, currentTime + time);
  53.                 }
  54.             }
  55.  
  56.  
  57.             input = scanner.nextLine();
  58.         }
  59.  
  60.         String band = scanner.nextLine();
  61.  
  62.         System.out.printf("Total time: %d%n", totalTime);
  63.         playTime.entrySet()
  64.                 .stream()
  65.                 .sorted((f, s) -> {
  66.                     int result = s.getValue() - f.getValue();// тук е минус, но е същото като Compare
  67.                     if (result == 0) {
  68.                         result = f.getKey().compareTo(s.getKey());// тук не може минус, защото са String-ове
  69.                     }
  70.                     return result;
  71.                 })
  72.                 .forEach(entry -> {
  73.                     System.out.println(entry.getKey() + " -> " + entry.getValue());
  74.                 });
  75.  
  76.         System.out.println(band);
  77.  
  78.         for (String name : bandAndNames.get(band)) {
  79.             System.out.println("=> " + name);
  80.  
  81.         }
  82.  
  83.  
  84.     }
  85. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement