/*
* FlightSystem.java
* The package system contains the different class allowing to create and to select the object to store in the database.
*/
package reservation.system;
import java.util.LinkedList;
import java.util.ListIterator;
import java.util.Collection;
import java.util.Vector;
import java.util.HashSet;
import java.util.Set;
import java.util.Iterator;
import java.io.Serializable;
/**
* The class hold the structure to access in the database.
* This object allows to modify the database with the different command available.
* It contains all the method executing all the command written by the user.
* @author Frederic Bidon and Mathieu Texier
*/
/**
* Constructor of the class FlightSystem.
* @return the variable fs that is instantiated to FlightSystem object.
*/
final static public FlightSystem getInstance () {
return fs;
}
/**
* Method which allows to add a new flight in a database.
* @throws Exception if the flight already exists.
* @return a boolean that confirm the creation of a new flight in the flight list.
* @param flightName The name of the flight
* @param rows the number of rows
* @param rowLength the length of each rows
*/
public final synchronized boolean create
(String flightName,
short rows,
short rowLength
) throws Exception {
if (flightList.select (flightName) != null)
throw new Exception ("The flight name "+ flightName
+" has already been inserted in the flight list.");
return flightList.add (new Flight (flightName, new Pos (rows, rowLength)));
}
/**
* Assert a new flight into the FlightList
* @throws Exception if the flight doesn't exist.
* @return true if the flight exists.
* @param flight The Flight that will be asserted
*/
final public synchronized boolean create
(Flight flight
) throws Exception {
return create (
flight.getFlightName (),
flight.getDimension ().getRow (),
flight.getDimension ().getCol ()
);
}
/**
* Method which allows to add one or more persons in a database.
* @throws Exception if the flight doen't exist or if the command contains no personName.
* @return the number of reservation(bookingNumber) for the group of person(s) registered.
* @param flightName The name of the flight
* @param personNames A list of person
*/
final public synchronized int reserve
(String flightName,
Set personNames
) throws Exception {
Flight flight = flightList.select (flightName);
if (flight == null)
throw new Exception ("The flight "+ flightName
+" has not been yet created.");
if (personNames == null)
throw new Exception ("the set of person is null");
Profile profile = new Profile ();
profile.setBookingNumber (++bookingNumber);
while (it.hasNext ()) {
profile.
setPersonName ( (String) it.
next ());
if (personList.contains (profile))
throw new Exception ("The list contain duplicate Name");
}
Pos posList[] = getFreePlace (flight, (short)personNames.size ());
int i = 0;
boolean success = true;
it = personNames.iterator ();
while (it.hasNext ()) {
success
&= personList.
add (new Person
((String) it.
next (), flight, bookingNumber, posList
[i
++]));
}
if (success)
return bookingNumber;
else return -1;
}
/**
* Method which allows to add one person to a database. The input is read from a file.
* @throws Exception if the flight doen't exist or if the command contains no personName.
* @return 1 if the person is added to a database or 0 if it is not added.
* @param person assert a Person Object
*/
final public synchronized int reserve
(Person person
) throws Exception {
bookingNumber ++;
return personList.add(person) ? 1 : 0;
}
/**
* Method which allows to cancel one or more personName that have reserved.
* @throws Exception if one or more name are invalid.
* @return true if the person(s) are canceled.
* @param bookingNumber the booking number with which the person have booked
* @param personNames The name of the person that wnat to cancel
*/
final public synchronized boolean cancel
(int bookingNumber,
Set personNames
) throws Exception {
Profile profile = new Profile ();
profile.setBookingNumber (bookingNumber);
while (it.hasNext ()) {
profile.
setPersonName ((String) it.
next ());
if (!personList.
contains ((Object) profile
))
throw new Exception ("One or more name on your person's list are invalid.");
else listToBeRemoved.add(personList.select(profile).get(0));
}
return personList.removeAll (listToBeRemoved);
}
/**
* Method which allows to cancel a group of persons which have the same bookingNumber.
* @throws Exception if the bookingNumber doesn't exist.
* @return true if the group of person is canceled.
* @param bookingNumber The booking number to be canceled
*/
final public synchronized boolean cancel
(int bookingNumber
) throws Exception {
return personList.removeAll (identify (bookingNumber));
}
/**
* Method which allows to see the personName which have the same bookingNumber.
* @throws Exception if the bookingNumber doesn't exist.
* @return the personName who have the same bookingNumber.
* @param bookingNumber the booking number to be identify
*/
final public synchronized Vector identify
(int bookingNumber
) throws Exception{
Profile profile = new Profile ();
profile.setBookingNumber (bookingNumber);
return new Vector (personList.
select (profile
));
}
/**
* Method which allows to see the personName present in a same flight.
* @throws Exception if the flightName doesn't exist.
* @return the personName present in a flight.
* @param flightName the name of the flight
*/
Flight flight = flightList.select (flightName);
if (flight == null)
throw new Exception ("The flight " +flightName
+" doesn't exist");
Profile profile = new Profile ();
profile.setFlight (flight);
return new Vector (personList.
select (profile
));
}
/**
* Returns a string representation of this <CODE>FlightSystem</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>FlightSystem</CODE> object.
*/
final public synchronized String toString
() {
return flightList.toString () + personList.toString ();
}
/**
* Method which allows to search a person in the person's list.
* @throws Exception if the invariants is violated
* @return the list of persons found.
* @param profile The person that will match this profile will be returned
*/
final public synchronized Vector search
(Profile profile
) throws Exception {
return new Vector (personList.
select (profile
));
}
/**
* Method which allows to select a flight in the flight's list according to the flightName.
* @throws Exception if the flightName doesn't exist.
* @return the flight selected if it exists.
* @param flightName the name of the flight
*/
final public synchronized Flight selectFlight
(String flightName
) throws Exception {
return flightList.select (flightName);
}
/**
* Accessor for the bookingNumberMax
* @return the number max of reservation on a flight.
*/
final public synchronized int getBookingNumberMax () {
return bookingNumber;
}
/**
* Method which allows to return the flights contained in the flights' list.
* @throws Exception if the invariants are violated.
* @return the list of flight(s) created.
*/
return flightList.getFlightList ();
}
/**
* Method which allows to return the list of the atribuated bookingNumber.
* @throws Exception if the invariants is violated.
* @return the list of bookingNumber(s) present in the database.
*/
return personList.getBookingtList ();
}
static final private FlightSystem fs = new FlightSystem ();
private int bookingNumber = 0;
private FlightList flightList;
private PersonList personList;
private FlightSystem () {
flightList = new FlightSystem.FlightList ();
personList = new FlightSystem.PersonList ();
}
/**
* Method which allows to atribute a place to a person.
* @throws Exception if the the flight doesn't exist.
* @return the position atribuated to a person.
*/
private Pos
[] getFreePlace
(Flight flight,
short number
) throws Exception {
Pos posList[] = new Pos[number];
short rest = number;
short count = 0;
Pos dimension = flight.getDimension ();
for (short row = 1; row <= dimension.getRow () && rest > 0; row ++) {
for (short col = 1; col <= dimension.getCol () && rest > 0; col++) {
Pos seat = new Pos (row,col);
Profile profile = new Profile ();
profile.setFlight (flight);
profile.setPos (seat);
if (!personList.contains (profile)) {
posList[count++] = seat;
rest --;
}
}
}
if (posList.length != number)
throw new Exception ("The place are not sucsesfully atribuated");
return posList;
}
/**
* The class hold the structure to access to the flights registered in the database.
* This object allows to modify the list of flight in the database.
* It contains all the methods accessing and altering the FlightList.
* @author Frederic Bidon and Mathieu Texier
*/
/**Constructor of the class*/
private FlightList () {
super ();
}
/**
* This method allows to obtain the flight(s) chosen.
* @throws Exception if the flight doesn't exist.
* @return a string containing the flight selected.
*/
while (it.hasNext () && flightName != null) {
Flight flight = (Flight) it.next ();
if (flightName.equals (flight.getFlightName ())) {
flight._check ();
return (Flight) flight.clone ();
}
}
return null;
}
/**
* Returns a string representation of this <CODE>FlightList</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>FlightList</CODE> object.
*/
String resultList
= "Flight List: " +"\r\n";
while (it.hasNext ()) {
resultList += "\t"+ (it.next ()).toString () +"\r\n";
}
return resultList;
}
/**
* This method allows to add a flight in the database.
* @throws an exception if the flight cannot be added.
* @return the flight added in the database.
*/
public boolean add
(Object o
) {
boolean retValue;
if (o != null && o instanceof Flight) {
try {
((Flight) o)._check ();
if (retValue = true)
retValue = super.add (o);
} else retValue = false;
return retValue;
}
/**
* This method allows to obtain the list of the flight(s) created.
* @throws Exception if the database contains no flight.
* @return an array of string containing the list of flight(s) created.
*/
for (int i=0; it.hasNext (); i++) {
Flight flight = (Flight) it.next ();
flight._check ();
resultList[i] = flight.getFlightName ();
}
return resultList;
}
}
/**
* The class hold the structure to access to the persons registered in the database.
* This object allows to modify the list of persons in the database.
* It contains all the methods accessing and altering the PersonList.
* @author Frederic Bidon and Mathieu Texier
*/
/**Constructor of the class*/
private PersonList () {
super ();
}
/**
* This method allows to obtain the person or the group of persons found in the PersonList.
* @throws Exception if the person or the group of persons doesn't exist.
* @return a string containing the person(s) selected.
*/
public PersonList select
(Profile profile
) throws Exception{
PersonList groupePersonList = new PersonList ();
while (it.hasNext ()) {
Person person = (Person) it.next ();
if (person.equals (profile)) {
person._check ();
if (groupePersonList.add (person) == false)
throw new Exception (person
+ " is not succesfully inserted in " +groupePersonList
) ;
}
}
return groupePersonList;
}
/**
* Returns a string representation of this <CODE>PersonList</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>PersonList</CODE> object.
*/
String resultList
= "Person List: " +"\r\n";
while (it.hasNext ()) {
resultList += "\t"+ (it.next ()).toString () +"\r\n";
}
return resultList;
}
/**
* This method allows to add a person or a group of persons in the database.
* @throws an exception if the person or the group of persons cannot be added.
* @return the group of person added on a flight.
*/
public boolean add
(Object o
) {
boolean retValue;
if (o != null && o instanceof Person && !(o instanceof Profile)) {
try {
((Person) o)._check ();
if (retValue = true)
retValue = super.add (o);
} else retValue = false;
return retValue;
}
/**
* This method allows to remove all the persons belong to a same booking number.
* @return the collection of the persons removed.
*/
boolean retValue;
if (!c.isEmpty ()) {
retValue = super.removeAll (c);
}
else retValue = false;
return retValue;
}
/**
* This method allows to remove one or more persons belong to a same booking number.
* @return the object of the persons removed.
*/
public boolean remove
(Object o
) {
boolean retValue;
if (o != null && o instanceof Person) {
try {
((Person) o)._check ();
if (retValue = true)
retValue = super.remove (o);
}
else retValue = false;
return retValue;
}
/**
* This method allows to obtain the list of the booking number.
* @throws Exception if the database contains no persons.
* @return an array of integer containing the list of the booking number atribuated.
*/
while (it.hasNext ()) {
Person person = (Person) it.next ();
person._check ();
set.
add (new Integer (person.
getBookingNumber ()));
}
}
}
}