added lab15 ex1

This commit is contained in:
Louis Heredero 2024-11-11 11:34:23 +01:00
parent ca9d0ec062
commit b964385405
Signed by: HEL
GPG Key ID: 8D83DE470F8544E7
5 changed files with 125 additions and 0 deletions

View File

@ -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);
}
}

View File

@ -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);
}
}

View File

@ -0,0 +1,70 @@
package lab15_observer.ex1;
import java.util.ArrayList;
import java.util.List;
public class PatientMonitoring {
private final List<PatientObserver> 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 "<Patient Position: " + position + ", BP: " + bloodPressure + ", PO: " + pulseOximetry + ">";
}
}

View File

@ -0,0 +1,5 @@
package lab15_observer.ex1;
public interface PatientObserver {
void update(Problem problem, PatientMonitoring pm);
}

View File

@ -0,0 +1,7 @@
package lab15_observer.ex1;
public enum Problem {
NO_PROBLEM,
BLOOD_PRESSURE,
OXIMETRY
}