Advertisement
Guest User

Hospital

a guest
Feb 21st, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 6.07 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4.  
  5. namespace SampleExam_P01.Hospital
  6. {
  7.     public class Hospital
  8.     {
  9.         public static void Main()
  10.         {
  11.             var hospital = new Dictionary<string, Department>();
  12.             var doctors = new List<Doctor>();
  13.            
  14.             FillHospitalDB(hospital, doctors);
  15.  
  16.             // executing commands from console
  17.             string command = Console.ReadLine();
  18.             while (command != "End")
  19.             {
  20.                 string[] commandArray = command.Split(' ', StringSplitOptions.RemoveEmptyEntries);
  21.  
  22.                 if (commandArray.Length == 1)
  23.                 {
  24.                     if (hospital.ContainsKey(commandArray[0]))
  25.                     {
  26.                         var departmentToPrint = hospital[commandArray[0]];
  27.  
  28.                         foreach (var room in departmentToPrint.Rooms)
  29.                         {
  30.                             foreach (var patient in room.Patients)
  31.                             {
  32.                                 Console.WriteLine(patient.Name);
  33.                             }
  34.                         }
  35.                     }
  36.                 }
  37.  
  38.                 else if (commandArray.Length == 2)
  39.                 {
  40.                     string firstName = commandArray[0];
  41.                     string lastName = commandArray[1];
  42.  
  43.                     var doctorToPrint = doctors.FirstOrDefault(x => x.FirstName == firstName && x.LastName == lastName);
  44.  
  45.                     if (doctorToPrint != null)
  46.                     {
  47.                         List<Patient> doctorsPatients = doctorToPrint.Patients.OrderBy(x => x.Name).ToList();
  48.  
  49.                         foreach (var patient in doctorsPatients)
  50.                         {
  51.                             Console.WriteLine(patient.Name);
  52.                         }
  53.                     }
  54.                     else
  55.                     {
  56.                         string department = commandArray[0];
  57.                         int roomNumber = int.Parse(commandArray[1]);
  58.  
  59.                         var currentRoom = hospital[department].Rooms.FirstOrDefault(x => x.Number == roomNumber);
  60.  
  61.                         if (currentRoom != null)
  62.                         {
  63.                             foreach (var patient in currentRoom.Patients.OrderBy(x => x.Name))
  64.                             {
  65.                                 Console.WriteLine(patient.Name);
  66.                             }
  67.                         }
  68.                     }
  69.                 }
  70.                 command = Console.ReadLine();
  71.             }
  72.         }
  73.  
  74.         private static void FillHospitalDB(Dictionary<string, Department> hospital, List<Doctor> doctors)
  75.         {
  76.             string[] input = Console.ReadLine().Split();
  77.             while (input[0] != "Output")
  78.             {
  79.                 string department = input[0];
  80.                 string doctorFistName = input[1];
  81.                 string doctorLastName = input[2];
  82.                 string patientName = input[3];
  83.  
  84.                 var newPatient = new Patient(patientName);
  85.                 var currentDoctor = doctors.FirstOrDefault(x => x.FirstName == doctorFistName && x.LastName == doctorLastName);
  86.  
  87.                 // adding patient to department
  88.                 if (!hospital.ContainsKey(department))
  89.                 {
  90.                     hospital.Add(department, new Department());
  91.                     hospital[department].Add(newPatient);
  92.                 }
  93.                 else
  94.                 {
  95.                     if (hospital[department].HasFreeRooms())
  96.                     {
  97.                         hospital[department].Add(newPatient);
  98.                     }
  99.                 }
  100.  
  101.                 //adding patient to doctor's list
  102.                 if (currentDoctor == null)
  103.                 {
  104.                     doctors.Add(new Doctor(doctorFistName, doctorLastName));
  105.                     currentDoctor = doctors.FirstOrDefault(x => x.FirstName == doctorFistName && x.LastName == doctorLastName);
  106.                 }
  107.  
  108.                 currentDoctor.Patients.Add(newPatient);
  109.  
  110.                 input = Console.ReadLine().Split();
  111.             }
  112.         }
  113.     }
  114.  
  115.     public class Department
  116.     {
  117.         private const int bedCapacity = 60;
  118.         private const int roomCapacity = 3;
  119.  
  120.         public Department()
  121.         {
  122.             this.Rooms = new List<Room>();
  123.         }
  124.  
  125.         public List<Room> Rooms { get; set; }
  126.  
  127.         public bool HasFreeRooms()
  128.         {
  129.             return bedCapacity - this.Rooms.Sum(x => x.Patients.Count) > 0;
  130.         }
  131.  
  132.         public void Add(Patient patient)
  133.         {
  134.             bool roomFound = false;
  135.  
  136.             foreach (var room in this.Rooms)
  137.             {
  138.                 if (room.Patients.Count == roomCapacity)
  139.                 {
  140.                     continue;
  141.                 }
  142.                 else
  143.                 {
  144.                     room.Patients.Add(patient);
  145.                     roomFound = true;
  146.                 }
  147.             }
  148.  
  149.             if (roomFound == false)
  150.             {
  151.                 var newRoom = new Room(this.Rooms.Count + 1);
  152.                 newRoom.Patients.Add(patient);
  153.                 this.Rooms.Add(newRoom);
  154.             }
  155.         }
  156.     }
  157.  
  158.     public class Room
  159.     {
  160.         public Room(int number)
  161.         {
  162.             this.Number = number;
  163.             this.Patients = new List<Patient>();
  164.         }
  165.  
  166.         public int Number { get; set; }
  167.         public List<Patient> Patients { get; set; }
  168.     }
  169.  
  170.     public class Patient
  171.     {
  172.         public Patient(string name)
  173.         {
  174.             this.Name = name;
  175.         }
  176.         public string Name { get; set; }
  177.     }
  178.  
  179.     public class Doctor
  180.     {
  181.         public Doctor(string firstName, string LastName)
  182.         {
  183.             this.Patients = new List<Patient>();
  184.             this.FirstName = firstName;
  185.             this.LastName = LastName;
  186.         }
  187.  
  188.         public string FirstName { get; set; }
  189.         public string LastName { get; set; }
  190.         public List<Patient> Patients { get; set; }
  191.     }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement