// penser à mettre l'opportunité de choisir plusieurs fois le meme element
package com.creperiee.test;
import java.awt.Button;
import java.awt.Checkbox;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.Frame;
import java.awt.Label;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.math.BigInteger;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.Choice;
public class Cartee extends Frame
{
Label entree = new Label("entree");
Checkbox salade = new Checkbox("salade");
Choice maCombo = new Choice();
Checkbox melon = new Checkbox("melon");
Label cs = new Label("crepe salee");
Checkbox jf = new Checkbox("jambon fromage");
Checkbox complete = new Checkbox("complete");
Label csu = new Label("crepe sucree");
Checkbox su = new Checkbox("crepe au sucre");
Checkbox choc = new Checkbox("crepe au chocolat");
Label boi = new Label("boisson");
Checkbox coca = new Checkbox("coca cola");
Checkbox cidre = new Checkbox("cidre");
Button bout = new Button("ticket");
Button quitter = new Button("quitter l'application");
Hashtable associationProduitsPrix;
public Cartee()
{
super("creperie");
initFrame();
addBouton();
initPrix();
initBoutonsListeners();
}
private void initFrame()
{
setSize(250, 250);
setLayout(new FlowLayout()); //sinon n'affiche qu'un seul element ds la fenetre
setVisible(true);
setBackground(Color.yellow);
setForeground(Color.black);
}
private void addBouton()
{
add(entree);
add(salade);
add(maCombo);
maCombo.add("1");
maCombo.add("2");
add(melon);
add(cs);
add(jf);
add(complete);
add(csu);
add(su);
add(choc);
add(boi);
add(coca);
add(cidre);
add(bout);
add(quitter);
}
private void initPrix()
{
associationProduitsPrix = new Hashtable();
associationProduitsPrix.put(salade, new BigInteger("10"));
associationProduitsPrix.put(melon, new BigInteger("5"));
}
private void initBoutonsListeners()
{
quitter.addActionListener(new ListenerQuitter());
bout.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
BigInteger total = new BigInteger("0");
Enumeration enu = associationProduitsPrix.keys();
while (enu.hasMoreElements())
{
Checkbox check = (Checkbox)enu.nextElement();
if (check.getState())
{
BigInteger prix = (BigInteger)associationProduitsPrix.get(check);
total = total.add(prix);
}
}
JOptionPane.showMessageDialog(null, "Total : " + total+ " \u20ac");
}
});
}
class ListenerQuitter implements ActionListener
{
public void actionPerformed(ActionEvent e)
{
System.exit(0);
}
}
public static void main(String[] args)
{
Frame f = new Cartee();
}
}