Advertisement
Kent_St_1

RetakeExamPart1-6January2017SoftuniKaraoke_02

Aug 23rd, 2021
899
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.77 KB | None | 0 0
  1. import java.util.*;
  2. import java.util.stream.Collectors;
  3.  
  4. public class SoftUniKaraoke_02 {
  5.     public static void main(String[] args) {
  6.         Scanner scan = new Scanner(System.in);
  7.         List<String> participantsInKaraoke = Arrays.stream(scan.nextLine().split(",\\s+")).collect(Collectors.toList());
  8.         List<String> songsInKaraoke = Arrays.stream(scan.nextLine().split(",\\s+")).collect(Collectors.toList());
  9.         Map<String, Participants> karaoke = new TreeMap<>();
  10.         String input = scan.nextLine();
  11.         while (!input.equals("dawn")) {
  12.             String[] tokens = input.split(",\\s+");
  13.             String participant = tokens[0];
  14.             String song = tokens[1];
  15.             String award = tokens[2];
  16.             if (participantsInKaraoke.contains(participant) && songsInKaraoke.contains(song) && !karaoke.containsKey(participant)) {
  17.                 Participants newParticipant = new Participants(song, new ArrayList<>());
  18.                 karaoke.put(participant, newParticipant);
  19.                 karaoke.get(participant).getAwards().add(award);
  20.             } else if (participantsInKaraoke.contains(participant) && songsInKaraoke.contains(song) && karaoke.containsKey(participant)) {
  21.                 if (!karaoke.get(participant).getAwards().contains(award)) {
  22.                     karaoke.get(participant).getAwards().add(award);
  23.                 }
  24.             }
  25.             input = scan.nextLine();
  26.         }
  27.         if (karaoke.size() > 0) {
  28.             karaoke
  29.                     .entrySet()
  30.                     .stream()
  31.                     .sorted((a, b) -> {
  32.                         int result = Integer.compare(b.getValue().getAwards().size(), a.getValue().getAwards().size());
  33.                         if (result == 0) {
  34.                             return a.getKey().compareTo(b.getKey());
  35.                         }
  36.                         return result;
  37.                     }).forEach(entry -> {
  38.                         System.out.printf("%s: %d awards%n", entry.getKey(), entry.getValue().getAwards().size());
  39.                         entry.getValue().getAwards().sort(String::compareTo);
  40.                         for (int i = 0; i <= entry.getValue().getAwards().size() - 1 ; i++) {
  41.                             String award = entry.getValue().getAwards().get(i);
  42.                             System.out.printf("--%s%n", award);
  43.                         }
  44.                     });
  45.         } else {
  46.             System.out.println("No awards");
  47.  
  48.  
  49.         }
  50.     }
  51.  
  52. }
  53. class Participants {
  54.     private String song;
  55.     private List<String> awards;
  56.  
  57.     public List<String> getAwards() {
  58.         return awards;
  59.     }
  60.     public Participants(String song, List<String> awards) {
  61.         this.song = song;
  62.         this.awards = awards;
  63.     }
  64. }
  65.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement