Advertisement
GameNationRDF

flightressystem

Nov 29th, 2016
115
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 10.93 KB | None | 0 0
  1. #ifndef RESERVATIONSYSTEM_H_
  2. #define RESERVATIONSYSTEM_H_
  3.  
  4. #include "Flight.h"
  5.  
  6. class ReservationSystem {
  7. public:
  8.     ReservationSystem();
  9.     ~ReservationSystem();
  10.     void addFlight(const int flightNo, const int rowNo, const int seatNo);
  11.     void cancelFlight(const int flightNo);
  12.     void showAllFlights();
  13.     void showFlight(const int flightNo);
  14.     int makeReservation(const int flightNo, const int numPassengers, const int *seatRow, const char *seatCol);
  15.     void cancelReservation(const int resCode);
  16.     void showReservation(const int resCode);
  17.     int findFlightIDIndex(const int flightID);
  18.  
  19. private:
  20.     int size = 0;
  21.     string reservation = "";
  22.     int resTrack = 0;
  23.     Flight *flightList;
  24. };
  25.  
  26. #endif
  27.  
  28. //-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
  29.  
  30. #include <string>
  31. #include <iostream>
  32.  
  33. using namespace std;
  34.  
  35. class Flight
  36. {
  37. public:
  38.  
  39.     // constructor - destructor
  40.     Flight();
  41.     Flight(const Flight &obj);
  42.     Flight(const int flightId, const int row, const int col);
  43.     ~Flight();
  44.  
  45.     // passenger
  46.     int addPassenger(const int row, const string col);
  47.     void removePassenger(const int row, const string col);
  48.     int checkSeatsAvailable(const int numPassengers, const int *seatRow, const char *seatCol);
  49.  
  50.     // accessor
  51.     string getPrintOut() const;
  52.     int getNumberOfEmptySeats() const;
  53.     int getFlightID() const;
  54.     int getRow() const;
  55.     int getCol() const;
  56.  
  57.     //setter
  58.     void setID(const int newID);
  59.     void setRowAndCol(const int newRow, const int newCol);
  60.  
  61. private:
  62.     int findColInt(const string colChar);
  63.     int flightID;
  64.     int emptySeats;
  65.     string **passengerList;
  66.     int row;
  67.     int col;
  68.     const string colLetterList[10] = { "A", "B", "C", "D", "E", "F", "G", "H", "I" }; // the biggest commercial airplane has 10 columns of seats
  69.    
  70.     // pointer for copy constructor
  71.     int * ptr;
  72.  
  73. };
  74.  
  75. #endif
  76.  
  77. // -*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-
  78.  
  79. #include "ReservationSystem.h"
  80. #include <string>
  81.  
  82. ReservationSystem::ReservationSystem()
  83. {
  84.     flightList = new Flight[0];
  85. }
  86. ReservationSystem::~ReservationSystem()
  87. {
  88.  
  89. }
  90. void ReservationSystem::addFlight(const int flightID, const int rowNo, const int seatNo)
  91. {
  92.     Flight *tempFlightList = new Flight[size+1];
  93.  
  94.     // copy original flightList to a temporary one with the new flight at the end.
  95.     for (int i = 0; i < size; i++)
  96.     {
  97.         tempFlightList[i] = flightList[i];
  98.         tempFlightList[i].setID(flightList[i].getFlightID());
  99.         tempFlightList[i].setRowAndCol(flightList[i].getRow(), flightList[i].getCol());
  100.     }
  101.  
  102.     // add the new flight to the end of temporary list
  103.     tempFlightList[size].setID(flightID);
  104.     tempFlightList[size].setRowAndCol(rowNo, seatNo);
  105.  
  106.     // dealocate previous original flightList so that it can be refreshed
  107.     delete[] flightList;
  108.  
  109.     // reallocate flightList
  110.     flightList = new Flight[size + 1];
  111.  
  112.     // copy temp list to the new flightList
  113.     for (int i = 0; i < size+1; i++)
  114.     {
  115.         flightList[i].setID(tempFlightList[i].getFlightID());
  116.         flightList[i].setRowAndCol(tempFlightList[i].getRow(), tempFlightList[i].getCol());
  117.     }
  118.    
  119.     // deallocate templist
  120.     delete[] tempFlightList;
  121.  
  122.     // report the new flight
  123.     cout << "Flight " + to_string(flightID) + " has been added." << endl;
  124.  
  125.     // increse the size variable to match the new flightList size
  126.     size++;
  127.  
  128. }
  129. void ReservationSystem::cancelFlight(const int flightID)
  130. {
  131.     Flight *tempFlightList = new Flight[size-1];
  132.     int index = findFlightIDIndex(flightID);
  133.  
  134.     if (index == -1)
  135.         cout << "Flight with ID " + to_string(flightID) + " could not be found." << endl;
  136.     else
  137.     {
  138.         // copy from 0 to fround index
  139.         for (int i = 0; i < index + 1; i++)
  140.         {
  141.             tempFlightList[i].setID(flightList[i].getFlightID());
  142.             tempFlightList[i].setRowAndCol(flightList[i].getRow(), flightList[i].getCol());
  143.         }
  144.  
  145.  
  146.         // copy from index to end
  147.         for (int i = index + 1; i < size; i++)
  148.         {
  149.             tempFlightList[i].setID(flightList[i].getFlightID());
  150.             tempFlightList[i].setRowAndCol(flightList[i].getRow(), flightList[i].getCol());
  151.         }
  152.  
  153.         // dealocate previous original flightList so that it can be refreshed
  154.         delete[] flightList;
  155.  
  156.         // reallocate flightList
  157.         flightList = new Flight[size - 1];
  158.  
  159.         // copy temp list to the new flightList
  160.         for (int i = 0; i < size - 1; i++)
  161.         {
  162.             flightList[i].setID(tempFlightList[i].getFlightID());
  163.             flightList[i].setRowAndCol(tempFlightList[i].getRow(), tempFlightList[i].getCol());
  164.         }
  165.  
  166.         // deallocate templist
  167.         delete[] tempFlightList;
  168.  
  169.         // report the new flight
  170.         cout << "Flight " + to_string(flightID) + " has been cancelled." << endl;
  171.  
  172.         // increse the size variable to match the new flightList size
  173.         size--;
  174.     }
  175. }
  176. void ReservationSystem::showAllFlights()
  177. {
  178.     if (size == 0)
  179.         cout << "there are no flights to be shown." << endl;
  180.     else
  181.         for (int i = 0; i < size; i++)
  182.         {
  183.             cout << flightList[i].getPrintOut();
  184.         }
  185. }
  186. void ReservationSystem::showFlight(const int flightID)
  187. {
  188.     int index = findFlightIDIndex(flightID);
  189.  
  190.     cout << flightList[index].getPrintOut();
  191. }
  192. int ReservationSystem::makeReservation(const int flightID, const int numPassengers, const int *seatRow, const char *seatCol)
  193. {
  194.     int index = findFlightIDIndex(flightID);
  195.     int reservationCode;
  196.  
  197.     if (index == -1)
  198.     {
  199.         cout << "Flight with ID " + to_string(flightID) + " could not be found.\n" << endl;
  200.         return -1;
  201.     }
  202.     else
  203.     {
  204.         int resAvailable = flightList[index].checkSeatsAvailable(numPassengers, seatRow, seatCol);
  205.  
  206.         if (resAvailable == -1)
  207.         {
  208.             for (int i = 0; i < numPassengers; i++)
  209.             {
  210.                 flightList[index].addPassenger(seatRow[i], string(1,seatCol[i]));
  211.             }
  212.  
  213.             return 1;
  214.         }
  215.  
  216.         else
  217.         {
  218.             cout << "Seat at " + to_string(seatRow[resAvailable]) + seatCol[resAvailable] + " is unavailable.\n" << endl;
  219.             return 0;
  220.         }
  221.     }
  222. }
  223. void ReservationSystem::cancelReservation(const int resCode)
  224. {
  225.  
  226. }
  227. void ReservationSystem::showReservation(const int resCode)
  228. {
  229.  
  230. }
  231. int ReservationSystem::findFlightIDIndex(const int flightID)
  232. {
  233.     // search the flightList
  234.     for (int i = 0; i < size; i++)
  235.     {
  236.         if (flightList[i].getFlightID() == flightID)
  237.             return i;
  238.     }
  239.  
  240.     return -1;
  241. }
  242.  
  243. int main()
  244. {
  245.     ReservationSystem r;
  246.  
  247.     r.showAllFlights();
  248.  
  249.     r.addFlight(120, 3, 5);
  250.  
  251.     r.showAllFlights();
  252.  
  253.     int numPass1 = 2;
  254.     int row1[2] = { 1, 2 };
  255.     char col1[2] = {'A', 'B'};
  256.  
  257.     int numPass2 = 2;
  258.     int row2[2] = { 1, 2 };
  259.     char col2[2] = {'E', 'B'};
  260.  
  261.     int numPass3 = 3;
  262.     int row3[3] = { 1, 2, 3};
  263.     char col3[3] = { 'C', 'C' , 'C'};
  264.  
  265.     r.makeReservation(120, numPass1, row1, col1);
  266.  
  267.     r.showAllFlights();
  268.  
  269.     r.makeReservation(120, numPass2, row2, col2);
  270.  
  271.     r.showAllFlights();
  272.  
  273.     r.makeReservation(120, numPass3, row3, col3);
  274.  
  275.     r.showAllFlights();
  276.  
  277.     r.cancelFlight(120);
  278.  
  279.     r.showAllFlights();
  280. }
  281.  
  282. // -**-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**--*-*-*-*-*-**--*-*
  283.  
  284. #include <iostream>
  285. #include <string>
  286. #include "Flight.h"
  287.  
  288. using namespace std;
  289.  
  290. // constructor
  291. Flight::Flight(const int flightId, const int row, const int col)
  292. {
  293.     // set properties of the flight
  294.     this->flightID = flightId;
  295.     this->row = row;
  296.     this->col = col;
  297.  
  298.     // init passengerList
  299.     passengerList= new string*[row];
  300.     for (int i = 0; i < row; ++i)
  301.         passengerList[i] = new string[col];
  302.  
  303.     // fill passengerList with empty seats
  304.     for (int i = 0; i < row; i++)
  305.         for (int j = 0; j < col; j++)
  306.             passengerList[i][j] = "o";
  307.  
  308.     // set number of available seats
  309.     emptySeats = row * col;
  310. }
  311.  
  312. // default constructor
  313. Flight::Flight()
  314. {
  315.     flightID = 0;
  316.     row = 1;
  317.     col = 1;
  318.    
  319.     // init passengerList
  320.     /*passengerList = new string*[row];
  321.     for (int i = 0; i < row; ++i)
  322.         passengerList[i] = new string[col];
  323.  
  324.     // fill passengerList with empty seats
  325.     for (int i = 0; i < row; i++)
  326.         for (int j = 0; j < col; j++)
  327.             passengerList[i][j] = "o";
  328.  
  329.     // set number of available seats
  330.     emptySeats = row * col;*/
  331. }
  332.  
  333. // copy constructor
  334. Flight::Flight(const Flight &obj)
  335. {
  336.     flightID = obj.flightID;
  337.     row = obj.row;
  338.     col = obj.col;
  339. }
  340.  
  341. // destructor
  342. Flight::~Flight()
  343. {
  344.     // dealocate passengerList
  345.     for (int i = 0; i < row; i++) {
  346.         delete[] passengerList[i];
  347.     }
  348.     delete[] passengerList;
  349. }
  350.  
  351. // generate the int vale of column from given char
  352. int Flight::findColInt(const string colChar)
  353. {
  354.     int colInt = 0;
  355.  
  356.     for (int i = 0; i < 10; i++)
  357.     {
  358.         if (colLetterList[i] == colChar)
  359.         {
  360.             colInt = i+1;
  361.             break;
  362.         }
  363.     }
  364.  
  365.     return colInt;
  366. }
  367.  
  368. // make the specified row,column 'x'
  369. int Flight::addPassenger(const int row, const string col)
  370. {
  371.     int colInt = findColInt(col);
  372.  
  373.     if (passengerList[row - 1][colInt - 1] == "x")
  374.         return -1;
  375.     else
  376.     {
  377.         passengerList[row - 1][colInt - 1] = "x";
  378.         emptySeats--;
  379.         return 0;
  380.     }        
  381. }
  382.  
  383. // make the specified row, column 'o'
  384. void Flight::removePassenger(const int row, const string col)
  385. {
  386.     int colInt = findColInt(col);
  387.  
  388.     passengerList[row-1][colInt-1] = "o";
  389.  
  390.     emptySeats++;
  391. }
  392.  
  393. // return empty seat number
  394. int Flight::getNumberOfEmptySeats()const
  395. {
  396.     return emptySeats;
  397. }
  398.  
  399. // return flight ID
  400. int Flight::getFlightID() const
  401. {
  402.     return flightID;
  403. }
  404.  
  405. // return the string containing flight and seat info in a nice layout
  406. string Flight::getPrintOut() const
  407. {
  408.     string flightRoute = "Flight " + to_string(flightID) + " has " + to_string(emptySeats) + " empty seats.";
  409.  
  410.     string flightList = "\t";
  411.  
  412.     for (int i = 0; i < col; i++)
  413.     {
  414.         flightList += colLetterList[i] + " ";
  415.     }
  416.  
  417.     flightList += "\n";
  418.  
  419.     for (int i = 0; i <row; i++)
  420.     {
  421.         flightList += to_string(i+1) + "\t";
  422.  
  423.         for (int j = 0; j < col; j++)
  424.         {
  425.             flightList += passengerList[i][j] + " ";
  426.         }
  427.  
  428.         flightList += "\n" ;
  429.     }
  430.    
  431.     return (flightRoute + "\n" + flightList + "\n");
  432. }
  433.  
  434. // set flightID
  435. void Flight::setID(const int newID)
  436. {
  437.     flightID = newID;
  438. }
  439.  
  440. // set new row and col for flight, refresh the 2D array accordingly
  441. void Flight::setRowAndCol(const int newRow, const int newCol)
  442. {
  443.     row = newRow;
  444.     col = newCol;
  445.  
  446.     // dealocate passengerList
  447.     /*for (int i = 0; i < row; i++) {
  448.         delete [] passengerList[i];
  449.     }
  450.     delete [] passengerList;
  451.     passengerList = 0;*/
  452.  
  453.     // init passengerList
  454.     passengerList = new string*[row];
  455.     for (int i = 0; i < row; ++i)
  456.         passengerList[i] = new string[col];
  457.  
  458.     // fill passengerList with empty seats
  459.     for (int i = 0; i < row; i++)
  460.         for (int j = 0; j < col; j++)
  461.             passengerList[i][j] = "o";
  462.  
  463.     // set number of available seats
  464.     emptySeats = row * col;
  465. }
  466.  
  467. // checks if given seats are available to book.
  468. int Flight::checkSeatsAvailable(const int numPassengers, const int *seatRow, const char *seatCol)
  469. {
  470.     for (int i = 0; i < numPassengers; i++)
  471.     {
  472.         if ( passengerList[seatRow[i] - 1][findColInt(string(1, seatCol[i]))-1] == "x")
  473.         {
  474.             return i;
  475.         }
  476.     }
  477.  
  478.     return -1;
  479. }
  480.  
  481. // return row
  482. int Flight::getRow() const
  483. {
  484.     return row;
  485. }
  486.  
  487. // return row
  488. int Flight::getCol() const
  489. {
  490.     return col;
  491. }
  492.  
  493. // FLight test main function in Flight.cpp
  494. /*int main()
  495. {
  496.     Flight f(120, 5, 3);
  497.  
  498.     cout << f.getPrintOut() << endl;
  499.  
  500.     f.addPassenger(3, "B");
  501.  
  502.     cout << f.getPrintOut() << endl;
  503.  
  504.     f.removePassenger(3, "B");
  505.  
  506.     cout << f.getPrintOut() << endl;
  507.  
  508.  
  509.  
  510.  
  511.     return 0;
  512. }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement