Skip to content
Snippets Groups Projects
Commit eddb9c29 authored by Clement Franckelemon's avatar Clement Franckelemon
Browse files

Avancement de la vue

parent 038db955
Branches
No related tags found
No related merge requests found
package fr.univlille.iut.r304.cpt.View.Controller;
import fr.univlille.iut.r304.cpt.View.Model.Personne;
import fr.univlille.iut.r304.cpt.View.Model.Plateforme;
import javafx.scene.control.Button;
import javafx.scene.control.TextField;
public class Controller {
Plateforme plateforme;
public void init(){
plateforme = new Plateforme();
}
public void buttonAdd(Button button,TextField name){
button.setOnAction(e->{
plateforme.add(new Personne(name.getText()));
});
}
public void buttonTirage(Button button){
button.setOnAction(e->{
plateforme.echangeCadeau();
});
}
}
package fr.univlille.iut.r304.cpt;
package fr.univlille.iut.r304.cpt.View.Model;
public interface Observer {
public void update(Subject subject);
......
package fr.univlille.iut.r304.cpt;
package fr.univlille.iut.r304.cpt.View.Model;
public class Personne {
......
package fr.univlille.iut.r304.cpt;
package fr.univlille.iut.r304.cpt.View.Model;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
public class Plateforme {
public class Plateforme extends Subject {
private ArrayList<Personne> inscrits;
public Plateforme(){
......@@ -29,6 +29,7 @@ public class Plateforme {
for(int i=0; i<inscrits.size();i++){
listEchange.put(inscrits.get(i), inscrits.get((i+1)%inscrits.size()));
}
notifyObservers(listEchange);
return listEchange;
}
......
package fr.univlille.iut.r304.cpt;
package fr.univlille.iut.r304.cpt.View.Model;
import java.util.ArrayList;
......@@ -13,12 +13,12 @@ public abstract class Subject {
}
protected void notifyObservers(){
for(Observer observer : observers){
observer.update();
observer.update(this);
}
}
protected void notifyObservers(Object obj){
for(Observer observer : observers){
observer.update(obj);
observer.update(this,obj);
}
}
}
package fr.univlille.iut.r304.cpt.View.View;
import java.util.HashMap;
import java.util.Map.Entry;
import fr.univlille.iut.r304.cpt.View.Controller.Controller;
import fr.univlille.iut.r304.cpt.View.Model.Observer;
import fr.univlille.iut.r304.cpt.View.Model.Personne;
import fr.univlille.iut.r304.cpt.View.Model.Subject;
import javafx.application.Application;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.scene.layout.HBox;
import javafx.scene.layout.VBox;
import javafx.stage.Stage;
public class SampleView extends Application implements Observer {
public static void main(String[] args) {
launch(args);
}
Controller ctrl;
TextArea circularContent;
TextArea alphaContent;
TextArea listPersonne;
@Override
public void start(Stage primaryStage) {
ctrl = new Controller();
ctrl.init();
VBox left = inscriptionInit();
VBox right = echangeInit();
HBox root = new HBox(left, right);
Scene scene = new Scene(root, 400, 400);
primaryStage.setTitle("Tirage au sort des cadeaux");
primaryStage.setScene(scene);
primaryStage.show();
}
private VBox inscriptionInit(){
VBox left = new VBox();
Button addBtn = new Button();
Label title = new Label("Inscrits :");
TextField input = new TextField();
input.setMinSize(10, 30);
addBtn.setText("+");
ctrl.buttonAdd(addBtn,input);
listPersonne = new TextArea();
left.getChildren().addAll(title, input, addBtn,listPersonne);
return left;
}
private VBox echangeInit(){
VBox right = new VBox();
Button btn = new Button();
btn.setText("Nouveau tirage !");
ctrl.buttonTirage(btn);
Label circularTitle = new Label("Affichage circulaire");
circularContent = new TextArea();
circularContent.appendText("Example1");
circularContent.appendText("\n -> Example2");
circularContent.setPrefSize(250,100);
Label alphaTitle = new Label("Affichage alphabétique");
alphaContent = new TextArea();
alphaContent.setPrefSize(250,100);
alphaContent.appendText("Example1 -> Example2");
right.getChildren().addAll(btn, circularTitle, circularContent, alphaTitle, alphaContent);
return right;
}
private void echangeUpdate(HashMap<Personne,Personne> newList){
circularContent.clear();
boolean first = true;
for(Entry<Personne, Personne> echange : newList.entrySet()){
if (first){
first = false;
circularContent.appendText(echange.getKey().getName());
circularContent.appendText("\n -> "+echange.getValue().getName());
} else {
circularContent.appendText("\n -> "+echange.getKey().getName());
circularContent.appendText("\n -> "+echange.getValue().getName());
}
}
alphaContent.clear();
for(Entry<Personne, Personne> echange : newList.entrySet()){
alphaContent.appendText(echange.getKey().getName()+" -> "+echange.getValue().getName()+"\n");
}
}
@Override
public void update(Subject subject) {
// tmp
}
@Override
public void update(Subject subject, Object obj) {
if(obj instanceof Personne){
Personne personne = (Personne) obj;
listPersonne.appendText(personne.getName());
} else {
HashMap<Personne,Personne> listEchange = (HashMap<Personne,Personne>) obj;
echangeUpdate(listEchange);
}
}
}
package fr.univlille.iut.r304.ctp;
import fr.univlille.iut.r304.cpt.Personne;
import static org.junit.jupiter.api.Assertions.assertEquals;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import fr.univlille.iut.r304.cpt.View.Model.Personne;
public class PersonneTest {
private Personne personne;
@Test
public void testCreatePersonne(){
personne = new Personne("Didier");
AssertEquals(personne.getName(),"Didier");
assertEquals(personne.getName(),"Didier");
}
}
package fr.univlille.iut.r304.ctp;
import static org.junit.Assert.assertEquals;
import static org.junit.jupiter.api.Assertions.assertTrue;
import java.beans.Transient;
import java.util.HashMap;
import fr.univlille.iut.r304.cpt.Personne;
import fr.univlille.iut.r304.cpt.Plateforme;
import org.junit.Test;
import org.junit.jupiter.api.BeforeEach;
import fr.univlille.iut.r304.cpt.View.Model.Personne;
import fr.univlille.iut.r304.cpt.View.Model.Plateforme;
public class PlateformeTest {
private Plateforme plateforme;
......@@ -16,14 +22,14 @@ public class PlateformeTest {
@Test
public void testCreateEmptyPlatforme(){
AssertTrue(plateforme.isEmpty());
assertTrue(plateforme.isEmpty());
}
@Test
public void testAddPersonneInPlatforme(){
Personne personne = new Personne("Didier");
AssertTrue(plateforme.add(personne));
AssertEquals(plateforme.get(0),personne);
assertTrue(plateforme.add(personne));
assertEquals(plateforme.get(0),personne);
}
@Test
......@@ -33,7 +39,7 @@ public class PlateformeTest {
plateforme.add(personne1);
plateforme.add(personne2);
HashMap<Personne,Personne> listeEchange = plateforme.echangeCadeau();
AssertEquals(listeEchange.get(personne2),personne1);
AssertEquals(listeEchange.get(personne1),personne2);
assertEquals(listeEchange.get(personne2),personne1);
assertEquals(listeEchange.get(personne1),personne2);
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment