Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.telerikacademy;
- import java.util.ArrayList;
- import java.util.HashMap;
- import java.util.Scanner;
- public class DoctorsOffice2 {
- public static final ArrayList<String> patientsList = new ArrayList<>();
- public static final HashMap<String, Integer> byCount = new HashMap<>();
- public static void main(String[] args) {
- Scanner input = new Scanner(System.in);
- String[] command = input.nextLine().split(" ");
- while (!command[0].equals("End")) {
- switch (command[0]) {
- case "Append":
- append(command);
- break;
- case "Insert":
- insert(command);
- break;
- case "Find":
- find(command);
- break;
- case "Examine":
- examine(command);
- break;
- }
- command = input.nextLine().split(" ");
- }
- }
- private static void examine(String[] command) {
- int patientsCount = Integer.parseInt(command[1]);
- if (patientsCount < 0 || patientsCount > patientsList.size()){
- System.out.println("Error");
- return;
- }
- if (patientsCount == patientsList.size()){
- System.out.println(String.join(" ", patientsList));
- patientsList.clear();
- byCount.clear();
- return;
- }
- for (int i = 0; i < patientsCount; i++) {
- String patient = patientsList.remove(0);
- int count = byCount.get(patient);
- if (count == 1){
- byCount.remove(patient);
- }
- else {
- byCount.replace(patient, count-1);
- }
- System.out.print(patient);
- if(i != patientsCount - 1){
- System.out.print(" ");
- }
- }
- System.out.println();
- }
- private static void find(String[] command) {
- String patient = command[1];
- System.out.println(byCount.getOrDefault(patient, 0));
- }
- private static void insert(String[] command) {
- int position = Integer.parseInt(command[1]);
- String patient = command[2];
- if (position < 0 || position > patientsList.size()){
- System.out.println("Error");
- return;
- }
- patientsList.add(position, patient);
- if (byCount.containsKey(patient)){
- int count = byCount.get(patient) + 1;
- byCount.replace(patient, count);
- }
- else {
- byCount.put(patient,1);
- }
- System.out.println("OK");
- }
- private static void append(String[] command) {
- String newPatient = command[1];
- patientsList.add(newPatient);
- if (byCount.containsKey(newPatient)){
- int count = byCount.get(newPatient) + 1;
- byCount.replace(newPatient, count);
- }
- else {
- byCount.put(newPatient,1);
- }
- System.out.println("OK");
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement