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 +}