booking system

  1. /*
  2.  * Functions.java
  3.  * The package functions contains the different class executing the different functions of the FlightSystem Reservation.
  4.  */
  5. package reservation.system.functions;
  6.  
  7. /**
  8.  * This class uniform the way to call each sub-class (Create, Reserve, Cancel, Lists, Identify) that processes the commands.
  9.  * It delegates to the sub-class the implementation of the function execute () and check ().
  10.  * Also it masks the machinery and helps the developer who want to reuse the program.
  11.  * @author Texier Mathieu and Frederic Bidon
  12.  */
  13. public abstract class Functions extends Object implements reservation.Action {    
  14.     /**
  15.      * Proceed the execution of the request
  16.      * @throws Exception if the excution is not complete
  17.      * @return The apropriate description
  18.      */    
  19.     public abstract String execute () throws Exception;
  20.    
  21.     /**
  22.      * Display the usage for the command (details the different arguments, his type...).
  23.      * @return the Usage
  24.      */
  25.     static public String usage (){
  26.         return new String (Create.usage () + "\r\n"
  27.                          + Reserve.usage ()+ "\r\n"
  28.                          + Cancel.usage () + "\r\n"
  29.                          + Identify.usage () + "\r\n"
  30.                          + Lists.usage () + "\r\n"
  31.                          + Store.usage () + "\r\n"
  32.                          + Reload.usage () + "\r\n");
  33.     }
  34.  
  35.     /**
  36.      * Process the array of argument
  37.      * @return the result of the request.
  38.      * @throws Exception If the command is not executed
  39.      * @param arg the list of command to be proceed
  40.      */
  41.     public String execute(String[] arg) throws Exception {
  42.         this.arg = (String[]) arg.clone ();
  43.          _check (arg);
  44.         return execute ();
  45.     }
  46.    
  47.     /**
  48.      * Verify invariants :
  49.      * <PRE>
  50.      * - <CODE>regex</CODE> ^0*[1-9]+0*
  51.      * * - <CODE>arg</CODE> belongs to [min max]
  52.      * </PRE>
  53.      * @return false if the invariants is violated
  54.      * @param min the minimun number allowed in <CODE>arg</CODE>
  55.      * @param max the maximum number allowed in <CODE>arg</CODE>
  56.      * @param arg the input String that will be checked
  57.      */
  58.     static public boolean checkInteger (String arg, int min, int max) {
  59.         if (arg.matches ("^0*[1-9]+0*"))
  60.             return ((Integer.parseInt (arg) >= min) && (Integer.parseInt (arg) <= max)) ;
  61.         return false;
  62.     }
  63.  
  64.     /**
  65.      * Verify the number of arguments of the command
  66.      * Verify invariants :
  67.      * <PRE>
  68.      * - <CODE>regex</CODE> ^[A-Z][a-z]*
  69.      * - seat < dimension of the flight and > 0
  70.      * </PRE>
  71.      * @param arg the input array that will be checked
  72.      * @param min the minimum argument allowed in <CODE>arg</CODE>
  73.      * @param max the maximum argument allowed in <CODE>arg</CODE>
  74.      * @throws Exception if the invariants is violated
  75.      */
  76.     public static void ArgumentIsValid (String[] arg, int min, int max) throws Exception {
  77.         boolean badFormat = false;
  78.         if (arg != null) {
  79.             for (int i = 0; i < arg.length; i++) {
  80.                 if (arg[i] == null)
  81.                     throw new IllegalArgumentException ("The command can not have null parameter\r\n");
  82.             }
  83.             if (arg.length < min || arg.length > max)
  84.                 badFormat = true;
  85.         } else badFormat = true;
  86.         if (badFormat) throw new IllegalArgumentException ("The number of argument for this method is not correct\r\n");
  87.     }
  88.  
  89.     /**
  90.      * Verify that at least one flight is created.
  91.      * @throws Exception if the FlightList contains no flight.
  92.      */
  93.     static void CheckFlightListNotEmpty () throws Exception {
  94.         if (fs.getFlightList ().length <= 0)
  95.             throw new Exception ("Please create a flight first");
  96.     }
  97.  
  98.     /**
  99.      * Verify that one reservation has been made.
  100.      * @throws Exception if the BookingList contains no BookingNumber.
  101.      */
  102.     static void CheckBookingListNotEmpty () throws Exception {
  103.         if (fs.getBookingList ().length <= 0)
  104.             throw new Exception ("Please reserve for a person first;");
  105.     }
  106.    
  107.    
  108.    /**
  109.     * Check if the arguments are correct and  can be executed
  110.     * @throws Exception if the argument is violated
  111.     */
  112.     abstract void _check (String[] arg) throws Exception;
  113.    
  114.     /**
  115.      * The list of argnument receive from the command
  116.      */    
  117.     protected String[] arg;
  118. }
  119.  

contact - link to this site