Advertisement
svephoto

Nikulden’s meals [Java]

Jul 18th, 2020
361
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.08 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class treasureHunt {
  4.     public static void main(String[] args) {
  5.         Scanner sc = new Scanner(System.in);
  6.  
  7.         int unLiked = 0;
  8.         TreeMap<String, List<String>> listMap = new TreeMap<>();
  9.  
  10.         List<String> dishes = new ArrayList<>();
  11.  
  12.  
  13.  
  14.          String input = sc.nextLine();
  15.  
  16.         while (!"Stop".equals(input)){
  17.             String[] token = input.split("-");
  18.             String likeUnlike = token[0];
  19.             String name = token[1];
  20.             String dish = token[2];
  21.  
  22.             if (likeUnlike.equals("Like")){
  23.  
  24.                 if (listMap.containsKey(name)){
  25.                     if (!listMap.get(name).contains(dish)){
  26.                         listMap.get(name).add(dish);
  27.                     }
  28.                 }else {
  29.                     dishes=new ArrayList<>();
  30.                     dishes.add(dish);
  31.                     listMap.put(name,dishes);
  32.                 }
  33.  
  34.             }else if (likeUnlike.equals("Unlike")){
  35.                 if (!listMap.containsKey(name)){
  36.                     System.out.printf("%s is not at the party.%n",name);
  37.                 } else if (!listMap.get(name).contains(dish)){
  38.                     System.out.printf("%s doesn't have the %s in his/her collection.%n", name,dish);
  39.                 }else  {
  40.                     unLiked++;
  41.                     listMap.get(name).remove(dish);
  42.                     System.out.printf("%s doesn't like the %s.%n",name,dish);
  43.  
  44.                 }
  45.             }
  46.  
  47.  
  48.             input= sc.nextLine();
  49.         }
  50.  
  51.         listMap.entrySet().stream()
  52.                 .sorted(Map.Entry.<String, List<String>>comparingByValue(Comparator.comparing(List::size))
  53.                         .reversed()
  54.                         .thenComparing(Map.Entry.comparingByKey()))
  55.                 .forEach(e -> {
  56.                     System.out.printf("%s: ", e.getKey());
  57.                     String meals = String.join(", ", e.getValue());
  58.                     System.out.println(meals);
  59.                 });
  60.      
  61.         System.out.printf("Unliked meals: %d", unLiked);
  62.     }
  63. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement