Advertisement
NadezhdaGeorgieva

Fin03_13Dec_Meals2

Dec 14th, 2020 (edited)
728
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.19 KB | None | 0 0
  1. package bg.softuni.javafundamentals;
  2.  
  3. import java.util.*;
  4.  
  5. public class Fin03_13Dec_Meals2 {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         Map<String, List<String>> nameLike = new HashMap<>();
  10.         int unlikedMeals = 0;
  11.         String word;
  12.         while (!(word = scanner.nextLine()).equals("Stop")) {
  13.             String[] tokens = word.split("-");
  14.             String name = tokens[1];
  15.             String meal = tokens[2];
  16.             switch (tokens[0]) {
  17.                 case "Like":
  18.                     if (!nameLike.containsKey(name)) {
  19.                         List<String> meals = new ArrayList<>();
  20.                         meals.add(meal);
  21.                         nameLike.put(name, meals);
  22.                     } else {
  23.                         nameLike.get(name).add(meal);
  24.                     }
  25.                     break;
  26.                 case "Unlike":
  27.                     if (!nameLike.containsKey(name)) {
  28.                         System.out.printf("%s is not at the party.%n", name);
  29.                     } else {
  30.  
  31.                         if (nameLike.get(name).contains(meal)) {
  32.                             nameLike.get(name).remove(meal);
  33.                             System.out.printf("%s doesn't like the %s.%n", name, meal);
  34.                             unlikedMeals++;
  35.                         } else {
  36.                             System.out.printf("%s doesn't have the %s in his/her collection.%n", name, meal);
  37.                         }
  38.                     }
  39.                     break;
  40.             }
  41.         }
  42.  
  43.         nameLike.entrySet()
  44.                 .stream()
  45.                 .sorted((e1, e2) -> {
  46.                     int result = Integer.compare(e2.getValue().size(), e1.getValue().size());
  47.                     if (result == 0){
  48.                         result = e1.getKey().compareTo(e2.getKey());
  49.                     }
  50.                     return result;
  51.                 })
  52.                 .forEach(entry -> {
  53.                     System.out.printf("%s: %s%n", entry.getKey(), String.join(", ", entry.getValue()));
  54.                 });
  55.         System.out.printf("Unliked meals: %d%n", unlikedMeals);
  56.     }
  57. }
  58.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement