Advertisement
Tsuki11

Qnislav

Mar 23rd, 2020
163
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.90 KB | None | 0 0
  1. package com.company;
  2.  
  3. import java.util.*;
  4.  
  5. public class group_Qnislav_InboxManager {
  6.  
  7.     public static void main(String[] args) {
  8.         Scanner scanner = new Scanner(System.in);
  9.  
  10.         String input = scanner.nextLine();
  11.  
  12.         TreeMap<String, List<String>> map = new TreeMap<>();
  13.         int counter = 0;
  14.  
  15.         while (!input.equals("Statistics")) {
  16.             String[] command = input.split("->");
  17.             String action = command[0];
  18.             String username = command[1];
  19.             switch (action) {
  20.                 case "Add":
  21.                     if (map.containsKey(username)) {
  22.                         System.out.printf("%s is already registered%n", username);
  23.                     } else {
  24.                         map.put(username, new ArrayList<>());
  25.                         counter++;
  26.                     }
  27.                     break;
  28.                 case "Send":
  29.                     String email = command[2];
  30.                     if (map.containsKey(username)) {
  31.                         map.get(username).add(email);
  32.                     }
  33.                     break;
  34.                 case "Delete":
  35.                     if (map.containsKey(username)) {
  36.                         map.remove(username);
  37.                         counter--;
  38.                     } else {
  39.                         System.out.printf("%s not found!%n", username);
  40.                     }
  41.                     break;
  42.             }
  43.  
  44.             input = scanner.nextLine();
  45.         }
  46.         System.out.printf("Users count: %d%n", counter);
  47.         map
  48.                 .entrySet()
  49.                 .stream()
  50.                 .sorted((s1, s2) -> s2.getValue().size() - s1.getValue().size())
  51.                 .forEach(a->{
  52.                     System.out.println(a.getKey());
  53.                     a.getValue()
  54.                             .forEach(b -> System.out.printf("- %s%n",b));
  55.                 });
  56.     }
  57. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement