// This file mostly contains code to manage the "fleet" class, which // organizes aircraft and the reservations for this aircraft. #include #include #include "errors.h" #include "time_slot.h" #include "reservations.h" #include "aircraft.h" #include "fleet.h" fleet::fleet() { head = NULL; ac_count = 0; } fleet::~fleet() { fleet_element *tmp; while (head != NULL) { tmp = head; head = head->next; delete tmp; } } void fleet::add_new_ac(const new_aircraft new_ac_data) { fleet_element *new_ac; fleet_element *ptr; // add the new aircraft at the end of the fleet's linked list new_ac = new fleet_element; if (head == NULL) { head = new_ac; } else { ptr = head; while (ptr->next != NULL) { ptr = ptr->next; } ptr->next = new_ac; } new_ac->next = NULL; ++ac_count; // load the new values into the new_ac new_ac->ac_data.update_unique_id(new_ac_data.ac_unique_id); new_ac->ac_data.update_name(new_ac_data.name); new_ac->ac_data.update_description(new_ac_data.description); } int fleet::remove_ac(const int ac_unique_id) { int outcome = SUCCESSFUL; int found = 0; int finished = 0; fleet_element *previous; fleet_element *target; if (head == NULL) { outcome = FLEET_ERR_NOT_FOUND; } else { // Look through the linked list of airplanes to find the one with // the right unique_id. Keep track of which fleet_element is prior // to the one being deleted, so we can change it's "next" link to // bypass the record being deleted. If the airplane being deleted is // the first element in the linked list, we will need some special // processing, since "head" will also need to be updated. if (ac_unique_id == head->ac_data.get_unique_id() ) { // the record to be deleted is at the front of the list target = head; head = head->next; delete target; } else { previous = head; while ( (!found) && (!finished) ) { if (previous->next == NULL) { finished = 1; } else if (ac_unique_id == previous->next->ac_data.get_unique_id() ) { found = 1; } else { previous = previous->next; } } if (!found) { outcome = FLEET_ERR_NOT_FOUND; } else { // rearrange the links to bypass the aircraft being deleted, then // delete the aircraft target = previous->next; previous->next = previous->next->next; delete target; } } } if (outcome == SUCCESSFUL) { --ac_count; } return outcome; } // look for a particular aircraft unique id in the linked list of aircraft fleet_element * find_aircraft(const int ac_unique_id, const char name[], fleet_element *head) { fleet_element *tmp = head; int found = 0; int finished = 0; new_aircraft aircraft_data; while ( (!found) && (!finished) ) { if (tmp == NULL) { finished = 1; } else { aircraft_data = tmp->ac_data.get_ac_data(); // either ac_unique_id or name may have been passed in with a NULL value if ( ( ac_unique_id != 0 ) && ( ac_unique_id == aircraft_data.ac_unique_id ) ) { found = 1; } else if ( (name != NULL) && ( strcmp(name, aircraft_data.name) == 0 ) ) { found = 1; } else { tmp = tmp->next; } delete [] aircraft_data.description; } } return tmp; } int fleet::update_ac_name(const int ac_unique_id, const char name[]) { int outcome = SUCCESSFUL; fleet_element *tmp = find_aircraft(ac_unique_id, NULL, head); if (tmp == NULL) { outcome = FLEET_ERR_NOT_FOUND; } else { tmp->ac_data.update_name(name); } return outcome; } int fleet::update_ac_description(const int ac_unique_id, const char *description) { int outcome = SUCCESSFUL; fleet_element *tmp = find_aircraft(ac_unique_id, NULL, head); if (tmp == NULL) { outcome = FLEET_ERR_NOT_FOUND; } else { tmp->ac_data.update_description(description); } return outcome; } // Make an array of new_aircraft structures, with name/id/description // info for each aircraft in the fleet. // The calling function will be responsible for freeing the memory for // the individual ac_array[].description pointers, and for the overall // ac_array new_aircraft * fleet::find_all_ac(int & count) { new_aircraft *ac_array = NULL; fleet_element *fleet_ptr = head; int i; count = ac_count; if (count > 0) { ac_array = new new_aircraft[count]; } for (i = 0; i < count; ++i) { ac_array[i] = fleet_ptr->ac_data.get_ac_data(); fleet_ptr = fleet_ptr->next; } return ac_array; } // Find one particular aircraft, based on either the aircraft unique id // or the aircraft name. The calling function will be responsible for // deleting the memory pointed to by found_ac.description. This function // will typically be called with either ac_unique_id set to 0, or // name set to NULL, but not both. The calling function should check the // value of 'outcome' before trying to use the new_aircraft data. new_aircraft fleet::find_one_ac(const int ac_unique_id, const char name[], int & outcome) { new_aircraft found_ac; fleet_element *fleet_ptr; outcome = SUCCESSFUL; found_ac.ac_unique_id = 0; found_ac.name[0] = '\0'; found_ac.description = NULL; fleet_ptr = find_aircraft(ac_unique_id, name, head); if (fleet_ptr == NULL) { outcome = FLEET_ERR_NOT_FOUND; } else { found_ac = fleet_ptr->ac_data.get_ac_data(); } return found_ac; } // Get the descriptive info and all the reservations for a particular // aircraft. // All functions calling this function should check the returned value // of "outcome" before attempting to use the data in all_ac_data. // All calling functions will be responsible for freeing memory inside // spec.description, and time_slot_info structures. all_ac_data fleet::find_all_ac_data(const int ac_unique_id, int & outcome) { outcome = SUCCESSFUL; all_ac_data null_data; fleet_element *tmp = find_aircraft(ac_unique_id, NULL, head); if (tmp == NULL) { outcome = FLEET_ERR_NOT_FOUND; return null_data; } else { return tmp->ac_data.get_all_ac_data(); } } int fleet::add_ac_reservation(const int ac_unique_id, const time_slot_info res_info) { int outcome = SUCCESSFUL; fleet_element *tmp = find_aircraft(ac_unique_id, NULL, head); if (tmp == NULL) { return FLEET_ERR_NOT_FOUND; } else return tmp->ac_data.add_res(res_info); } int fleet::remove_ac_reservation(const int ac_unique_id, const int slot_unique_id) { int outcome = SUCCESSFUL; fleet_element *tmp; tmp = find_aircraft(ac_unique_id, NULL, head); if (tmp == NULL) { return FLEET_ERR_NOT_FOUND; } else { return tmp->ac_data.remove_res(slot_unique_id); } }