Advertisement
chavdardim1990

Untitled

Jan 24th, 2022
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.39 KB | None | 0 0
  1. package com.telerikacademy;
  2.  
  3. import java.util.ArrayList;
  4. import java.util.HashMap;
  5. import java.util.Scanner;
  6.  
  7. public class DoctorsOffice2 {
  8.  
  9. public static final ArrayList<String> patientsList = new ArrayList<>();
  10. public static final HashMap<String, Integer> byCount = new HashMap<>();
  11.  
  12. public static void main(String[] args) {
  13. Scanner input = new Scanner(System.in);
  14. String[] command = input.nextLine().split(" ");
  15.  
  16. while (!command[0].equals("End")) {
  17. switch (command[0]) {
  18. case "Append":
  19. append(command);
  20. break;
  21. case "Insert":
  22. insert(command);
  23. break;
  24. case "Find":
  25. find(command);
  26. break;
  27. case "Examine":
  28. examine(command);
  29. break;
  30. }
  31. command = input.nextLine().split(" ");
  32. }
  33. }
  34.  
  35. private static void examine(String[] command) {
  36. int patientsCount = Integer.parseInt(command[1]);
  37. if (patientsCount < 0 || patientsCount > patientsList.size()){
  38. System.out.println("Error");
  39. return;
  40. }
  41.  
  42. if (patientsCount == patientsList.size()){
  43. System.out.println(String.join(" ", patientsList));
  44. patientsList.clear();
  45. byCount.clear();
  46. return;
  47. }
  48. for (int i = 0; i < patientsCount; i++) {
  49. String patient = patientsList.remove(0);
  50. int count = byCount.get(patient);
  51. if (count == 1){
  52. byCount.remove(patient);
  53. }
  54. else {
  55. byCount.replace(patient, count-1);
  56. }
  57. System.out.print(patient);
  58. if(i != patientsCount - 1){
  59. System.out.print(" ");
  60. }
  61. }
  62. System.out.println();
  63. }
  64.  
  65. private static void find(String[] command) {
  66. String patient = command[1];
  67. System.out.println(byCount.getOrDefault(patient, 0));
  68. }
  69.  
  70. private static void insert(String[] command) {
  71. int position = Integer.parseInt(command[1]);
  72. String patient = command[2];
  73.  
  74. if (position < 0 || position > patientsList.size()){
  75. System.out.println("Error");
  76. return;
  77. }
  78.  
  79. patientsList.add(position, patient);
  80.  
  81. if (byCount.containsKey(patient)){
  82. int count = byCount.get(patient) + 1;
  83. byCount.replace(patient, count);
  84. }
  85. else {
  86. byCount.put(patient,1);
  87. }
  88. System.out.println("OK");
  89. }
  90.  
  91. private static void append(String[] command) {
  92. String newPatient = command[1];
  93.  
  94. patientsList.add(newPatient);
  95.  
  96. if (byCount.containsKey(newPatient)){
  97. int count = byCount.get(newPatient) + 1;
  98. byCount.replace(newPatient, count);
  99. }
  100. else {
  101. byCount.put(newPatient,1);
  102. }
  103. System.out.println("OK");
  104. }
  105. }
  106.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement