Système de réservation

  1. /*
  2.  * Command.java
  3.  * The package reservation contains the different class allowing to choose the action to realise.
  4.  */
  5. package reservation;
  6. import reservation.system.functions.*;
  7.  
  8. import java.util.StringTokenizer;
  9. import java.io.InputStreamReader;
  10. import java.io.BufferedReader;
  11. import java.io.FileInputStream;
  12. import java.io.FileNotFoundException;
  13. import java.io.IOException;
  14.  
  15. /**
  16.  * This class hold Command structure. This object analyse the instruction written by the user and determine according to the first element the command to execute.
  17.  * Then it instancie a new object which depends to the command and which will execute the instruction of the user.
  18.  * @author Texier Mathieu and Frederic Bidon
  19.  */
  20. public class Command implements Mode{
  21.    
  22.     /** Creates a new instance of Command */
  23.     public Command () {
  24.     }
  25.    
  26.     /**
  27.      * Take over the instruction written by the user and also if it want execute the command in Verbose Mode
  28.      * @param in The input stream from which the data are interpreted
  29.      * @param VerboseMode the verbosity level (QUIET, NORMAL, DEBUG)
  30.      */
  31.     public Command (BufferedReader in, int VerboseMode) {
  32.         this.in = in;
  33.         this.VerboseMode = VerboseMode;
  34.     }
  35.    
  36.    /**
  37.     * This method determine the number of element of the user's instruction and analyse the first element of this one.
  38.     * If it corresponds to an available command of the system, it create a new object depending of the command.
  39.     * This new object will execute the instruction with the other elements of the command.
  40.     */
  41.     public void process () {
  42.         Action action = null;
  43.             boolean escape = false;
  44.         do {
  45.             boolean boolError = false;
  46.             StringTokenizer st = null;
  47.             do {
  48.                 try {
  49.                     if (VerboseMode > QUIET)
  50.                         System.out.print ("CMD>");
  51.                     String readLine = in.readLine ();
  52.                     if (readLine == null)
  53.                         return;
  54.                     st = new StringTokenizer (readLine);
  55.                 } catch (IOException e){ System.err.println (e);}
  56.             } while (st.countTokens () < 1);
  57.            
  58.             int AgumentNumber= st.countTokens ()-1;
  59.             String line[] = new String[AgumentNumber];
  60.             String Command = st.nextToken ().toLowerCase ();
  61.            
  62.             for (int i = 0;st.hasMoreTokens (); i++)
  63.                 line[i] = st.nextToken ();
  64.  
  65.             try {
  66.                 if (Command.equals ("create"))
  67.                     action = new Create ();
  68.                 else if (Command.equals ("reserve"))
  69.                     action = new Reserve ();
  70.                 else if (Command.equals ("list"))
  71.                     action = new Lists ();
  72.                 else if (Command.equals ("identify"))
  73.                     action = new Identify ();
  74.                 else if (Command.equals ("cancel"))
  75.                     action = new Cancel ();
  76.                 else if (Command.equals ("flights"))
  77.                     action = new Flights ();
  78.                 else if (Command.equals ("store"))
  79.                     action = new Store ();
  80.                 else if (Command.equals ("reload"))
  81.                     action = new Reload ();
  82.                 else if (Command.equals ("exit")) {
  83.                     Functions.ArgumentIsValid (line, 0, 0);
  84.                     escape = true;
  85.                 }
  86.                 else {throw new Exception ("This command doesn't exist.");}
  87.             } catch (Exception e) {
  88.                 System.err.println (e.getMessage ());
  89.                 if (VerboseMode > HIGH) {
  90.                         e.printStackTrace ();
  91.                 }
  92.                 boolError = true;
  93.             }
  94.             System.out.flush ();
  95.             String res = "";
  96.             try {
  97.                 if (!boolError && !escape) {
  98.                     res = action.execute (line);
  99.                 }
  100.                 if (VerboseMode > QUIET)
  101.                     System.out.println (res);
  102.                 if (VerboseMode > NORMAL)
  103.                     System.out.println (Action.fs);
  104.             } catch (Exception e) {
  105.                 System.err.println (e.getMessage ());
  106.                 if (VerboseMode > HIGH)
  107.                     e.printStackTrace ();
  108.             }
  109.             System.err.flush ();
  110.         } while (!escape) ;
  111.     }
  112.    
  113.     private BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
  114.     private int VerboseMode = NORMAL;
  115. }
  116.  

contact - faire un lien