Stanford CS106A Section Assignment 8 Data Structures FlightPlanner Solution

26 Nov

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<String> enteredCities = new ArrayList<String>();
	private String firstCity;

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

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

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

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

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

		}
	}

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

}

FlightDB.java

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

import java.io.BufferedReader;
import java.io.FileReader;
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 <String, FlightCities> flightRoutes = new HashMap <String, FlightCities>();
	private String fromCity = " ";
	private String destination = " ";

	public FlightDB(String filename) {
		readFile(filename);
	}

	private void readFile(String filename) {
		try{
			BufferedReader rd = new BufferedReader(new FileReader(filename));
			while(true) {
				String line = rd.readLine();
				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) {
						flightRoutes.get(fromCity).addDestination(destination);
					}
				}
			}
			rd.close();
		} catch(IOException ex) {
				throw new ErrorException(ex);
		}
	}

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

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

	public Iterator<String> getCities() {
		return flightRoutes.keySet().iterator();
	}

	public Iterator<String> findRoute(String fromCity) {
		if(flightRoutes.containsKey(fromCity)) {
			Iterator<String> 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 <String> destinations = new ArrayList<String>();
	private String city = " ";
	private String destination = " ";

	public FlightCities(String fromCity, String aDestination) {
		city = fromCity;
		destination = aDestination;
		destinations.add(destination);
	}

	public String getFromCity() {
		return city;
	}

	public Iterator<String> getDestinations() {
		return destinations.iterator();
	}

	public void addDestination(String aDestination) {
		destinations.add(aDestination);
	}

}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s