Système de réservation

  1. package reservation.system.functions;
  2.  
  3. import reservation.system.Flight;
  4. import reservation.system.Pos;
  5. import reservation.system.Person;
  6.  
  7. import java.io.PrintWriter;
  8. import java.io.FileWriter;
  9. import java.io.BufferedWriter;
  10. import java.io.File;
  11. import java.util.Vector;
  12.  
  13. /**
  14.  * This class hold Store structure. This object allows to save in a file the different command generating the content of the database.
  15.  * @author Texier Mathieu and Frederic Bidon
  16.  */
  17. public class Store_alternatif extends Functions{
  18.    
  19.     /**Name of the file where we want to save the data.*/
  20.     private File datafile;
  21.    
  22.     /**
  23.      * Verify invariants :
  24.      * <PRE>
  25.      * - Argument not null
  26.      * - The number of argument equals 1
  27.      * - File's name doesn't exist
  28.      * - The file can be created
  29.      * </PRE>
  30.      * @throws Exception if the file exists or cannot be created.
  31.      */    
  32.     void _check (String[] arg) throws Exception {
  33.         CheckFlightListNotEmpty ();
  34.         ArgumentIsValid (arg, 1, 1);
  35.         File datafile = new File (arg[0]);
  36.         if (!datafile.exists ()) {
  37.             if (!datafile.createNewFile ())
  38.                 throw new Exception ("Cannot create the file: " + datafile.getPath ());
  39.         } else
  40.             throw new Exception ("file " + datafile.getPath () + " already exist");
  41.        
  42.         this.datafile = datafile;
  43.     }
  44.    
  45.     /**
  46.      * Display the usage for the command <CODE>Save</code>.
  47.      * @return the usage.
  48.      */
  49.     static public String usage() {
  50.         String usage = " Usage command Save: save <fileName>\n\r";
  51.         usage += " - The <fileName> is the path to the file to be save \n\r";
  52.         usage +=" The file extention is .rcm .\n";
  53.         return usage;
  54.     }
  55.    
  56.     /**
  57.      * Store in a file the different command generating the content of the database.
  58.      * @throws Exception if the file is not saved.
  59.      * @return a confimation's message if the file is saved.
  60.      */
  61.     public String execute () throws Exception {
  62.         String[] listFlight = fs.getFlightList ();
  63.         Integer[] listBooking = fs.getBookingList ();
  64.         Vector listPerson;
  65.         PrintWriter data =
  66.         new PrintWriter (new BufferedWriter (new FileWriter (datafile)));
  67.         for(int i = 0; i < listFlight.length; i++) {
  68.             Flight flight = fs.selectFlight (listFlight[i]);
  69.             String flightName = flight.getFlightName ();
  70.             Pos dim = flight.getDimension ();
  71.             short row = dim.getRow ();
  72.             short col = dim.getCol ();
  73.             String create = "create " + flightName + " " + row + " " + col;
  74.             data.println (create);
  75.         }
  76.        
  77.         for (int j = 0; j < fs.getBookingNumberMax (); j++) {
  78.             listPerson = fs.identify (listBooking[j].intValue ());
  79.             String Passengers = " ";
  80.             Flight flightPerson = null;
  81.             Person personName = null;
  82.             for (int k = 0; k < listPerson.size (); k++) {
  83.                 personName = (Person)listPerson.get (k);
  84.                 Passengers += personName.getPersonName () + " ";
  85.                 flightPerson = personName.getFlight ();
  86.             }
  87.             String reserve = "reserve " + flightPerson.getFlightName () + Passengers;
  88.             data.println (reserve);
  89.         }
  90.        
  91.         data.close ();
  92.         return "File saved";
  93.     }
  94. }
  95.  

contact - faire un lien