# Stanford CS106A Section Assignment 8 Data Structures FlightPlanner Solution

After watching Stanford CS106A Lecture 24 about the principles of good design, I decided to apply these principles to Section Assignment 8: Data Structures. I was also surprised to see that the official solution to this assignment did not actually follow the principles discussed in class!

So if you are as interested as I was in practicing some of the stuff discussed in class, here is my solution.

You can also find this code on github.

## FlightPlanner.java

```/* This is the solution to Stanford CS106A Section Assignment 8 - Data Structures.
* Your task for this section is to write a  program that reads in a file containing flight
destinations from various cities, and then allow the user to plan a round-trip flight route.
*
*/

import java.util.*;
import acm.program.*;

public class FlightPlanner extends ConsoleProgram {

private FlightDB flights;
private ArrayList&lt;String&gt; enteredCities = new ArrayList&lt;String&gt;();
private String firstCity;

public void init() {
flights = new FlightDB(&quot;flights.txt&quot;);
}

public void run() {
welcome();
printFinalRoute();
}

private void welcome() {
println(&quot;Welcome to Flight Planner&quot;);
println(&quot;Here is a list of all the cities in our database&quot;);
Iterator&lt;String&gt; it = flights.getCities();
while(it.hasNext()) {
println(&quot; &quot; + it.next());
}
println(&quot;Let's plan a round-trip route!&quot;);
}

while(true) {
firstCity = readLine(&quot;Enter the starting city: &quot;);
if(flights.ContainsKey(firstCity)) {
break;
}
else{
println(&quot;You can't get to that city by a direct flight.&quot;);
println(&quot;Here is a list of all the cities in our database&quot;);
Iterator&lt;String&gt; it = flights.getCities();
while(it.hasNext()) {
println(&quot; &quot; + it.next());
}
}
}
println(&quot;From &quot; + firstCity + &quot; you can fly directly to:&quot;);
Iterator&lt;String&gt; it = flights.findRoute(firstCity);
while(it.hasNext()) {
println(&quot; &quot; + it.next());
}
}

String city = firstCity;
String lastCity = city;
while(true) {
city = readLine(&quot;Where do you want to go from &quot; + city + &quot;? &quot;);
if(city.equals(firstCity)) {
break;
}
if(flights.ContainsKey(city) == true) {
lastCity = city;
}
else{
city = lastCity;
println(&quot;You can't get to that city by a direct flight.&quot;);
}
println(&quot;From &quot; + city + &quot; you can fly directly to:&quot;);
Iterator&lt;String&gt; it = flights.findRoute(city);
while(it.hasNext()) {
println(&quot; &quot; + it.next());
}

}
}

private void printFinalRoute() {
println(&quot;The route you've chosen is&quot;);
String route = enteredCities.get(0);
for(int i = 1; i&lt;enteredCities.size(); i++) {
route += &quot; -&gt; &quot; + enteredCities.get(i);
}
route += &quot; -&gt; &quot; + enteredCities.get(0);
println(route);
}

}
```

## FlightDB.java

```/* This database keeps track of all the flight routes
*/

import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;
import acm.util.*;

import java.util.*;

public class FlightDB {

/* Private instance variables */
private Map &lt;String, FlightCities&gt; flightRoutes = new HashMap &lt;String, FlightCities&gt;();
private String fromCity = &quot; &quot;;
private String destination = &quot; &quot;;

public FlightDB(String filename) {
}

try{
while(true) {
if(line == null) break;
if(line.length() != 0) {
parse(line);
if(flightRoutes.containsKey(fromCity) == false) {
FlightCities tracker = new FlightCities(fromCity, destination);
flightRoutes.put(fromCity, tracker);
}
else if(flightRoutes.containsKey(fromCity) == true) {
}
}
}
rd.close();
} catch(IOException ex) {
throw new ErrorException(ex);
}
}

private void parse(String line) {
//name of the from City
int fromCityEnd = line.indexOf(&quot; -&gt;&quot;);
fromCity = line.substring(0, fromCityEnd);

//name of the to City
int desinationStart = fromCityEnd + 4;
destination = line.substring(desinationStart);
}

public Iterator&lt;String&gt; getCities() {
return flightRoutes.keySet().iterator();
}

public Iterator&lt;String&gt; findRoute(String fromCity) {
if(flightRoutes.containsKey(fromCity)) {
Iterator&lt;String&gt; it = flightRoutes.get(fromCity).getDestinations();
return it;
}
else{
return null;
}
}

public boolean ContainsKey(String fromCity) {
if(flightRoutes.containsKey(fromCity) == true) {
return true;
}
else{
return false;
}
}

}
```

## FlightCities.java

```import acm.util.*;
import java.util.*;

public class FlightCities {

private ArrayList &lt;String&gt; destinations = new ArrayList&lt;String&gt;();
private String city = &quot; &quot;;
private String destination = &quot; &quot;;

public FlightCities(String fromCity, String aDestination) {
city = fromCity;
}

public String getFromCity() {
return city;
}

public Iterator&lt;String&gt; getDestinations() {
return destinations.iterator();
}