Advertisement
Guest User

Untitled

a guest
Dec 9th, 2019
113
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 23.16 KB | None | 0 0
  1. // Mattéo Colavita
  2. //Remise 1 / Remise 2 est commencée
  3.  
  4. //Bibliothèques
  5. #include<locale>
  6. #include<iostream>
  7. #include<string>
  8. #include<Windows.h>
  9. #include<fstream>
  10. #include<iomanip>
  11.  
  12. using namespace std;
  13.  
  14. //Structure pour l'identification des produits
  15. struct produit
  16. {
  17.     int id;
  18.     string nomp;
  19.     int qte;
  20.     float prix;
  21.     string cat;
  22.     string ref;
  23.     string nomf;
  24. };
  25.  
  26. // Prototypes des fonctions
  27. void lireFichierProduits(produit[]);  // Lit le fichier "produits.txt" et remplit le tableau de produits
  28. void menuPrincipal(produit[]); //Menu avec lequel l'utlisateur peu réaliser plusieurs actions
  29. void afficherInfosProduits(produit[]);  // Contient le sous-menu pour le choix des options d'affichage des produits. C'est à partir de cette fonction que les fonction d'affichage sont appelées
  30. void afficherTousLesProduits(produit[]);  // Affiche la liste complète de tous les produits
  31. void listeProdsFab(produit[]);   // Affiche la liste des produits d'un fabricant choisi par l'utilisateur
  32. void listeProdsCat(produit[]);   // Affiche la liste des produits d'une catégorie choisie par l'utilisateur
  33. void listeProdsRup(produit[]);   // Affiche la liste des produits qui seront bientôt en rupture de stock (quantité < 50 unités)
  34. void triCrIdProds(produit[]);  // Affiche la liste des produits triée dans l'ordre croissant des identifiants
  35. void ajouterProduits(produit[]);  // Fonction qui permet d'ajouter un nouveau produit dans le tableau de produits
  36. int chercherIdMax(produit[]);  // Permet de rechercher l'identifiant "id" le plus grand, à appeler dans la fonction "ajouterProduits"
  37. string remplacerEspaces(string); // Permet de remplacer tous les espaces d'une chaîne de caractères en tirets en bas "_", à appeler dans la fonction "ajouterProduits"
  38. void ajouterProdDansFichier(produit[], int); // Permet d'ajouter le nouveau produit dans le fichier
  39. void modifierProduits(produit[]);  // Modifier un ou plusieurs champs d'un ou de plusieurs produits
  40. int rechercheIndiceProd(produit[], int);  // Permet de chercher l'indice (dans le tableau) du produit à modifier, à appeler dans la fonction "modifierProduits"
  41. void reecrireFichier(produit[], int); // Permet de réécrire le fichier "produits.txt", à appeler dans la fonction "modifierProduits"
  42. float valoriserStock(produit[]); // Permet de calculer l'inventaire du stock (somme totale des prix * qte)
  43.  
  44. //Déclaration de la constante globale nbproduit qui repsésente le plus de produits que l'utilisateur peut ajouter dans le programme
  45. const int nbproduitmax = 500;
  46.  
  47. //Déclaration de la variable globale cpt qui compte le nombre de produits dans le programme
  48. int cpt = 0;
  49.  
  50.  
  51. int main()
  52. {
  53.     setlocale(LC_ALL, "");
  54.  
  55.     // Ajouter un titre pour la console
  56.     SetConsoleTitle("Application de gestion du stock");
  57.     // Changer la taille de la console
  58.     system("MODE CON COLS=150 LINES=200");
  59.  
  60.     //Déclaration du tableau dans lequel se retrouve les informations de tous les produits
  61.     produit tabprod[nbproduitmax];
  62.    
  63.     //Appel de la fonction "lireFichierProduits"
  64.     lireFichierProduits(tabprod);
  65.  
  66.     // Appel de la fonction "menuPrincipal"
  67.     menuPrincipal(tabprod);
  68.  
  69.     system("pause");
  70.  
  71.     return 0;
  72. }
  73.  
  74. //Déclaration de la fonction qui lit le fichier produits.txt et qui remplit le tableau tabprod
  75. void lireFichierProduits(produit tabprod[])
  76. {
  77.     //Pour pouvoir lire le fichier:
  78.     fstream inventaire("produits.txt", ios::in);
  79.  
  80.     //Si le programme ne peut pas lire le fichier:
  81.     if (!inventaire)
  82.     {
  83.         cout << "Impossible de lire le fichier" << endl;
  84.         system("pause");
  85.         exit(1);
  86.     }
  87.  
  88.     //Déclaration des variables du tablreau tabprod:
  89.     int idp;
  90.     string nompp;
  91.     int qtep;
  92.     float prixp;
  93.     string catp;
  94.     string refp;
  95.     string nomfp;
  96.  
  97.     //Incoporation des variable dans le tableau tabprod
  98.     while (inventaire >> idp >> nompp >> qtep >> prixp >> catp >> refp >> nomfp)
  99.     {
  100.         tabprod[cpt].id = idp;
  101.         tabprod[cpt].nomp = nompp;
  102.         tabprod[cpt].qte = qtep;
  103.         tabprod[cpt].prix = prixp;
  104.         tabprod[cpt].cat = catp;
  105.         tabprod[cpt].ref = refp;
  106.         tabprod[cpt].nomf = nomfp;
  107.  
  108.         cpt++;
  109.     }
  110.  
  111.     //Fermeture du fichier
  112.     inventaire.close();
  113.  
  114.     return;
  115. }
  116.  
  117. //Déclaration de la fonction qui affiche le menu principal et qui permet à l'utilisateur d'accomplir certaines tâches
  118. void menuPrincipal(produit tabprod[])
  119. {
  120.     int choix;
  121.  
  122.     //do while pour vérifier la saisie de l'utilisateur
  123.     do
  124.     {
  125.  
  126.  
  127.  
  128.         cout << "                                                                              " << endl;
  129.         cout << "                                                                              " << endl;
  130.         cout << "                                                                              " << endl;
  131.         cout << "                                                                              " << endl;
  132.         cout << ".::.:: ::  ::.::.::     .:::::.            ::.     .::  ::::::::  :::::::::  " << endl;
  133.         cout << ".::    ::  ::   .::    .::   ::.           ::::. .::::  ::    ::  ::         " << endl;
  134.         cout << ".::    ::  ::   .::   .::     ::.          :: ::::  ::  ::    ::  ::         " << endl;
  135.         cout << ".::::::::  ::.::.::  .::       ::.  :::::  ::  ::   ::  ::::::::  ::  :::::  " << endl;
  136.         cout << ".::        ::   .::   .::     ::.          ::       ::  ::    ::  ::     ::  " << endl;
  137.         cout << ".::        ::    .::   .::   ::.           ::       ::  ::    ::  ::     ::  " << endl;
  138.         cout << ".::        ::     .::   .:::::.            ::       ::  ::    ::  :::::::::  " << endl;
  139.         cout << "                                                                              " << endl;
  140.         cout << "                       Système de gestion de stock                            " << endl;
  141.         cout << "                                                                              " << endl;
  142.         cout << "                                                                              " << endl;
  143.         cout << " (1) Afficher les informations des produits" << endl;
  144.         cout << " (2) Ajouter de nouveau(x) produits(s)" << endl;
  145.         cout << " (3) Modifier des informations de produit(s) " << endl;
  146.         cout << " (4) Valorisation du stock " << endl;
  147.         cout << " (5) Sortir du programme" << endl << endl;
  148.  
  149.         cout << "Votre choix ==> "; cin >> choix;
  150.  
  151.         //Switch pour les différentes options de l'utilisateur
  152.         switch (choix)
  153.         {
  154.         case 1:
  155.  
  156.             system("cls");
  157.  
  158.             //Affichage du sous-menu
  159.             afficherInfosProduits(tabprod);
  160.  
  161.             break;
  162.  
  163.         case 2:
  164.  
  165.             system("cls");
  166.  
  167.             //Ajout du produit
  168.             ajouterProduits(tabprod);
  169.  
  170.             break;
  171.  
  172.         case 3:
  173.  
  174.             system("cls");
  175.  
  176.             modifierProduits(tabprod);
  177.  
  178.             break;
  179.  
  180.  
  181.  
  182.         case 4:
  183.  
  184.             system("cls");
  185.  
  186.             cout << "\t\t\t\t\t\t VALORISATION DU STOCK" << endl << endl;
  187.  
  188.             cout << "Le prix total de tout l'inventaire est de: " << valoriserStock(tabprod) << "$" << endl << endl;
  189.  
  190.             system("pause");
  191.  
  192.             system("cls");
  193.  
  194.             break;
  195.  
  196.         case 5:
  197.  
  198.             exit(0);
  199.  
  200.             break;
  201.  
  202.         default:
  203.  
  204.             system("cls");
  205.  
  206.             cout << endl << "ERREUR, saisissez une valeur valide!" << endl << endl;
  207.  
  208.         }
  209.  
  210.     } while (choix < 1 || choix > 5 || choix == 4);
  211.  
  212.     return;
  213. }
  214.  
  215. //Déclaration de la fonction qui afficher le sous-menu après avoir choisi 1 dans le menu
  216. void afficherInfosProduits(produit tabprod[])
  217. {
  218.     //Déclaration de la variable choix1
  219.     int choix1;
  220.  
  221.     //Déclaration du tableau de type produit dans lequel la copie triée de tabprod se trouvera
  222.     produit tabprodtri[nbproduitmax];
  223.  
  224.     //do while pour permettre à l'utilisateur d'utiliser le programme sans interruptions
  225.     do
  226.     {
  227.         system("cls");
  228.  
  229.         //Affichage des différents choix de l'utilisateur
  230.             cout << "Voulez vous:" << endl << endl;
  231.  
  232.             cout << "(1) Afficher tous les produits" << endl;
  233.             cout << "(2) Liste des produits d'un fabricant" << endl;
  234.             cout << "(3) Liste des produits d'une même catégorie" << endl;
  235.             cout << "(4) Liste des produits en rupture de stock" << endl;
  236.             cout << "(5) Trier dans l'ordre croissant les identifiants" << endl;
  237.             cout << "(6) Revenir au menu principal" << endl;
  238.             cout << "(7) Sortir du programme" << endl << endl;
  239.  
  240.             cout << "Votre choix ==>"; cin >> choix1;
  241.  
  242.             switch (choix1)
  243.             {
  244.  
  245.             case 1:
  246.  
  247.                 //Appel de la fonction pour afficher tous les produits
  248.                 afficherTousLesProduits(tabprod);
  249.  
  250.                 system("pause");
  251.  
  252.                 break;
  253.  
  254.             case 2:
  255.  
  256.                 //Appel de la fonction pour chercher selon le fabricant
  257.                 listeProdsFab(tabprod);
  258.  
  259.                 system("pause");
  260.  
  261.                 break;
  262.  
  263.             case 3:
  264.  
  265.                 //Appel de la fonction pour chercher selon la catégorie
  266.                 listeProdsCat(tabprod);
  267.  
  268.                 system("pause");
  269.  
  270.                 break;
  271.  
  272.             case 4:
  273.  
  274.                 //Appel de la fonction listeprodrup pour afficher les produits qui sont presque en rupture de stock <50
  275.                 listeProdsRup(tabprod);
  276.  
  277.                 system("pause");
  278.  
  279.                 break;
  280.  
  281.             case 5:
  282.  
  283.                 //Assignation des valeurs de chacune des variables pour le tableautri avant le triage
  284.                 for (int i = 0; i < cpt; i++)
  285.                 {
  286.                     tabprodtri[i].id = tabprod[i].id;
  287.                     tabprodtri[i].nomp = tabprod[i].nomp;
  288.                     tabprodtri[i].qte = tabprod[i].qte;
  289.                     tabprodtri[i].prix = tabprod[i].prix;
  290.                     tabprodtri[i].cat = tabprod[i].cat;
  291.                     tabprodtri[i].ref = tabprod[i].ref;
  292.                     tabprodtri[i].nomf = tabprod[i].nomf;
  293.                 }
  294.  
  295.                 triCrIdProds(tabprodtri);
  296.  
  297.                 system("pause");
  298.  
  299.                 break;
  300.  
  301.             case 6:
  302.  
  303.                 system("cls");
  304.  
  305.                 //Retour au menu principal
  306.                 menuPrincipal(tabprod);
  307.  
  308.                 break;
  309.  
  310.             case 7:
  311.                
  312.  
  313.                 //Quitte le programme
  314.                 exit(0);
  315.  
  316.                 break;
  317.  
  318.             default:
  319.  
  320.                 system("cls");
  321.  
  322.                 //Message d'erreur si la saisie est incorrecte
  323.                 cout << endl << "ERREUR! Veuillez choisir une des options ci-dessus." << endl << endl;
  324.  
  325.             }
  326.  
  327.     } while (choix1 != 6 && choix1 != 7 || choix1 < 1 || choix1 > 6);
  328.  
  329.     return;
  330. }
  331.  
  332. //Déclaration de la fonction qui affiche tous les produits en groupe de 15 de l'inventaire
  333. void afficherTousLesProduits(produit tabprod[])
  334. {
  335.     cout << endl << "\t\t\t\t\t\t\t LISTE DES PRODUITS" << endl << endl;
  336.  
  337.     cout << setiosflags(ios::left) << setw(10) << "Id Prod." << setw(50) << "Nom Produit" << setw(10) << "Qte" << setw(10) << "Prix" << setw(15) << "Catégorie" << setw(15) << "Réf. Fab." << setw(15) << "Fabricant" << endl << endl;
  338.  
  339.     //Affichage des produits
  340.     for (int i = 0; i < cpt; i++)
  341.     {
  342.         cout << setw(10);
  343.         cout << setiosflags(ios::left) << setw(10) << tabprod[i].id << setw(50) << tabprod[i].nomp << setw(10) << tabprod[i].qte << setw(10) << tabprod[i].prix << setw(15) << tabprod[i].cat << setw(15) << tabprod[i].ref << setw(15) << tabprod[i].nomf << endl;
  344.  
  345.         //À chaque 15 affichages system pause
  346.         if (i == 15 || i == 30 || i == 45 || i == 60 || i == 75 || i == 90 || i == 105 || i == cpt )
  347.         {
  348.             system("pause");
  349.  
  350.             cout << endl << setiosflags(ios::left) << setw(10) << "Id Prod." << setw(50) << "Nom Produit" << setw(10) << "Qte" << setw(10) << "Prix" << setw(15) << "Catégorie" << setw(15) << "Réf. Fab." << setw(15) << "Fabricant" << endl << endl;
  351.  
  352.         }
  353.  
  354.     }
  355.  
  356.     return;
  357. }
  358.  
  359. //Déclaration de la fonction qui permet à l'utilisateur de chercher un produit selon le fabricant
  360. void listeProdsFab(produit tabprod[])
  361. {
  362.     string fab;
  363.     int nb = 0;
  364.  
  365.     cout << endl << "\t\t\t\t\t\t\t LISTE DES PRODUITS DU FABRICANT" << endl << endl;
  366.  
  367.     //Do while pour vérifier la saisie de l'utilisateur
  368.     do
  369.     {
  370.         cout << "De quel fabricant voulez-vous connaître les produits?"; cin >> fab; cout << endl << endl;
  371.  
  372.         //Parcours du tableau pour trouver si la saisie correspond à un fabricant
  373.         for (int i = 0; i < cpt; i++)
  374.         {
  375.             if (tabprod[i].nomf == fab)
  376.             {
  377.                 nb++;
  378.             }
  379.         }
  380.  
  381.         //Si la saisie ne correspond à aucun fabricant:
  382.         if (nb == 0)
  383.         {
  384.             cout << "Erreur, veuillez saisir un fabricant valide!" << endl << endl;
  385.         }
  386.  
  387.  
  388.     } while (nb == 0);
  389.  
  390.     //Affichage des titres
  391.     cout << setiosflags(ios::left) << setw(10) << "Id Prod." << setw(50) << "Nom Produit" << setw(10) << "Qte" << setw(10) << "Prix" << setw(15) << "Catégorie" << setw(15) << "Réf. Fab." << setw(15) << "Fabricant" << endl << endl;
  392.  
  393.     //Affichage des informations du produit d'un fabricant
  394.     for (int i = 0; i < cpt; i++)
  395.     {
  396.         //Affichage des produits du fabricant
  397.         if (tabprod[i].nomf == fab)
  398.         {
  399.             cout << setiosflags(ios::left) << setw(10) << tabprod[i].id << setw(50) << tabprod[i].nomp << setw(10) << tabprod[i].qte << setw(10) << tabprod[i].prix << setw(15) << tabprod[i].cat << setw(15) << tabprod[i].ref << setw(15) << tabprod[i].nomf << endl;
  400.         }
  401.     }
  402.  
  403.     return;
  404. }
  405.  
  406. //Déclaration de la fonction qui permet à l'utilisateur de chercher un produit selon sa catégorie
  407. void listeProdsCat(produit tabprod[])
  408. {
  409.     string categ;
  410.     int nb = 0;
  411.  
  412.     cout << endl << "\t\t\t\t\t\t\t LISTE DES PRODUITS D'UNE MÊME CATÉGORIE" << endl << endl;
  413.    
  414.     do
  415.     {
  416.         //Demande à l'utilisateur de quel categorie il veut connaitre les produits
  417.         cout << "De quel catégorie voulez-vous connaître les produits?"; cin >> categ; cout << endl << endl;
  418.  
  419.         //Parcours le tableau pour verifier si la saisie correspond à une categorie
  420.         for (int i = 0; i < cpt; i++)
  421.         {
  422.             if (tabprod[i].cat == categ)
  423.             {
  424.                 nb++;
  425.             }
  426.         }
  427.  
  428.         if (nb == 0)
  429.         {
  430.             cout << "Erreur, veuillez saisir une catégorie valide!" << endl << endl;
  431.         }
  432.  
  433.     } while (nb == 0);
  434.  
  435.     //Affichage des titres
  436.     cout << setiosflags(ios::left) << setw(10) << "Id Prod." << setw(50) << "Nom Produit" << setw(10) << "Qte" << setw(10) << "Prix" << setw(15) << "Catégorie" << setw(15) << "Réf. Fab." << setw(15) << "Fabricant" << endl << endl;
  437.  
  438.     //affichage des informations des produits correspondant à la catégorie
  439.     for (int i = 0; i < cpt; i++)
  440.     {
  441.         if (tabprod[i].cat == categ)
  442.         {
  443.             cout << setiosflags(ios::left) << setw(10) << tabprod[i].id << setw(50) << tabprod[i].nomp << setw(10) << tabprod[i].qte << setw(10) << tabprod[i].prix << setw(15) << tabprod[i].cat << setw(15) << tabprod[i].ref << setw(15) << tabprod[i].nomf << endl;
  444.         }
  445.     }
  446.  
  447.     return;
  448. }
  449.  
  450. //Déclaration de la fonction qui affiche les produits qui sont presqu'en rupture de stock
  451. void listeProdsRup(produit tabprod[])
  452. {
  453.     string rup;
  454.  
  455.     cout << endl << "\t\t\t\t\t\t LISTE DES PRODUITS PRESQU'EN RUPTURE DE STOCK" << endl << endl;
  456.  
  457.     //Affichage des titres
  458.     cout << setiosflags(ios::left) << setw(10) << "Id Prod." << setw(50) << "Nom Produit" << setw(10) << "Qte" << setw(10) << "Prix" << setw(15) << "Catégorie" << setw(15) << "Réf. Fab." << setw(15) << "Fabricant" << endl << endl;
  459.  
  460.     //Affichage des informations des produits en rupture de stock
  461.     for (int i = 0; i < cpt; i++)
  462.     {
  463.         if (tabprod[i].qte < 50)
  464.         {
  465.             cout << setiosflags(ios::left) << setw(10) << tabprod[i].id << setw(50) << tabprod[i].nomp << setw(10) << tabprod[i].qte << setw(10) << tabprod[i].prix << setw(15) << tabprod[i].cat << setw(15) << tabprod[i].ref << setw(15) << tabprod[i].nomf << endl;
  466.         }
  467.     }
  468.  
  469.     return;
  470. }
  471.  
  472. //Déclaration de la fonction qui tri les produits en ordre croissant selon leur identifiant
  473. void triCrIdProds(produit tabprodtri[])
  474. {
  475.     int temp;
  476.  
  477.     cout << endl << "\t\t\t\t\t\t LISTE DES PRODUITS EN ORDRE" << endl << endl;
  478.  
  479.     //Triage des produits
  480.     for (int i = 0; i < cpt; i++)
  481.     {
  482.         for (int j = 1; j < cpt; j++)
  483.         {
  484.             if (tabprodtri[i].id < tabprodtri[j].id)
  485.             {
  486.                 temp = tabprodtri[i].id;
  487.                 tabprodtri[i].id = tabprodtri[j].id;
  488.                 tabprodtri[j].id = temp;
  489.                 j--;
  490.             }
  491.  
  492.         }
  493.  
  494.     }
  495.  
  496.     temp = tabprodtri[0].id;
  497.  
  498.     for (int i = 0; i < cpt; i++)
  499.     {
  500.         tabprodtri[i].id = tabprodtri[i + 1].id;
  501.         tabprodtri[cpt - 1].id = temp;
  502.     }
  503.  
  504.     //Affichage de tous les produits après tri
  505.     afficherTousLesProduits(tabprodtri);
  506.  
  507.     return;
  508. }
  509.  
  510.  
  511. void ajouterProduits(produit tabprod[])// Fonction qui permet d'ajouter un nouveau produit dans le tableau de produits
  512. {
  513.     char continuer;
  514.  
  515.     do
  516.     {
  517.         int idp = chercherIdMax(tabprod);
  518.         string nompp;
  519.         int qtep;
  520.         float prixp;
  521.         string catp;
  522.         string refp;
  523.         string nomfp;
  524.  
  525.         system("cls");
  526.        
  527.         tabprod[idp].id = tabprod[cpt].id;
  528.  
  529.         cout << "\t\t\t\t\t\t AJOUT D'UN NOUVEAU PRODUIT" << endl << endl;
  530.  
  531.         cout << "Identifiant: " << idp << endl << endl;
  532.  
  533.         cout << "Nom du produit: "; cin.ignore(); getline(cin, nompp); cout << endl;
  534.  
  535.         nompp = remplacerEspaces(nompp);
  536.  
  537.         cout << "Quantité: "; cin >> qtep; cout << endl;
  538.         cout << "Prix du produit: "; cin >> prixp; cout << endl;
  539.  
  540.         cout << "Catégorie du produit: "; cin.ignore();  getline(cin, catp); cout << endl;
  541.  
  542.         catp = remplacerEspaces(catp);
  543.  
  544.         cout << "Référence du fabricant: "; cin.ignore();  getline(cin, refp); cout << endl;
  545.  
  546.         refp = remplacerEspaces(refp);
  547.  
  548.         cout << "Nom du fabricant: "; cin.ignore(); getline(cin, nomfp); cout << endl;
  549.  
  550.          nomfp = remplacerEspaces(nomfp);
  551.  
  552.         tabprod[idp].id = idp;
  553.         tabprod[idp].nomp = nompp;
  554.         tabprod[idp].qte = qtep;
  555.         tabprod[idp].prix = prixp;
  556.         tabprod[idp].cat = catp;
  557.         tabprod[idp].ref = refp;
  558.         tabprod[idp].nomf = nomfp;
  559.  
  560.         ajouterProdDansFichier(tabprod, idp);
  561.  
  562.         cout << "Voulez-vous ajouter une nouveau produit? (O/N): "; cin >> continuer; cout << endl << endl;
  563.  
  564.         if (continuer == 'n' || continuer == 'N')
  565.         {
  566.             cout << "Merci!" << endl << endl;
  567.  
  568.             system("pause");
  569.  
  570.             system("cls");
  571.  
  572.             lireFichierProduits(tabprod);
  573.  
  574.             menuPrincipal(tabprod);
  575.  
  576.         }
  577.  
  578.     } while (continuer == 'o' || continuer == 'O');
  579.  
  580.  
  581.     return;
  582. }
  583.  
  584. int chercherIdMax(produit tabprod[]) // Permet de rechercher l'identifiant "id" le plus grand, à appeler dans la fonction "ajouterProduits"
  585. {
  586.     int IdMax = 0;
  587.  
  588.     for (int i = 0; i < cpt; i++)
  589.     {
  590.         if (tabprod[i].id > tabprod[IdMax].id)
  591.         {
  592.             IdMax = i;
  593.         }
  594.     }
  595.  
  596.     return tabprod[IdMax].id++;
  597. }
  598.  
  599. string remplacerEspaces(string mot)// Permet de remplacer tous les espaces d'une chaîne de caractères en tirets en bas "_", à appeler dans la fonction "ajouterProduits"
  600. {
  601.  
  602.     for (int i = 0; i < mot.length(); i++)
  603.     {
  604.         if (mot.at(i) == ' ')
  605.         {
  606.             mot[i] = '_';
  607.         }
  608.     }
  609.  
  610.     return mot;
  611. }
  612.  
  613. void ajouterProdDansFichier(produit tabprod[], int idp)
  614. {
  615.     fstream inventaire("produits.txt", ios::app);
  616.  
  617.     if (!inventaire)
  618.     {
  619.         cout << "Impossible de lire le fichier" << endl;
  620.         system("pause");
  621.         exit(1);
  622.     }
  623.  
  624.     inventaire << setiosflags(ios::left) << setw(10) << tabprod[idp].id << setw(50) << tabprod[idp].nomp << setw(10) << tabprod[idp].qte << setw(10) << tabprod[idp].prix << setw(15) << tabprod[idp].cat << setw(15) << tabprod[idp].ref << setw(15) << tabprod[idp].nomf << endl;
  625.  
  626.     inventaire.close();
  627.  
  628.     return;
  629. }
  630.  
  631. void modifierProduits(produit tabprod[])  // Modifier un ou plusieurs champs d'un ou de plusieurs produits
  632. {
  633.     int idprod;
  634.     int nb = 0;
  635.     int choix1;
  636.     int choix2;
  637.     int choix3;
  638.     string nom;
  639.     int qte;
  640.     float prix;
  641.     string cat;
  642.     int ref;
  643.     string fab;
  644.  
  645.     fstream inventaire("produits.txt", ios::app);
  646.  
  647.     if (!inventaire)
  648.     {
  649.         cout << "Impossible de lire le fichier" << endl;
  650.         system("pause");
  651.         exit(1);
  652.     }
  653.    
  654.     do
  655.     {
  656.  
  657.  
  658.         system("cls");
  659.  
  660.         cout << "\t\t\t\t\t\t Modification des données d'un produit" << endl << endl;
  661.  
  662.         cout << "Identifiant de produit: "; cin >> idprod; cout << endl;
  663.  
  664.         int idprodf = rechercheIndiceProd(tabprod, idprod);
  665.  
  666.         if (idprodf == 0)
  667.         {
  668.             cout << "La saisie ne correspond à aucun identifiant. Veuillez réessayer" << endl << endl;
  669.  
  670.             system("pause");
  671.         }
  672.         else
  673.         {
  674.  
  675.  
  676.             do
  677.             {
  678.                 system("cls");
  679.  
  680.  
  681.                 cout << setiosflags(ios::left) << setw(10) << "Id Prod." << setw(50) << "Nom Produit" << setw(10) << "Qte" << setw(10) << "Prix" << setw(15) << "Catégorie" << setw(15) << "Réf. Fab." << setw(15) << "Fabricant" << endl << endl;
  682.  
  683.                 cout << setiosflags(ios::left) << setw(10) << tabprod[idprodf].id << setw(50) << tabprod[idprodf].nomp << setw(10) << tabprod[idprodf].qte << setw(10) << tabprod[idprodf].prix << setw(15) << tabprod[idprodf].cat << setw(15) << tabprod[idprodf].ref << setw(15) << tabprod[idprodf].nomf << endl;
  684.  
  685.                 cout << "Vous voulez modifier: " << endl;
  686.                 cout << "(1) Le nom" << endl;
  687.                 cout << "(2) La quantité" << endl;
  688.                 cout << "(3) Le prix" << endl;
  689.                 cout << "(4) La catégorie" << endl;
  690.                 cout << "(5) La référence" << endl;
  691.                 cout << "(6) Le fabricant" << endl << endl;
  692.                 cout << "(7) Valider les modifications..." << endl << endl;
  693.                 cout << "Votre choix: "; cin >> choix1; cout << endl;
  694.  
  695.                 switch (choix1)
  696.                 {
  697.                 case 1:
  698.  
  699.                     cout << "Nouveau nom: "; cin.ignore(); getline(cin, nom);
  700.  
  701.                     tabprod[idprodf].nomp = nom;
  702.  
  703.                     inventaire << tabprod[idprodf].nomp;
  704.  
  705.  
  706.                     break;
  707.  
  708.                 case 2:
  709.  
  710.                     cout << "Nouvelle quantité: "; cin >> qte;
  711.  
  712.                     tabprod[idprodf].qte = qte;
  713.  
  714.                     inventaire << tabprod[idprodf].qte;
  715.  
  716.                     break;
  717.  
  718.                 case 3:
  719.  
  720.                     cout << "Nouveau prix: "; cin >> prix;
  721.  
  722.                     tabprod[idprodf].prix = prix;
  723.  
  724.                     inventaire << tabprod[idprodf].prix;
  725.  
  726.  
  727.                     break;
  728.  
  729.                 case 4:
  730.  
  731.                     cout << "Nouvelle catégorie: "; cin.ignore(); getline(cin, cat);
  732.  
  733.                     tabprod[idprodf].cat = cat;
  734.  
  735.                     inventaire << tabprod[idprodf].cat;
  736.  
  737.  
  738.                     break;
  739.  
  740.                 case 5:
  741.  
  742.                     cout << "Nouvelle référence: "; cin >> ref;
  743.  
  744.                     tabprod[idprodf].ref = ref;
  745.  
  746.                     inventaire << tabprod[idprodf].ref;
  747.  
  748.  
  749.                     break;
  750.  
  751.                 case 6:
  752.  
  753.                     cout << "Nouveau fabricant: "; cin.ignore(); getline(cin, fab);
  754.  
  755.                     tabprod[idprodf].nomf = fab;
  756.  
  757.                     inventaire << tabprod[idprodf].nomf;
  758.  
  759.                     break;
  760.  
  761.                 case 7:
  762.  
  763.                     reecrireFichier(tabprod, idprodf);
  764.  
  765.                     cout << "Modifications effectuées avec succès!" << endl << endl;
  766.  
  767.                     do
  768.                     {
  769.                         cout << "(1) Modifier un autre produit" << endl;
  770.                         cout << "(2) Revenir au menu principal" << endl;
  771.                         cout << "(3) Sortir du programme" << endl << endl;
  772.                         cout << "Votre choix: "; cin >> choix2; cout << endl;
  773.  
  774.                         switch (choix2)
  775.                         {
  776.                         case 1:
  777.  
  778.                             break;
  779.  
  780.                         case 2:
  781.  
  782.                             system("cls");
  783.  
  784.                             menuPrincipal(tabprod);
  785.  
  786.                             break;
  787.  
  788.                         case 3:
  789.  
  790.                             cout << "Merci!";
  791.  
  792.                             exit(0);
  793.                         }
  794.  
  795.                         if (choix2 > 3 || choix2 < 1)
  796.                         {
  797.                             cout << "Erreur, la saisie ne correspond à aucun choix!" << endl << endl;
  798.                         }
  799.  
  800.  
  801.                     } while (choix2 > 3 || choix2 < 1);
  802.  
  803.                     break;
  804.  
  805.                 }
  806.  
  807.                 if (choix1 == 7)
  808.                 {
  809.                     break;
  810.                 }
  811.  
  812.  
  813.             } while (choix1 < 7 || choix1 > 0);
  814.  
  815.         }
  816.  
  817.     }while (choix2 == 1);
  818.  
  819.         inventaire.close();
  820.    
  821.     return;
  822. }
  823.  
  824. int rechercheIndiceProd(produit tabprod[], int choix)  // Permet de chercher l'indice (dans le tableau) du produit à modifier, à appeler dans la fonction "modifierProduits"
  825. {
  826.     int nb = 0;
  827.  
  828.     for (int i = 0; i < cpt; i++)
  829.     {
  830.         if (tabprod[i].id == choix)
  831.         {
  832.             return i;
  833.         }
  834.         else
  835.         {
  836.             nb++;
  837.         }
  838.     }
  839.  
  840.     if (nb == cpt)
  841.     {
  842.         return 0;
  843.     }
  844. }
  845.  
  846. void reecrireFichier(produit tabprod[], int idprodf) // Permet de réécrire le fichier "produits.txt", à appeler dans la fonction "modifierProduits"
  847. {
  848.  
  849.     fstream inventaire("produits.txt", ios::out);
  850.  
  851.     if (!inventaire)
  852.     {
  853.         cout << "Impossible de lire le fichier" << endl;
  854.         system("pause");
  855.         exit(1);
  856.     }
  857.  
  858.     for (int i = 0; i < cpt; i++)
  859.     {
  860.         inventaire << setiosflags(ios::left) << setw(10) << tabprod[i].id << setw(50) << tabprod[i].nomp << setw(10) << tabprod[i].qte << setw(10) << tabprod[i].prix << setw(15) << tabprod[i].cat << setw(15) << tabprod[i].ref << setw(15) << tabprod[i].nomf << endl;
  861.     }
  862.  
  863.     inventaire.close();
  864.  
  865.     return;
  866. }
  867.  
  868. float valoriserStock(produit tabprod[])
  869. {
  870.     float produit;
  871.     float somme = 0;
  872.  
  873.  
  874.     for (int i = 0; i < cpt; i++)
  875.     {
  876.         produit = tabprod[i].prix * tabprod[i].qte;
  877.         somme = somme + produit;
  878.     }
  879.  
  880.     return somme;
  881. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement