ex-col-04-training/waiting_room/WaitingRoom/WaitingRoom.cs
2025-01-13 20:30:20 +01:00

78 lines
No EOL
2.5 KiB
C#

namespace WaitingRoom;
/// <summary>
/// Represents the waiting room at a doctor's office.
/// Keeps an emergency and a normal waiting queue.
/// Emergency patients are treated first; patients within the same queue are
/// treated according to their arrival time.
/// </summary>
public sealed class WaitingRoom
{
private readonly PatientQueue _emergencyQueue = new();
private readonly PatientQueue _normalQueue = new();
/// <summary>
/// Gets the current number of emergency patients waiting
/// </summary>
public int CountEmergency => _emergencyQueue.Size;
/// <summary>
/// Gets the current number of normal patients waiting
/// </summary>
public int CountNormal => _normalQueue.Size;
/// <summary>
/// Gets the current number of all patients waiting
/// </summary>
public int CountAll => CountEmergency + CountNormal;
/// <summary>
/// Creates a string representation of the waiting room, showing statistics
/// about waiting patients
/// </summary>
/// <returns>String representation of the waiting room</returns>
public override string ToString() => $"{CountEmergency} Emergency {CountNormal} Normal";
/// <summary>
/// Admits a new patient to the waiting room.
/// Patient will be added to either the normal or the emergency queue, depending on its state.
/// </summary>
/// <param name="patient">Patient to admit</param>
public void AddPatient(Patient patient)
{
if (patient.IsEmergency)
{
_emergencyQueue.Enqueue(patient);
}
else
{
_normalQueue.Enqueue(patient);
}
}
/// <summary>
/// Returns the next patient to be treated, removing it from its waiting queue
/// </summary>
/// <returns>Next patient to treat, if any</returns>
public Patient? Next()
{
var emergency = _emergencyQueue.Dequeue();
return emergency ?? _normalQueue.Dequeue();
}
/// <summary>
/// Promotes a patient to be an emergency case.
/// The patient has to be moved from the normal to the emergency queue.
/// </summary>
/// <param name="patient">Patient to declare an emergency for</param>
public void SetPatientToEmergency(Patient patient)
{
if (!_normalQueue.Remove(patient))
{
return;
}
patient.IsEmergency = true;
_emergencyQueue.Enqueue(patient);
}
}