1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288
| import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.Graphics;
import java.awt.Image;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import javax.imageio.ImageIO;
import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.DefaultListCellRenderer;
import javax.swing.DefaultListModel;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFormattedTextField;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JRadioButton;
import javax.swing.JScrollPane;
import javax.swing.JSeparator;
import javax.swing.border.LineBorder;
public class Onglet_commande extends JPanel {
private JButton commander = new JButton("Commander et payer");
private JButton majprod = new JButton("MAJ Produits");
private JLabel res = new JLabel();
private JList<String> liste_produit = new JList<String>();
private JFormattedTextField nom_client = new JFormattedTextField();
private DefaultListModel<String> listModel = new DefaultListModel<String>();
private JList<String> res_commande = new JList<String>();
private DefaultListModel<String> listModel2 = new DefaultListModel<String>();
private JRadioButton cb = new JRadioButton("CB", true);
private JRadioButton cheque_resto = new JRadioButton("Chèque restaurant");
private JRadioButton especes = new JRadioButton("Espèces");
// Pour permettre la sélection d'un seul moyen de paiement
private ButtonGroup bg = new ButtonGroup();
// En cas d'absence du nom de client
public Onglet_commande(){
// Les panels
JPanel pan1 = new JPanel();
JPanel pan1b = new JPanel();
JPanel pan2 = new JPanel();
JPanel pan2b = new JPanel();
JPanel pan3 = new JPanel();
JPanel pan4 = new JPanel();
JPanel princ = new JPanel();
// Pour les séparateurs
JPanel s1 = new JPanel();
JPanel s2 = new JPanel();
JPanel resultat = new JPanel();
// On associe les Layout managers aux JPanel
pan1.setLayout(new BoxLayout(pan1, BoxLayout.LINE_AXIS));
pan1b.setLayout(new BoxLayout(pan1b, BoxLayout.LINE_AXIS));
pan2.setLayout(new BoxLayout(pan2, BoxLayout.LINE_AXIS));
pan2b.setLayout(new BoxLayout(pan2b, BoxLayout.LINE_AXIS));
pan3.setLayout(new BoxLayout(pan3, BoxLayout.LINE_AXIS));
pan4.setLayout(new BoxLayout(pan4, BoxLayout.LINE_AXIS));
princ.setLayout(new BoxLayout(princ, BoxLayout.PAGE_AXIS));
s1.setLayout(new BoxLayout(s1, BoxLayout.LINE_AXIS));
s2.setLayout(new BoxLayout(s2, BoxLayout.LINE_AXIS));
resultat.setLayout(new BoxLayout(resultat, BoxLayout.LINE_AXIS));
// les Label
JLabel pres = new JLabel("Bienvenue chez Homer");
pres.setFont(new Font("Tahoma", Font.BOLD, 30));
pres.setForeground(Color.ORANGE);
JLabel label_prod = new JLabel("Produits disponibles :");
label_prod.setFont(new Font("Tahoma", Font.BOLD, 20));
label_prod.setForeground(Color.GREEN);
nom_client.setBackground(Color.WHITE);
nom_client.setPreferredSize(new Dimension(120,30));
JLabel client = new JLabel("Nom du client :");
res = new JLabel("En attente d'une commande...");
res.setForeground(Color.RED);
JLabel choix_paiement = new JLabel ("Choisissez votre mode de paiement:");
// les séparateurs
JSeparator sep1 = new JSeparator();
JSeparator sep2 = new JSeparator();
// Les listes
liste_produit.setCellRenderer(new DefaultListCellRenderer());
liste_produit.setModel(listModel);
liste_produit.setBorder(new LineBorder(Color.GREEN));
JScrollPane scroll1 = new JScrollPane(liste_produit);
for (int i=ProduitsEnVente.getProducts().size()-1 ; i>=0;i--){
ProduitsEnVente.getInstance();
String temp = ProduitsEnVente.getProducts().get(i).getNom_produit();
listModel.addElement(temp);
}
res_commande.setVisibleRowCount(5);
res_commande.setCellRenderer(new DefaultListCellRenderer());
res_commande.setModel(listModel2);
res_commande.setBorder(new LineBorder(Color.RED));
res_commande.setBackground(Color.YELLOW);
JScrollPane scroll2 = new JScrollPane(res_commande);
// RadioButtons (CB est le moyen de paiement par défaut)
bg.add(cb);
bg.add(especes);
bg.add(cheque_resto);
// On modifie les 2 boutons
commander.setBackground(Color.RED);
commander.addActionListener(new BoutonCommanderListener());
majprod.setBackground(Color.YELLOW);
majprod.addActionListener(new BoutonMAJListener());
// On construit le message d'erreur
// On ajuste les "sous" JPanel via des RigidArea pour créer des composants invisibles
pan1.add(pres);
s1.add(sep1);
pan1b.add(client);
pan1b.add(Box.createRigidArea(new Dimension(30,0)));
pan1b.add(nom_client);
s2.add(sep2);
pan2.add(label_prod);
pan2.add(Box.createRigidArea(new Dimension(30,0)));
pan2.add(scroll1);
pan2b.add(choix_paiement);
pan2b.add(Box.createRigidArea(new Dimension(15,0)));
pan2b.add(cb);
pan2b.add(Box.createRigidArea(new Dimension(30,0)));
pan2b.add(especes);
pan2b.add(Box.createRigidArea(new Dimension(30,0)));
pan2b.add(cheque_resto);
pan3.add(commander);
pan3.add(Box.createRigidArea(new Dimension(30,0)));
pan3.add(majprod);
pan4.add(scroll2);
resultat.add(res);
// On ajuste les "sous" JPanel dans le container "princ"
princ.add(pan1);
princ.add(Box.createRigidArea(new Dimension(0,30)));
princ.add(s1);
princ.add(Box.createRigidArea(new Dimension(0,30)));
princ.add(pan1b);
princ.add(Box.createRigidArea(new Dimension(0,30)));
princ.add(s2);
princ.add(Box.createRigidArea(new Dimension(0,30)));
princ.add(pan2);
princ.add(Box.createRigidArea(new Dimension(0,30)));
princ.add(pan2b);
princ.add(Box.createRigidArea(new Dimension(0,20)));
princ.add(pan3);
princ.add(Box.createRigidArea(new Dimension(0,30)));
princ.add(resultat);
princ.add(Box.createRigidArea(new Dimension(0,30)));
princ.add(pan4);
add(princ);
}
// On met une image en fond
public void paintComponent(Graphics g){
super.paintComponent(g);
try {
Image img = ImageIO.read(new File("fastfood.jpg") );
g.drawImage(img, 0, 0, this.getWidth(), this.getHeight(), this);
}
catch (IOException e){
e.printStackTrace();
}
}
// ***************************** La classe intérieure pour le bouton "Commander et payer" ************************
class BoutonCommanderListener implements ActionListener{
public void actionPerformed(ActionEvent arg0) {
// On s'assure qu'un nom de client a été rentré
if (nom_client.getText().trim().length()>0){
listModel2.clear();
@SuppressWarnings("deprecation")
Object temp [] = liste_produit.getSelectedValues();
ArrayList<String> liste = new ArrayList<String>();
for (Object obj : temp ){
liste.add(obj.toString());
}
Commande com = new Commande();
com.CommanderProduits(nom_client.getText(), liste);
// On récupère le bouton coché pour le choix du mode de paiement
ArrayList<JRadioButton> liste_bouton = new ArrayList<JRadioButton>();
liste_bouton.add(cb);
liste_bouton.add(cheque_resto);
liste_bouton.add(especes);
String mode_pay = null;
for (JRadioButton bt : liste_bouton){
if (bt.isSelected()){
mode_pay=bt.getText();
}
}
// On affiche le résultat de la commande
res.setText(nom_client.getText()+" vient de régler la commande suivante, d'un montant de "+com.getMontant()+"€, par "+mode_pay+" :");
// Et on met à jour la liste des produits disponibles
for (String prod : liste){
int i;
i=0;
for (int j=listModel.size()-1;j>=0;j--){
String prodbis = listModel.get(j).toString();
if (prod.equalsIgnoreCase(prodbis) && i<1){
listModel.remove(j);
listModel2.addElement(prodbis);
i++;
}
}
}
} // ferme le if du départ (assurance qu'un nom client est entré)
// Si pas de nom de client on affiche une boîte de dialogue
else{
JOptionPane erreur = new JOptionPane();
ImageIcon img = new ImageIcon("chat.jpg");
erreur.showMessageDialog(null, "Vous n'avez pas saisi votre nom!", "Nom de client manquant", JOptionPane.WARNING_MESSAGE, img);
}
Ici je voudrais faire un truc du genre tableau.addRow("nom_client.getText()", "com.getMontant()", "mode_pay");
} // ferme la méthode
} // ferme la classe intérieure
// ***************************** La classe intérieure pour le bouton "MAJ Produits" ************************
class BoutonMAJListener implements ActionListener{
// On affiche les éléments présents dans le stock de produits
public void actionPerformed(ActionEvent e) {
listModel.clear();
for (int i=ProduitsEnVente.getProducts().size()-1 ; i>=0;i--){
ProduitsEnVente.getInstance();
String temp = ProduitsEnVente.getProducts().get(i).getNom_produit();
listModel.addElement(temp);
}
} // ferme la méthode
} // ferme la classe intérieure
} // ferme la classe |
Partager