// class_travel_database_application.h (c) 2001 Kari Laitinen // 09.02.2001 File created. // 06.03.2001 Last modification. #ifndef CLASS_TRAVEL_DATABASE_APPLICATION_H #define CLASS_TRAVEL_DATABASE_APPLICATION_H #include "class_travel_application.h" class Travel_database_application : public Travel_application { public: void add_new_place_to_travel_database() ; void load_initial_place_coordinates() ; void load_initial_travel_info_records() ; void store_travel_data_to_file() ; void run() ; } ; void Travel_database_application::add_new_place_to_travel_database() { char name_of_new_place[ 40 ] ; char country_of_new_place[ 40 ] ; int degrees_of_latitude, minutes_of_latitude ; int degrees_of_longitude, minutes_of_longitude ; cout << "\n Give the name of the new place: " ; cin.getline( name_of_new_place, sizeof( name_of_new_place ) ) ; cout << "\n Give country of the new place: " ; cin.getline( country_of_new_place, sizeof( country_of_new_place ) ) ; degrees_of_latitude = ask_integer( "\n Give degrees of latitude: " ) ; minutes_of_latitude = ask_integer( "\n Give minutes of latitude: " ) ; degrees_of_longitude = ask_integer( "\n Give degrees of longitude: " ) ; minutes_of_longitude = ask_integer( "\n Give minutes of longitude: " ) ; Place_on_earth new_place( name_of_new_place, country_of_new_place, degrees_of_latitude, minutes_of_latitude, degrees_of_longitude, minutes_of_longitude ) ; Text travel_info_text ; cout << "\n Write travel info for the new place: \n" ; travel_info_text.edit_text() ; new_place.set_travel_info( travel_info_text ) ; places_on_earth.push_back( new_place ) ; // Finally, we shall update a file which contains a log // of modifications done to travel_database.data ofstream modification_log_file( "travel_database_modifications.txt", ios::app ) ; if ( modification_log_file.fail() ) { cout << "\n\n ERROR opening travel_database_modifications.txt\n" ; } else { modification_log_file << "\n {\"" << name_of_new_place << "\", \"" << country_of_new_place << "\", " << degrees_of_latitude << ", " << minutes_of_latitude << ", " << degrees_of_longitude << ", " << minutes_of_longitude << " },\n\n" ; modification_log_file << "\n { \"" << name_of_new_place << "\", \"" << country_of_new_place << "\", \n" ; travel_info_text.print_with_indentation( modification_log_file ) ; } } void Travel_database_application::load_initial_place_coordinates() { int number_of_places_in_table = sizeof( initial_place_coordinates ) / sizeof( Place_coordinate_data ) ; for ( int coordinates_index = 0 ; coordinates_index < number_of_places_in_table ; coordinates_index ++ ) { places_on_earth.push_back( Place_on_earth( initial_place_coordinates[ coordinates_index ].place_name, initial_place_coordinates[ coordinates_index ].country_name, initial_place_coordinates[ coordinates_index ]. degrees_of_latitude, initial_place_coordinates[ coordinates_index ]. minutes_of_latitude, initial_place_coordinates[ coordinates_index ]. degrees_of_longitude, initial_place_coordinates[ coordinates_index ]. minutes_of_longitude ) ) ; } } void Travel_database_application::load_initial_travel_info_records() { int number_of_initial_records = sizeof( initial_travel_info_records ) / sizeof( Initial_travel_info_record ) ; for ( int record_index = 0 ; record_index < number_of_initial_records ; record_index ++ ) { string place_name_in_record = initial_travel_info_records[ record_index ].place_name ; string country_name_in_record = initial_travel_info_records[ record_index ].country_name ; int index_of_place ; Place_on_earth existing_place_data ; if ( search_place_in_country( place_name_in_record, country_name_in_record, existing_place_data, index_of_place ) ) { existing_place_data.set_travel_info( initial_travel_info_records[ record_index ].travel_info ) ; places_on_earth[ index_of_place ] = existing_place_data ; } else { cout << "\n Could not find \"" << place_name_in_record << "\" in \"" << country_name_in_record << "\".\n" ; } } } void Travel_database_application::store_travel_data_to_file() { ofstream travel_data_file( "travel_database.data", ios::binary ) ; if ( travel_data_file.fail() ) { cout << "\n ERROR -- could not write travel data file.\n" ; } else { int number_of_places_for_travel = places_on_earth.size() ; travel_data_file.write( (char*) &number_of_places_for_travel, sizeof( int ) ) ; for ( int place_index = 0 ; place_index < number_of_places_for_travel ; place_index ++ ) { places_on_earth[ place_index ]. store_to_binary_file( travel_data_file ) ; } } } void Travel_database_application::run() { string user_selection = "????" ; cout << "\n This program is a system to maintain the travel" "\n information that is used by \"travel.cpp\"." ; while ( user_selection[ 0 ] != 'e' && user_selection[ 0 ] != 'q' && user_selection[ 0 ] != 'x' ) { cout << "\n" "\n 'a' Add new place to travel database." "\n 's' Store all travel data to file." "\n 'l' Load travel data from file." "\n 'i' Load initial travel data." "\n 't' Run the actual Travel Application." "\n 'x' or 'q' or 'e' Exit the program.\n\n " ; getline( cin, user_selection ) ; if ( user_selection[ 0 ] == 'a' ) { add_new_place_to_travel_database() ; } else if ( user_selection[ 0 ] == 's' ) { store_travel_data_to_file() ; } else if ( user_selection[ 0 ] == 'l' ) { load_travel_data_from_file() ; } else if ( user_selection[ 0 ] == 'i' ) { places_on_earth.clear() ; // deleting old data load_initial_place_coordinates() ; load_initial_travel_info_records() ; } else if ( user_selection[ 0 ] == 't' ) { // Here we run the Travel Application with the data // that is currently in this Travel Update Application. Travel_application::run() ; } } } #endif