diff --git a/src/main/java/fr/univlille/iut/r304/tp3/q1/Observable.java b/src/main/java/fr/univlille/iut/r304/tp3/q1/Observable.java
index 86b4182fd64039f9c2a8b98489358679bf1d3eec..ab01f57c8d3ce4f703aeddffe680570a8867cadf 100644
--- a/src/main/java/fr/univlille/iut/r304/tp3/q1/Observable.java
+++ b/src/main/java/fr/univlille/iut/r304/tp3/q1/Observable.java
@@ -2,32 +2,33 @@ package fr.univlille.iut.r304.tp3.q1;
import java.util.ArrayList;
-import java.util.concurrent.CopyOnWriteArrayList;
+import java.util.concurrent.ConcurrentLinkedQueue;
import java.util.List;
public abstract class Observable {
- private List<Observer> observers = new ArrayList<>();
-
+ protected ConcurrentLinkedQueue<Observer> observers = new ConcurrentLinkedQueue<>();
protected void notifyObservers() {
- List<Observer> Observers = new ArrayList<>(observers);
- for (Observer obs : Observers) {
- obs.update(this);
+ for (Observer ob: observers){
+ ob.update(this);
}
}
protected void notifyObservers(Object data) {
- List<Observer> Observers = new ArrayList<>(observers);
- for (Observer obs : Observers) {
- obs.update(this, data);
+ for (Observer ob : observers){
+ ob.update(this,data);
}
}
public void attach(Observer observer) {
+
observers.add(observer);
+
}
public void detach(Observer observer) {
+
observers.remove(observer);
+
}
}
diff --git a/src/main/java/fr/univlille/iut/r304/tp3/q2/Main.java b/src/main/java/fr/univlille/iut/r304/tp3/q2/Main.java
index b81b0c2cc95c2dffd569f72c3fbcf7831cdf3be2..5338514418d4128203705d9a558d3a7732b0b2ba 100644
--- a/src/main/java/fr/univlille/iut/r304/tp3/q2/Main.java
+++ b/src/main/java/fr/univlille/iut/r304/tp3/q2/Main.java
@@ -7,12 +7,14 @@ Main {
Text texte = new Text(System.in);
// Créer un objectif
- Objectif objectif = new Objectif("1", 4);
-
+ Objectif objectifLigne = new ObjectifLigne("1", 14);
+ Objectif objectifLettre = new ObjectifLettre("2",15,'e');
// Attacher l'objectif comme observateur du texte
- texte.attach(objectif);
-
- // Lancer la saisie
+ texte.attach(objectifLigne);
+ texte.attach(objectifLettre);
+ System.out.println("hello");
texte.lancer();
+
+
}
}
diff --git a/src/main/java/fr/univlille/iut/r304/tp3/q2/Objectif.java b/src/main/java/fr/univlille/iut/r304/tp3/q2/Objectif.java
index e444041ec893a74499c40675078371c5901bf47a..31894fdfece17a44de37f39e00e8b342d1e23ff3 100644
--- a/src/main/java/fr/univlille/iut/r304/tp3/q2/Objectif.java
+++ b/src/main/java/fr/univlille/iut/r304/tp3/q2/Objectif.java
@@ -3,35 +3,38 @@ package fr.univlille.iut.r304.tp3.q2;
import fr.univlille.iut.r304.tp3.q1.Observer;
import fr.univlille.iut.r304.tp3.q1.Observable;
-public class Objectif implements Observer {
- private int nbRestant;
- private String nom;
+public abstract class Objectif implements Observer {
+ protected int nbRestant;
+ protected String nom;
+ private final String type;
- public Objectif(String nom, int nbLignes) {
+ public Objectif(String nom, int nbRestant, String type) {
this.nom = nom;
- this.nbRestant = nbLignes;
+ this.nbRestant = nbRestant+1;
+ this.type = type;
}
+
@Override
public void update(Observable subj) {
+ if(nbRestant <= 1){
+ subj.detach(this);
+ System.out.println("Objectif " +nom + " -nom de "+ type + "atteint");
+ }else{
+ System.out.println("Objectif " +nom +" -nom de "+ type + "restant :" + nbRestant);
+ }
+
}
@Override
- public void update(Observable o, Object arg) {
- if (arg instanceof String) {
- String ligne = (String) arg;
-
-
- if (!ligne.equals("!fin")) {
- nbRestant--;
- if (nbRestant > 0) {
- System.out.println(">>>Objectif " + nom + " - nombre de lignes : encore " + nbRestant + "<<<");
- } else {
- System.out.println(">>>Objectif " + nom + " - nombre de lignes atteint<<<");
- System.out.println("Mission accomplie");
- }
- }
- }
+ public void update(Observable subj, Object data) {
+ int dimunition = calculerDimunition((String )data);
+ nbRestant -= dimunition;
+ update(subj);
}
+
+
+ public abstract int calculerDimunition(String texte);
+
}
diff --git a/src/main/java/fr/univlille/iut/r304/tp3/q2/ObjectifLettre.java b/src/main/java/fr/univlille/iut/r304/tp3/q2/ObjectifLettre.java
new file mode 100644
index 0000000000000000000000000000000000000000..200988020a0e0bfe04eac29688c1cb04e61fa8ce
--- /dev/null
+++ b/src/main/java/fr/univlille/iut/r304/tp3/q2/ObjectifLettre.java
@@ -0,0 +1,21 @@
+package fr.univlille.iut.r304.tp3.q2;
+
+public class ObjectifLettre extends Objectif{
+ char lettre;
+
+ public ObjectifLettre(String nom,int Nbrestant,char lettre){
+ super(nom,Nbrestant,""+lettre);
+ this.lettre = lettre;
+ }
+
+ @Override
+ public int calculerDimunition(String texte) {
+ int compteur = 0;
+ for(int i = 0; i<texte.length();i++){
+ if(texte.charAt(i)==lettre){
+ compteur ++;
+ }
+ }
+ return compteur;
+ }
+}
diff --git a/src/main/java/fr/univlille/iut/r304/tp3/q2/ObjectifLigne.java b/src/main/java/fr/univlille/iut/r304/tp3/q2/ObjectifLigne.java
new file mode 100644
index 0000000000000000000000000000000000000000..333404ab66a23baee80807ac418ce31ccc2a2e19
--- /dev/null
+++ b/src/main/java/fr/univlille/iut/r304/tp3/q2/ObjectifLigne.java
@@ -0,0 +1,16 @@
+package fr.univlille.iut.r304.tp3.q2;
+
+public class ObjectifLigne extends Objectif{
+
+ public ObjectifLigne(String n ,int nbr){
+ super(n,nbr,"lignes");
+
+ }
+
+ @Override
+ public int calculerDimunition(String texte) {
+ return 1;
+ }
+
+
+}
diff --git a/src/main/java/fr/univlille/iut/r304/tp3/q2/Text.java b/src/main/java/fr/univlille/iut/r304/tp3/q2/Text.java
index f5df549e56334e8bd93d56d7463f36c414fcd47d..17314b72c4710123f3d9a4c122067e71c2b0741d 100644
--- a/src/main/java/fr/univlille/iut/r304/tp3/q2/Text.java
+++ b/src/main/java/fr/univlille/iut/r304/tp3/q2/Text.java
@@ -5,7 +5,7 @@ import java.util.Scanner;
import fr.univlille.iut.r304.tp3.q1.Observable;
public class Text extends Observable {
- private Scanner scan;
+ protected Scanner scan;
// Constructeur qui initialise le Scanner
public Text(InputStream in) {
@@ -14,14 +14,12 @@ public class Text extends Observable {
// Méthode pour lancer la saisie de texte
public void lancer() {
- String ligne;
- while (true) {
- ligne = scan.nextLine(); // Lecture de l'entrée
- notifyObservers(ligne); // Notification des observateurs avec la ligne saisie
- if (ligne.equals("!fin")) {
- System.out.println("Fin de la saisie.");
- break;
- }
+ String text = "";
+
+ while(!text.equalsIgnoreCase("!fin")){
+ notifyObservers(text);
+ text = scan.next();
}
+ System.out.println("Fin de la session");
}
}
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 ce7269d5ecfe5adf17694c31683a359b9322d7ca..7d0d9119f848d7cd5289a3923934f71d3f7cb3a9 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,34 @@
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);
+ this.setValue(other.getValue());
}
public void biconnectTo(ConnectableProperty other) {
+ other.attach(this);
+ this.attach(other);
+ other.setValue(this.getValue());
}
public void unconnectFrom(ConnectableProperty other) {
+ other.detach(this);
+
}
+ @Override
+ public void update(Observable subj) {
+
+ }
+
+ @Override
+ public void update(Observable subj, Object data) {
+ super.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 4fe250c1b28a1036c86d634bbc124f6a0e51fd4c..fa20e2c7841253f3c3988ffb9d20e2c441cd3a5c 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,17 +1,22 @@
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 {
+ Object obj;
public void setValue(Object i) {
+ if(obj == i) return;
+ obj =i;
+ super.notifyObservers(i);
+
}
public Object getValue() {
- return null;
+ return obj;
}
- public void attach(Observer observer) {
+ /*public void attach(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
}
@@ -19,6 +24,7 @@ public class ObservableProperty {
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
- }
+
+ }*/
}
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 0000000000000000000000000000000000000000..887e14d9a8b4ebfbf851f40cff4f6a59658210bc
--- /dev/null
+++ b/src/main/java/fr/univlille/iut/r304/tp3/q4/Devise.java
@@ -0,0 +1,48 @@
+package fr.univlille.iut.r304.tp3.q4;
+
+import fr.univlille.iut.r304.tp3.q1.Observable;
+import fr.univlille.iut.r304.tp3.q3.ConnectableProperty;
+
+public class Devise extends ConnectableProperty {
+ private final String typeDevise;
+ private final double tauxConversion;
+ private boolean enCoursDeMiseAJour = false;
+
+ public Devise(double montantInit, String type, double tauxConversion) {
+ super.setValue(montantInit);
+ this.typeDevise = type;
+ this.tauxConversion = tauxConversion;
+ }
+
+ public String getTypeDevise() {
+ return typeDevise;
+ }
+
+ public double getMontant() {
+ return (double) super.getValue();
+ }
+
+ public void setMontant(double m) {
+ super.setValue(m);
+ System.out.println("fait setmontant");
+ }
+
+ @Override
+ public void update(Observable subj, Object data) {
+ if (!enCoursDeMiseAJour && data instanceof Double && subj instanceof Devise) {
+ try {
+ enCoursDeMiseAJour = true; // Active le verrou
+ double autreMontant = (Double) data;
+ double montantConverti = autreMontant / this.tauxConversion;
+ super.setValue(montantConverti); // Met à jour la valeur
+ } finally {
+ enCoursDeMiseAJour = false; // Libère le verrou
+ }
+ }
+ }
+
+ @Override
+ public String toString() {
+ return String.format("%.2f %s", getMontant(), typeDevise);
+ }
+}
diff --git a/src/main/java/fr/univlille/iut/r304/tp3/q4/Main.java b/src/main/java/fr/univlille/iut/r304/tp3/q4/Main.java
new file mode 100644
index 0000000000000000000000000000000000000000..bee9138fde9ca83d4889e419dc428a441967edff
--- /dev/null
+++ b/src/main/java/fr/univlille/iut/r304/tp3/q4/Main.java
@@ -0,0 +1,28 @@
+package fr.univlille.iut.r304.tp3.q4;
+
+public class Main {
+ public static void main(String[] args) {
+ // Taux de conversion
+ double tauxEuroToDollar = 1.11; // 1 € = 1.11 $
+ double tauxDollarToEuro = 1 / tauxEuroToDollar; // 1 $ = 0.90 €
+
+ // Création des devises
+ Devise euros = new Devise(0, "EUR", tauxDollarToEuro);
+ Devise dollars = new Devise(0, "USD", tauxEuroToDollar);
+
+ // Synchronisation bidirectionnelle
+ euros.biconnectTo(dollars);
+
+ // Ajout de 20 $ et affichage
+ System.out.println("Ajout de 20 $");
+ dollars.setMontant(20); // Mise à jour des dollars
+ System.out.println(euros); // Affiche : 18.02 EUR
+ System.out.println(dollars); // Affiche : 20.00 USD
+
+ // Retrait de 11 € et affichage
+ System.out.println("Retrait de 11 €");
+ euros.setMontant(euros.getMontant() - 11); // Mise à jour des euros
+ System.out.println(euros); // Affiche : 7.02 EUR
+ System.out.println(dollars); // Affiche : 7.78 USD
+ }
+}