Système de réservation

  1. /*
  2.  * Person.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.io.Serializable;
  8.  
  9. /**
  10.  * This class hold person structure. This object could be asserted in the <CODE>PersonList</CODE>.
  11.  * @author Texier Mathieu and Frederic Bidon
  12.  */
  13. public class Person extends Object implements Cloneable, Serializable{
  14.     /**
  15.      * Construct an empty person
  16.      */
  17.     Person (){
  18.     }
  19.    
  20.     /**
  21.      * Construct a <CODE>Person</CODE> named <CODE>personName</CODE> booked on the <CODE>flight</CODE> the seat <CODE>pos</CODE> and has the booking number : <CODE>bookingNumber</CODE>.
  22.      * @param personName Name of the person.
  23.      * @param flight Flight on witch the person has booked.
  24.      * @param bookingNumber used to {@link  reservation.system.functions.Identify} a group of person
  25.      * @param pos The position of the seat on the <CODE>flight</CODE>
  26.      * @throws Exception if the invariant _check() is violated
  27.      */
  28.     Person (
  29.     String personName,
  30.     Flight flight,
  31.     int bookingNumber,
  32.     Pos pos) throws Exception {
  33.         this.personName = personName;
  34.         this.flight = flight;
  35.         this.bookingNumber = new Integer (bookingNumber);
  36.         try {
  37.             this.pos = (Pos) pos.clone ();
  38.         } catch (CloneNotSupportedException e) {System.err.println (e);}
  39.         _check ();
  40.     }
  41.    
  42.     /**
  43.      * Set the name of the person
  44.      * @param personName name of the person
  45.      * @throws Exception if the invariant _check() is violated
  46.      */
  47.     void setPersonName (String personName) throws Exception {
  48.         this.personName = personName;
  49.         _check ();
  50.     }
  51.    
  52.     /**
  53.      * Set the flight
  54.      * @param flight Flight on witch the person has booked.
  55.      * @throws Exception if the invariant _check() is violated
  56.      */
  57.     void setFlight (Flight flight) throws Exception {
  58.         this.flight = (Flight) flight.clone ();
  59.         _check ();
  60.     }
  61.    
  62.     /**
  63.      * Set the booking number
  64.      * @param bookingNumber the booking number
  65.      * @throws Exception if the invariant _check() is violated
  66.      */
  67.     void setBookingNumber (int bookingNumber) throws Exception {
  68.         this.bookingNumber = new Integer (bookingNumber);
  69.         _check ();
  70.     }
  71.    
  72.     /**
  73.      * Set the position
  74.      * @param pos The position of the seat on the <CODE>flight</CODE>
  75.      * @throws Exception if the invariant _check() is violated
  76.      */
  77.     void setPos (Pos pos) throws Exception {
  78.         this.pos = (Pos) pos.clone ();
  79.         _check ();
  80.     }
  81.    
  82.     /**
  83.      * Return the name of the person
  84.      * @return the name of the person
  85.      * @throws Exception if the invariant _check() is violated
  86.      */
  87.     final public String getPersonName () throws Exception {
  88.         return personName;
  89.     }
  90.    
  91.     /**
  92.      * Return the flight
  93.      * @return the flight on witch the person has booked.
  94.      * @throws Exception if the invariant _check() is violated
  95.      */
  96.     final public Flight getFlight () throws Exception {
  97.         if (flight == null)
  98.             return null;
  99.         else
  100.             return (Flight) flight.clone ();
  101.     }
  102.    
  103.     /**
  104.      * Return the booking number
  105.      * @return the booking number
  106.      * @throws Exception if the invariant _check() is violated
  107.      */
  108.     final public int getBookingNumber ()  throws Exception {
  109.         if (bookingNumber == null)
  110.             return 0;
  111.         return bookingNumber.intValue ();
  112.     }
  113.    
  114.     /**
  115.      * Return the position of the seat
  116.      * @return pos the position of the seat in the <CODE>Flight</CODE>
  117.      * @throws Exception if the invariant _check() is violated
  118.      */
  119.     final public Pos getPos () throws Exception {
  120.         if (pos == null)
  121.             return null;
  122.         else
  123.             return (Pos) pos.clone ();
  124.     }
  125.    
  126.     /**
  127.      * Returns a string representation of this <CODE>Person</CODE> object. This method
  128.      * is intended to be used only for debugging purposes, and the content and format
  129.      * of the returned string may vary between implementations. The returned string may
  130.      * be empty but may not be <CODE>null</CODE>
  131.      * @return a string representation of this <CODE>Person</CODE> object.
  132.      */
  133.     public String toString () {
  134.         return personName +" has booked on "+ flight +" the seat "+ pos +" under the booking number : "+ bookingNumber + "\r\n";
  135.     }
  136.    
  137.     /**
  138.      * Overide equals
  139.      * @return <CODE>true</CODE> if this object is the same as the obj argument or match a {@link  reservation.system.Profile} object;
  140.      * <CODE>false</CODE> otherwise
  141.      * @param anObject <CODE>anObject</CODE> - the reference object with which to compare
  142.      */
  143.     public boolean equals (Object anObject) {
  144.         if (anObject != null && anObject instanceof Profile) {
  145.             boolean bPersonName = false;
  146.             boolean bBookingNumber = false;
  147.             boolean bFlightName = false;
  148.             boolean bPos = false;
  149.             Profile profile = (Profile) anObject;
  150.             try {
  151.                 if (personName.equals (profile.getPersonName ()) || profile.personNameIsMask ())
  152.                     bPersonName = true;
  153.                 if (bookingNumber.intValue () == profile.getBookingNumber () || profile.bookingNumberIsMask ())
  154.                     bBookingNumber = true;
  155.                 if (flight.equals (profile.getFlight ()) || profile.flightIsMask ())
  156.                     bFlightName = true;
  157.                 if (pos.equals (profile.getPos ()) || profile.posIsMask ())
  158.                     bPos = true;
  159.             } catch (Exception  e){System.err.println (e);}
  160.             return bPersonName && bBookingNumber && bFlightName && bPos;
  161.         }
  162.         else if (anObject != null && anObject instanceof Person) {
  163.             Person person = (Person) anObject;
  164.             return (flight.equals (person.flight)
  165.             && (personName == person.personName)
  166.             && (bookingNumber == person.bookingNumber)
  167.             && (pos.equals (person.pos)));
  168.         }
  169.         else
  170.             return false;
  171.     }
  172.    
  173.     /**
  174.      * Verify invariants :
  175.      <PRE>
  176.      - <CODE>regex</CODE> ^[A-Z][a-z]*
  177.      - seat < dimension of the flight and > 0
  178.      </PRE>
  179.      * @throws NullPointerException if pos or name is null
  180.      * @throws Exception if the invariant is violated
  181.      */
  182.     public void _check () throws Exception, NullPointerException {
  183.         if (flight == null || pos == null || personName == null ||  bookingNumber == null)
  184.             throw new NullPointerException ("The person "+ this +" has a null value.") ;
  185.        
  186.         if (bookingNumber.intValue () < 1 || bookingNumber.intValue () > Integer.MAX_VALUE
  187.         || pos.getRow () < 1 || pos.getRow () > flight.getDimension ().getRow ()
  188.         || pos.getCol () < 1 || pos.getCol () > flight.getDimension ().getCol ()) {
  189.             throw new Exception ("The person "+ this +" has an out of range value.");
  190.         }
  191.        
  192.         if (!personName.matches ("^[A-Z][a-z]*") || personName.equals (""))
  193.             throw new Exception ("The name : "+ personName +" has illegal character(s)\r\n" +
  194.             "Remember that the first letter of a name has to be in upper case and the rest in lower case " +
  195.             "and that no number or special characters are allowed");
  196.     }
  197.    
  198.     /**
  199.      * Creates and returns a copy of this object.
  200.      * @return a copy of this <CODE>Person</CODE> object
  201.      * @throws CloneNotSupportedException if the object's class does not support the <CODE>Cloneable</CODE> interface.
  202.      */
  203.     protected Object clone () throws CloneNotSupportedException {
  204.         Person retPerson = null;
  205.         try {
  206.             retPerson = new Person (personName, flight, bookingNumber.intValue (), (Pos) pos.clone ());
  207.         } catch (Exception e){System.err.println (e);}
  208.         return retPerson;
  209.     }
  210.    
  211.     private Flight flight = null;
  212.     private String personName = null;
  213.     private Integer bookingNumber = null;
  214.     private Pos pos = null;
  215. }

contact - faire un lien