From b9643854052a16460027c9593fcf9a9575dc18c7 Mon Sep 17 00:00:00 2001 From: LordBaryhobal Date: Mon, 11 Nov 2024 11:34:23 +0100 Subject: [PATCH] added lab15 ex1 --- src/lab15_observer/ex1/Main.java | 24 +++++++ src/lab15_observer/ex1/MedicalEmployee.java | 19 +++++ src/lab15_observer/ex1/PatientMonitoring.java | 70 +++++++++++++++++++ src/lab15_observer/ex1/PatientObserver.java | 5 ++ src/lab15_observer/ex1/Problem.java | 7 ++ 5 files changed, 125 insertions(+) create mode 100644 src/lab15_observer/ex1/Main.java create mode 100644 src/lab15_observer/ex1/MedicalEmployee.java create mode 100644 src/lab15_observer/ex1/PatientMonitoring.java create mode 100644 src/lab15_observer/ex1/PatientObserver.java create mode 100644 src/lab15_observer/ex1/Problem.java diff --git a/src/lab15_observer/ex1/Main.java b/src/lab15_observer/ex1/Main.java new file mode 100644 index 0000000..26edf66 --- /dev/null +++ b/src/lab15_observer/ex1/Main.java @@ -0,0 +1,24 @@ +package lab15_observer.ex1; + +public class Main { + public static void main(String[] args) { + PatientMonitoring pm = new PatientMonitoring(1, 140, 85); + + MedicalEmployee jean = new MedicalEmployee("Jean", pm); + MedicalEmployee pauline = new MedicalEmployee("Pauline", pm); + MedicalEmployee matthieu = new MedicalEmployee("Matthieu", pm); + MedicalEmployee symeon = new MedicalEmployee("Symeon", pm); + + pm.setBloodPressure(110); + pm.setPosition(3); + + pm.setPulseOximetry(90); + pm.setPulseOximetry(70); + pm.setPosition(7); + pm.setBloodPressure(150); + + pm.removeObserver(matthieu); + pm.setBloodPressure(145); + pm.setPosition(9); + } +} diff --git a/src/lab15_observer/ex1/MedicalEmployee.java b/src/lab15_observer/ex1/MedicalEmployee.java new file mode 100644 index 0000000..f68fef5 --- /dev/null +++ b/src/lab15_observer/ex1/MedicalEmployee.java @@ -0,0 +1,19 @@ +package lab15_observer.ex1; + +public class MedicalEmployee implements PatientObserver { + private final String name; + + public MedicalEmployee(String name, PatientMonitoring pm) { + this.name = name; + pm.registerObserver(this); + } + + public String getName() { + return name; + } + + @Override + public void update(Problem problem, PatientMonitoring pm) { + System.out.println(getName() + " has been notified of " + problem + " for patient " + pm); + } +} diff --git a/src/lab15_observer/ex1/PatientMonitoring.java b/src/lab15_observer/ex1/PatientMonitoring.java new file mode 100644 index 0000000..190eb48 --- /dev/null +++ b/src/lab15_observer/ex1/PatientMonitoring.java @@ -0,0 +1,70 @@ +package lab15_observer.ex1; + +import java.util.ArrayList; +import java.util.List; + +public class PatientMonitoring { + private final List observers = new ArrayList<>(); + + private int position; + private int bloodPressure; + private int pulseOximetry; + + public PatientMonitoring(int position, int bloodPressure, int pulseOximetry) { + this.position = position; + this.bloodPressure = bloodPressure; + this.pulseOximetry = pulseOximetry; + } + + public void registerObserver(PatientObserver o) { + observers.add(o); + } + + public void removeObserver(PatientObserver o) { + observers.remove(o); + } + + private void notifyObservers(Problem problem) { + for (PatientObserver o : observers) { + o.update(problem, this); + } + } + + public int getPosition() { + return position; + } + public int getBloodPressure() { + return bloodPressure; + } + public int getPulseOximetry() { + return pulseOximetry; + } + + public void setPosition(int position) { + this.position = position; + notifyObservers(Problem.NO_PROBLEM); + } + + public void setBloodPressure(int bloodPressure) { + this.bloodPressure = bloodPressure; + if (getBloodPressure() > 145) { + notifyObservers(Problem.BLOOD_PRESSURE); + } else { + notifyObservers(Problem.NO_PROBLEM); + } + } + + public void setPulseOximetry(int pulseOximetry) { + this.pulseOximetry = pulseOximetry; + if (getPulseOximetry() > 85) { + notifyObservers(Problem.OXIMETRY); + } else { + notifyObservers(Problem.NO_PROBLEM); + } + } + + @Override + public String toString() { + return ""; + } +} diff --git a/src/lab15_observer/ex1/PatientObserver.java b/src/lab15_observer/ex1/PatientObserver.java new file mode 100644 index 0000000..d025d85 --- /dev/null +++ b/src/lab15_observer/ex1/PatientObserver.java @@ -0,0 +1,5 @@ +package lab15_observer.ex1; + +public interface PatientObserver { + void update(Problem problem, PatientMonitoring pm); +} diff --git a/src/lab15_observer/ex1/Problem.java b/src/lab15_observer/ex1/Problem.java new file mode 100644 index 0000000..ad3708d --- /dev/null +++ b/src/lab15_observer/ex1/Problem.java @@ -0,0 +1,7 @@ +package lab15_observer.ex1; + +public enum Problem { + NO_PROBLEM, + BLOOD_PRESSURE, + OXIMETRY +}