//FlightDatabase.cpp -- assignment 3, summer 2001 //author: Rebecca Price, id: XXXXXXX, section: AA, TA: Man Chun Liu #include "FlightDatabase.h" const int DEFAULT_CAP = 5; // default constructor FDatabase::FDatabase(){ capacity = DEFAULT_CAP; size = 0; flightArray = new Flight[capacity]; assert (flightArray != NULL); } // copy constructor FDatabase::FDatabase(const FDatabase& other){ copy(other); } // overload operator to copy a flight into a new instance FDatabase& FDatabase::operator =(const FDatabase& other){ if(this != &other){ cleanup(); copy(other); } return *this; } // destructor calls cleanup FDatabase::~FDatabase(){ cleanup(); } // deallocates dynamic memory void FDatabase::cleanup(){ delete [] flightArray; } // copies info from one instance of flight into another void FDatabase::copy(const FDatabase& other){ capacity = other.capacity; flightArray = new Flight[capacity]; for(int i = 0; i < capacity; i++){ flightArray[i] = other.flightArray[i]; } } // updates/replaces old info any flights that have the same flight number bool FDatabase::updateInfo(Flight& other){ for(int i = 0; i < size; i++){ // search through current flight info for match. // if there is a then new info copied over and return true. if(flightArray[i].GetFlightNum() == other.GetFlightNum()){ flightArray[i].copy(other); return true; } } return false; } // adds the info in other into the flightArray void FDatabase::AddFlight(Flight& other, int count){ EnsureCapacity(count + 1); #ifdef DEBUGGING cout<<"addFlight befoer assign"<>fileName; cout<>amount; getline(Info, dummy); temp.SetFlightNum(number); temp.SetDepatureLoc(dLoc); temp.SetArrivalLoc(aLoc); temp.SetDepartTime(dTime); temp.SetArrivalTime(aTime); temp.SetDate(dDate); temp.SetPrice(amount); // if the flight number does not occur in the current database the // flight is added and the size of the array increases, keeping tab on // the necessary capacity for the dynamic memory of flightArray. // if(!(updateInfo(temp))){ AddFlight(temp, GetSize()); SetSize(GetSize() + 1); } getline(Info, number); } } //Debug to check if flight info has been entered correctly. #ifdef DEBUGGING PrintFlightInfo(); cout<<"File reading complete"<= requireCapacity) return; // returns if capacity is large enough to hold info. // since capacity was not large enough it is doubled or set // equal to the required size. int newCapacity = capacity *2; if(newCapacity <= requireCapacity){ newCapacity = requireCapacity; } // set a new dynamic memory the size of the new capacity. Flight* newFlightArray = new Flight[newCapacity]; // copy old flight info into new Flight Array for(int i = 0; i < size; i++){ newFlightArray[i] = flightArray[i]; } // delete the old array and reset new array to old variable name. delete [] flightArray; flightArray = newFlightArray; capacity = newCapacity; } // cancels/deletes the flight the user requests to be deleted from the database void FDatabase::CancelFlight(){ PrintFlightInfo(); string flightToBeCanceled; // stores the flight number to be canceled. bool exit = false; do{ cout<<"Please enter the flight number you would like to cancel: "; cin>>flightToBeCanceled; if(flightToBeCanceled == "-1"){ exit = true; }else{ // look thru the flight DataBase and remove this flight number bool flightFound = false; for(int j = 0; j < size; j++){ // see if flightArray[j].number matches if(flightArray[j].GetFlightNum() == flightToBeCanceled){ // have a match - this one is to be removed flightArray[j].SetCanceledFlight(true); flightFound = true; } } if(!flightFound){ cout << "No flight found "; } else { // All the flights that matched were marked as canceled // Compact the database and leave some empty slots for new reservations CompactFlightDataBase(DEFAULT_CAP); //Debig check to make sure that the flight info has been removed. #ifdef DEBUGGING PrintFlightInfo(); cout<<"The size of the database is: "< fArray[k].GetPrice()){ temp = fArray[i]; fArray[i] = fArray[k]; fArray[k] = temp; } } } } // print Info, by default prints contents of flightArray void FDatabase::PrintFlightInfo(){ PrintFlightInfo(flightArray, GetSize(), "Current Flight List"); } // prints out the necessary information of a Flight array void FDatabase::PrintFlightInfo(Flight* fArray, int fCount, string title){ SortFlight(fArray, fCount); cout<>dLoc; if(dLoc == "-1"){ exit = true; }else{ temp.SetDepatureLoc(dLoc); cout<>aLoc; temp.SetArrivalLoc(aLoc); cout<<"Please enter the desired departure date: "; cin>>date; temp.SetDate(date); // temp comtains the target flight details // look thru flightArray and store any matches in // reservationMatches // bool flightsFound = false; for(int i = 0; i < size; i++){ if(temp == flightArray[i]){ // have found a potential match for the user // save flightArray[i] into next available slot // in reservationMatches // matchCount++; // ensure capacity in reservationMatches // save the flight we found :) reservationMatches[matchCount] = flightArray[i]; flightsFound = true; } } // if flightsFound is true, there is a match, then those matches are sent to be printed. if(flightsFound){ PrintReservationInfo(reservationMatches, matchCount+1, "Reservation matches found!"); int reservedNumber; cout<<"Please select the number you want: "; cin>>reservedNumber; cout<<"Here is all the relevant information for the flight you selected"<