Advertisement
simonradev

Hospital

Jun 27th, 2017
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 4.20 KB | None | 0 0
  1. namespace _04.Hospital
  2. {
  3.     using System;
  4.     using System.Collections.Generic;
  5.     using System.Linq;
  6.     using System.Text;
  7.  
  8.     public class StartUp
  9.     {
  10.         public static void Main()
  11.         {
  12.             Dictionary<string, List<string>> doctorsAndPatients = new Dictionary<string, List<string>>();
  13.             Dictionary<string, Dictionary<int, List<string>>> deparmentsAndRooms = new Dictionary<string, Dictionary<int, List<string>>>();
  14.  
  15.             string inputLine;
  16.             while ((inputLine = Console.ReadLine()) != "Output")
  17.             {
  18.                 string[] tokens = SplitString(inputLine);
  19.  
  20.                 string department = tokens[0];
  21.                 string doctorName = tokens[1] + " " + tokens[2];
  22.                 string patientName = tokens[3];
  23.  
  24.                 //subscribing the patient to the doctor
  25.                 if (!doctorsAndPatients.ContainsKey(doctorName))
  26.                 {
  27.                     doctorsAndPatients[doctorName] = new List<string>();
  28.                 }
  29.  
  30.                 doctorsAndPatients[doctorName].Add(patientName);
  31.                 //END OF SUBSCRIPTION
  32.                
  33.  
  34.                 //Place the patient in a room
  35.                 if (!deparmentsAndRooms.ContainsKey(department))
  36.                 {
  37.                     deparmentsAndRooms[department] = new Dictionary<int, List<string>>();
  38.                 }
  39.  
  40.                 int roomIndex = 1;
  41.                 if (deparmentsAndRooms[department].Count > 0)
  42.                 {
  43.                     roomIndex = deparmentsAndRooms[department].Keys.Last();
  44.                 }
  45.  
  46.                 if (!deparmentsAndRooms[department].ContainsKey(roomIndex))
  47.                 {
  48.                     deparmentsAndRooms[department][roomIndex] = new List<string>();
  49.                 }
  50.  
  51.                 if (deparmentsAndRooms[department][roomIndex].Count < 3)
  52.                 {
  53.                     deparmentsAndRooms[department][roomIndex].Add(patientName);
  54.                 }
  55.                 else
  56.                 {
  57.                     roomIndex++;
  58.  
  59.                     if (roomIndex > 20)
  60.                     {
  61.                         continue;
  62.                     }
  63.  
  64.                     deparmentsAndRooms[department][roomIndex] = new List<string>
  65.                     {
  66.                         patientName
  67.                     };
  68.                 }
  69.             }
  70.  
  71.             StringBuilder result = new StringBuilder();
  72.             while ((inputLine = Console.ReadLine()) != "End")
  73.             {
  74.                 string[] tokens = SplitString(inputLine);
  75.  
  76.                 if (tokens.Length == 2)
  77.                 {
  78.                     int roomIndex;
  79.                     if (int.TryParse(tokens[1], out roomIndex))
  80.                     {
  81.                         string department = tokens[0];
  82.  
  83.                         List<string> patients = deparmentsAndRooms[department][roomIndex];
  84.  
  85.                         foreach (string patient in patients.OrderBy(n => n))
  86.                         {
  87.                             result.AppendLine(patient);
  88.                         }
  89.                     }
  90.                     else
  91.                     {
  92.                         string doctorName = tokens[0] + " " + tokens[1];
  93.  
  94.                         List<string> patients = doctorsAndPatients[doctorName];
  95.  
  96.                         foreach (string patient in patients.OrderBy(n => n))
  97.                         {
  98.                             result.AppendLine(patient);
  99.                         }
  100.                     }
  101.                 }
  102.                 else
  103.                 {
  104.                     string department = tokens[0];
  105.  
  106.                     foreach (KeyValuePair<int, List<string>> kvp in deparmentsAndRooms[department])
  107.                     {
  108.                         foreach (string patient in kvp.Value)
  109.                         {
  110.                             result.AppendLine(patient);
  111.                         }
  112.                     }
  113.                 }
  114.             }
  115.  
  116.             Console.Write(result);
  117.         }
  118.  
  119.         private static string[] SplitString(string stringToSplit)
  120.         {
  121.             return stringToSplit.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
  122.         }
  123.     }
  124. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement