From 140a79f5db1b83423a885ccf44e57a90d7333f36 Mon Sep 17 00:00:00 2001
From: gabriel mille <gabriel.mille.etu@univ-lille.fr>
Date: Fri, 27 Sep 2024 11:47:42 +0200
Subject: [PATCH] tp3 dev efficace

---
 .../univlille/iut/r304/tp3/q1/Observable.java | 14 ++++
 .../univlille/iut/r304/tp3/q2/Objectif.java   | 49 +++++++++++++
 .../iut/r304/tp3/q2/ObjectifNbLettre.java     | 37 ++++++++++
 .../fr/univlille/iut/r304/tp3/q2/Texte.java   | 25 +++++++
 .../iut/r304/tp3/q3/ConnectableProperty.java  | 23 +++++-
 .../iut/r304/tp3/q3/ObservableProperty.java   | 19 ++---
 .../fr/univlille/iut/r304/tp3/q4/Devise.java  | 72 +++++++++++++++++++
 7 files changed, 225 insertions(+), 14 deletions(-)
 create mode 100644 src/main/java/fr/univlille/iut/r304/tp3/q2/Objectif.java
 create mode 100644 src/main/java/fr/univlille/iut/r304/tp3/q2/ObjectifNbLettre.java
 create mode 100644 src/main/java/fr/univlille/iut/r304/tp3/q2/Texte.java
 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/q1/Observable.java b/src/main/java/fr/univlille/iut/r304/tp3/q1/Observable.java
index 264f8c5..2e0a79c 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
@@ -1,17 +1,31 @@
 package fr.univlille.iut.r304.tp3.q1;
 
+import java.util.Queue;
+import java.util.Set;
+import java.util.concurrent.ConcurrentLinkedQueue;
 
 public abstract class Observable {
+	protected Queue<Observer> observers = new ConcurrentLinkedQueue<>();
 
 	protected void notifyObservers() {
+		for (Observer observer:observers
+			 ) {
+			observer.update(this);
+		}
 	}
 
 	protected void notifyObservers(Object data) {
+		for (Observer observer:observers
+		) {
+			observer.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/Objectif.java b/src/main/java/fr/univlille/iut/r304/tp3/q2/Objectif.java
new file mode 100644
index 0000000..09aeaf1
--- /dev/null
+++ b/src/main/java/fr/univlille/iut/r304/tp3/q2/Objectif.java
@@ -0,0 +1,49 @@
+package fr.univlille.iut.r304.tp3.q2;
+
+import fr.univlille.iut.r304.tp3.q1.Observable;
+import fr.univlille.iut.r304.tp3.q1.Observer;
+
+public class Objectif implements Observer {
+    protected int nbRestant;
+    protected String nom;
+
+    Objectif(String nom, int nbRestant){
+        this.nom = nom;
+        this.nbRestant = nbRestant;
+    }
+    public int getNbRestant(){
+        return this.nbRestant;
+    }
+
+    public String getNom(){
+        return this.nom;
+    }
+
+
+    protected void updateObjectif(Object data){
+        nbRestant --;
+    }
+    @Override
+    public void update(Observable subj) {
+
+    }
+
+    @Override
+    public String toString() {
+        return nom + " - nombre de lignes restantes : encore " + nbRestant;
+    }
+
+    @Override
+    public void update(Observable subj, Object data) {
+        updateObjectif(data);
+        if (!data.equals("!fin")) {
+            if (nbRestant > 0) {
+                System.out.println(this.toString());
+            } else {
+                System.out.println(nom + " - nombre atteint");
+            }
+        }
+
+    }
+
+}
diff --git a/src/main/java/fr/univlille/iut/r304/tp3/q2/ObjectifNbLettre.java b/src/main/java/fr/univlille/iut/r304/tp3/q2/ObjectifNbLettre.java
new file mode 100644
index 0000000..268ca5e
--- /dev/null
+++ b/src/main/java/fr/univlille/iut/r304/tp3/q2/ObjectifNbLettre.java
@@ -0,0 +1,37 @@
+package fr.univlille.iut.r304.tp3.q2;
+
+public class ObjectifNbLettre extends Objectif{
+    String lettre;
+
+    ObjectifNbLettre(String nom, int nbRestant, String lettre){
+        super(nom, nbRestant);
+        this.lettre = lettre;
+    }
+
+    @Override
+    protected void updateObjectif(Object data){
+        String text = (String) data;
+        int nbLettre = 0;
+        nbLettre = text.length() - text.replaceAll(lettre, "").length();
+        super.nbRestant -= nbLettre;
+    }
+
+    @Override
+    public String toString() {
+        return nom + " - nombre de "+ lettre+" restants : encore " + nbRestant;
+    }
+
+    public static void main(String[] args) {
+        Objectif objectif = new Objectif("Objectif 1", 3);
+        ObjectifNbLettre objectif2 = new ObjectifNbLettre("Objectif 2", 3, "e");
+        Texte texte = new Texte(System.in);
+        texte.attach(objectif);
+        texte.attach(objectif2);
+        System.out.println(objectif.toString());
+        System.out.println(objectif2.toString());
+        texte.lancer();
+        System.out.println("Mission accomplie");
+        texte.detach(objectif);
+
+    }
+}
diff --git a/src/main/java/fr/univlille/iut/r304/tp3/q2/Texte.java b/src/main/java/fr/univlille/iut/r304/tp3/q2/Texte.java
new file mode 100644
index 0000000..6b4c5c4
--- /dev/null
+++ b/src/main/java/fr/univlille/iut/r304/tp3/q2/Texte.java
@@ -0,0 +1,25 @@
+package fr.univlille.iut.r304.tp3.q2;
+
+import fr.univlille.iut.r304.tp3.q1.Observable;
+
+import java.io.InputStream;
+import java.util.Scanner;
+
+public class Texte extends Observable {
+    private InputStream IS;
+    protected Scanner scan;
+    private String text;
+    Texte(InputStream IS){
+        this.IS = IS;
+        this.text = "";
+    }
+
+    public void lancer(){
+        while (!text.equals("!fin")){
+            scan = new Scanner(IS);
+            text = scan.nextLine();
+            this.notifyObservers(text);
+        }
+        scan.close();
+    }
+}
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..ad0c3bd 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,35 @@
 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);
+		other.notifyObservers(other.getValue());
+		this.attach(other);
+		this.notifyObservers(this.getValue());
+
 	}
 
 	public void unconnectFrom(ConnectableProperty other) {
+		other.detach(this);
 	}
 
+	@Override
+	public void update(Observable subj) {
+	}
+
+	@Override
+	public void update(Observable subj, Object data) {
+		if (this.getValue() != data) {
+			this.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 4fe250c..d3d1f89 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,17 @@
 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 property;
 	public void setValue(Object i) {
+		property = i;
+		this.notifyObservers(property);
 	}
 
 	public Object getValue() {
-		return null;
-	}
-
-	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
-	}
-
-	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 property;
 	}
 
 }
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..af7d666
--- /dev/null
+++ b/src/main/java/fr/univlille/iut/r304/tp3/q4/Devise.java
@@ -0,0 +1,72 @@
+package fr.univlille.iut.r304.tp3.q4;
+
+import fr.univlille.iut.r304.tp3.q3.ConnectableProperty;
+
+public class Devise extends ConnectableProperty {
+    private String nom;
+    private double tauxDeChange;
+
+    public Devise(String nom, double tauxDeChange) {
+        this.nom = nom;
+        this.tauxDeChange = tauxDeChange;
+    }
+
+    @Override
+    public void update(Observable subj, Object data) {
+        if (subj instanceof Devise) {
+            Devise autreDevise = (Devise) subj;
+            if (this.nom.equals("Euros")) {
+                // Conversion de dollars à euros
+                double montantEnDollars = (double) data;
+                double montantEnEuros = montantEnDollars / tauxDeChange;
+                this.setValue(montantEnEuros);
+            } else if (this.nom.equals("Dollars")) {
+                // Conversion d'euros à dollars
+                double montantEnEuros = (double) data;
+                double montantEnDollars = montantEnEuros * tauxDeChange;
+                this.setValue(montantEnDollars);
+            }
+        }
+    }
+
+    public String getNom() {
+        return nom;
+    }
+
+    public double getTauxDeChange() {
+        return tauxDeChange;
+    }
+
+    public static void main(String[] args) {
+        // Taux de change: 1 Euro = 1.1 Dollars
+        Devise euros = new Devise("Euros", 1.1);
+        Devise dollars = new Devise("Dollars", 1 / 1.1);
+
+        // Connexion des devises
+        euros.biconnectTo(dollars);
+
+        // Ajout de 20$
+        dollars.setValue(20.0);
+        System.out.println("Ajout 20$");
+        System.out.println(euros.getValue() + " €");
+        System.out.println(dollars.getValue() + " $");
+
+        // Retrait de 11€
+        euros.setValue(euros.getValue() - 11);
+        System.out.println("Retrait 11€");
+        System.out.println(euros.getValue() + " €");
+        System.out.println(dollars.getValue() + " $");
+
+        // Ajout d'une troisième devise : Yen
+        Devise yen = new Devise("Yen", 0.0075); // 1 Euro = 133.33 Yen
+        euros.biconnectTo(yen);
+        yen.biconnectTo(euros);
+
+        // Ajout de 1000 Yen
+        yen.setValue(1000.0);
+        System.out.println("Ajout 1000 Yen");
+        System.out.println(euros.getValue() + " €");
+        System.out.println(dollars.getValue() + " $");
+        System.out.println(yen.getValue() + " Yen");
+    }
+}
\ No newline at end of file
-- 
GitLab