Probleme de calcul en JAVA

Résolu
cs_oceane751 Messages postés 151 Date d'inscription jeudi 23 décembre 2004 Statut Membre Dernière intervention 20 décembre 2012 - 25 juil. 2005 à 03:11
cs_oceane751 Messages postés 151 Date d'inscription jeudi 23 décembre 2004 Statut Membre Dernière intervention 20 décembre 2012 - 26 juil. 2005 à 00:37
bonjour à tous!!
voila comme le dit le sujet, je rencontre un problee lorsque je veux faire l'addition du prix dun "melon" et celui dune "salade"
en effet, mon code ne me donne que le prix du melon PUIS celui de la salade et il ne me fais pas l'addition des 2
ps : mon projet étant celui du calcul d'un prix total


voici mon code :



// 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;


public class Cartee extends Frame


{


Label entree = new Label("entree");


Checkbox salade = new Checkbox("salade");


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(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();


}


}






de plus, est ce que vous pouvez me mettre sur la voie pour un autre truc..


en efet je viens de m'apercevoir que l'on ne peut pas (celon mon code) prendre plusieurs fois le meme element comme par exemple : 2 melon et 3 salade, donc si quelqu'un pouvait me mettre sur un piste, m'aider quoi...


merci beaucoup !!

2 réponses

cs_oceane751 Messages postés 151 Date d'inscription jeudi 23 décembre 2004 Statut Membre Dernière intervention 20 décembre 2012
26 juil. 2005 à 00:37
3
super_toinou Messages postés 764 Date d'inscription mardi 25 mai 2004 Statut Membre Dernière intervention 8 mars 2011 6
25 juil. 2005 à 12:28
pr info, le total etait bien calculé
j te conseille qd mm de bien regarder les indications qu on te donne, et d ajouter ta touche personnelle
l informatique c est aussi chercher par soit meme
voila ton code avec des nb saisissables

++ Toinou


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.JTextField;


import sun.security.util.BigInt;


public
class Cartee
extends Frame


{


Label entree =
new Label(
"entree");


JTextField saladeField =
new JTextField(2);


Label salade =
new Label(
"salade");


JTextField melonField =
new JTextField(2);


Label melon =
new Label(
"melon");


JTextField totalPrix =
new JTextField(3);


Label totalLabel =
new Label(
"total");


Button bout =
new Button(
"ticket");


Button quitter =
new Button(
"quitter l'application");


Hashtable associationProduitsPrix;



public Cartee()


{



super(
"creperie");


initFrame();


addBouton();


initPrix();


initBoutonsListeners();


pack();


}



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(saladeField);


add(salade);


add(melonField);


add(melon);


add(totalPrix);


add(totalLabel);


add(bout);


add(quitter);


}



private
void initPrix()


{


associationProduitsPrix =
new Hashtable();


associationProduitsPrix.put(saladeField,
new BigInteger(
"10"));


associationProduitsPrix.put(melonField,
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())


{


JTextField field = (JTextField)enu.nextElement();


BigInteger nombre =
new BigInteger(
"0");



try


{


String nb = field.getText();


nombre =
new BigInteger(nb);


}



catch (Exception ex)


{


JOptionPane.showMessageDialog(
null, ex);


}


BigInteger prix = (BigInteger)associationProduitsPrix.get(field);


total = total.add(prix.multiply(nombre));


}


totalPrix.setText(
"" + total.intValue());


repaint();


}


});


}



class ListenerQuitter
implements ActionListener


{



public
void actionPerformed(ActionEvent e)


{


System.exit(0);


}


}



public
static
void main(String[] args)


{


Frame f =
new Cartee();


}


}
0
Rejoignez-nous