logo CodeStepByStep logo

FlightPlanner

Related Links:

Write a console program named FlightPlanner that reads in a file containing flight destinations from various cities, and then allow the user to plan a round-trip flight route. Here's what a sample run of the program might look like:

...

The flight data come from a file named flights.txt , which has the following format:

  • Each line consists of a pair of cities separated by an arrow indicated by the two character combination ->, as in:
    New York -> Anchorage
  • The file may contain blank lines for readability (you should just ignore these).

The entire data file used to produce this sample run appears below.

San Jose -> San Francisco
San Jose -> Anchorage
New York -> Anchorage
New York -> San Jose
New York -> San Francisco
New York -> Honolulu
Anchorage -> New York
Anchorage -> San Jose
Honolulu -> New York
Honolulu -> San Francisco
Denver -> San Jose
San Francisco -> New York
San Francisco -> Honolulu
San Francisco -> Denver

Your program should:

  • Read in the flight information from the file flights.txt and store it in an appropriate data structure.
  • Display the complete list of cities.
  • Allow the user to select a city from which to start.
  • In a loop, print out all the destinations that the user may reach directly from the current city, and prompt the user to select the next city.
  • Once the user has selected a round-trip route (i.e., once the user has selected a flight that returns them to the starting city), exit from the loop and print out the route that was chosen.

A critical issue in building this program is designing appropriate data structures to keep track of the information you'll need in order to produce flight plans. You'll need to both have a way of keeping track of information on available flights that you read in from the flights.txt file, as well as a means for keeping track of the flight routes that the user is choosing in constructing their flight plan. Consider how both ArrayLists and HashMaps might be useful to keep track of the information you care about.

Class: Write a complete Java class.

You must log in before you can solve this problem.

Log In

Need help?

Stuck on an exercise? Contact your TA or instructor.

If something seems wrong with our site, please

Is there a problem? Contact us.