Se connecter avec
S'enregistrer | Connectez-vous

fichier texte et JAVA

Dernière réponse : dans Programmation

bonjour
voilà j'ai delà un code qui est presque fini mais je me suis aperçu que pour modifier ou rajouter des elemnts il fallait aller dans le code, ce qui n'est pas tres pratique...
alors l'utilisation d'un fichier texte (et non dune base de données, inutile dans ce que je veux faire)serait beaucoup plus approprié...

mais voilà, je voudrais savoir si je suis obligé de refaire totalement mon code, car je suis debitante et j'ai un peu les boulles de devoir tout refaire car j'ai vraiment du mal

alors je vous donne mon code et dites moi ce que je dois faire :

  1. // penser à mettre l'opportunité de choisir plusieurs fois le meme element
  2.  
  3. package com.creperiee.test;
  4.  
  5. import java.awt.Button;
  6.  
  7. import java.awt.Checkbox;
  8.  
  9. import java.awt.Color;
  10.  
  11. import java.awt.FlowLayout;
  12.  
  13. import java.awt.Frame;
  14.  
  15. import java.awt.Label;
  16.  
  17. import java.awt.event.ActionEvent;
  18.  
  19. import java.awt.event.ActionListener;
  20.  
  21. import java.math.BigInteger;
  22.  
  23. import java.util.Enumeration;
  24.  
  25. import java.util.Hashtable;
  26. import javax.swing.JOptionPane;
  27. import javax.swing.*;
  28. import java.awt.Choice;
  29.  
  30.  
  31. public class Cartee extends Frame
  32.  
  33. {
  34.  
  35. Label entree = new Label("entree");
  36.  
  37. Checkbox salade = new Checkbox("salade");
  38. Choice maCombo = new Choice();
  39.  
  40.  
  41.  
  42. Checkbox melon = new Checkbox("melon");
  43.  
  44. Label cs = new Label("crepe salee");
  45.  
  46. Checkbox jf = new Checkbox("jambon fromage");
  47.  
  48. Checkbox complete = new Checkbox("complete");
  49.  
  50. Label csu = new Label("crepe sucree");
  51.  
  52. Checkbox su = new Checkbox("crepe au sucre");
  53.  
  54. Checkbox choc = new Checkbox("crepe au chocolat");
  55.  
  56. Label boi = new Label("boisson");
  57.  
  58. Checkbox coca = new Checkbox("coca cola");
  59.  
  60. Checkbox cidre = new Checkbox("cidre");
  61.  
  62. Button bout = new Button("ticket");
  63.  
  64. Button quitter = new Button("quitter l'application");
  65.  
  66. Hashtable associationProduitsPrix;
  67.  
  68. public Cartee()
  69.  
  70. {
  71.  
  72. super("creperie");
  73.  
  74. initFrame();
  75.  
  76. addBouton();
  77.  
  78. initPrix();
  79.  
  80.  
  81.  
  82.  
  83.  
  84. initBoutonsListeners();
  85.  
  86. }
  87.  
  88. private void initFrame()
  89.  
  90. {
  91.  
  92. setSize(250, 250);
  93.  
  94. setLayout(new FlowLayout()); //sinon n'affiche qu'un seul element ds la fenetre
  95.  
  96. setVisible(true);
  97.  
  98. setBackground(Color.yellow);
  99.  
  100. setForeground(Color.black);
  101.  
  102. }
  103.  
  104. private void addBouton()
  105.  
  106. {
  107.  
  108. add(entree);
  109.  
  110. add(salade);
  111. add(maCombo);
  112. maCombo.add("1");
  113. maCombo.add("2");
  114.  
  115. add(melon);
  116.  
  117. add(cs);
  118.  
  119. add(jf);
  120.  
  121. add(complete);
  122.  
  123. add(csu);
  124.  
  125. add(su);
  126.  
  127. add(choc);
  128.  
  129. add(boi);
  130.  
  131. add(coca);
  132.  
  133. add(cidre);
  134.  
  135.  
  136. add(bout);
  137.  
  138. add(quitter);
  139.  
  140. }
  141.  
  142. private void initPrix()
  143.  
  144. {
  145.  
  146. associationProduitsPrix = new Hashtable();
  147.  
  148. associationProduitsPrix.put(salade, new BigInteger("10"));
  149.  
  150. associationProduitsPrix.put(melon, new BigInteger("5"));
  151.  
  152. }
  153.  
  154. private void initBoutonsListeners()
  155.  
  156. {
  157.  
  158. quitter.addActionListener(new ListenerQuitter());
  159.  
  160. bout.addActionListener(new ActionListener()
  161.  
  162. {
  163.  
  164. public void actionPerformed(ActionEvent e)
  165.  
  166. {
  167.  
  168. BigInteger total = new BigInteger("0");
  169.  
  170. Enumeration enu = associationProduitsPrix.keys();
  171.  
  172. while (enu.hasMoreElements())
  173.  
  174. {
  175.  
  176. Checkbox check = (Checkbox)enu.nextElement();
  177.  
  178. if (check.getState())
  179.  
  180. {
  181.  
  182. BigInteger prix = (BigInteger)associationProduitsPrix.get(check);
  183.  
  184. total = total.add(prix);
  185. }
  186. }
  187. JOptionPane.showMessageDialog(null, "Total : " + total+ " \u20ac");
  188.  
  189.  
  190.  
  191.  
  192.  
  193. }
  194.  
  195. });
  196.  
  197. }
  198.  
  199.  
  200.  
  201.  
  202.  
  203.  
  204. class ListenerQuitter implements ActionListener
  205.  
  206. {
  207.  
  208. public void actionPerformed(ActionEvent e)
  209.  
  210. {
  211.  
  212. System.exit(0);
  213.  
  214. }
  215.  
  216. }
  217.  
  218. public static void main(String[] args)
  219.  
  220. {
  221.  
  222. Frame f = new Cartee();
  223.  
  224. }
  225.  
  226. }


voilà merci pour votre aide car suis vraiment degoutée... :cry: 

Autres pages sur : fichier texte java

Lassé par la pub ? Créez un compte

Je ne comprends pas vraiment ce que tu veux faire...
Mais si j'ai suivi, ce que tu appelles éléments, c'est un bouton, pour lequel tu dois faire add(elt) dans addBouton, la même dans initPrix(), ...


Donc si tu veux un fichier en entrée, il te suffit de paramétrer tes fonctions ; et ensuite de lire ton fichier texte (dans ton constructeur Cartee(), et de boucler sur chaque ligne, en appelant pour chaque ligne tes 2 focntions...


A moins que j'ai mal compris... [:spamafote]
Lassé par la pub ? Créez un compte