Advertisement
Tsuki11

group_Task3

Mar 23rd, 2020
148
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.22 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.         HashMap<String, List<String>> map = new HashMap<>();
  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.  
  47.         System.out.printf("Users count: %d%n",map.keySet().size());
  48.         map
  49.                 .entrySet()
  50.                 .stream()
  51.                 .sorted((a,b)->{
  52.                     int first = a.getValue().size();
  53.                     int second = b.getValue().size();
  54.                     int compare = second-first;
  55.                     if(compare==0){
  56.                         compare = a.getKey().compareTo(b.getKey());
  57.                     }
  58.                     return compare;
  59.                 })
  60.                 .forEach(a->{
  61.                     System.out.println(a.getKey());
  62.                     a.getValue()
  63.                             .forEach(b -> System.out.printf("- %s%n",b));
  64.                 });
  65.  
  66.  
  67.     }
  68. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement