Commit 159fe3fa authored by Antoine Hazebrouck's avatar Antoine Hazebrouck

fix bug immutable Proposition

parent 15f0bad4
...@@ -3,16 +3,20 @@ package imt.concours; ...@@ -3,16 +3,20 @@ package imt.concours;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
import java.util.Set; import java.util.Set;
import java.util.stream.Collectors;
import imt.personnes.Jury; import imt.personnes.Jury;
import lombok.AccessLevel;
import lombok.Data; import lombok.Data;
import lombok.Setter;
@Data @Data
public class Concours { public class Concours {
private final Jury jury1; private final Jury jury1;
private final Jury jury2; private final Jury jury2;
private final Jury jury3; private final Jury jury3;
private final Set<Proposition> propositions; // minimun 5 @Setter(value = AccessLevel.NONE)
private Set<Proposition> propositions; // minimun 5
public Concours(Jury jury1, Jury jury2, Jury jury3, Set<Proposition> propositions) { public Concours(Jury jury1, Jury jury2, Jury jury3, Set<Proposition> propositions) {
this.jury1 = jury1; this.jury1 = jury1;
...@@ -38,11 +42,14 @@ public class Concours { ...@@ -38,11 +42,14 @@ public class Concours {
} }
public void noter(Proposition proposition, int note) { public void noter(Proposition proposition, int note) {
for (Proposition propositionLoop : propositions) { propositions = propositions.stream()
.map(propositionLoop -> {
if (propositionLoop.equals(proposition)) { if (propositionLoop.equals(proposition)) {
propositionLoop.setNote(Optional.of(note)); return propositionLoop.withNote(Optional.of(note)); // .setNote(Optional.of(note));
}
} }
return propositionLoop;
})
.collect(Collectors.toSet());
} }
public List<Proposition> classement() { public List<Proposition> classement() {
......
package imt.concours; package imt.concours;
import java.util.ArrayList;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List; import java.util.List;
import java.util.Queue; import java.util.Queue;
...@@ -9,9 +10,11 @@ import imt.personnes.Chef; ...@@ -9,9 +10,11 @@ import imt.personnes.Chef;
public class FileConcours { public class FileConcours {
private final Queue<Concours> concours; private final Queue<Concours> concours;
private final List<Concours> concoursTermines;
public FileConcours(Concours... concours) { public FileConcours(Concours... concours) {
this.concours = new LinkedList<Concours>(Stream.of(concours).toList()); this.concours = new LinkedList<Concours>(Stream.of(concours).toList());
this.concoursTermines = new ArrayList<>();
} }
public List<Concours> concoursAuquelParticipe(Chef chef) { public List<Concours> concoursAuquelParticipe(Chef chef) {
...@@ -20,4 +23,22 @@ public class FileConcours { ...@@ -20,4 +23,22 @@ public class FileConcours {
.contains(chef)) .contains(chef))
.toList(); .toList();
} }
public Concours concoursActuel() {
return concours.peek();
}
// true is ca a marché, false si le concours actuel est pas bien fini
public boolean passerAuConcoursSuivant() {
if (concoursActuel().estTermine()) {
concoursTermines.add(concours.poll());
return true;
} else {
return false;
}
}
public List<Concours> concoursTermines() {
return concoursTermines;
}
} }
...@@ -5,16 +5,22 @@ import java.util.Optional; ...@@ -5,16 +5,22 @@ import java.util.Optional;
import imt.ingredients.Plat; import imt.ingredients.Plat;
import imt.personnes.Chef; import imt.personnes.Chef;
import lombok.Data; import lombok.Data;
import lombok.With;
@Data @Data
public class Proposition { public class Proposition {
private final Plat platPropose; private final Plat platPropose;
private final Chef chef; private final Chef chef;
private Optional<Integer> note; @With
private final Optional<Integer> note;
public Proposition(Plat platPropose, Chef chef) { private Proposition(Plat platPropose, Chef chef, Optional<Integer> note) {
this.platPropose = platPropose; this.platPropose = platPropose;
this.chef = chef; this.chef = chef;
this.note = Optional.empty(); this.note = note;
}
public Proposition(Plat platPropose, Chef chef) {
this(platPropose, chef, Optional.empty());
} }
} }
...@@ -19,11 +19,11 @@ import imt.personnes.Jury; ...@@ -19,11 +19,11 @@ import imt.personnes.Jury;
import imt.personnes.Specialite; import imt.personnes.Specialite;
public class ConcoursTest { public class ConcoursTest {
public static Jury jury1 = new Jury(1, "nom1", "prenom1", "06", Genre.HOMME); public static Jury JURY_1 = new Jury(1, "nom1", "prenom1", "06", Genre.HOMME);
public static Jury jury2 = new Jury(2, "nom2", "prenom2", "06", Genre.HOMME); public static Jury JURY_2 = new Jury(2, "nom2", "prenom2", "06", Genre.HOMME);
public static Jury jury3 = new Jury(3, "nom3", "prenom3", "06", Genre.HOMME); public static Jury JURY_3 = new Jury(3, "nom3", "prenom3", "06", Genre.HOMME);
public static Chef chef1 = new Chef(1, "chef1", "prenomchef1", "07", Genre.HOMME, 2, Specialite.PATISSIER, public static Chef CHEF_1 = new Chef(1, "chef1", "prenomchef1", "07", Genre.HOMME, 2, Specialite.PATISSIER,
Set.of(new Plat(1, "plat1", new Ingredient[] {}))); Set.of(new Plat(1, "plat1", new Ingredient[] {})));
public static Chef chef2 = new Chef(2, "chef2", "prenomchef2", "07", Genre.HOMME, 2, Specialite.PATISSIER, public static Chef chef2 = new Chef(2, "chef2", "prenomchef2", "07", Genre.HOMME, 2, Specialite.PATISSIER,
Set.of(new Plat(2, "plat2", new Ingredient[] {}))); Set.of(new Plat(2, "plat2", new Ingredient[] {})));
...@@ -34,75 +34,79 @@ public class ConcoursTest { ...@@ -34,75 +34,79 @@ public class ConcoursTest {
public static Chef chef5 = new Chef(5, "chef5", "prenomchef5", "07", Genre.HOMME, 2, Specialite.PATISSIER, public static Chef chef5 = new Chef(5, "chef5", "prenomchef5", "07", Genre.HOMME, 2, Specialite.PATISSIER,
Set.of(new Plat(5, "plat5", new Ingredient[] {}))); Set.of(new Plat(5, "plat5", new Ingredient[] {})));
public static Proposition proposition1 = new Proposition(new Plat(1, "plat1", new Ingredient[] {}), chef1); public static Proposition PROPOSITION_1 = new Proposition(new Plat(1, "plat1", new Ingredient[] {}), CHEF_1);
public static Proposition proposition2 = new Proposition(new Plat(2, "plat2", new Ingredient[] {}), chef2); public static Proposition PROPOSITION_2 = new Proposition(new Plat(2, "plat2", new Ingredient[] {}), chef2);
public static Proposition proposition3 = new Proposition(new Plat(3, "plat3", new Ingredient[] {}), chef3); public static Proposition PROPOSITION_3 = new Proposition(new Plat(3, "plat3", new Ingredient[] {}), chef3);
public static Proposition proposition4 = new Proposition(new Plat(4, "plat4", new Ingredient[] {}), chef4); public static Proposition PROPOSITION_4 = new Proposition(new Plat(4, "plat4", new Ingredient[] {}), chef4);
public static Proposition proposition5 = new Proposition(new Plat(5, "plat5", new Ingredient[] {}), chef5); public static Proposition PROPOSITION_5 = new Proposition(new Plat(5, "plat5", new Ingredient[] {}), chef5);
public static Set<Proposition> propositions = Set.of( public static Set<Proposition> PROPOSITIONS = Set.of(
proposition1, PROPOSITION_1,
proposition2, PROPOSITION_2,
proposition3, PROPOSITION_3,
proposition4, PROPOSITION_4,
proposition5); PROPOSITION_5);
@Test @Test
void concours_trois_jurys_cinq_chefs() { void concours_trois_jurys_cinq_chefs() {
var propositions = Set.of( var propositions = Set.of(
proposition1, PROPOSITION_1,
proposition2, PROPOSITION_2,
proposition3, PROPOSITION_3,
proposition4); PROPOSITION_4);
assertThrows(IllegalArgumentException.class, () -> new Concours(jury1, jury2, jury3, propositions)); assertThrows(IllegalArgumentException.class, () -> new Concours(JURY_1, JURY_2, JURY_3, propositions));
} }
@Test @Test
void les_chefs_proposent_un_plat_qu_ils_savent_faire() { void les_chefs_proposent_un_plat_qu_ils_savent_faire() {
var propositions = Set.of( var propositions = Set.of(
proposition1, PROPOSITION_1,
proposition2, PROPOSITION_2,
proposition3, PROPOSITION_3,
proposition4, PROPOSITION_4,
new Proposition(new Plat(99, "je sais pas faire donc exception", new Ingredient[]{}), chef5)); new Proposition(new Plat(99, "je sais pas faire donc exception", new Ingredient[]{}), chef5));
assertThrows(IllegalArgumentException.class, () -> new Concours(jury1, jury2, jury3, propositions)); assertThrows(IllegalArgumentException.class, () -> new Concours(JURY_1, JURY_2, JURY_3, propositions));
} }
@Test @Test
void fin_du_concours() { void fin_du_concours() {
Concours concours = new Concours(jury1, jury2, jury3, propositions); Concours concours = new Concours(JURY_1, JURY_2, JURY_3, PROPOSITIONS);
assertEquals(false, concours.estTermine()); assertEquals(false, concours.estTermine());
concours.noter(proposition1, 10); concours.noter(PROPOSITION_1, 10);
concours.noter(proposition2, 3); concours.noter(PROPOSITION_2, 3);
concours.noter(proposition3, 8); concours.noter(PROPOSITION_3, 8);
concours.noter(proposition4, 9); concours.noter(PROPOSITION_4, 9);
concours.noter(proposition5, 18); concours.noter(PROPOSITION_5, 18);
assertEquals(true, concours.estTermine()); assertEquals(true, concours.estTermine());
} }
@Test @Test
void classement_actuel() { void classement_actuel() {
Concours concours1 = new Concours(jury1, jury2, jury3, propositions); Proposition proposition1 = PROPOSITION_1.withNote(Optional.of(4));
Proposition proposition2 = PROPOSITION_2.withNote(Optional.of(3));
Proposition proposition3 = PROPOSITION_3.withNote(Optional.of(2));
Proposition proposition4 = PROPOSITION_4.withNote(Optional.of(1));
Proposition proposition5 = PROPOSITION_5;
Concours concours1 = new Concours(JURY_1, JURY_2, JURY_3, Set.of(
proposition1,
proposition2,
proposition3,
proposition4,
proposition5
));
List<Proposition> expected = new ArrayList<>(); List<Proposition> expected = new ArrayList<>();
expected.add(proposition1);
expected.add(proposition2);
expected.add(proposition3);
expected.add(proposition4); expected.add(proposition4);
expected.add(proposition3);
expected.add(proposition2);
expected.add(proposition1);
expected.add(proposition5); expected.add(proposition5);
proposition1.setNote(Optional.of(1));
proposition2.setNote(Optional.of(2));
proposition3.setNote(Optional.of(3));
proposition4.setNote(Optional.of(4));
// proposition5.setNote(Optional.of(100));
assertThat(concours1.classement()).isEqualTo(expected); assertThat(concours1.classement()).isEqualTo(expected);
System.out.println(concours1.classement());
} }
} }
package imt.concours; package imt.concours;
import static imt.concours.ConcoursTest.chef1; import static imt.concours.ConcoursTest.CHEF_1;
import static imt.concours.ConcoursTest.jury1; import static imt.concours.ConcoursTest.JURY_1;
import static imt.concours.ConcoursTest.jury2; import static imt.concours.ConcoursTest.JURY_2;
import static imt.concours.ConcoursTest.jury3; import static imt.concours.ConcoursTest.JURY_3;
import static imt.concours.ConcoursTest.proposition1; import static imt.concours.ConcoursTest.PROPOSITIONS;
import static imt.concours.ConcoursTest.propositions; import static imt.concours.ConcoursTest.*;
import static org.assertj.core.api.Assertions.assertThat; import static org.assertj.core.api.Assertions.assertThat;
import java.util.HashSet; import java.util.HashSet;
...@@ -23,18 +23,48 @@ import imt.personnes.Specialite; ...@@ -23,18 +23,48 @@ import imt.personnes.Specialite;
public class FileConcoursTest { public class FileConcoursTest {
@Test @Test
void fileDeConcours_tout_les_concours_du_chef() { void fileDeConcours_tout_les_concours_du_chef() {
Set<Proposition> propositionsSansTelChef = new HashSet<>(propositions); Set<Proposition> propositionsSansTelChef = new HashSet<>(PROPOSITIONS);
propositionsSansTelChef.remove(proposition1); propositionsSansTelChef.remove(PROPOSITION_1);
propositionsSansTelChef.add( propositionsSansTelChef.add(
new Proposition(new Plat(99, "plat99", new Ingredient[] {}), new Chef(99, "nom99", "prenom99", "QSD", new Proposition(new Plat(99, "plat99", new Ingredient[] {}), new Chef(99, "nom99", "prenom99", "QSD",
Genre.HOMME, 4, Specialite.CUISINIER, Set.of(new Plat(99, "plat99", new Ingredient[] {}))))); Genre.HOMME, 4, Specialite.CUISINIER, Set.of(new Plat(99, "plat99", new Ingredient[] {})))));
Concours concours1 = new Concours(jury1, jury2, jury3, propositions); Concours concours1 = new Concours(JURY_1, JURY_2, JURY_3, PROPOSITIONS);
Concours concours2 = new Concours(jury1, jury2, jury3, propositionsSansTelChef); Concours concours2 = new Concours(JURY_1, JURY_2, JURY_3, propositionsSansTelChef);
Concours concours3 = new Concours(jury1, jury2, jury3, new HashSet<>(propositions)); Concours concours3 = new Concours(JURY_1, JURY_2, JURY_3, new HashSet<>(PROPOSITIONS));
FileConcours fileConcours = new FileConcours(concours1, concours2, concours3); FileConcours fileConcours = new FileConcours(concours1, concours2, concours3);
assertThat(fileConcours.concoursAuquelParticipe(chef1)).isEqualTo(List.of(concours1, concours3)); assertThat(fileConcours.concoursAuquelParticipe(CHEF_1)).isEqualTo(List.of(concours1, concours3));
}
@Test
void concours_termines() {
Concours concours1 = new Concours(JURY_1, JURY_2, JURY_3, PROPOSITIONS);
Concours concours2 = new Concours(JURY_1, JURY_2, JURY_3, PROPOSITIONS);
Concours concours3 = new Concours(JURY_1, JURY_2, JURY_3, PROPOSITIONS);
FileConcours fileConcours = new FileConcours(concours1, concours2, concours3);
assertThat(fileConcours.passerAuConcoursSuivant()).isFalse();
forcer_la_fin_du_concours(concours1);
assertThat(fileConcours.passerAuConcoursSuivant()).isTrue();
assertThat(fileConcours.concoursTermines()).isEqualTo(List.of(concours1));
}
void forcer_la_fin_du_concours(Concours concours) {
assertThat(concours.estTermine()).isFalse();
concours.noter(PROPOSITION_1, 10);
concours.noter(PROPOSITION_2, 20);
concours.noter(PROPOSITION_3, 30);
concours.noter(PROPOSITION_4, 40);
concours.noter(PROPOSITION_5, 50);
assertThat(concours.estTermine()).isTrue();
}
@Test
void concours_auquel_un_chef_a_participe() {
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment