Guest User

Untitled

a guest
Jan 24th, 2019
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 10.23 KB | None | 0 0
  1. import java.io.InputStream;
  2. import java.util.Arrays;
  3.  
  4. /**
  5. * gestion des actions associees a la reconnaissance syntaxique
  6. * des fiches de livraison de vin
  7. *
  8. * @author Aguessy, Donou
  9. */
  10. public class ActVin extends AutoVin {
  11.      
  12.     /** table des actions */
  13.     private final int[][] action =
  14.     {/* Etat       BJ    BG   IDENT  NBENT   ,     ;     /  AUTRES  */
  15.     /* 0 */      { 10,   10,    1,    10,   10,   10,   10,   10   },
  16.     /* 1 */      {  3,    4,    5,     2,   10,   10,   10,   10   },
  17.     /* 2 */      {  3,    4,   5,    10,   10,   10,   10,   10   },
  18.     /* 3 */      { 10,   10,    5,    10,   10,   10,   10,   10   },
  19.     /* 4 */      { 10,   10,   10,     6,   10,   10,   10,   10   },
  20.     /* 5 */      { 10,   10,    5,    10,    7,    8,   10,   10   },
  21.     /* 6 */      {  10,    10,    1,    10,   10,   10,   9,   10   },
  22.     /* 7 */      { 10,   10,    10,    10,   10,   10,    10,   10   },
  23.    
  24.     /*!!! A COMPLETER !!!*/    
  25.     // Action Erreur : 10
  26.     // Etat erreur : 7
  27.     } ;        
  28.    
  29.     /** constructeur classe ActVin */
  30.     public ActVin(InputStream flot) {
  31.         super(flot);
  32.     }
  33.  
  34.     /** types d'erreurs detectees */
  35.     private static final int FATALE = 0, NONFATALE = 1, INITVOLUME = 100;
  36.  
  37.     /** taille d'une colonne pour affichage ecran */
  38.     private static final int MAXLGID = 20;
  39.     /** nombre maximum de chauffeurs */
  40.     private static final int MAXCHAUF = 10;
  41.     /** tableau des chauffeurs et resume des livraison de chacun */
  42.     private Chauffeur[] tabChauf = new Chauffeur[MAXCHAUF];
  43.    
  44.     /** indice courant du nombre de chauffeurs dans le tableau tabChauf */
  45.     private int ichauf,  bj = 0, bg = 0, ordin = 0, volume = 0, nextChauf = 0, quantiteLivree = 0;
  46.    
  47.     private static String[] qualite = {"ORDINAIRE", "BEAUJOLAIS", "BOURGOGNE"};
  48.     private String typeQualite = "ORDINAIRE";
  49.  
  50.     /** variables de comptes */
  51.     private static int bj_total = 0, bg_total = 0, ordin_total = 0;
  52.    
  53.     /** SmallSet des magasins */
  54.     private static SmallSet magdif = new SmallSet();
  55.    
  56.     /** utilitaire d'affichage a l'ecran
  57.      * @param ch est une chaine de longueur quelconque
  58.      * @return chaine ch cadree a gauche sur MAXLGID caracteres
  59.      * */
  60.     private String chaineCadrageGauche(String ch) {
  61.         int lgch = Math.min(MAXLGID, ch.length());
  62.         String chres = ch.substring(0, lgch);
  63.         for (int k = lgch; k < MAXLGID; k++)
  64.             chres = chres + " ";
  65.         return chres;
  66.     }
  67.  
  68.     /** affichage de tout le tableau de chauffeurs a l'ecran */
  69.     private void afficherchauf() {
  70.         String idchaufcour, ch;
  71.         Ecriture.ecrireStringln("");
  72.         ch = "CHAUFFEUR                   BJ        BG       ORD     NBMAG\n"
  73.                 + "---------                   --        --       ---     -----";
  74.         Ecriture.ecrireStringln(ch);
  75.         for (int i = 0; i <= ichauf; i++) {
  76.             System.out.println(" numchauf courant "+tabChauf[i].numchauf);
  77.             idchaufcour = ((LexVin)lex).repId(tabChauf[i].numchauf);
  78.             Ecriture.ecrireString(chaineCadrageGauche(idchaufcour));
  79.             Ecriture.ecrireInt(tabChauf[i].bj, 10);
  80.             Ecriture.ecrireInt(tabChauf[i].bg, 10);
  81.             Ecriture.ecrireInt(tabChauf[i].ordin, 10);
  82.             Ecriture.ecrireInt(tabChauf[i].magdif.size(), 10);
  83.             Ecriture.ecrireStringln("");
  84.         }
  85.     }
  86.    
  87.     /** gestion des erreurs
  88.      * @param te type de l'erreur
  89.      * @param messErr message associe a l'erreur
  90.      */
  91.     private void erreur(int te, String messErr) {
  92.         Lecture.attenteSurLecture(messErr);
  93.         switch (te) {
  94.         case FATALE:
  95.             errFatale = true;
  96.             break;
  97.         case NONFATALE:
  98.             etatCourant = etatErreur;
  99.             break;
  100.         default:
  101.             Lecture.attenteSurLecture("parametre incorrect pour erreur");
  102.         }
  103.     }
  104.  
  105.     /**
  106.      * initialisations a effectuer avant les actions
  107.      */
  108.     private void initialisations() {
  109.         ichauf = -1;
  110.         /*!!! A COMPLETER SI BESOIN !!!*/
  111.     }
  112.    
  113.     /**
  114.      * acces a un attribut lexical
  115.      * cast pour preciser que lex est de type LexVin
  116.      * @return valNb associe a l'unite lexicale nbentier
  117.      */
  118.     private int valNb() {
  119.         return ((LexVin)lex).getValNb();
  120.     }
  121.     /**
  122.      * acces a un attribut lexical
  123.      * cast pour preciser que lex est de type LexVin
  124.      * @return numId associe a l'unite lexicale ident
  125.      */
  126.     private int numId() {
  127.         return ((LexVin)lex).getNumId();
  128.     }
  129.    
  130.     private int findChauf(Chauffeur[] tabChauf, int ichauf) {
  131.         int i = 0;
  132.         while(i < nextChauf) {
  133.             if(tabChauf[i].numchauf == ichauf) return i;
  134.             i++;
  135.         }
  136.         return -1;
  137.     }
  138.  
  139.     /**
  140.      * execution d'une action
  141.      * @param numact numero de l'action a executer
  142.      */
  143.     public void executer(int numact) {
  144.  
  145.         switch (numact) {
  146.        
  147.         case -1: break;
  148.        
  149.         // lecture ident nom chauffeur
  150.         case 1: ichauf = numId();
  151.                 if((findChauf(tabChauf, ichauf) == -1) && ((nextChauf + 1) == MAXCHAUF))
  152.                     erreur(FATALE, "Nombre maximum de chauffeurs atteint");
  153.                  break;
  154.  
  155.         // lecture volume citerne
  156.         case 2: volume = valNb();
  157.                 if ((volume < 100) || (volume > 200))
  158.                     volume = INITVOLUME;
  159.                 break;
  160.        
  161.         // lecture de BEAUJOLAIS
  162.         case 3: typeQualite = qualite[1];
  163.                 break;
  164.        
  165.         // lecture de BOURGOGNE
  166.         case 4: typeQualite = qualite[2];
  167.                 break;
  168.                
  169.         // lecture ident nom du magasin et ajout si absent
  170.         case 5: if (!magdif.contains(numId()))
  171.             //System.out.println("contenu "+numId());
  172.             System.out.println("Magasin ajouté:"+((LexVin)lex).repId(numId()));
  173.                     magdif.add(numId());
  174.                 break;
  175.  
  176.         // quantite livree dans le magasin
  177.         case 6: quantiteLivree = valNb();
  178.                 if (quantiteLivree <= 0)
  179.                     erreur(NONFATALE, "Quantite livree incorrecte");
  180.                 switch (typeQualite) {
  181.                
  182.                     case "ORDINAIRE":
  183.                         ordin += quantiteLivree;
  184.                         break;
  185.  
  186.                     case "BEAUJOLAIS":
  187.                         bj += quantiteLivree;
  188.                         break;
  189.  
  190.                     case "BOURGOGNE":
  191.                         bg += quantiteLivree;
  192.                         break;
  193.                 }
  194.                 break;
  195.                
  196.         // lecture de ','
  197.         case 7: if ((bj + bg + ordin) > volume)
  198.                     erreur(NONFATALE, "Quantite livree supeieure a la capacite de la citerne");
  199.                 switch (typeQualite) {
  200.                     case "ORDINAIRE":
  201.                         ordin_total += ordin;
  202.                         ordin = 0;
  203.                         break;
  204.  
  205.                     case "BEAUJOLAIS":
  206.                         bj_total += bj;
  207.                         bj = 0;
  208.                         break;
  209.                     case "BOURGOGNE":
  210.                         bg_total += bg;
  211.                         bg = 0;
  212.                         break;
  213.                 }
  214.                 typeQualite = "ORDINAIRE";
  215.                 break;
  216.        
  217.         // lecture de ';'
  218.         case 8: switch (typeQualite) {
  219.                     case "ORDINAIRE":
  220.                         ordin_total += ordin;
  221.                         ordin = 0;
  222.                         break;
  223.  
  224.                     case "BEAUJOLAIS":
  225.                         bj_total += bj;
  226.                         bj = 0;
  227.                         break;
  228.                     case "BOURGOGNE":
  229.                         bg_total += bg;
  230.                         bg = 0;
  231.                         break;
  232.                 }                              
  233.                 if (findChauf(tabChauf, ichauf) == -1) {
  234.                     tabChauf[nextChauf] = new Chauffeur(ichauf, bj_total, bg_total, ordin_total, magdif);
  235.                     nextChauf++;
  236.                    
  237.                 } else {
  238.                     Chauffeur current = tabChauf[findChauf(tabChauf, ichauf)];
  239.                     current.bg += bg_total;
  240.                     current.bj += bj_total;
  241.                     current.ordin += ordin_total;
  242.                     //current.magdif.union(magdif);
  243.                     //System.out.println("toto"+current.magdif.size());
  244.                    
  245.                 }
  246.                 typeQualite = "ORDINAIRE"; volume = 0;
  247.                 bj_total = 0; bg_total = 0; ordin_total = 0;
  248.                 //System.out.println("totoa"+magdif.size());
  249.                 magdif.clear();
  250.                
  251.                 afficherchauf();
  252.                 break;
  253.            
  254.         // lecture de '/'
  255.         case 9: int max = -1, vip = 0, i = 0;
  256.                 while (i < nextChauf) {
  257.                     Chauffeur current = tabChauf[i];
  258.                     if (current.magdif.size() > max) {
  259.                         max = current.magdif.size();
  260.                         vip = current.numchauf;
  261.                     }
  262.                     i++;
  263.                 }
  264.                 System.out.println("Le chauffeur ayant livre le plus de magasins differents ce mois-ci est : " + ((LexVin)lex).repId(vip));
  265.                 break;
  266.        
  267.         // Action erreur
  268.         case 10: erreur(NONFATALE, "Syntaxe invalide");
  269.                  break;
  270.                
  271.         default:
  272.             Lecture.attenteSurLecture("action " + numact + " non prevue");
  273.         }
  274.     }
  275.    
  276.     /**
  277.      * definition methode abstraite faireAction de Automate
  278.      */
  279.      public void faireAction(int etat, int unite) {
  280.          executer(action[etat][unite]);
  281.      };
  282.      
  283.      /**
  284.       * definition methode abstraite initAction de Automate
  285.       */
  286.      public void initAction() {
  287.         // action 0 a effectuer a l'init
  288.          initialisations();
  289.      };
  290.  
  291.      /**
  292.       * definition methode abstraite getAction de Automate
  293.       */
  294.      public int getAction(int etat, int unite) {
  295.         // return -1;
  296.         return action[etat][unite];
  297.      };
  298.  
  299.  
  300. } // class Actvin
Advertisement
Add Comment
Please, Sign In to add comment