booking system

  1. import java.util.StringTokenizer;
  2. import java.io.InputStreamReader;
  3. import java.io.BufferedReader;
  4. import java.io.FileInputStream;
  5. import java.io.FileNotFoundException;
  6. import java.io.IOException;
  7.  
  8. import reservation.Mode;
  9. import reservation.*;
  10. import reservation.Command;
  11. import reservation.CustomDialog;
  12. import reservation.system.functions.Functions;
  13. import gnu.getopt.LongOpt;
  14. import gnu.getopt.Getopt;
  15.  
  16. /**
  17.  * <B>The main entry to the Flight Reservation System </B>
  18.  * The class FlightSystemUser implements the entry point of the program. The main () method receives and process the different parameters passed when the program is launched. The user can choose between read the command from the console, from a file, or from a graphic interface. The command is then analysed and the appropriate class is instantiated.
  19.  * @author Frederic Bidon and Mathieu Texier
  20.  * @version 1.0
  21.  * @see FlightSystem
  22.  */
  23. class FlightSystemUser {
  24.    
  25.     /**
  26.     * Display the usage for the program.
  27.     */
  28.     static public void usage (){
  29.         String usage = "\r\n"+
  30.         "Flight System v1.0\r\n" +
  31.         "\r\n"+
  32.         "Usage: java FlighSystemUser [option] {mode}+\r\n" +  
  33.         "\r\n"+
  34.         "Where mandatory mode is a combination of:\r\n"+
  35.         " -g[n]\t--graphic\r\n" +
  36.         "\t graphical mode: \r\n"+
  37.         "\t A Gui instance is launched to interface the program.\r\n"+
  38.         "\t n is an integer (between 1 and 5) coresponding to the number of windows"+
  39.         "\t launched.\r\n\r\n"+
  40.         " -c\t--command\r\n" +
  41.         "\t command line: \r\n"+
  42.         "\tThe command could be written on a terminal and processed by the program \twhen enter is pressed.\r\n"+
  43.         " -f <file>\t--file <file>\r\n" +
  44.         "\t input from a file:\r\n"+
  45.         "\t the <file> containing the command is processed by the system.\r\n" +
  46.         "\r\n" +
  47.         "Option include :\r\n"+
  48.         " -v[n]\t--verbose\r\n" +
  49.         "\t verbose mode: \r\n" +
  50.         "\t n is the degree of verbosity (between 1 and 4) of the program.\r\n" +
  51.         "\t In command line this option will provide more explanations. \r\n" +  
  52.         "\t 1 corespond to the quiet mode\r\n" +
  53.         "\t 2 will display normal output\r\n" +
  54.         "\t 3 will add the state of the Flight System after each request\r\n" +
  55.         "\t 4 will print the stack is printed for each execption thrown.\r\n\r\n" +
  56.         "Example:\r\n" +
  57.         " The following instruction will process the file\r\n" +
  58.         " Filename.txt in verbose mode and then will leave the hand in\r\n" +
  59.         " the console for more instruction." +
  60.         "\tJava FlightSystemUser -v -f filename.txt --command\r\n" +
  61.         "This instruction will launch the command mode and one exit\r\n" +
  62.         " the graphical mode"+ 
  63.         "\tJava FlightSystemUser -c -g\r\n\r\n" +
  64.         "The command syntax:\r\n";
  65.         usage += Functions.usage();
  66.         usage += "\r\n"+
  67.         "Example of request command message:\r\n" +
  68.         "If you want to create a new flight, you may write in a first time \r\n"+
  69.         "the command create, then the name of the flight (for example SK234), \r\n"+
  70.         "the number of rows (for example 35) and the number of seat for each row \r\n"+
  71.         "(for example 6).\r\n"+
  72.         "Then the command line is as follows:\r\n"+
  73.         "\t create SK234 35 6.\r\n\r\n"+
  74.         "Note: All the characters of the command are converts in lowercase. \r\n"+
  75.         "Therefore, you can write the name with something else case.\r\n"+  
  76.         "For example, this command is also correct: Create SK234 35 6.\r\n"+
  77.         "The fallowing entries are erroneous command:\r\n"+
  78.         "\t create SK-235 10 2\r\n"+
  79.         "\t create SK235 120 12\r\n"+
  80.         "In a first case, a special character (forbidden for all the arguments) \r\n"+
  81.         "is inserted in the flight's name. \r\n"+
  82.         "In the second case, the syntax is correct but the two numbers are out of range:\r\n"+
  83.         "The maximum size for a flight is 100 rows with a rows length of 10 seats.\r\n"+
  84.         "\r\n"+
  85.         "Java Programming : course 02215\r\n" +
  86.         "author: Frederic Bidon, Texier Mathieu\r\n" +
  87.         "DTU\r\n";
  88.         System.out.println(usage);
  89.     }
  90.    
  91.     /**
  92.     * Execute the operation's mode of the system chosen by the user.
  93.     */
  94.     static public void main (String[] argv) {
  95.  
  96.         LongOpt[] longopts = new LongOpt[5];
  97.         StringBuffer sb = new StringBuffer ();
  98.        
  99.         longopts[0] = new LongOpt ("verbose", LongOpt.OPTIONAL_ARGUMENT, sb, 'v');
  100.         longopts[1] = new LongOpt ("file", LongOpt.REQUIRED_ARGUMENT, sb, 'f');
  101.         longopts[2] = new LongOpt ("command", LongOpt.NO_ARGUMENT, null, 'c');
  102.         longopts[3] = new LongOpt ("graphical", LongOpt.OPTIONAL_ARGUMENT, sb, 'g');
  103.         longopts[4] = new LongOpt ("help", LongOpt.NO_ARGUMENT, null, 'h');
  104.        
  105.        
  106.         Getopt g = new Getopt ("FlightSystemUser", argv, "-v::f:cg::h:", longopts);
  107.         g.setOpterr (false);
  108.        
  109.         BufferedReader in = new BufferedReader (new InputStreamReader (System.in));
  110.         int VerboseMode = Mode.NORMAL;
  111.  
  112.         Mode mode = null;
  113.         int c = g.getopt ();
  114.         do
  115.             switch (c) {
  116.                 case 'v' : // Verbose Mode
  117.                     String level = g.getOptarg ();
  118.                     if (level != null) {
  119.                     if (Functions.checkInteger (level, Mode.QUIET, Mode.DEBUG))
  120.                         {VerboseMode = Integer.parseInt(level);}
  121.                     else {System.err.println("The verbosity Level is comprised between 1 (-quit) and 4 (-debug)");}
  122.                     }
  123.                     else VerboseMode = Mode.NORMAL;
  124.                     System.out.println ("Verbosity level : " + VerboseMode);
  125.                     break;
  126.                 case 'c' : // command Line
  127.                     System.out.println ("Command Line");
  128.                     mode = new Command (in,VerboseMode);
  129.                     mode.process ();
  130.                     break;
  131.                    
  132.                 case 'f' : // File
  133.                      
  134.                     String fileName = g.getOptarg ();
  135.                     try{
  136.                         in = new BufferedReader (new InputStreamReader (new FileInputStream (fileName)));
  137.                     } catch (FileNotFoundException e){ System.err.println ("The file doesn't exist.");}
  138.                     mode = new Command (in,VerboseMode);
  139.                     mode.process ();
  140.                     break;
  141.                    
  142.                 case 'g' : // graphical mode
  143.                     String numberOfInstance = g.getOptarg ();
  144.                     int number = 1;
  145.                     if (numberOfInstance != null) {
  146.                         if (Functions.checkInteger (numberOfInstance, 1, 5))
  147.                             {number = Integer.parseInt(numberOfInstance);}
  148.                         else {System.err.println("The number of instance is limited between 1 and 5");}
  149.                     }
  150.                     System.out.println ("Graphical mode " + number + " instance(s).");
  151.                    
  152.                     for (int i=0;i<number;i++) {
  153.                         mode= new CustomDialog (VerboseMode);
  154.                         mode.process ();          
  155.                     }
  156.                     break;
  157.                    
  158.                 case 'h' : // help
  159.                     System.out.println ("help");
  160.                     usage ();
  161.                     break;
  162.                 case -1:
  163.                     String Help = "Flight System v1.0\r\n";
  164.                     Help += "\r\n";
  165.                     Help += "Right usage: java FlighSystemUser [option] {mode}+\r\n";
  166.                     Help += "Execute the program with argument -h for help.";
  167.                     System.out.println(Help);
  168.                     break;
  169.                 default:
  170.                     System.out.println ("Nonvalid argument.");
  171.                     usage ();
  172.                     System.exit(1);
  173.                     break;
  174.             }
  175.             while ((c = g.getopt ()) != -1);
  176.            
  177.             for (int i = g.getOptind (); i < argv.length ; i++) {
  178.                 System.out.println ("Nonvalid argument: " + argv[i] + "\n");
  179.                 usage ();
  180.                 System.exit(1);
  181.             }
  182.     }
  183. }
  184.  

contact - link to this site