Flight Times

due Wednesday, Jan. 25, 2012, 10pm

Write a program to determine the best (shortest), average, and worst (longest) flight times from a set of inputs. The first input to the program should be how many flights are being compared. Next, for each flight, read the take off time and landing time. Each time should be input in military format as a single 4 digit number, adding 12 to each pm hour. For example, 2:30pm should be input as 1430; midnight is 0000; noon is 1200; 10:54am is 1054. All data for a flight should be on one line of input. Do error checking and do not process any line of data with an invalid time. Instead, output an error message for each bad time. After reading and processing the data, the program should output the start and stop time of the shortest and longest flights, and their elapsed times, as well as the average flight time (for the valid inputs). A sample run is below.

INPUT (file, see below):
5
1030 1415
1535 1800
2523 2304
0530 0835
0605 1270

OUTPUT (screen):
how many flights?
enter flight times in military HHMM format, one per line

invalid flight time: 2523
invalid flight time: 1270

best flight: 5:30-8:35, elapsed time 3 hrs 5 minutes 
worst flight: 15:35-18:0, elapsed time 2 hrs 25 minutes 
average flight: 10 hrs 31 mins
You should compile your code on the ugradx servers, as that's where the code will be tested. We'll test your code with the following command:
$ g++ -o flighttimes Main.cpp FlightTime.cpp TravelAgent.cpp
$ ./flighttimes < sampleinput
The base code for the project can be found in zip form at flight-code.tar.gz, or you can look at the individual files in the archive at Flight Code. In the archive you'll find the following files: You can also find the relevant code we wrote in class:

Resources

Besides the slides, you may find the references for string and vector in C++ to be useful:

Hand In

You will need to flesh out all of the functions in the FlightTime.cpp and the TravelAgent.cpp files, in accordance with the comments and the below questions. Turn in a .tar.gz file that contains all of the .cpp and .h files, as well as a README that answers all of the questions below.

    Comprehension Questions

  1. Why do we need the new and delete keywords in Main.cpp?

  2. Several of the functions -- FlightTime::departure, FlightTime::arrival -- use parameters of type integer reference (int &). Using references as parameters allows us to put some results of a function in the variables we've passed in as arguments. Why is this useful for the functions in this program?

  3. We have an lt_compare function in the TravelAgent code, which decides whether one FlightTime object comes before another. Why is there a whole function dedicated to just this purpose? What comparison needs to happen under each possible SortBy condition? What other function will use the lt_compare function?

  4. We have two constructors for FlightTime objects, defined in the header file. Take a look at them. (Remember that the constructor for a class is a function with the same name as the class, which gets called when an object of that class gets created.) What is the second constructor doing? (What changes does it make to which variables?) Why? What do the variables of the FlightTime class contain after that constructor gets called?

  5. Writing Code

  6. Finish out the FlightTime code, as well as the TravelAgent code, so that everything compiles and works. To write a function, think through how you'd manipulate the data itself for all of the available objects,

    Don't hand in your code yet.

  7. Once your code is successfully compiling, add an option after getting the flight information that allows the user to choose what sort they'd like - Departure, Arrival or Length. Display a menu that looks like so:
    What sort would you like?
    0 - Departure
    1 - Arrival
    2 - Length
    enter your chosen sort type here: 1
    
    The user's choice should be taken into account when doing the sort.

    If you can't figure out a good way to do this, write in your README the thoughts you had about what you might need to change.

  8. OPTIONAL: Make this travel agency a little more useful! Here's some ideas...