tp2Imunes - devMadelaine - Q1ControleBlancDev32
This commit is contained in:
15
DEV/DEV3.2/Controle_Machine_Blanc/Q1_Annulation/Coche.java
Normal file
15
DEV/DEV3.2/Controle_Machine_Blanc/Q1_Annulation/Coche.java
Normal 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;
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
||||
}
|
@@ -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);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
38
DEV/DEV3.2/Controle_Machine_Blanc/Q1_Annulation/Fenetre.java
Normal file
38
DEV/DEV3.2/Controle_Machine_Blanc/Q1_Annulation/Fenetre.java
Normal 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);
|
||||
}
|
||||
}
|
@@ -0,0 +1,5 @@
|
||||
enum Ingredient{
|
||||
SALADE,
|
||||
TOMATES,
|
||||
OIGNONS
|
||||
}
|
@@ -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();
|
||||
}
|
||||
}
|
11
DEV/DEV3.2/Controle_Machine_Blanc/Q1_Annulation/Q4Main.java
Normal file
11
DEV/DEV3.2/Controle_Machine_Blanc/Q1_Annulation/Q4Main.java
Normal 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);
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user