diff --git a/HospitalSystem/Program.cs b/HospitalSystem/Program.cs index 9aa5319..bc1fbad 100644 --- a/HospitalSystem/Program.cs +++ b/HospitalSystem/Program.cs @@ -13,6 +13,7 @@ static void Main(string[] args) while (cont) { Console.WriteLine("1. Add a patient"); Console.WriteLine("2. List all patients"); + Console.WriteLine("3. Find patient by Id"); Console.WriteLine("Q. Quit "); Console.WriteLine("Choose an option:"); @@ -23,9 +24,11 @@ static void Main(string[] args) PatientHelper.CreatePatient(); break; case "2": - Console.WriteLine("List of all patients:"); PatientHelper.ListAll(); break; + case "3": + PatientHelper.DisplayPatientById(); + break; case "q": case "Q": cont = false; diff --git a/Library.HospitalSystem/Helpers/PatientHelper.cs b/Library.HospitalSystem/Helpers/PatientHelper.cs index 0f3a0eb..a8755c0 100644 --- a/Library.HospitalSystem/Helpers/PatientHelper.cs +++ b/Library.HospitalSystem/Helpers/PatientHelper.cs @@ -125,6 +125,22 @@ public void CreatePatient() // Get next ID from the service public void ListAll() { + Console.WriteLine("List of all patients:"); patientService.ListAll(); } + + public void DisplayPatientById() + { + uint id; + do { + Console.WriteLine("Enter the id of your patient:"); + } while (!uint.TryParse(Console.ReadLine(), out id)); + + if (patientService.TryFindPatientByID(id, out Patient? patient)) + { + Console.WriteLine(patient); + } else { + Console.WriteLine("Patient not found!"); + } + } } diff --git a/Library.HospitalSystem/Services/PatientService.cs b/Library.HospitalSystem/Services/PatientService.cs index 91295de..1f1650d 100644 --- a/Library.HospitalSystem/Services/PatientService.cs +++ b/Library.HospitalSystem/Services/PatientService.cs @@ -41,4 +41,10 @@ public void ListAll() { patients.ForEach(Console.WriteLine); } + + public bool TryFindPatientByID(uint id, out Patient? patient) { + patient = patients.FirstOrDefault(p => p.Id == id); + if (patient == null) return false; + else return true; + } }