📄 flightsystem.java
字号:
/* * 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 */public final class FlightSystem extends Object implements Serializable { /** * 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); Iterator it = personNames.iterator (); 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 { Iterator it = personNames.iterator (); Profile profile = new Profile (); profile.setBookingNumber (bookingNumber); Collection listToBeRemoved= new LinkedList(); 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 */ final public synchronized Vector list (String flightName) throws Exception { 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. */ final public synchronized String [] getFlightList () throws Exception { 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. */ final public synchronized Integer [] getBookingList () throws Exception { return personList.getBookingtList (); } static final private FlightSystem fs = new FlightSystem (); private int bookingNumber = 0; private FlightList flightList;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -