Advertisement
ivanmitkoff

demo

Dec 7th, 2019
240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.43 KB | None | 0 0
  1. package PastFinalExams.finexam;
  2.  
  3. import java.util.*;
  4.  
  5. public class Inbox {
  6.     public static void main(String[] args) {
  7.         Scanner scanner = new Scanner(System.in);
  8.  
  9.         Map<String, List<String>> usersInfo = new LinkedHashMap<>();
  10.         Map<String, Integer> sentEmails = new LinkedHashMap<>();
  11.  
  12.         String command;
  13.         while (!(command = scanner.nextLine()).equals("Statistics")) {
  14.             String[] tokens = command.split("->");
  15.             String subCommand = tokens[0];
  16.             String username = tokens[1];
  17.             int sentCount = 0;
  18.  
  19.             switch (subCommand) {
  20.  
  21.                 case "Add":
  22.                     if (usersInfo.containsKey(username)) {
  23.                         System.out.println(String.format("%s is already registered", username));
  24.                     } else {
  25.                         usersInfo.put(username, new ArrayList<>());
  26.                     }
  27.                     /*List<String> sentEmails = usersInfo.get(username);
  28.                     if (!sentEmails.contains(usersInfo.get(username))) {
  29.                         sentEmails.add(username);
  30.                     }
  31.                     */
  32.                     break;
  33.                 case "Send":
  34.                     String email = tokens[2];
  35.                     sentEmails.put(username, 1);
  36.                     List<String> sentList = usersInfo.get(username);
  37.                     usersInfo.get(username).add(email);
  38.                     break;
  39.                 case "Delete":
  40.                     if (usersInfo.containsKey(username)) {
  41.                         usersInfo.remove(username);
  42.                     } else {
  43.                         System.out.println(String.format("%s not found!", username));
  44.                     }
  45.                     break;
  46.             }
  47.         }
  48.         System.out.println(String.format("Users count: %d", usersInfo.size()));
  49.  
  50.  
  51.         sentEmails
  52.                 .entrySet()
  53.                 .stream()
  54.                 .sorted((a, b) -> {
  55.                     int result = b.getValue().compareTo(a.getValue());
  56.                     if (result == 0) {
  57.                         result = a.getKey().compareTo(b.getKey());
  58.                     }
  59.                     return result;
  60.                 }).forEach(e -> {
  61.                     System.out.println(e.getKey());
  62.         });
  63.  
  64.         for (String user : usersInfo) {
  65.             System.out.println(String.format("- %s", sentEmails));
  66.     }
  67. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement