Advertisement
medon3

Untitled

Jun 15th, 2022
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.77 KB | None | 0 0
  1. import java.util.*;
  2.  
  3. public class DoctorsOffice {
  4.  
  5. public static void main(String[] args) {
  6.  
  7. Scanner sc = new Scanner(System.in);
  8. String[] inputCommand = sc.nextLine().split(" ");
  9.  
  10. List<String> queuePeople = new ArrayList<>();
  11. Map<String, Integer> names = new HashMap<>();
  12.  
  13. while (!inputCommand[0].equals("Еnd")) {
  14. switch (inputCommand[0]) {
  15. case "Append":
  16. String name = inputCommand[1];
  17. queuePeople.add(name);
  18. names.putIfAbsent(inputCommand[1], 0);
  19. names.replace(inputCommand[1], names.get(inputCommand[1]) + 1);
  20. System.out.println("OK");
  21. break;
  22. case "Insert":
  23. int position = Integer.parseInt(inputCommand[1]);
  24. name = inputCommand[2];
  25. if(position>queuePeople.size()){
  26. System.out.println("Error");
  27. break;
  28. }
  29. queuePeople.add(position, name);
  30. names.putIfAbsent(name, 0);
  31. names.replace(name, names.get(inputCommand[2]) + 1);
  32. System.out.println("OK");
  33. break;
  34. case "Find":
  35. name = inputCommand[1];
  36. if(names.containsKey(name)){
  37. System.out.println(names.get(name));
  38. break;
  39. }
  40. System.out.println(0);
  41. break;
  42. case "Examine":
  43. int count = Integer.parseInt(inputCommand[1]);
  44. if (count > queuePeople.size()) {
  45. System.out.println("Error");
  46. break;
  47. }
  48. Stack<String> examineStack = new Stack<>();
  49. for (int i = 0; i < count; i++) {
  50. examineStack.push(queuePeople.remove(0));
  51. names.replace(examineStack.peek(),
  52. names.get(examineStack.peek()) - 1);
  53. }
  54. for (String person : examineStack) {
  55. System.out.print(person + " ");
  56. }
  57. System.out.println();
  58. break;
  59.  
  60. }
  61.  
  62. inputCommand = sc.nextLine().split(" ");
  63. }
  64.  
  65. // for (String str : queuePeople) {
  66. // if (names.containsKey(str)) {
  67. // int currentCount = names.get(str);
  68. // names.put(str, currentCount + 1);
  69. // } else {
  70. // names.put(str, 1);
  71. // }
  72. }
  73.  
  74. }
  75.  
  76.  
  77.  
  78.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement