You need to create the class BaseballFileReader, which reads in the provided text file and returns an
Question:
You need to create the class BaseballFileReader, which reads in the provided text file and returns an ArrayList of Connection objects. Review the provided Connection class so that you know what a Connection object is. An instance of the BaseballFileReader class is created in the constructor of RouteFinder and then the readFile() method (which you need to create) is called by the instance and the return is assigned to the instance variable of the RouteFinder class. Remember that all the data in the text file are strings and that the miles need to be integers.
Connection.Java
public class Connection
{
// the name of the source city
private String source;
// the name of destination city
private String destination;
// the distance between cities
private int distance;
/**
* Constructor takes name for source and name of destination.
* The distance is the cost between them.
* @param source name of the source
* @param dest name of the destination
* @param dist the cost between source and dest
*/
public Connection(String source, String dest, int dist)
{
this.source = source;
this.destination = dest;
this.distance = dist;
}
/**
* Source city
* @return the city name
*/
public String getSource()
{ return source; }
/**
* Destination city
* @return the city name
*/
public String getDestination()
{ return destination; }
/**
* Distance between source and destination city
* @return int value of distance
*/
public int getDistance()
{ return distance; }
/**
* Two connections are the same if the source and destinations have the same name.
* @return true if same connection. Otherwise false.
*/
@Override
public boolean equals(Object other)
{
if (other instanceof Connection)
{
Connection otherConn = (Connection)other;
if (otherConn.source.equals(this.source) &&
otherConn.destination.equals(this.destination))
return true;
}
return false;
}
/**
* Returns a string representation with the item name and count.
*/
public String toString()
{
return String.format("%s, %s, %d", source, destination, distance);
}
}
Routefinder.java
package shortestPath;
import java.util.ArrayList;
import java.util.Arrays;
public class RouteFinder
{
private ArrayList readRoutesBetweenCities;
/**
* Parameterized constructor for an object of class RouteFinder.
* Reads in a CSV file of source and destination cities and
* converts it to a graph.
* @param filePath The input file to parse.
*/
public RouteFinder(String filePath)
{
// TODO: Define the class BaseballFileReader
BaseballFileReader reader = new BaseballFileReader();
// TODO: Define the readFile() method which reads the CSV (Comma Separated Value)
// file of connections between cities and creates a specified ArrayList
// of Connection objects.
//
// NOTE: Catch all exceptions in the readFile() method.
// That means readFile() method should not throw an exception.
readRoutesBetweenCities = reader.readFile(filePath);
// Check the size of the resulting ArrayList object.
if (readRoutesBetweenCities.size() < 1)
{
System.out.println("WARNING: The list of cities is empty.");
return;
}
System.out.printf("The list of cities has %d items. ", readRoutesBetweenCities.size());
}
/**
* Accessor method returns the list of items read from input file.
* @return the routes between cities.
*/
public ArrayList getConnectionsBetweenCities()
{
return readRoutesBetweenCities;
}
public static void main(String[] args) throws Exception
{
// NOTE: Make sure to use *relative* path instead of specifying the entire path.
// Otherwise, your program will result in run time errors when the instructor
// tests your implementation.
final String FILEPATH = "resources/BaseballCitiesEdgeCosts.txt";
RouteFinder seeker = new RouteFinder(FILEPATH);
ArrayList connections = seeker.getConnectionsBetweenCities();
// displays the prices of items in the input file
System.out.println("Connections found between cities hosting Baseball games:");
System.out.println(connections);
// build graph
FHgraph baseBallRoutes = new FHgraph();
connections.forEach(current ->
baseBallRoutes.addEdge(current.getSource(),current.getDestination(), current.getDistance()));
baseBallRoutes.showAdjTable();
// dijkstra called from inside
// TODO: Update dijsktra() method to avoid paths which go through the
// requested list of cities to avoid.
final String startingCity = "San Francisco";
final String [] destinations = {"Boston", "Chicago A", "Chicago N"};
final ArrayList citiesToAvoid = new ArrayList(Arrays.asList(new String[]{"Los Angeles X", "Milwaukee"}));
System.out.println("Showing distances to " + startingCity);
System.out.println("Cities to avoid are " + citiesToAvoid);
baseBallRoutes.showDistancesTo(startingCity, citiesToAvoid);
System.out.println();
for (String current : destinations)
{
// TODO: Update showShortestPath() to call dijkstra with requested cities to avoid.
baseBallRoutes.showShortestPath(startingCity, current, citiesToAvoid);
System.out.println();
}
System.out.println("Done.");
}
}
Java An Introduction To Problem Solving And Programming
ISBN: 9780134462035
8th Edition
Authors: Walter Savitch