/* * Fichier: Vente.java * Crée le: 03 décembre 2006. * Modifié: 7 juillet 2007. * Auteurs: Sébastien ESTIENNE. * SiteWeb: http://www.prog-info.org/ * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation; either version 2 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program; if not, write to the Free Software * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */ package chapitre4; import java.text.NumberFormat; import java.util.Locale; import javax.swing.JOptionPane; import javax.swing.JScrollPane; import javax.swing.JTextArea; /** *
Classe de simulation de vente d'articles.
* @author Sébastien ESTIENNE. */ public class Vente { /** *Lance la simulation de vente d'articles.
* @param args Les paramètres de la ligne de commande. */ public static void main(String[] args) { // Le numéro du produit entré par l'utilisateur. String sNoProduit; int noProduit = 0; // La quantité du produit entré par l'utilisateur. String sQuantite; int quantite = 0; // 1 produit * quantite. double sousTotal; // Le total des produits. double total; // Indique si la saisie de l'utilisateur est valide. boolean saisieValide; // Créer un JTextArea pour afficher les résultats. JTextArea zoneSortie = new JTextArea(10, 28); zoneSortie.setEditable(false); // Relier le JTextArea à un JScrollPane pour que l'utilisateur puisse faire défiler // les résultats. JScrollPane defilant = new JScrollPane(zoneSortie); // Le contenu du JTextArea. StringBuilder sortie = new StringBuilder(); // Créer un decimalFormat pour mettre en forme les nombres à virgules flottantes avec // deux chiffres à droite du point décimal et un signe $. NumberFormat dollard = NumberFormat.getCurrencyInstance(Locale.FRANCE); // Demander le numéro du produit à l'utilisateur. do { sNoProduit = JOptionPane.showInputDialog("Entrez le numéro du produit[1-5] " + "(0 pour terminer) :\n\n"); // Convertir sNoProduit de String en int. try { noProduit = Integer.parseInt(sNoProduit); saisieValide = true; } // La valeur saisie comme numéro de produit est invalide. catch(NumberFormatException e) { saisieValide = false; } } while(!saisieValide || noProduit < 0 || noProduit > 5); // Continuer d'ajouter des produits tant que la valeur sentinelle n'est pas rentrée. total = 0.0; sortie.append("Produit\t Quantite\t Total\n"); while(noProduit != 0) { // Demander le numéro du produit à l'utilisateur. do { sQuantite = JOptionPane.showInputDialog("Entrez la quantité du produit :\n\n"); // Convertir sNoProduit de String en int. try { quantite = Integer.parseInt(sQuantite); saisieValide = true; } // La valeur saisie comme quantité est invalide. catch(NumberFormatException e) { saisieValide = false; } } while(!saisieValide || quantite < 0); // Calculer le sous total pour 1 produit donné. switch(noProduit) { case 1: sousTotal = 2.67 * quantite; break; case 2: sousTotal = 9.65 * quantite; break; case 3: sousTotal = 3.78 * quantite; break; case 4: sousTotal = 5.26 * quantite; break; case 5: sousTotal = 7.21 * quantite; break; default: sousTotal = 0.0; break; } // Calcul du montant total. total += sousTotal; // Pour ne pas afficher les produits non valides. if(noProduit > 0 && noProduit < 6) { sortie.append(noProduit).append("\t ").append(quantite).append("\t ").append( dollard.format(sousTotal)).append("\n"); } // Demander le numéro du produit à l'utilisateur. do { sNoProduit = JOptionPane.showInputDialog("Entrez le numéro du produit[1-5] " + "(0 pour terminer) :\n\n"); // Convertir sNoProduit de String en int. try { noProduit = Integer.parseInt(sNoProduit); saisieValide = true; } // La valeur saisie comme numéro de produit est invalide. catch(NumberFormatException e) { saisieValide = false; } } while(!saisieValide || noProduit < 0 || noProduit > 5); } // Ajouter le total à la chaine de sortie. sortie.append("\nTOTAL : ").append(dollard.format(total)); // Placer les résultats dans un JTextArea. zoneSortie.setText(sortie.toString()); // Afficher le contenu de la sortie dans le JTextArea avec la barre de défilement. JOptionPane.showMessageDialog(null, defilant, "Vente", JOptionPane.INFORMATION_MESSAGE); // Terminer l'application. System.exit(0); } }