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
|
package com.carte_crepee.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.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.util.Enumeration;
import java.util.Hashtable;
import javax.swing.JOptionPane;
import javax.swing.*;
import java.awt.Choice;
import java.util.Properties;
import java.io.*;
import java.awt.*;
import com.carte_crepee.outil.Produit;
public class carte extends Frame implements ActionListener
{
private Properties props = new Properties();
private Hashtable associationProduitsPrix= new Hashtable();
private GridLayout grid;
private TextField text;
private TextField serveur;
private Label entree;
private Label crepesalee;
private Label crepesucree;
private Label boisson;
private Checkbox salade;
private Label lab;
private Label lab2;
private Label lab3;
private Choice maCombo;
private Checkbox melon;
private Label cs;
private Checkbox jf;
private Button bouton;
private Button quitter;
class WindowEventHandler extends WindowAdapter
{
public void windowClosing(WindowEvent we)
{
dispose(); // quitte l'objet
}
}
public carte()
{
super("creperie");
addWindowListener(new WindowEventHandler());
// Lit les données du fichier de propriétés
//Properties props = new Properties();
try
{
props.load(new FileInputStream("carte.txt"));
}
catch (FileNotFoundException e)
{
System.err.println("Fichier carte.txt non trouvé");
}
catch (IOException e)
{
System.err.println("Impossible de lire le fichier carte.txt");
}
// Création des champs
//nombre d'article = au nombre de clé dans le fichier properties
//puisque chaque article forme un couple de clés libellé/prix
entree = new Label ("entree");
add(entree);
crepesalee = new Label("crepe salée");
add(crepesalee);
crepesucree = new Label("crepe sucree");
add(crepesucree);
boisson = new Label("boisson");
add(boisson);
Panel panel = new Panel();
GridLayout jo = new GridLayout(3, 2, 5, 5);
panel.setLayout(jo);
panel.applyComponentOrientation( ComponentOrientation.RIGHT_TO_LEFT);
int length=props.size()/2;
for (int i=0;i<length;i++)
{
String valeur = props.getProperty("libelle_"+i);
//String crepe = props.getProperty("libelle_crepe_"+i);
if (valeur!=null)
{
Choice combo = new Choice();
combo.add("1");
combo.add("2");
add(combo);
Checkbox checkbox = new Checkbox(valeur);
add(checkbox);
String prix = props.getProperty("prix_"+i);
Label label = new Label(prix);
add(label);
Produit produit = new Produit();
produit.setComboQuantite(combo);
produit.setPrix(new BigDecimal(prix));
// ajout dans la table de hash pour le calcul du prix
// la clé étant l'objet chechbox correspondant à l'article
associationProduitsPrix.put(checkbox, produit);
}
}
// on met les boutons
text = new TextField ("numero de table");
add(text);
serveur = new TextField ("nom du serveur");
add(serveur);
bouton = new Button("ticket");
add(bouton);
quitter = new Button("quitter l'application");
add(quitter);
// on intialise les évènements sur les boutons
initBoutonsListeners();
// Initialise la fenêtre
setLayout(new FlowLayout()); //sinon n'affiche qu'un seul element ds la fenetre
setBackground(Color.white);
setForeground(Color.black);
quitter.addActionListener(this);
}
// Initialise les évènements des boutons
private void initBoutonsListeners()
{
bouton.addActionListener(new ActionListener()
{
public void actionPerformed(ActionEvent e)
{
BigDecimal total = new BigDecimal("0.00"); //initialise à 0
Enumeration enu = associationProduitsPrix.keys();
while (enu.hasMoreElements())
{
Checkbox check = (Checkbox)enu.nextElement();
if (check.getState()) // si un check a été coché
{
Produit produit = (Produit)associationProduitsPrix.get(check);
BigDecimal prix = produit.getPrix();
BigDecimal quantite = new BigDecimal (new BigInteger(produit.getComboQuantite().getSelectedItem()));
total = total.add(prix.multiply(quantite));
}
}
JOptionPane.showMessageDialog(null,"A la bonne crêpe" + "\n" + "67 rue de vaugirard" + "\n" + "75006 paris" + "\n" + " Tel : 01.45.44.38.46" + "\n" + "serveur : " + serveur.getText() + "\n" + "Total : " + total+ "\n" + "Numero de table : "+text.getText()+ "\n" );
}
});
}
public void actionPerformed(ActionEvent event)
{
if (event.getSource() == quitter)
{
dispose();
System.exit(0);
}
}
public static void main(String[] args)
{
Frame f = new carte();
// f.setContentPane(panel);
f.setTitle("Ticket client");
f.setBounds(100,100,300,200);
f.pack();
f.setSize(500,500);
f.show();
}
} |
Partager