Gestion des Livres d'une Bibliotheque

Résolu
jsand094 - 29 sept. 2012 à 20:38
 jsand094 - 5 oct. 2012 à 05:38
Bonjour,
Voici un programme qui compile très bien. j'ai voulu stocker les entrees dans un fichier. Le problème c'est qu'à la fin du programme, le fichier est vide (bien qu'il existe "le Bouquineur.txt").

package lebouquineur;

/**
 *
 * @author sandé
 */
   import java.io.*;
   import java.util.Scanner;
public class Lebouquineur {

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) throws IOException {
        Scanner sc = new Scanner(System.in);
        File f = new File("Le Bouquineur.txt");
boolean ajout = true;
        PrintWriter pw = new PrintWriter(new BufferedWriter(new FileWriter(f, ajout)));	
        
        String reponse;
        int type, couv, form;
        float prix;
        boolean ValideP;
        int quantite;
        String types[]={"BD","Cuisine","Roman","Autre"};
        String couvert[]={"Souple","Rigide"};
        String format[]={"Poche","Regulier","Geant"};
        String reponse1;
        float total=0;
        float TPS, TVQ;
        float Total=0;
        String reponse2;
        int Cptfact=0;
        float Cpttotal=0;
        float CptTotal=0;
        int CptBD=0;
        int CptCuisine=0;
        int CptRoman=0;
        int CptAutre=0;

try{
        System.out.println("*******************************************************************\n"+
                           "*                Bienvenue chez « LE BOUQUINEUR »                 *\n"+
                           "*                          ~~***~~                                *\n"+
                           "*          traitement automatique des emprunts de livres          *\n"+
                           "*******************************************************************\n");
   
        do{                        // do-while : validation de l'entree : a ou A ou f ou F
            System.out.println("Voulez vous traiter une facture ? (f ou F) ou arreter le programme ? (a ou A)");
            reponse = sc.next();
                if(reponse.compareTo("f")!=0 && reponse.compareTo("F")!=0 && reponse.compareTo("a")!=0 && reponse.compareTo("A")!=0){
                          System.out.println("Reponse invalide ! recommencez");
                }
          }while(reponse.compareTo("f")!=0 && reponse.compareTo("F")!=0 && reponse.compareTo("a")!=0 && reponse.compareTo("A")!=0);
        // ----------------la reponse entree est a ou A ou f ou F
        //---- si c'est f ou F, je peux donc faire le traitement
        if(reponse.compareTo("f")==0 || reponse.compareTo("F")==0){
              do{     //do-while dont le while se trouve a la toute fin
                    do{                    // do-while : validation du type de livre
                          System.out.println("TYPE DE LIVRE : 1=BD, 2=Cuisine, 3=Roman et 4=Autre ");
                          type = sc.nextInt();
                          if(type!=1 && type!=2 && type!=3 && type!=4){
                              System.out.println("Erreur ! le type de livre est invalide");
                          }
                      }while(type!=1 && type!=2 && type!=3 && type!=4);
                                    //fin de la validation
                                 

                    do{                   // do-while : validation de la couverture
                          System.out.println("COUVERTURE : 1=souple, 2=rigide :");
                          couv = sc.nextInt();
                          if(couv!=1 && couv!=2){
                             System.out.println("Erreur ! la couverture est invalide");
                          }
                      }while(couv!=1 && couv!=2);
                                         // fin de la validation
                    do{                  // do-while : validation du format
                          System.out.println("FORMAT DU LIVRE : 1=Poche, 2=Régulier et 3=Géant");
                          form = sc.nextInt();
                          if(form!=1 && form!=2 && form!=3){
                                System.out.println("Erreur ! le format est invalide");
                          }
                      }while(form!=1 && form!=2 && form!=3);
                   
                          System.out.println("Veuillez entrez le prix :");
                          prix = sc.nextFloat();
     
do{          // do-while validation de la quantite d'un article
                          System.out.println("Veuillez entrer la quantite que vous prennez :");
                          quantite = sc.nextInt();
                          if(quantite<=0){
                              System.out.println("Erreur ! vous devez absolument en prendre au moins 1");
                          }
                      }while(quantite<=0);

                                 if(type==1){                              // tous les compteur de type de livre
                                   CptBD=CptBD+quantite;
                                 }else if(type==2){
                                         CptCuisine=CptCuisine+quantite;
                                       }else if(type==3){
                                               CptRoman=CptRoman+quantite;
                                             }else if(type==4){
                                                     CptAutre=CptAutre+quantite;
                                                   }
                    
                          // AFFICHAGE : type de produit, couverture, format
                                       total = quantite*prix;
                                         TPS = total*5/100;
                                         TVQ = total*85/1000;
                         do{         // do-while  pour valider les caracteres o ou O ou n ou N
                                   System.out.print("vous avez commande : "+quantite+" article(s)\n"+   // ERREUR A CETTE LIGNE !! C'EST QUOI L'ERREUR ????
                                                    "           de type : "+types[type-1]+"\n"+
                                                    "     de couverture : "+couvert[couv-1]+"\n"+
                                                    "      et de format : "+format[form-1]+"\n"+
                                                    "     Sous total de : "+quantite+" x "+prix+" $ = "+total+" $\n\n"+
                                                    "        TPS  5%    : "+TPS+"\n"+
                                                    "        TVQ  8,5%  : "+TVQ+"\n"+
                                                    "les informations de la sous-facture sont -ils exactes ? (o ou O)=oui , (n ou N)=non");
                                        reponse1=sc.next();
                                 if(reponse1.compareTo("o")!=0 && reponse1.compareTo("O")!=0 && reponse1.compareTo("n")!=0 && reponse1.compareTo("N")!=0){
                                        System.out.println("reponse invalide !");
                                 }
                                       

                            }while(reponse1.compareTo("o")!=0 && reponse1.compareTo("O")!=0 && reponse1.compareTo("n")!=0 && reponse1.compareTo("N")!=0);
                                     // si c'est bon, on l'ajoute a la facture
                            if(reponse1.compareTo("o")==0 || reponse1.compareTo("O")==0){
                                Total = total + TPS + TVQ;
                                Cptfact = Cptfact+1;                             //  le compteur des factures
                                System.out.println("            facture N° : "+Cptfact+"\n"+
                                                    "            Sous total : "+total+" $\n\n"+
                                                    "            TPS  5%    : "+TPS+"\n"+
                                                    "            TVQ  8,5%  : "+TVQ+"\n"+
                                                    "         Total facture : "+Total+"\n");
                                
                                        pw.println("            facture N° : "+Cptfact+"\n");
                                        pw.println("            Sous total : "+total+" $\n\n");
                                        pw.println("            TPS  5%    : "+TPS+"\n");
                                        pw.println("            TVQ  8,5%  : "+TVQ+"\n");
                                        pw.println("         Total facture : "+Total+"\n");
                                
                                Cpttotal = Cpttotal + total;                     // le compteur des totaux sans taxes
                                CptTotal = CptTotal + Total;                     // le compteur des totaux TTC

                                     if(quantite>=5){
                                         Total=Total*80/100;
                                         System.out.println("Rabais de 80% pour un achat de plus de 5 article \n"+
                                                            "nouveau Total : "+Total);
                                                 pw.println("Rabais de 80% pour un achat de plus de 5 article \n");
                                                 pw.println("nouveau Total : "+Total);
                                                 
                                     }  pw.println("                       .....                       ");
                            }
                            System.out.println("desirez-vous entrer un autre type de livre ? (o ou O)=oui , (n ou N)=non");
                            reponse2 = sc.next();
                                do{       // pour valider la reponse dans un 1er temps
                                    if(reponse2.compareTo("o")!=0 && reponse2.compareTo("O")!=0 && reponse2.compareTo("n")!=0 && reponse2.compareTo("N")!=0){
                                            System.out.println("reponse invalide !");
                                     }
                                  }while(reponse2.compareTo("o")!=0 && reponse2.compareTo("O")!=0 && reponse2.compareTo("n")!=0 && reponse2.compareTo("N")!=0);
                          // voici le while du 2e do-while quand le client veut une facture 'f' ou 'F'
                }while(reponse2.compareTo("o")==0 || reponse2.compareTo("O")==0);
                                 if(reponse2.compareTo("n")==0 || reponse2.compareTo("N")==0){
                                     System.out.println("Vous avez enregistre : "+Cptfact+" factures\n"+
                                                        " dont : "+CptBD+" BD\n"+
                                                        "      : "+CptCuisine+" Cuisine(s)\n"+
                                                        "      : "+CptRoman+" Roman(s)\n"+
                                                        "      : "+CptAutre+" Autre"+
                                                        "  les totaux avant taxes s'elevent a : "+Cpttotal+" $\n"+
                                                        "  les Totaux TTC s'elevent a : "+CptTotal+" $\n"+
                                                        "           ~~  Bonne journee  ~~         ");
                                     pw.println("Vous avez enregistre : "+Cptfact+" factures\n");
                                     pw.println("                dont : "+CptBD+" BD");
                                     pw.println("                     : "+CptCuisine+" Cuisine(s)");
                                     pw.println("                     : "+CptRoman+" Roman(s)");
                                     pw.println("                     : "+CptAutre+" Autre");
                                     pw.println("  les totaux avant taxes s'elevent a : "+Cpttotal+" $");
                                     pw.println("          les Totaux TTC s'elevent a : "+CptTotal+" $");
                                     pw.println("                ~~  Bonne journee  ~~                ");               
                                 }
            }else
                 {   System.out.println("FIN NORMAL DU PROGRAMME");   
            }
        }catch(Exception E){
            System.out.println("probleme dans la facturation... reessayez a nouveau");
        } 
    }
}

Merci d'avance pour vos corrections Jsande

2 réponses

cs_Julien39 Messages postés 6414 Date d'inscription mardi 8 mars 2005 Statut Modérateur Dernière intervention 29 juillet 2020 371
1 oct. 2012 à 11:01
salut,

Je pense qu'on pw.close(); à la fin de traitement résoudra le problème...
2
Rejoignez-nous