Système de réservation

  1. /*
  2.  * FlightSystem.java
  3.  * The package system contains the different class allowing to create and to select the object to store in the database.
  4.  */
  5. package reservation.system;
  6.  
  7. import java.util.LinkedList;
  8. import java.util.ListIterator;
  9. import java.util.Collection;
  10. import java.util.Vector;
  11. import java.util.HashSet;
  12. import java.util.Set;
  13. import java.util.Iterator;
  14. import java.io.Serializable;
  15.  
  16. /**
  17.  * The class hold the structure to access in the database.
  18.  * This object allows to modify the database with the different command available.
  19.  * It contains all the method executing all the command written by the user.
  20.  * @author Frederic Bidon and Mathieu Texier
  21.  */
  22. public final class FlightSystem extends Object implements Serializable {
  23.  
  24.     /**
  25.      * Constructor of the class FlightSystem.
  26.      * @return the variable fs that is instantiated to FlightSystem object.
  27.      */
  28.     final static public FlightSystem getInstance () {
  29.         return fs;
  30.     }
  31.  
  32.     /**
  33.      * Method which allows to add a new flight in a database.
  34.      * @throws Exception if the flight already exists.
  35.      * @return a boolean that confirm the creation of a new flight in the flight list.
  36.      * @param flightName The name of the flight
  37.      * @param rows the number of rows
  38.      * @param rowLength the length of each rows
  39.      */
  40.     public final synchronized boolean create(String flightName, short rows, short rowLength) throws Exception {
  41.         if (flightList.select (flightName) != null)
  42.             throw new Exception ("The flight name "+ flightName +" has already been inserted in the flight list.");
  43.         return flightList.add (new Flight (flightName, new Pos (rows, rowLength)));
  44.     }
  45.    
  46.     /**
  47.      * Assert a new flight into the FlightList
  48.      * @throws Exception if the flight doesn't exist.
  49.      * @return true if the flight exists.
  50.      * @param flight The Flight that will be asserted
  51.      */
  52.     final public synchronized boolean create (Flight flight)  throws Exception {
  53.         return create (
  54.         flight.getFlightName (),
  55.         flight.getDimension ().getRow (),
  56.         flight.getDimension ().getCol ()
  57.         );
  58.     }
  59.    
  60.     /**
  61.      * Method which allows to add one or more persons in a database.
  62.      * @throws Exception if the flight doen't exist or if the command contains no personName.
  63.      * @return the number of reservation(bookingNumber) for the group of person(s) registered.
  64.      * @param flightName The name of the flight
  65.      * @param personNames A list of person
  66.      */
  67.     final public synchronized int reserve (String flightName, Set personNames) throws Exception {
  68.         Flight flight = flightList.select (flightName);
  69.         if (flight == null)
  70.             throw new Exception ("The flight "+ flightName +" has not been yet created.");
  71.         if (personNames == null)
  72.             throw new Exception ("the set of person is null");
  73.        
  74.         Profile profile = new Profile ();
  75.         profile.setBookingNumber (++bookingNumber);
  76.        
  77.         Iterator it = personNames.iterator ();
  78.         while (it.hasNext ()) {
  79.             profile.setPersonName ( (String) it.next ());
  80.             if (personList.contains (profile))
  81.                 throw new Exception ("The list contain duplicate Name");
  82.         }
  83.         Pos posList[] = getFreePlace (flight, (short)personNames.size ());
  84.        
  85.         int i = 0;
  86.         boolean success = true;
  87.         it = personNames.iterator ();
  88.         while (it.hasNext ()) {
  89.             success &= personList.add (new Person ((String) it.next (), flight, bookingNumber, posList[i++]));
  90.         }
  91.         if (success)
  92.             return bookingNumber;
  93.         else return -1;
  94.     }
  95.    
  96.     /**
  97.      * Method which allows to add one person to a database. The input is read from a file.
  98.      * @throws Exception if the flight doen't exist or if the command contains no personName.
  99.      * @return 1 if the person is added to a database or 0 if it is not added.
  100.      * @param person assert a Person Object
  101.      */
  102.     final public synchronized int reserve (Person person) throws Exception {
  103.         bookingNumber ++;
  104.         return personList.add(person) ? 1 : 0;
  105.     }
  106.    
  107.     /**
  108.      * Method which allows to cancel one or more personName that have reserved.
  109.      * @throws Exception if one or more name are invalid.
  110.      * @return true if the person(s) are canceled.
  111.      * @param bookingNumber the booking number with which the person have booked
  112.      * @param personNames The name of the person that wnat to cancel
  113.      */
  114.     final public synchronized boolean cancel (int bookingNumber, Set personNames) throws Exception {
  115.         Iterator it = personNames.iterator ();
  116.         Profile profile = new Profile ();
  117.         profile.setBookingNumber (bookingNumber);
  118.         Collection listToBeRemoved= new LinkedList();
  119.         while (it.hasNext ()) {
  120.             profile.setPersonName ((String) it.next ());
  121.             if (!personList.contains ((Object) profile))
  122.                 throw new Exception ("One or more name on your person's list are invalid.");  
  123.             else listToBeRemoved.add(personList.select(profile).get(0));
  124.         }
  125.         return personList.removeAll (listToBeRemoved);
  126.     }
  127.    
  128.     /**
  129.      * Method which allows to cancel a group of persons which have the same bookingNumber.
  130.      * @throws Exception if the bookingNumber doesn't exist.
  131.      * @return true if the group of person is canceled.
  132.      * @param bookingNumber The booking number to be canceled
  133.      */
  134.     final public synchronized boolean cancel (int bookingNumber) throws Exception {
  135.         return personList.removeAll (identify (bookingNumber));
  136.     }
  137.    
  138.     /**
  139.      * Method which allows to see the personName which have the same bookingNumber.
  140.      * @throws Exception if the bookingNumber doesn't exist.
  141.      * @return the personName who have the same bookingNumber.
  142.      * @param bookingNumber the booking number to be identify
  143.      */
  144.     final public synchronized Vector identify (int bookingNumber)  throws Exception{
  145.         Profile profile = new Profile ();
  146.         profile.setBookingNumber (bookingNumber);
  147.         return new Vector (personList.select (profile));
  148.     }
  149.    
  150.     /**
  151.      * Method which allows to see the personName present in a same flight.
  152.      * @throws Exception if the flightName doesn't exist.
  153.      * @return the personName present in a flight.
  154.      * @param flightName the name of the flight
  155.      */
  156.     final public synchronized Vector list (String flightName) throws Exception {
  157.         Flight flight = flightList.select (flightName);
  158.         if (flight == null)
  159.             throw new Exception ("The flight " +flightName+" doesn't exist");
  160.         Profile profile = new Profile ();
  161.         profile.setFlight (flight);
  162.         return new Vector (personList.select (profile));
  163.     }
  164.    
  165.    /**
  166.      * Returns a string representation of this <CODE>FlightSystem</CODE> object. This method
  167.      * is intended to be used only for debugging purposes, and the content and format
  168.      * of the returned string may vary between implementations. The returned string may
  169.      * be empty but may not be <CODE>null</CODE>
  170.      * @return a string representation of this <CODE>FlightSystem</CODE> object.
  171.      */
  172.     final public synchronized String toString () {
  173.         return flightList.toString () + personList.toString ();
  174.     }
  175.    
  176.     /**
  177.      * Method which allows to search a person in the person's list.
  178.      * @throws Exception if the invariants is violated
  179.      * @return the list of persons found.
  180.      * @param profile The person that will match this profile will be returned
  181.      */
  182.     final public synchronized Vector search (Profile profile) throws Exception {
  183.         return new Vector (personList.select (profile));
  184.     }
  185.    
  186.     /**
  187.      * Method which allows to select a flight in the flight's list according to the flightName.
  188.      * @throws Exception if the flightName doesn't exist.
  189.      * @return the flight selected if it exists.
  190.      * @param flightName the name of the flight
  191.      */
  192.     final public synchronized Flight selectFlight (String flightName) throws Exception {
  193.         return flightList.select (flightName);
  194.     }
  195.    
  196.    /**
  197.     * Accessor for the bookingNumberMax
  198.     * @return the number max of reservation on a flight.
  199.     */
  200.     final public synchronized int getBookingNumberMax () {
  201.         return bookingNumber;
  202.     }
  203.    
  204.    /**
  205.     * Method which allows to return the flights contained in the flights' list.
  206.     * @throws Exception if the invariants are violated.
  207.     * @return the list of flight(s) created.
  208.     */
  209.     final public synchronized String [] getFlightList () throws Exception {
  210.         return flightList.getFlightList ();
  211.     }
  212.    
  213.    /**
  214.     * Method which allows to return the list of the atribuated bookingNumber.
  215.     * @throws Exception if the invariants is violated.
  216.     * @return the list of bookingNumber(s) present in the database.
  217.     */
  218.     final public synchronized Integer [] getBookingList () throws Exception {
  219.         return personList.getBookingtList ();
  220.     }
  221.    
  222.     static final private FlightSystem fs = new FlightSystem ();
  223.    
  224.     private int bookingNumber = 0;
  225.    
  226.     private FlightList flightList;
  227.    
  228.     private PersonList personList;
  229.    
  230.     private FlightSystem () {
  231.         flightList = new FlightSystem.FlightList ();
  232.         personList =  new FlightSystem.PersonList ();
  233.     }
  234.    
  235.    /**
  236.     * Method which allows to atribute a place to a person.
  237.     * @throws Exception if the the flight doesn't exist.
  238.     * @return the position atribuated to a person.
  239.     */
  240.     private Pos[] getFreePlace (Flight flight, short number) throws Exception  {
  241.         Pos posList[] = new Pos[number];
  242.         short rest = number;
  243.         short count = 0;
  244.         Pos dimension = flight.getDimension ();
  245.         for (short row = 1; row <= dimension.getRow () && rest > 0; row ++) {
  246.             for (short col = 1; col <= dimension.getCol () && rest > 0; col++) {
  247.                 Pos seat = new Pos (row,col);
  248.                 Profile profile = new Profile ();
  249.                 profile.setFlight (flight);
  250.                 profile.setPos (seat);
  251.                 if (!personList.contains (profile)) {
  252.                     posList[count++] = seat;
  253.                     rest --;
  254.                 }
  255.             }
  256.         }
  257.         if (posList.length !=  number)
  258.             throw new Exception ("The place are not sucsesfully atribuated");
  259.         return posList;
  260.     }
  261.    
  262.     /**
  263.      * The class hold the structure to access to the flights registered in the database.
  264.      * This object allows to modify the list of flight in the database.
  265.      * It contains all the methods accessing and altering the FlightList.
  266.      * @author Frederic Bidon and Mathieu Texier
  267.      */
  268.     private class FlightList extends LinkedList implements Serializable{
  269.         /**Constructor of the class*/
  270.         private FlightList () {
  271.             super ();
  272.         }
  273.        
  274.        /**
  275.         * This method allows to obtain the flight(s) chosen.
  276.         * @throws Exception if the flight doesn't exist.
  277.         * @return a string containing the flight selected.
  278.         */
  279.         public Flight select (String flightName) throws Exception, CloneNotSupportedException {
  280.             Iterator it = iterator ();
  281.             while (it.hasNext () && flightName != null) {
  282.                 Flight flight = (Flight) it.next ();
  283.                 if (flightName.equals (flight.getFlightName ())) {
  284.                     flight._check ();
  285.                     return (Flight) flight.clone ();
  286.                 }
  287.             }
  288.             return null;
  289.         }
  290.        
  291.     /**
  292.      * Returns a string representation of this <CODE>FlightList</CODE> object. This method
  293.      * is intended to be used only for debugging purposes, and the content and format
  294.      * of the returned string may vary between implementations. The returned string may
  295.      * be empty but may not be <CODE>null</CODE>
  296.      * @return a string representation of this <CODE>FlightList</CODE> object.
  297.      */
  298.         public String toString () {
  299.             Iterator it = iterator ();
  300.             String resultList = "Flight List: " +"\r\n";
  301.             while (it.hasNext ()) {
  302.                 resultList += "\t"+ (it.next ()).toString () +"\r\n";
  303.             }
  304.             return resultList;
  305.         }
  306.    
  307.     /**
  308.      * This method allows to add a flight in the database.
  309.      * @throws an exception if the flight cannot be added.
  310.      * @return the flight added in the database.
  311.      */    
  312.         public boolean add (Object o) {
  313.             boolean retValue;
  314.             if (o != null && o instanceof Flight) {
  315.                 try {
  316.                     ((Flight) o)._check ();
  317.                 } catch (Exception e) { System.err.println (e);}
  318.                 if (retValue = true)
  319.                     retValue = super.add (o);
  320.             } else retValue = false;
  321.             return retValue;
  322.         }
  323.      
  324.        /**
  325.         * This method allows to obtain the list of the flight(s) created.
  326.         * @throws Exception if the database contains no flight.
  327.         * @return an array of string containing the list of flight(s) created.
  328.         */
  329.         public String [] getFlightList () throws Exception {
  330.             Iterator it = iterator ();
  331.             String[] resultList =  new String[flightList.size ()];
  332.             for (int i=0; it.hasNext (); i++) {
  333.                 Flight flight = (Flight) it.next ();
  334.                 flight._check ();
  335.                 resultList[i] = flight.getFlightName ();
  336.             }
  337.             return resultList;
  338.         }
  339.     }
  340.    
  341.      /**
  342.      * The class hold the structure to access to the persons registered in the database.
  343.      * This object allows to modify the list of persons in the database.
  344.      * It contains all the methods accessing and altering the PersonList.
  345.      * @author Frederic Bidon and Mathieu Texier
  346.      */
  347.     private class PersonList extends LinkedList implements Serializable{
  348.         /**Constructor of the class*/
  349.         private PersonList () {
  350.             super ();
  351.         }
  352.        
  353.        /**
  354.         * This method allows to obtain the person or the group of persons found in the PersonList.
  355.         * @throws Exception if the person or the group of persons doesn't exist.
  356.         * @return a string containing the person(s) selected.
  357.         */
  358.         public PersonList select (Profile profile) throws Exception{
  359.             PersonList groupePersonList = new PersonList ();
  360.             Iterator it = iterator ();
  361.             while (it.hasNext ()) {
  362.                 Person person = (Person) it.next ();
  363.                 if (person.equals (profile)) {
  364.                     person._check ();
  365.                     if (groupePersonList.add (person) == false)
  366.                         throw new Exception (person + " is not succesfully inserted in " +groupePersonList) ;
  367.                 }
  368.             }
  369.             return groupePersonList;
  370.         }
  371.        
  372.         /**
  373.          * Returns a string representation of this <CODE>PersonList</CODE> object. This method
  374.          * is intended to be used only for debugging purposes, and the content and format
  375.          * of the returned string may vary between implementations. The returned string may
  376.          * be empty but may not be <CODE>null</CODE>
  377.          * @return a string representation of this <CODE>PersonList</CODE> object.
  378.          */
  379.         public String toString () {
  380.             Iterator it = iterator ();
  381.             String resultList = "Person List: " +"\r\n";
  382.             while (it.hasNext ()) {
  383.                 resultList += "\t"+ (it.next ()).toString () +"\r\n";
  384.             }
  385.             return resultList;
  386.         }
  387.        
  388.         /**
  389.          * This method allows to add a person or a group of persons in the database.
  390.          * @throws an exception if the person or the group of persons cannot be added.
  391.          * @return the group of person added on a flight.
  392.          */
  393.         public boolean add (Object o) {
  394.             boolean retValue;
  395.             if (o != null && o instanceof Person && !(o instanceof Profile)) {
  396.                 try {
  397.                     ((Person) o)._check ();
  398.                 } catch (Exception e) { System.err.println (e);}
  399.                 if (retValue = true)
  400.                     retValue = super.add (o);
  401.             } else retValue = false;
  402.             return retValue;
  403.         }
  404.        
  405.         /**
  406.          * This method allows to remove all the persons belong to a same booking number.
  407.          * @return the collection of the persons removed.
  408.          */
  409.         public boolean removeAll (Collection c) {
  410.             boolean retValue;
  411.             if (!c.isEmpty ()) {
  412.                 retValue = super.removeAll (c);
  413.             }
  414.             else retValue = false;
  415.             return retValue;
  416.         }
  417.        
  418.         /**
  419.          * This method allows to remove one or more persons belong to a same booking number.
  420.          * @return the object of the persons removed.
  421.          */
  422.         public boolean remove (Object o) {
  423.             boolean retValue;
  424.             if (o != null && o instanceof Person) {
  425.                 try {
  426.                     ((Person) o)._check ();
  427.                 } catch (Exception e) { System.err.println (e);}
  428.                 if (retValue = true)
  429.                     retValue = super.remove (o);
  430.             }
  431.             else retValue = false;
  432.             return retValue;
  433.         }
  434.        
  435.        /**
  436.         * This method allows to obtain the list of the booking number.
  437.         * @throws Exception if the database contains no persons.
  438.         * @return an array of integer containing the list of the booking number atribuated.
  439.         */
  440.         public Integer [] getBookingtList () throws Exception {
  441.             Iterator it = personList.iterator ();
  442.             Set set = new HashSet ();
  443.             while (it.hasNext ()) {
  444.                 Person person = (Person) it.next ();
  445.                 person._check ();
  446.                 set.add (new Integer (person.getBookingNumber ()));
  447.             }
  448.             return (Integer[]) set.toArray (new Integer[] {});
  449.         }
  450.     }
  451. }
  452.  

contact - faire un lien