/*
* Profile.java
* The package system contains the different class allowing to create and to select the object to store in the database.
*/
package reservation.system;
/**
* The overall idea of this class is to provide a method of comparison between a person and an incomplete Person structure. In another way, a class such as
*
* Person
*
* <PRE>
* - flight:Flight = aFlight
* - personName:String = aName
* - bookingNumber: Integer = aBooking
* - pos: Pos = aSeat
* </PRE>
*
* equals
*
* Profile
* <PRE>
* - flight:Flight = aFlight
* - personNameMask:boolean = true
* - bookingNumberMask:boolean = true
* - posMask:boolean = true
* </PRE>
*
* @author Texier Mathieu and Frederic Bidon
*/
public class Profile extends Person {
/**
* Construct an empty profile
*/
public Profile (){
}
/**
* Set the name of the person and unmask the value
* @param personName name of the person
* @throws Exception if the invariant _check() is violated
* @throws NullPointerException if personName is null
*/
if (personName == null)
super.setPersonName (personName);
personNameMask = false;
_check ();
}
/**
* Set the flight and unmask the value
* @param flight Flight on witch the person has booked.
* @throws Exception if the invariant _check() is violated
* @throws NullPointerException if flight is null
*/
if (flight == null)
super.setFlight (flight);
flightMask = false;
_check ();
}
/**
* Set the booking number and unmask the value
* @param bookingNumber the booking number
* @throws Exception if the invariant _check() is violated
*/
public void setBookingNumber
(int bookingNumber
) throws Exception {
if (bookingNumber == 0)
throw new Exception ("Try to set a mask with the value 0 for booking number");
super.setBookingNumber (bookingNumber);
bookingNumberMask = false;
_check ();
}
/**
* Set the position and unmask the value
* @param pos The position of the seat on the <CODE>flight</CODE>
* @throws Exception if the invariant _check() is violated
* @throws NullPointerException if pos is null
*/
if (pos == null)
super.setPos (pos);
posMask = false;
_check ();
}
/**
* Return true if <CODE>personName</CODE> is masked
* @return the value of the mask on personName
*/
public boolean personNameIsMask () {
return personNameMask;
}
/**
* Return true if <CODE>flight</CODE> is masked
* @return the value of the mask on flight
*/
public boolean flightIsMask () {
return flightMask;
}
/**
* Return true if <CODE>bookingNumber</CODE> is masked
* @return the value of the mask on bookingNumber
*/
public boolean bookingNumberIsMask () {
return bookingNumberMask;
}
/**
* Return true if <CODE>pos</CODE> is masked
* @return the value of the mask on pos
*/
public boolean posIsMask () {
return posMask;
}
/**
* Verify invariants :
<PRE>
- either the mask is true or the object = null
</PRE>
* @throws Exception if the invariant is violated
*/
String errorStr
= "is not masked and have null value";
if (!personNameMask && getPersonName () == null) {
throw new Exception ("Person name" + errorStr
) ;
}
if (!flightMask && getFlight () == null) {
throw new Exception ("Flight name" + errorStr
) ;
}
if (!bookingNumberMask && getBookingNumber () == 0){
throw new Exception ("Booking number" + errorStr
) ;
}
if (!posMask && getPos () == null) {
throw new Exception ("Dimension" + errorStr
) ;
}
}
/**
* Overide equals
* return true if for all fields:
* person.field. == profile.field || profile.fieldIsMask
*
* Where the fields to test are: the name, the flight, the booking number, the seat.
*
* The method respects the following conditions: Reflexivity, symmetry, consistency, non-nullity. However, the transitivity in not respected:
*
* <PRE>
* Profile A = new Profile();
* A.setpersonName(“Person”)
* Profile B() = new Profile();
* B.setBookingNumber(1);
* Person P(personName=“Person”, bookingNumber = 1,…);
* A.equals(P); // true
* B.equals(P); // true
* A.equals(B); // false
* </PRE>
*
* That is why this function of comparison was first named match (). Although the previous properties were not all respected the name of this method have been changed to equals(). The advantage to override this method is that the {@link import java.util.List} interface, called the equals() methods to identify the object on the following functions:
* <PRE>
* • boolean contains(Object o)
* • int indexOf(Object o)
* • boolean remove(Object o)
* </PRE>
* If Object is a Profile, then these methods means respectively: the list match a pattern, return or remove the first element matching a pattern.
* @return <CODE>true</CODE> if this object is the same as the obj argument or match a {@link reservation.system.Person} object;
* <CODE>false</CODE> otherwise
* @param anObject <CODE>anObject</CODE> - the reference object with which to compare
*/
public boolean equals
(Object anObject
) {
if (anObject != null && anObject instanceof Person) {
Person person = (Person) anObject;
return person.equals (this);
}
else if (anObject != null && anObject instanceof Profile) {
Profile profile = (Profile) anObject;
return (personNameMask == profile.personNameMask)
&& (flightMask == profile.flightMask)
&& (bookingNumberMask == profile.bookingNumberMask)
&& (posMask == profile.posMask)
&& super.equals ( (Person) profile);
}
return false;
}
/**
* Returns a string representation of this <CODE>Flight</CODE> object. This method
* is intended to be used only for debugging purposes, and the content and format
* of the returned string may vary between implementations. The returned string may
* be empty but may not be <CODE>null</CODE>
* @return a string representation of this <CODE>Profile</CODE> object.
*/
String retString
= "Profile : \r\n";
try {
retString += "personName " + (!personNameMask ? getPersonName ():" masked ");
retString += "flight " + (!flightMask ? getFlight ().toString ():" masked ");
retString += "pos " + (!posMask ? getPos ().toString ():" masked ");
retString += "bookingNumber " + (!bookingNumberMask ? getBookingNumber ()+"" :" masked ")+"\r\n";
return retString;
}
private boolean personNameMask = true;
private boolean flightMask = true;
private boolean bookingNumberMask = true;
private boolean posMask = true;
}