Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef RESERVATIONSYSTEM_H_
- #define RESERVATIONSYSTEM_H_
- #include "Flight.h"
- class ReservationSystem {
- public:
- ReservationSystem();
- ~ReservationSystem();
- void addFlight(const int flightNo, const int rowNo, const int seatNo);
- void cancelFlight(const int flightNo);
- void showAllFlights();
- void showFlight(const int flightNo);
- int makeReservation(const int flightNo, const int numPassengers, const int *seatRow, const char *seatCol);
- void cancelReservation(const int resCode);
- void showReservation(const int resCode);
- int findFlightIDIndex(const int flightID);
- private:
- int size = 0;
- string reservation = "";
- int resTrack = 0;
- Flight *flightList;
- };
- #endif
- //-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*
- #include <string>
- #include <iostream>
- using namespace std;
- class Flight
- {
- public:
- // constructor - destructor
- Flight();
- Flight(const Flight &obj);
- Flight(const int flightId, const int row, const int col);
- ~Flight();
- // passenger
- int addPassenger(const int row, const string col);
- void removePassenger(const int row, const string col);
- int checkSeatsAvailable(const int numPassengers, const int *seatRow, const char *seatCol);
- // accessor
- string getPrintOut() const;
- int getNumberOfEmptySeats() const;
- int getFlightID() const;
- int getRow() const;
- int getCol() const;
- //setter
- void setID(const int newID);
- void setRowAndCol(const int newRow, const int newCol);
- private:
- int findColInt(const string colChar);
- int flightID;
- int emptySeats;
- string **passengerList;
- int row;
- int col;
- const string colLetterList[10] = { "A", "B", "C", "D", "E", "F", "G", "H", "I" }; // the biggest commercial airplane has 10 columns of seats
- // pointer for copy constructor
- int * ptr;
- };
- #endif
- // -*-*-*-*-*-*-*-*-*-*-**-*-*-*-*-*-*-*-*-*-*-*-*-
- #include "ReservationSystem.h"
- #include <string>
- ReservationSystem::ReservationSystem()
- {
- flightList = new Flight[0];
- }
- ReservationSystem::~ReservationSystem()
- {
- }
- void ReservationSystem::addFlight(const int flightID, const int rowNo, const int seatNo)
- {
- Flight *tempFlightList = new Flight[size+1];
- // copy original flightList to a temporary one with the new flight at the end.
- for (int i = 0; i < size; i++)
- {
- tempFlightList[i] = flightList[i];
- tempFlightList[i].setID(flightList[i].getFlightID());
- tempFlightList[i].setRowAndCol(flightList[i].getRow(), flightList[i].getCol());
- }
- // add the new flight to the end of temporary list
- tempFlightList[size].setID(flightID);
- tempFlightList[size].setRowAndCol(rowNo, seatNo);
- // dealocate previous original flightList so that it can be refreshed
- delete[] flightList;
- // reallocate flightList
- flightList = new Flight[size + 1];
- // copy temp list to the new flightList
- for (int i = 0; i < size+1; i++)
- {
- flightList[i].setID(tempFlightList[i].getFlightID());
- flightList[i].setRowAndCol(tempFlightList[i].getRow(), tempFlightList[i].getCol());
- }
- // deallocate templist
- delete[] tempFlightList;
- // report the new flight
- cout << "Flight " + to_string(flightID) + " has been added." << endl;
- // increse the size variable to match the new flightList size
- size++;
- }
- void ReservationSystem::cancelFlight(const int flightID)
- {
- Flight *tempFlightList = new Flight[size-1];
- int index = findFlightIDIndex(flightID);
- if (index == -1)
- cout << "Flight with ID " + to_string(flightID) + " could not be found." << endl;
- else
- {
- // copy from 0 to fround index
- for (int i = 0; i < index + 1; i++)
- {
- tempFlightList[i].setID(flightList[i].getFlightID());
- tempFlightList[i].setRowAndCol(flightList[i].getRow(), flightList[i].getCol());
- }
- // copy from index to end
- for (int i = index + 1; i < size; i++)
- {
- tempFlightList[i].setID(flightList[i].getFlightID());
- tempFlightList[i].setRowAndCol(flightList[i].getRow(), flightList[i].getCol());
- }
- // dealocate previous original flightList so that it can be refreshed
- delete[] flightList;
- // reallocate flightList
- flightList = new Flight[size - 1];
- // copy temp list to the new flightList
- for (int i = 0; i < size - 1; i++)
- {
- flightList[i].setID(tempFlightList[i].getFlightID());
- flightList[i].setRowAndCol(tempFlightList[i].getRow(), tempFlightList[i].getCol());
- }
- // deallocate templist
- delete[] tempFlightList;
- // report the new flight
- cout << "Flight " + to_string(flightID) + " has been cancelled." << endl;
- // increse the size variable to match the new flightList size
- size--;
- }
- }
- void ReservationSystem::showAllFlights()
- {
- if (size == 0)
- cout << "there are no flights to be shown." << endl;
- else
- for (int i = 0; i < size; i++)
- {
- cout << flightList[i].getPrintOut();
- }
- }
- void ReservationSystem::showFlight(const int flightID)
- {
- int index = findFlightIDIndex(flightID);
- cout << flightList[index].getPrintOut();
- }
- int ReservationSystem::makeReservation(const int flightID, const int numPassengers, const int *seatRow, const char *seatCol)
- {
- int index = findFlightIDIndex(flightID);
- int reservationCode;
- if (index == -1)
- {
- cout << "Flight with ID " + to_string(flightID) + " could not be found.\n" << endl;
- return -1;
- }
- else
- {
- int resAvailable = flightList[index].checkSeatsAvailable(numPassengers, seatRow, seatCol);
- if (resAvailable == -1)
- {
- for (int i = 0; i < numPassengers; i++)
- {
- flightList[index].addPassenger(seatRow[i], string(1,seatCol[i]));
- }
- return 1;
- }
- else
- {
- cout << "Seat at " + to_string(seatRow[resAvailable]) + seatCol[resAvailable] + " is unavailable.\n" << endl;
- return 0;
- }
- }
- }
- void ReservationSystem::cancelReservation(const int resCode)
- {
- }
- void ReservationSystem::showReservation(const int resCode)
- {
- }
- int ReservationSystem::findFlightIDIndex(const int flightID)
- {
- // search the flightList
- for (int i = 0; i < size; i++)
- {
- if (flightList[i].getFlightID() == flightID)
- return i;
- }
- return -1;
- }
- int main()
- {
- ReservationSystem r;
- r.showAllFlights();
- r.addFlight(120, 3, 5);
- r.showAllFlights();
- int numPass1 = 2;
- int row1[2] = { 1, 2 };
- char col1[2] = {'A', 'B'};
- int numPass2 = 2;
- int row2[2] = { 1, 2 };
- char col2[2] = {'E', 'B'};
- int numPass3 = 3;
- int row3[3] = { 1, 2, 3};
- char col3[3] = { 'C', 'C' , 'C'};
- r.makeReservation(120, numPass1, row1, col1);
- r.showAllFlights();
- r.makeReservation(120, numPass2, row2, col2);
- r.showAllFlights();
- r.makeReservation(120, numPass3, row3, col3);
- r.showAllFlights();
- r.cancelFlight(120);
- r.showAllFlights();
- }
- // -**-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-*-**--*-*-*-*-*-**--*-*
- #include <iostream>
- #include <string>
- #include "Flight.h"
- using namespace std;
- // constructor
- Flight::Flight(const int flightId, const int row, const int col)
- {
- // set properties of the flight
- this->flightID = flightId;
- this->row = row;
- this->col = col;
- // init passengerList
- passengerList= new string*[row];
- for (int i = 0; i < row; ++i)
- passengerList[i] = new string[col];
- // fill passengerList with empty seats
- for (int i = 0; i < row; i++)
- for (int j = 0; j < col; j++)
- passengerList[i][j] = "o";
- // set number of available seats
- emptySeats = row * col;
- }
- // default constructor
- Flight::Flight()
- {
- flightID = 0;
- row = 1;
- col = 1;
- // init passengerList
- /*passengerList = new string*[row];
- for (int i = 0; i < row; ++i)
- passengerList[i] = new string[col];
- // fill passengerList with empty seats
- for (int i = 0; i < row; i++)
- for (int j = 0; j < col; j++)
- passengerList[i][j] = "o";
- // set number of available seats
- emptySeats = row * col;*/
- }
- // copy constructor
- Flight::Flight(const Flight &obj)
- {
- flightID = obj.flightID;
- row = obj.row;
- col = obj.col;
- }
- // destructor
- Flight::~Flight()
- {
- // dealocate passengerList
- for (int i = 0; i < row; i++) {
- delete[] passengerList[i];
- }
- delete[] passengerList;
- }
- // generate the int vale of column from given char
- int Flight::findColInt(const string colChar)
- {
- int colInt = 0;
- for (int i = 0; i < 10; i++)
- {
- if (colLetterList[i] == colChar)
- {
- colInt = i+1;
- break;
- }
- }
- return colInt;
- }
- // make the specified row,column 'x'
- int Flight::addPassenger(const int row, const string col)
- {
- int colInt = findColInt(col);
- if (passengerList[row - 1][colInt - 1] == "x")
- return -1;
- else
- {
- passengerList[row - 1][colInt - 1] = "x";
- emptySeats--;
- return 0;
- }
- }
- // make the specified row, column 'o'
- void Flight::removePassenger(const int row, const string col)
- {
- int colInt = findColInt(col);
- passengerList[row-1][colInt-1] = "o";
- emptySeats++;
- }
- // return empty seat number
- int Flight::getNumberOfEmptySeats()const
- {
- return emptySeats;
- }
- // return flight ID
- int Flight::getFlightID() const
- {
- return flightID;
- }
- // return the string containing flight and seat info in a nice layout
- string Flight::getPrintOut() const
- {
- string flightRoute = "Flight " + to_string(flightID) + " has " + to_string(emptySeats) + " empty seats.";
- string flightList = "\t";
- for (int i = 0; i < col; i++)
- {
- flightList += colLetterList[i] + " ";
- }
- flightList += "\n";
- for (int i = 0; i <row; i++)
- {
- flightList += to_string(i+1) + "\t";
- for (int j = 0; j < col; j++)
- {
- flightList += passengerList[i][j] + " ";
- }
- flightList += "\n" ;
- }
- return (flightRoute + "\n" + flightList + "\n");
- }
- // set flightID
- void Flight::setID(const int newID)
- {
- flightID = newID;
- }
- // set new row and col for flight, refresh the 2D array accordingly
- void Flight::setRowAndCol(const int newRow, const int newCol)
- {
- row = newRow;
- col = newCol;
- // dealocate passengerList
- /*for (int i = 0; i < row; i++) {
- delete [] passengerList[i];
- }
- delete [] passengerList;
- passengerList = 0;*/
- // init passengerList
- passengerList = new string*[row];
- for (int i = 0; i < row; ++i)
- passengerList[i] = new string[col];
- // fill passengerList with empty seats
- for (int i = 0; i < row; i++)
- for (int j = 0; j < col; j++)
- passengerList[i][j] = "o";
- // set number of available seats
- emptySeats = row * col;
- }
- // checks if given seats are available to book.
- int Flight::checkSeatsAvailable(const int numPassengers, const int *seatRow, const char *seatCol)
- {
- for (int i = 0; i < numPassengers; i++)
- {
- if ( passengerList[seatRow[i] - 1][findColInt(string(1, seatCol[i]))-1] == "x")
- {
- return i;
- }
- }
- return -1;
- }
- // return row
- int Flight::getRow() const
- {
- return row;
- }
- // return row
- int Flight::getCol() const
- {
- return col;
- }
- // FLight test main function in Flight.cpp
- /*int main()
- {
- Flight f(120, 5, 3);
- cout << f.getPrintOut() << endl;
- f.addPassenger(3, "B");
- cout << f.getPrintOut() << endl;
- f.removePassenger(3, "B");
- cout << f.getPrintOut() << endl;
- return 0;
- }*/
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement