tp2Imunes - devMadelaine - Q1ControleBlancDev32

This commit is contained in:
2023-12-18 21:05:47 +01:00
parent 24cc7baddc
commit 1fc92e50c3
32 changed files with 1034 additions and 3 deletions

View File

@@ -0,0 +1,15 @@
import javax.swing.JCheckBox;
public class Coche extends JCheckBox{
private Ingredient valeur;
public Coche(Ingredient valeur){
super(valeur.name());
this.valeur = valeur;
}
public Ingredient getValeur(){
return this.valeur;
}
}

View File

@@ -0,0 +1,21 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class EvenementIngredient implements ItemListener{
private PileIngredient historiqueChoix;
private JButton boutonRetour;
public EvenementIngredient(PileIngredient historiqueChoix, JButton boutonRetour){
this.historiqueChoix = historiqueChoix;
this.boutonRetour = boutonRetour;
}
@Override
public void itemStateChanged(ItemEvent e){
Coche coche = (Coche) e.getSource();
this.historiqueChoix.push(coche.getValeur());
this.boutonRetour.setEnabled(true);
}
}

View File

@@ -0,0 +1,33 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class EvenementRetour implements ActionListener{
private PileIngredient historique;
private Coche[] listeCoche;
private EvenementIngredient evenementIngredient;
public EvenementRetour(PileIngredient historique, Coche[] listeCoche, EvenementIngredient evenementIngredient){
this.historique = historique;
this.listeCoche = listeCoche;
this.evenementIngredient = evenementIngredient;
}
@Override
public void actionPerformed(ActionEvent e){
Ingredient dernierChoisis = this.historique.pop();
JButton boutonRetour = (JButton) e.getSource();
for (Coche coche : this.listeCoche){
if (coche.getValeur() == dernierChoisis){
coche.removeItemListener(evenementIngredient);
coche.setSelected(!coche.isSelected());
coche.addItemListener(evenementIngredient);
if (historique.isEmpty()){
boutonRetour.setEnabled(false);
}
}
}
}
}

View File

@@ -0,0 +1,38 @@
import javax.swing.*;
import java.awt.*;
public class Fenetre extends JFrame{
private PileIngredient historique;
public Fenetre(){
super();
this.setTitle("Question1");
this.setSize(500, 300);
this.setLocation(0, 0);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
this.historique = new PileIngredient();
this.addIngredient();
}
private void addIngredient(){
JButton retour = new JButton("RETOUR");
EvenementIngredient evenementIngredient = new EvenementIngredient(this.historique, retour);
Ingredient[] listeIngredient = Ingredient.values();
int nbIngredient = listeIngredient.length;
Coche[] listeCoche = new Coche[nbIngredient];
int i;
this.setLayout(new GridLayout(1,nbIngredient+1));
for (i=0; i<nbIngredient; i++){
listeCoche[i] = new Coche(listeIngredient[i]);
listeCoche[i].addItemListener(evenementIngredient);
this.add(listeCoche[i]);
}
EvenementRetour evenementRetour = new EvenementRetour(this.historique, listeCoche, evenementIngredient);
retour.setEnabled(false);
retour.addActionListener(evenementRetour);
this.add(retour);
}
}

View File

@@ -0,0 +1,5 @@
enum Ingredient{
SALADE,
TOMATES,
OIGNONS
}

View File

@@ -0,0 +1,23 @@
import java.util.*;
public class PileIngredient{
private Deque<Ingredient> pile;
public PileIngredient(){
this.pile = new ArrayDeque<>();
}
public void push(Ingredient valeur){
this.pile.push(valeur);
}
public Ingredient pop(){
return this.pile.pop();
}
public boolean isEmpty(){
return this.pile.isEmpty();
}
}

View File

@@ -0,0 +1,11 @@
import javax.swing.*;
import java.awt.*;
import java.awt.event.*;
public class Q4Main{
public static void main(String[] args) {
Fenetre fenetre = new Fenetre();
fenetre.setVisible(true);
}
}