deiom

Untitled

Aug 3rd, 2019
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.36 KB | None | 0 0
  1. //Messages Manager
  2. //Create a program that manages messages sent and received of users. You need to keep information about username, their sent and received messages. You will receive the capacity of possible messages kept at once per user. You will be receiving lines with commands until you receive the "Statistics" command. There are three possible commands:
  3. //• "Add={username}={sent}={received}":
  4. //o Add the username, his/her sent and received messages to your records. If person with the given username already exists ignore the line.
  5. //• "Message={sender}={receiver}":
  6. //o Check if both usernames exist and if they do, increase the sender’s sent messages by 1 and the receiver’s received messages by 1. If anyone reaches the capacity (first check the sender), he/she should be removed from the record and you should print the following message:
  7. // "{username} reached the capacity!"
  8. //• "Empty={username}":
  9. //o Delete all records of the given user, if he exists. If "All" is given as username - delete all records you have.
  10. //In the end, you have to print the count of users, each person with his/her messages (the count of both sent and received) sorted in descending order by the received messages and then by their username in ascending order in the following format:
  11. //Users count: {count}
  12. //{username} - {messages}
  13. //{username} - {messages}
  14. //Input
  15. //• On the first line, you will receive the capacity - an integer number in the range [1-10000].
  16. //• You will be receiving lines until you receive the "Statistics" command.
  17. //• The initial messages (sent and received) will always be below the capacity.
  18. //• The input will always be valid.
  19. //Output
  20. //• Print the appropriate message after the "Message" command, if someone reaches the capacity.
  21. //• Print the users with their messages in the format described above.
  22. // 
  23. //Examples
  24. //Input Output
  25. //10
  26. //Add=Mark=5=4
  27. //Add=Clark=3=5
  28. //Add=Berg=9=0
  29. //Add=Kevin=0=0
  30. //Message=Berg=Kevin
  31. //Statistics
  32. // Berg reached the capacity!
  33. //Users count: 3
  34. //Clark - 8
  35. //Mark - 9
  36. //Kevin - 1
  37. //Comments
  38. //First, we receive the capacity (10). Then we start receiving commands. The first four commands are for adding new users, so we do it. Then we have the command “Message=Berg=Kevin” and Berg reached the capacity, so we remove him, but Kevin has only his received messages incremented. When we receive the “Statistics” command, we print the output as described above.
  39. //
  40. //20
  41. //Add=Mark=3=9
  42. //Add=Berry=5=5
  43. //Add=Clark=4=0
  44. //Empty=Berry
  45. //Add=Blake=9=3
  46. //Add=Michael=3=9
  47. //Add=Amy=9=9
  48. //Message=Blake=Amy
  49. //Message=Michael=Amy
  50. //Statistics Amy reached the capacity!
  51. //Users count: 4
  52. //Mark - 12
  53. //Michael - 13
  54. //Blake - 13
  55. //Clark - 4
  56. //
  57. //12
  58. //Add=Bonnie=3=5
  59. //Add=Johny=4=4
  60. //Empty=All
  61. //Add=Bonnie=3=3
  62. //Statistics Users count: 1
  63. //Bonnie - 6
  64.  
  65. package PracticalExam;
  66.  
  67. import com.sun.jdi.Value;
  68.  
  69. import java.security.Key;
  70. import java.util.*;
  71.  
  72. public class MessagesManager {
  73. public static void main(String[] args) {
  74. Scanner scanner = new Scanner(System.in);
  75. int capacity = Integer.parseInt(scanner.nextLine());
  76. TreeMap<String, Integer> senders = new TreeMap<>();
  77. TreeMap<String, Integer> receivers = new TreeMap<>();
  78. int countReceivers = 0;
  79. int countSenders = 0;
  80.  
  81. String input = scanner.nextLine();
  82.  
  83. while (!input.equals("Statistics")) {
  84. String[] tokens = input.split("=");
  85. String command = tokens[0];
  86. switch (command) {
  87. case "Add":
  88. String username = tokens[1];
  89. int sent = Integer.parseInt(tokens[2]);
  90. int received = Integer.parseInt(tokens[3]);
  91. receivers.putIfAbsent(username, received);
  92. countReceivers++;
  93. senders.putIfAbsent(username, sent);
  94. countSenders++;
  95. break;
  96. case "Message":
  97. String sender = tokens[1];
  98. String receiver = tokens[2];
  99.  
  100. if (senders.containsKey(sender)) {
  101. senders.replace(username, sent + 1);
  102. }
  103. if (receivers.containsKey(receiver)) {
  104. receivers.replace(username, received + 1);
  105. }
  106.  
  107.  
  108. //Integer replace = mining.get(resourceName) + resourceQty;
  109. //mining.replace(resourceName, replace);
  110. break;
  111. case "Empty":
  112. String name = tokens[1];
  113. if (senders.containsKey(name)) {
  114. senders.remove(name);
  115. countSenders--;
  116. } else if (receivers.containsKey(name)) {
  117. receivers.remove(name);
  118. countReceivers--;
  119. } else if (name.equals("All")) {
  120. senders.remove(username);
  121. countSenders = 0;
  122. receivers.remove(username);
  123. countReceivers = 0;
  124. break;
  125. }
  126. }
  127. input = scanner.nextLine();
  128. }
  129. System.out.println("Users count: " + (countReceivers + countSenders) / 2);
  130. }
  131. }
Advertisement
Add Comment
Please, Sign In to add comment