AirInv Logo  0.1.2
C++ Simulated Airline Inventory Management System library
FlightDateStruct.cpp
Go to the documentation of this file.
00001 // //////////////////////////////////////////////////////////////////////
00002 // Import section
00003 // //////////////////////////////////////////////////////////////////////
00004 // STL
00005 #include <cassert>
00006 #include <sstream>
00007 // StdAir
00008 #include <stdair/basic/BasConst_General.hpp>
00009 #include <stdair/service/Logger.hpp>
00010 // AIRINV
00011 #include <airinv/AIRINV_Types.hpp>
00012 #include <airinv/bom/FlightDateStruct.hpp>
00013 
00014 namespace AIRINV {
00015 
00016   // //////////////////////////////////////////////////////////////////////
00017   FlightDateStruct::FlightDateStruct ()
00018     : _flightDate (stdair::DEFAULT_DATE),
00019       _flightTypeCode (FlightTypeCode::DOMESTIC),
00020       _flightVisibilityCode (FlightVisibilityCode::NORMAL),
00021       _itSeconds (0), _legAlreadyDefined (false) {
00022   }
00023 
00024   // //////////////////////////////////////////////////////////////////////
00025   stdair::Date_T FlightDateStruct::getDate() const {
00026     return stdair::Date_T (_itYear + 2000, _itMonth, _itDay);
00027   }
00028 
00029   // //////////////////////////////////////////////////////////////////////
00030   stdair::Duration_T FlightDateStruct::getTime() const {
00031     return boost::posix_time::hours (_itHours)
00032       + boost::posix_time::minutes (_itMinutes)
00033       + boost::posix_time::seconds (_itSeconds);
00034   }
00035   
00036   // //////////////////////////////////////////////////////////////////////
00037   const std::string FlightDateStruct::describe() const {
00038     std::ostringstream ostr;
00039     ostr << _airlineCode << _flightNumber << ", " << _flightDate
00040          << " (" << _flightTypeCode;
00041     if (_flightVisibilityCode.getCode() != FlightVisibilityCode::NORMAL) {
00042       ostr << "/" << _flightVisibilityCode;
00043     }
00044     ostr << ")" << std::endl;
00045       
00046     for (LegStructList_T::const_iterator itLeg = _legList.begin();
00047          itLeg != _legList.end(); ++itLeg) {
00048       const LegStruct& lLeg = *itLeg;
00049       ostr << lLeg.describe();
00050     }
00051 
00052     for (SegmentStructList_T::const_iterator itSegment = _segmentList.begin();
00053          itSegment != _segmentList.end(); ++itSegment) {
00054       const SegmentStruct& lSegment = *itSegment;
00055       ostr << lSegment.describe();
00056     }
00057 
00058     //ostr << "[Debug] - Staging Leg: ";
00059     //ostr << _itLeg.describe();
00060     //ostr << "[Debug] - Staging Cabin: ";
00061     //ostr << _itCabin.describe();
00062 
00063     return ostr.str();
00064   }
00065 
00066   // //////////////////////////////////////////////////////////////////////
00067   void FlightDateStruct::addAirport (const stdair::AirportCode_T& iAirport) {
00068     AirportList_T::const_iterator itAirport = _airportList.find (iAirport);
00069     if (itAirport == _airportList.end()) {
00070       // Add the airport code to the airport set
00071       const bool insertSuccessful = _airportList.insert (iAirport).second;
00072 
00073       if (insertSuccessful == false) {
00074         // TODO: throw an exception
00075       }
00076           
00077       // Add the airport code to the airport vector
00078       _airportOrderedList.push_back (iAirport);
00079     }
00080   }
00081 
00082   // //////////////////////////////////////////////////////////////////////
00083   void FlightDateStruct::buildSegments () {
00084     // The list of airports encompasses all the airports on which
00085     // the flight takes off or lands. Moreover, that list is
00086     // time-ordered: the first airport is the initial departure of
00087     // the flight, and the last airport is the eventual point of
00088     // rest of the flight.
00089     // Be l the size of the ordered list of airports.
00090     // We want to generate all the segment combinations from the legs
00091     // and, hence, from all the possible (time-ordered) airport pairs.
00092     // Thus, we both iterator on i=0...l-1 and j=i+1...l
00093     assert (_airportOrderedList.size() >= 2);
00094 
00095     _segmentList.clear();
00096     for (AirportOrderedList_T::const_iterator itAirport_i =
00097            _airportOrderedList.begin();
00098          itAirport_i != _airportOrderedList.end()-1; ++itAirport_i) {
00099       for (AirportOrderedList_T::const_iterator itAirport_j = itAirport_i + 1;
00100            itAirport_j != _airportOrderedList.end(); ++itAirport_j) {
00101         SegmentStruct lSegmentStruct;
00102         lSegmentStruct._boardingPoint = *itAirport_i;
00103         lSegmentStruct._offPoint = *itAirport_j;
00104           
00105         _segmentList.push_back (lSegmentStruct);
00106       }
00107     }
00108 
00109     // Clear the lists of airports, so that it is ready for the next flight
00110     _airportList.clear();
00111     _airportOrderedList.clear();
00112   }
00113       
00114   // //////////////////////////////////////////////////////////////////////
00115   void FlightDateStruct::
00116   addSegmentCabin (const SegmentStruct& iSegment,
00117                    const SegmentCabinStruct& iCabin) {
00118     // Retrieve the Segment structure corresponding to the (boarding, off) point
00119     // pair.
00120     SegmentStructList_T::iterator itSegment = _segmentList.begin();
00121     for ( ; itSegment != _segmentList.end(); ++itSegment) {
00122       const SegmentStruct& lSegment = *itSegment;
00123 
00124       const stdair::AirportCode_T& lBoardingPoint = iSegment._boardingPoint;
00125       const stdair::AirportCode_T& lOffPoint = iSegment._offPoint;
00126       if (lSegment._boardingPoint == lBoardingPoint
00127           && lSegment._offPoint == lOffPoint) {
00128         break;
00129       }
00130     }
00131 
00132     // If the segment key (airport pair) given in the schedule input file
00133     // does not correspond to the leg (boarding, off) points, throw an exception
00134     // so that the user knows the schedule input file is corrupted.
00135     if (itSegment == _segmentList.end()) {
00136       STDAIR_LOG_ERROR ("Within the inventory input file, there is a "
00137                         << "flight for which the airports of segments "
00138                         << "and those of the legs do not correspond.");
00139       throw SegmentDateNotFoundException ("Within the inventory input file, "
00140                                           "there is a flight for which the "
00141                                           "airports of segments and those of "
00142                                           "the legs do not correspond.");
00143     }
00144 
00145     // Add the Cabin structure to the Segment Cabin structure.
00146     assert (itSegment != _segmentList.end());
00147     SegmentStruct& lSegment = *itSegment;
00148     lSegment._cabinList.push_back (iCabin);
00149   }
00150     
00151   // //////////////////////////////////////////////////////////////////////
00152   void FlightDateStruct::
00153   addSegmentCabin (const SegmentCabinStruct& iCabin) {
00154     // Iterate on all the Segment structures (as they get the same cabin
00155     // definitions)
00156       
00157     for (SegmentStructList_T::iterator itSegment = _segmentList.begin();
00158          itSegment != _segmentList.end(); ++itSegment) {
00159       SegmentStruct& lSegment = *itSegment;
00160 
00161       lSegment._cabinList.push_back (iCabin);
00162     }
00163   }
00164 
00165   // //////////////////////////////////////////////////////////////////////
00166   void FlightDateStruct::
00167   addFareFamily (const SegmentStruct& iSegment,
00168                  const SegmentCabinStruct& iCabin,
00169                  const FareFamilyStruct& iFareFamily) {
00170     // Retrieve the Segment structure corresponding to the (boarding, off) point
00171     // pair.
00172     SegmentStructList_T::iterator itSegment = _segmentList.begin();
00173     for ( ; itSegment != _segmentList.end(); ++itSegment) {
00174       const SegmentStruct& lSegment = *itSegment;
00175 
00176       const stdair::AirportCode_T& lBoardingPoint = iSegment._boardingPoint;
00177       const stdair::AirportCode_T& lOffPoint = iSegment._offPoint;
00178       if (lSegment._boardingPoint == lBoardingPoint
00179           && lSegment._offPoint == lOffPoint) {
00180         break;
00181       }
00182     }
00183 
00184     // If the segment key (airport pair) given in the schedule input file
00185     // does not correspond to the leg (boarding, off) points, throw an exception
00186     // so that the user knows the schedule input file is corrupted.
00187     if (itSegment == _segmentList.end()) {
00188       STDAIR_LOG_ERROR ("Within the schedule input file, there is a flight "
00189                         << "for which the airports of segments and "
00190                         << "those of the legs do not correspond.");
00191       throw SegmentDateNotFoundException ("Within the schedule input file, "
00192                                           "there is a flight for which the "
00193                                           "airports of segments and those of "
00194                                           "the legs do not correspond.");
00195     }
00196 
00197     // Add the Cabin structure to the Segment Cabin structure.
00198     assert (itSegment != _segmentList.end());
00199     SegmentStruct& lSegment = *itSegment;
00200 
00201     // Retrieve the Segment cabin structure given the cabin code
00202     SegmentCabinStructList_T::iterator itCabin = lSegment._cabinList.begin();
00203     for ( ; itCabin != lSegment._cabinList.end(); ++itCabin) {
00204       const SegmentCabinStruct& lCabin = *itCabin;
00205 
00206       const stdair::CabinCode_T& lCabinCode = lCabin._cabinCode;
00207       if (iCabin._cabinCode == lCabinCode) {
00208         break;
00209       }
00210     }
00211 
00212     // If the segmentCabin key (cabin code) given in the schedule input file
00213     // does not correspond to the stored cabin codes, throw an exception
00214     // so that the user knows the schedule input file is corrupted.
00215     if (itCabin == lSegment._cabinList.end()) {
00216       STDAIR_LOG_ERROR ("Within the schedule input file, there is a flight "
00217                         << "for which the cabin code does not exist.");
00218       throw SegmentDateNotFoundException ("Within the schedule input file, "
00219                                           "there is a flight for which the "
00220                                           "cabin code does not exist.");
00221     }
00222 
00223     // Add the Cabin structure to the Segment Cabin structure.
00224     assert (itCabin != lSegment._cabinList.end());
00225     SegmentCabinStruct& lCabin = *itCabin;
00226     lCabin._fareFamilies.push_back (iFareFamily);
00227   }
00228     
00229   // //////////////////////////////////////////////////////////////////////
00230   void FlightDateStruct::
00231   addFareFamily (const SegmentCabinStruct& iCabin,
00232                  const FareFamilyStruct& iFareFamily) {
00233     // Iterate on all the Segment structures (as they get the same cabin
00234     // definitions)
00235       
00236     for (SegmentStructList_T::iterator itSegment = _segmentList.begin();
00237          itSegment != _segmentList.end(); ++itSegment) {
00238       SegmentStruct& lSegment = *itSegment;
00239 
00240       // Retrieve the Segment cabin structure given the cabin code
00241       SegmentCabinStructList_T::iterator itCabin = lSegment._cabinList.begin();
00242       for ( ; itCabin != lSegment._cabinList.end(); ++itCabin) {
00243         const SegmentCabinStruct& lCabin = *itCabin;
00244 
00245         const stdair::CabinCode_T& lCabinCode = lCabin._cabinCode;
00246         if (iCabin._cabinCode == lCabinCode) {
00247           break;
00248         }
00249       }
00250 
00251       // If the segmentCabin key (cabin code) given in the schedule input file
00252       // does not correspond to the stored cabin codes, throw an exception
00253       // so that the user knows the schedule input file is corrupted.
00254       if (itCabin == lSegment._cabinList.end()) {
00255         STDAIR_LOG_ERROR ("Within the schedule input file, there is a flight"
00256                           << " for which the cabin code does not exist.");
00257         throw SegmentDateNotFoundException ("Within the schedule input file, "
00258                                             "there is a flight for which the "
00259                                             "cabin code does not exist.");
00260       }
00261 
00262       // Add the Cabin structure to the Segment Cabin structure.
00263       assert (itCabin != lSegment._cabinList.end());
00264       SegmentCabinStruct& lCabin = *itCabin;
00265       lCabin._fareFamilies.push_back (iFareFamily);
00266     }
00267   }
00268 
00269 }