From b14e88daefda0a478f406f5090e2b77ea3364a39 Mon Sep 17 00:00:00 2001 From: Mathys Bollengier <mathys.bollengier.etu@univ-lille.fr> Date: Fri, 27 Sep 2024 11:50:01 +0200 Subject: [PATCH] Exo 4 --- .../iut/r304/tp3/q3/ConnectableProperty.java | 26 ++++++++++- .../iut/r304/tp3/q3/ObservableProperty.java | 23 +++++----- .../fr/univlille/iut/r304/tp3/q4/Devise.java | 46 +++++++++++++++++++ 3 files changed, 82 insertions(+), 13 deletions(-) create mode 100644 src/main/java/fr/univlille/iut/r304/tp3/q4/Devise.java diff --git a/src/main/java/fr/univlille/iut/r304/tp3/q3/ConnectableProperty.java b/src/main/java/fr/univlille/iut/r304/tp3/q3/ConnectableProperty.java index ce7269d..e782319 100644 --- a/src/main/java/fr/univlille/iut/r304/tp3/q3/ConnectableProperty.java +++ b/src/main/java/fr/univlille/iut/r304/tp3/q3/ConnectableProperty.java @@ -1,14 +1,38 @@ package fr.univlille.iut.r304.tp3.q3; -public class ConnectableProperty extends ObservableProperty { +import fr.univlille.iut.r304.tp3.q1.Observable; +import fr.univlille.iut.r304.tp3.q1.Observer; +public class ConnectableProperty extends ObservableProperty implements Observer{ public void connectTo(ConnectableProperty other) { + other.attach(this); + other.notifyObservers(other.getValue()); } public void biconnectTo(ConnectableProperty other) { + other.attach(this); + this.attach(other); + if (!changing){ + changing=true; + other.notifyObservers(other.getValue()); + this.notifyObservers(this.getValue()); + } + changing=false; } public void unconnectFrom(ConnectableProperty other) { + other.detach(this); } + @Override + public void update(Observable subj) { + } + + @Override + public void update(Observable subj, Object data) { + if (!data.equals(getValue())){ + setValue(data); + } + + } } diff --git a/src/main/java/fr/univlille/iut/r304/tp3/q3/ObservableProperty.java b/src/main/java/fr/univlille/iut/r304/tp3/q3/ObservableProperty.java index b126380..07fa586 100644 --- a/src/main/java/fr/univlille/iut/r304/tp3/q3/ObservableProperty.java +++ b/src/main/java/fr/univlille/iut/r304/tp3/q3/ObservableProperty.java @@ -1,24 +1,23 @@ package fr.univlille.iut.r304.tp3.q3; +import fr.univlille.iut.r304.tp3.q1.Observable; import fr.univlille.iut.r304.tp3.q1.Observer; -public class ObservableProperty { +public class ObservableProperty extends Observable { + protected Object value=0; + protected boolean changing=false; public void setValue(Object i) { + if(!changing){ + changing=true; + this.value= i; + this.notifyObservers(i); + } + changing=false; } public Object getValue() { - return null; - } - - public void attach(Observer observer) { - // methode cree pour que les tests compilent sans erreurs - // n'est pas censée rester une fois que vous avez fini Q2.1 - } - - public void detach(Observer observer) { - // methode cree pour que les tests compilent sans erreur - // n'est pas censée rester une fois que vous avez fini Q2.1 + return value; } } diff --git a/src/main/java/fr/univlille/iut/r304/tp3/q4/Devise.java b/src/main/java/fr/univlille/iut/r304/tp3/q4/Devise.java new file mode 100644 index 0000000..a8e74ba --- /dev/null +++ b/src/main/java/fr/univlille/iut/r304/tp3/q4/Devise.java @@ -0,0 +1,46 @@ +package fr.univlille.iut.r304.tp3.q4; + +import fr.univlille.iut.r304.tp3.q1.Observable; +import fr.univlille.iut.r304.tp3.q2.Objectif; +import fr.univlille.iut.r304.tp3.q2.Texte; +import fr.univlille.iut.r304.tp3.q3.ConnectableProperty; + +import java.io.ByteArrayInputStream; +import java.io.InputStream; + +public class Devise extends ConnectableProperty { + + private double valeur=0.0; + @Override + public void update(Observable subj, Object data) { + double taux=1.12; + if (!data.equals(getValue())){ + setValue((double)data*taux); + } + + } + public double getValeur() { + return valeur; + } + + public void setValeur(double valeur) { + this.valeur = valeur; + } + + + public static void main(String[] args){ + Devise euro=new Devise(); + Devise dollar=new Devise(); + dollar.biconnectTo(euro); + + dollar.setValue(8.0); + System.out.println(euro.getValue()+" €"); + System.out.println(dollar.getValue()+" $"); + + dollar.setValue(11.0); + System.out.println(euro.getValue()+" €"); + System.out.println(dollar.getValue()+ " $"); + } + + +} -- GitLab