Implement patient methods in WaitingRoom

This commit is contained in:
MarcUs7i 2025-01-13 20:30:20 +01:00
parent 3941f2e4b6
commit 02ff071ebe
4 changed files with 97 additions and 18 deletions

View file

@ -6,5 +6,11 @@
/// </summary>
public sealed class Node
{
// TODO
public Patient Data { get; }
public Node? Next { get; set; }
public Node(Patient patient)
{
Data = patient;
}
}

View file

@ -5,7 +5,18 @@
/// </summary>
public sealed class Patient
{
// TODO ctor
public Patient(string name, DateTime arrival)
{
Name = name;
Arrival = arrival;
}
public Patient(string name, DateTime arrival, bool isEmergency)
{
Name = name;
Arrival = arrival;
IsEmergency = isEmergency;
}
/// <summary>
/// Gets the name of the patient

View file

@ -6,7 +6,7 @@
/// </summary>
public sealed class PatientQueue
{
//private Node? _head;
private Node? _head;
/// <summary>
/// Gets the number of patients currently waiting in this queue
@ -20,7 +20,25 @@ public sealed class PatientQueue
/// <param name="patient">Patient to add to the queue</param>
public void Enqueue(Patient patient)
{
// TODO
var newNode = new Node(patient);
if (_head == null || patient.Arrival < _head.Data.Arrival)
{
newNode.Next = _head;
_head = newNode;
Size++;
return;
}
var current = _head;
while (current.Next != null && current.Next.Data.Arrival <= patient.Arrival)
{
current = current.Next;
}
newNode.Next = current.Next;
current.Next = newNode;
Size++;
}
/// <summary>
@ -29,10 +47,17 @@ public sealed class PatientQueue
/// <returns>The next patient to be treated; null if no patient is waiting</returns>
public Patient? Dequeue()
{
// TODO
if (_head == null)
{
return null;
}
var patient = _head.Data;
_head = _head.Next;
Size--;
return patient;
}
/// <summary>
/// Removes the supplied patient from the queue.
/// Only patients actually waiting in this queue can be removed.
@ -41,7 +66,31 @@ public sealed class PatientQueue
/// <returns>True if the patient could be removed; false otherwise</returns>
public bool Remove(Patient patient)
{
// TODO
if (_head == null)
{
return false;
}
if (_head.Data == patient)
{
_head = _head.Next;
Size--;
return true;
}
var current = _head;
while (current.Next != null && current.Next.Data != patient)
{
current = current.Next;
}
if (current.Next == null)
{
return false;
}
current.Next = current.Next.Next;
Size--;
return true;
}
}

View file

@ -14,20 +14,17 @@ public sealed class WaitingRoom
/// <summary>
/// Gets the current number of emergency patients waiting
/// </summary>
// TODO
public int CountEmergency => -1;
public int CountEmergency => _emergencyQueue.Size;
/// <summary>
/// Gets the current number of normal patients waiting
/// </summary>
// TODO
public int CountNormal => -1;
public int CountNormal => _normalQueue.Size;
/// <summary>
/// Gets the current number of all patients waiting
/// </summary>
// TODO
public int CountAll => -1;
public int CountAll => CountEmergency + CountNormal;
/// <summary>
/// Creates a string representation of the waiting room, showing statistics
@ -43,15 +40,25 @@ public sealed class WaitingRoom
/// <param name="patient">Patient to admit</param>
public void AddPatient(Patient patient)
{
// TODO
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>
// TODO
public Patient? Next() => null;
public Patient? Next()
{
var emergency = _emergencyQueue.Dequeue();
return emergency ?? _normalQueue.Dequeue();
}
/// <summary>
/// Promotes a patient to be an emergency case.
@ -60,6 +67,12 @@ public sealed class WaitingRoom
/// <param name="patient">Patient to declare an emergency for</param>
public void SetPatientToEmergency(Patient patient)
{
// TODO
if (!_normalQueue.Remove(patient))
{
return;
}
patient.IsEmergency = true;
_emergencyQueue.Enqueue(patient);
}
}