AirInv Logo  1.00.0
C++ Simulated Airline Inventory Management System library
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
FlightDateStruct.cpp
Go to the documentation of this file.
1 // //////////////////////////////////////////////////////////////////////
2 // Import section
3 // //////////////////////////////////////////////////////////////////////
4 // STL
5 #include <cassert>
6 #include <sstream>
7 // StdAir
8 #include <stdair/basic/BasConst_General.hpp>
9 #include <stdair/service/Logger.hpp>
10 // AIRINV
11 #include <airinv/AIRINV_Types.hpp>
13 
14 namespace AIRINV {
15 
16  // //////////////////////////////////////////////////////////////////////
18  : _flightDate (stdair::DEFAULT_DATE),
19  _flightTypeCode (FlightTypeCode::DOMESTIC),
20  _flightVisibilityCode (FlightVisibilityCode::NORMAL),
21  _itSeconds (0), _legAlreadyDefined (false) {
22  }
23 
24  // //////////////////////////////////////////////////////////////////////
25  stdair::Date_T FlightDateStruct::getDate() const {
26  return stdair::Date_T (_itYear + 2000, _itMonth, _itDay);
27  }
28 
29  // //////////////////////////////////////////////////////////////////////
30  stdair::Duration_T FlightDateStruct::getTime() const {
31  return boost::posix_time::hours (_itHours)
32  + boost::posix_time::minutes (_itMinutes)
33  + boost::posix_time::seconds (_itSeconds);
34  }
35 
36  // //////////////////////////////////////////////////////////////////////
37  const std::string FlightDateStruct::describe() const {
38  std::ostringstream ostr;
39  ostr << _airlineCode << _flightNumber << ", " << _flightDate
40  << " (" << _flightTypeCode;
42  ostr << "/" << _flightVisibilityCode;
43  }
44  ostr << ")" << std::endl;
45 
46  for (LegStructList_T::const_iterator itLeg = _legList.begin();
47  itLeg != _legList.end(); ++itLeg) {
48  const LegStruct& lLeg = *itLeg;
49  ostr << lLeg.describe();
50  }
51 
52  for (SegmentStructList_T::const_iterator itSegment = _segmentList.begin();
53  itSegment != _segmentList.end(); ++itSegment) {
54  const SegmentStruct& lSegment = *itSegment;
55  ostr << lSegment.describe();
56  }
57 
58  //ostr << "[Debug] - Staging Leg: ";
59  //ostr << _itLeg.describe();
60  //ostr << "[Debug] - Staging Cabin: ";
61  //ostr << _itCabin.describe();
62 
63  return ostr.str();
64  }
65 
66  // //////////////////////////////////////////////////////////////////////
67  void FlightDateStruct::addAirport (const stdair::AirportCode_T& iAirport) {
68  AirportList_T::const_iterator itAirport = _airportList.find (iAirport);
69  if (itAirport == _airportList.end()) {
70  // Add the airport code to the airport set
71  const bool insertSuccessful = _airportList.insert (iAirport).second;
72 
73  if (insertSuccessful == false) {
74  // TODO: throw an exception
75  }
76 
77  // Add the airport code to the airport vector
78  _airportOrderedList.push_back (iAirport);
79  }
80  }
81 
82  // //////////////////////////////////////////////////////////////////////
84  // The list of airports encompasses all the airports on which
85  // the flight takes off or lands. Moreover, that list is
86  // time-ordered: the first airport is the initial departure of
87  // the flight, and the last airport is the eventual point of
88  // rest of the flight.
89  // Be l the size of the ordered list of airports.
90  // We want to generate all the segment combinations from the legs
91  // and, hence, from all the possible (time-ordered) airport pairs.
92  // Thus, we both iterator on i=0...l-1 and j=i+1...l
93  assert (_airportOrderedList.size() >= 2);
94 
95  _segmentList.clear();
96  for (AirportOrderedList_T::const_iterator itAirport_i =
97  _airportOrderedList.begin();
98  itAirport_i != _airportOrderedList.end()-1; ++itAirport_i) {
99  for (AirportOrderedList_T::const_iterator itAirport_j = itAirport_i + 1;
100  itAirport_j != _airportOrderedList.end(); ++itAirport_j) {
101  SegmentStruct lSegmentStruct;
102  lSegmentStruct._boardingPoint = *itAirport_i;
103  lSegmentStruct._offPoint = *itAirport_j;
104 
105  _segmentList.push_back (lSegmentStruct);
106  }
107  }
108 
109  // Clear the lists of airports, so that it is ready for the next flight
110  _airportList.clear();
111  _airportOrderedList.clear();
112  }
113 
114  // //////////////////////////////////////////////////////////////////////
115  void FlightDateStruct::
117  const SegmentCabinStruct& iCabin) {
118  // Retrieve the Segment structure corresponding to the (boarding, off) point
119  // pair.
120  SegmentStructList_T::iterator itSegment = _segmentList.begin();
121  for ( ; itSegment != _segmentList.end(); ++itSegment) {
122  const SegmentStruct& lSegment = *itSegment;
123 
124  const stdair::AirportCode_T& lBoardingPoint = iSegment._boardingPoint;
125  const stdair::AirportCode_T& lOffPoint = iSegment._offPoint;
126  if (lSegment._boardingPoint == lBoardingPoint
127  && lSegment._offPoint == lOffPoint) {
128  break;
129  }
130  }
131 
132  // If the segment key (airport pair) given in the schedule input file
133  // does not correspond to the leg (boarding, off) points, throw an exception
134  // so that the user knows the schedule input file is corrupted.
135  if (itSegment == _segmentList.end()) {
136  STDAIR_LOG_ERROR ("Within the inventory input file, there is a "
137  << "flight for which the airports of segments "
138  << "and those of the legs do not correspond.");
139  throw SegmentDateNotFoundException ("Within the inventory input file, "
140  "there is a flight for which the "
141  "airports of segments and those of "
142  "the legs do not correspond.");
143  }
144 
145  // Add the Cabin structure to the Segment Cabin structure.
146  assert (itSegment != _segmentList.end());
147  SegmentStruct& lSegment = *itSegment;
148  lSegment._cabinList.push_back (iCabin);
149  }
150 
151  // //////////////////////////////////////////////////////////////////////
152  void FlightDateStruct::
154  // Iterate on all the Segment structures (as they get the same cabin
155  // definitions)
156 
157  for (SegmentStructList_T::iterator itSegment = _segmentList.begin();
158  itSegment != _segmentList.end(); ++itSegment) {
159  SegmentStruct& lSegment = *itSegment;
160 
161  lSegment._cabinList.push_back (iCabin);
162  }
163  }
164 
165  // //////////////////////////////////////////////////////////////////////
166  void FlightDateStruct::
167  addFareFamily (const SegmentStruct& iSegment,
168  const SegmentCabinStruct& iCabin,
169  const FareFamilyStruct& iFareFamily) {
170  // Retrieve the Segment structure corresponding to the (boarding, off) point
171  // pair.
172  SegmentStructList_T::iterator itSegment = _segmentList.begin();
173  for ( ; itSegment != _segmentList.end(); ++itSegment) {
174  const SegmentStruct& lSegment = *itSegment;
175 
176  const stdair::AirportCode_T& lBoardingPoint = iSegment._boardingPoint;
177  const stdair::AirportCode_T& lOffPoint = iSegment._offPoint;
178  if (lSegment._boardingPoint == lBoardingPoint
179  && lSegment._offPoint == lOffPoint) {
180  break;
181  }
182  }
183 
184  // If the segment key (airport pair) given in the schedule input file
185  // does not correspond to the leg (boarding, off) points, throw an exception
186  // so that the user knows the schedule input file is corrupted.
187  if (itSegment == _segmentList.end()) {
188  STDAIR_LOG_ERROR ("Within the schedule input file, there is a flight "
189  << "for which the airports of segments and "
190  << "those of the legs do not correspond.");
191  throw SegmentDateNotFoundException ("Within the schedule input file, "
192  "there is a flight for which the "
193  "airports of segments and those of "
194  "the legs do not correspond.");
195  }
196 
197  // Add the Cabin structure to the Segment Cabin structure.
198  assert (itSegment != _segmentList.end());
199  SegmentStruct& lSegment = *itSegment;
200 
201  // Retrieve the Segment cabin structure given the cabin code
202  SegmentCabinStructList_T::iterator itCabin = lSegment._cabinList.begin();
203  for ( ; itCabin != lSegment._cabinList.end(); ++itCabin) {
204  const SegmentCabinStruct& lCabin = *itCabin;
205 
206  const stdair::CabinCode_T& lCabinCode = lCabin._cabinCode;
207  if (iCabin._cabinCode == lCabinCode) {
208  break;
209  }
210  }
211 
212  // If the segmentCabin key (cabin code) given in the schedule input file
213  // does not correspond to the stored cabin codes, throw an exception
214  // so that the user knows the schedule input file is corrupted.
215  if (itCabin == lSegment._cabinList.end()) {
216  STDAIR_LOG_ERROR ("Within the schedule input file, there is a flight "
217  << "for which the cabin code does not exist.");
218  throw SegmentDateNotFoundException ("Within the schedule input file, "
219  "there is a flight for which the "
220  "cabin code does not exist.");
221  }
222 
223  // Add the Cabin structure to the Segment Cabin structure.
224  assert (itCabin != lSegment._cabinList.end());
225  SegmentCabinStruct& lCabin = *itCabin;
226  lCabin._fareFamilies.push_back (iFareFamily);
227  }
228 
229  // //////////////////////////////////////////////////////////////////////
230  void FlightDateStruct::
232  const FareFamilyStruct& iFareFamily) {
233  // Iterate on all the Segment structures (as they get the same cabin
234  // definitions)
235 
236  for (SegmentStructList_T::iterator itSegment = _segmentList.begin();
237  itSegment != _segmentList.end(); ++itSegment) {
238  SegmentStruct& lSegment = *itSegment;
239 
240  // Retrieve the Segment cabin structure given the cabin code
241  SegmentCabinStructList_T::iterator itCabin = lSegment._cabinList.begin();
242  for ( ; itCabin != lSegment._cabinList.end(); ++itCabin) {
243  const SegmentCabinStruct& lCabin = *itCabin;
244 
245  const stdair::CabinCode_T& lCabinCode = lCabin._cabinCode;
246  if (iCabin._cabinCode == lCabinCode) {
247  break;
248  }
249  }
250 
251  // If the segmentCabin key (cabin code) given in the schedule input file
252  // does not correspond to the stored cabin codes, throw an exception
253  // so that the user knows the schedule input file is corrupted.
254  if (itCabin == lSegment._cabinList.end()) {
255  STDAIR_LOG_ERROR ("Within the schedule input file, there is a flight"
256  << " for which the cabin code does not exist.");
257  throw SegmentDateNotFoundException ("Within the schedule input file, "
258  "there is a flight for which the "
259  "cabin code does not exist.");
260  }
261 
262  // Add the Cabin structure to the Segment Cabin structure.
263  assert (itCabin != lSegment._cabinList.end());
264  SegmentCabinStruct& lCabin = *itCabin;
265  lCabin._fareFamilies.push_back (iFareFamily);
266  }
267  }
268 
269 }