Skip to content
Snippets Groups Projects
Commit f56c4b4f authored by Yvan Serikoff's avatar Yvan Serikoff
Browse files

q2

parent 04833119
No related branches found
No related tags found
No related merge requests found
......@@ -5,9 +5,9 @@ import java.util.*;
public abstract class Observable {
Collection<Observer> observers = new ArrayList<>();
protected Collection<Observer> observers = new ArrayList<>();
protected void notifyObservers() {
public void notifyObservers() {
for (Observer observer : observers) {
observer.update(this);
}
......
package fr.univlille.iut.r304.tp3.q2;public class Main {
public static void main(String[] args) {
Texte texte = new Texte(System.in);
Objectif obj1 = new Objectif("Objectif 1", 3);
ObjectifPlus obj2 = new ObjectifPlus("Objectif 2", 3);
texte.attach(obj1);
texte.attach(obj2);
texte.lancer();
}
}
package fr.univlille.iut.r304.tp3.q2;
import fr.univlille.iut.r304.tp3.q1.*;
public class Objectif implements Observer {
protected static final String expectedValue = "!fin";
protected String nom;
protected boolean wasNotified;
protected int nbRestant;
protected String data;
public Objectif(String nom, int nbLignes) {
this.nom = nom;
this.wasNotified = false;
this.nbRestant = nbLignes;
this.data = "";
}
public void update(Observable Texte) {
wasNotified = true;
System.out.println(nom + " - nombre de lignes : encore " + nbRestant);
nbRestant--;
}
public void update(Observable texte, Object line) {
wasNotified = true;
data = (String) line;
if (nbRestant > 0) {
System.out.println(nom + " - nombre de lignes : encore " + nbRestant);
}else if (nbRestant == 0) {
System.out.println(nom + " - nombre de lignes : atteint");
}
nbRestant--;
if (data.equals(expectedValue)) {
texte.detach(this);
}
}
public boolean wasNotified() {
return wasNotified;
}
}
package fr.univlille.iut.r304.tp3.q2;
import fr.univlille.iut.r304.tp3.q1.*;
public class ObjectifPlus extends Objectif{
Character expectedValue = '!';
public ObjectifPlus(String nom, int nbLignes) {
super(nom, nbLignes);
}
public void update(Observable Texte) {
wasNotified = true;
System.out.println(nom + " - nombre de " + expectedValue + " : encore " + nbRestant);
nbRestant--;
}
public void update(Observable texte, Object line) {
wasNotified = true;
data = (String) line;
if (nbRestant > 0) {
System.out.println(nom + " - nombre de " + expectedValue + " : encore " + nbRestant);
}if (data.contains(expectedValue.toString())) {
nbRestant--;
}
if (nbRestant == 0){
System.out.println(nom + " - nombre de lignes : atteint");
texte.detach(this);
}
}
}
package fr.univlille.iut.r304.tp3.q2;
import fr.univlille.iut.r304.tp3.q1.*;
import java.io.InputStream;
import java.util.Collection;
import java.util.Scanner;
import java.util.ArrayList;
public class Texte extends Observable {
protected Scanner scan = new Scanner(System.in);
public Texte (InputStream in) {
scan = new Scanner(in);
}
public void lancer() {
String line = "";
notifyObservers();
while (!observers.isEmpty()) {
line = scan.nextLine();
notifyObservers(line);
}
System.out.println("Fin de la saisie");
}
}
package fr.univlille.iut.r304.tp3.q2;
import fr.univlille.iut.r304.tp3.q1.*;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.w3c.dom.Text;
import static org.junit.jupiter.api.Assertions.*;
import java.io.*;
public class TestQ2 {
protected String text = "abc\ndef\n!fin";
protected InputStream is = new ByteArrayInputStream(text.getBytes());
protected Texte texte;
protected Objectif objectif;
@BeforeEach
public void setup() {
texte = new Texte(is);
objectif = new Objectif("objectif", 2);
}
@Test
public void test_does_not_notify_unattached_observer() {
texte.notifyObservers();
assertFalse( objectif.wasNotified);
}
@Test
public void test_notifies_observer() {
texte.attach(objectif);
texte.lancer();
assertTrue(objectif.wasNotified);
}
@Test
public void test_notifies_several_attached_observers(){
Objectif objectif2 = new Objectif("objectif2", 2);
Objectif objectif3 = new Objectif("objectif3", 2);
texte.attach(objectif);
texte.attach(objectif2);
texte.attach(objectif3);
texte.lancer();
assertTrue(objectif.wasNotified);
assertTrue(objectif2.wasNotified);
assertTrue(objectif3.wasNotified);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment