Java program doesn't display text from another class (graphs)

170 views Asked by At

So it's probably something wrong with my Flight class, but I can't figure out what exactly. I'm trying to display flight info, but only the airports are being shown when I run it (try Edinburgh -> Toronto). Here's my main class:

import java.util.ArrayList;
import java.util.List;
import java.util.Scanner;
import java.util.StringTokenizer;

import org.jgrapht.alg.DijkstraShortestPath;
import org.jgrapht.graph.*;

public class FlightItinerary2
{
    private static ArrayList<String[]> myEdges;
    private static ArrayList<Flight> flight;

    public FlightItinerary2()
    {
    } // ensure non-instantiability.


    @SuppressWarnings("resource")
    public static void main(String [] args)
    {

        // create a graph based on Strings
        myEdges = new ArrayList<String[]>();
        flight = new ArrayList<Flight>();
        SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> hrefGraph = createHrefGraph();

        // note directed edges are printed as: (<v1>,<v2>)
        //printAirports(hrefGraph);
        //System.out.println(hrefGraph.toString());

        Scanner sc = new Scanner(System.in);
        System.out.println("\n Enter 1st airport");
        String startVertex = sc.nextLine();
        while(!hrefGraph.containsVertex(startVertex))
        {
            System.err.println("Please enter an existing airport.");
            startVertex = sc.nextLine();
        }
        System.out.println("Enter 2nd airport");
        String endVertex = sc.nextLine();
        while(!hrefGraph.containsVertex(endVertex))
        {
            System.err.println("Please enter an existing airport.");
            endVertex = sc.nextLine();
        }

        calculatePath(hrefGraph, startVertex, endVertex);

     }

    /**
     * Creates a directed graph based on Strings that represents link
     * structure.
     *
     * @return a graph based on Strings.
     */
    private static SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> createHrefGraph()
    {
        SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> g =
            (SimpleDirectedWeightedGraph<String, DefaultWeightedEdge>) new SimpleDirectedWeightedGraph<String, DefaultWeightedEdge>(DefaultWeightedEdge.class);

        {

            // add the vertices
            g.addVertex("Edinburgh");
            g.addVertex("Heathrow");
            g.addVertex("Amsterdam");
            g.addVertex("Boston");
            g.addVertex("Chicago");
            g.addVertex("Montreal");
            g.addVertex("Toronto");
            g.addVertex("New Delhi");
            g.addVertex("Shanghai");
            g.addVertex("Hong Kong");

            // add edges to create linking structure
            createTwoWayWeightedEdge(g, "Edinburgh", "Heathrow", 110);
            generateFlight("1600", "1800", "BA445", 110);
            generateFlight("0400", "0600", "BA452", 110);
            createTwoWayWeightedEdge(g, "Heathrow", "Amsterdam", 100);
            generateFlight("1400", "1600", "DC123", 100);
            generateFlight("0400", "0600", "AB712", 100);
            createTwoWayWeightedEdge(g, "Heathrow", "Boston", 230);
            generateFlight("1800", "2200", "AD412", 230);
            generateFlight("0900", "1300", "AA413", 230);
            createTwoWayWeightedEdge(g, "Boston", "Chicago", 150);
            generateFlight("0700", "1000", "BA178", 150);
            generateFlight("1300", "1600", "YU712", 150);
            createTwoWayWeightedEdge(g, "Boston", "Montreal", 100);
            generateFlight("1600", "1900", "AG102", 100);
            generateFlight("2100", "2300", "AH140", 100);
            createTwoWayWeightedEdge(g, "Montreal", "Toronto", 90);
            generateFlight("1100", "1200", "AC968", 90);
            generateFlight("2000", "2100", "BA360", 90);
            createTwoWayWeightedEdge(g, "Edinburgh", "Chicago", 560);
            generateFlight("1200", "2000", "BI712", 560);
            generateFlight("2100", "0500", "AX023", 560);
            createTwoWayWeightedEdge(g, "New Delhi", "Shanghai", 430);
            generateFlight("1400", "2000", "AA550", 430);
            generateFlight("0600", "1200", "BB102", 430);
            createTwoWayWeightedEdge(g, "Shanghai", "Hong Kong", 230);
            generateFlight("1400", "1700", "NX450", 230);
            generateFlight("2200", "0100", "BL860", 230);
        } 
        return g;
    }

    private static void createTwoWayWeightedEdge(SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> g, String vertex1, String vertex2, double weight)
    {
        g.addEdge(vertex1, vertex2);
        g.addEdge(vertex2, vertex1);

        g.setEdgeWeight(g.getEdge(vertex1, vertex2), weight);
        g.setEdgeWeight(g.getEdge(vertex2, vertex1), weight);

        String[] tmp1 = {vertex1, vertex2};
        myEdges.add(tmp1);
        String[] tmp2 = {vertex2, vertex1};
        myEdges.add(tmp2);
    }

    private static void generateFlight(String depTime, String arrTime, String flightNo, double price)
    {
        Flight f = new Flight(depTime, arrTime, flightNo, price);
        flight.add(f);
    }

    // for displaying flight info nicely
    private static String textToPrint(String[] format)
    {
        String text = "    ";
        for(int i = 0; i < format.length; i++)
        {
            switch(i)
            {
                case 0:
                    text = text + format[i];
                    for(int j = format[i].length(); j < 6 ; j++)
                        text = text + " ";
                    break;

                case 1:
                    text = text + format[i];
                    for(int j = format[i].length(); j < 15 ; j++)
                        text = text + " ";
                    break;

                case 2:
                    text = text + format[i];
                    for(int j = format[i].length(); j < 10 ; j++)
                        text = text + " ";
                    break;

                case 3:
                    text = text + format[i];
                    for(int j = format[i].length(); j < 10 ; j++)
                        text = text + " ";
                    break;

                case 4:
                    text = text + format[i];
                    for(int j = format[i].length(); j < 15 ; j++)
                        text = text + " ";
                    break;

                case 5:
                    text = text + format[i];
                    for(int j = format[i].length(); j < 10 ; j++)
                        text = text + " ";
                    break;
            }
        }
        return text;
    }


    @SuppressWarnings({ "rawtypes", "unchecked", "unused" })
    private static void calculatePath(SimpleDirectedWeightedGraph<String, DefaultWeightedEdge> g, String startVertex, String endVertex)
    {
        DijkstraShortestPath path = new DijkstraShortestPath(g, startVertex, endVertex);
        path.getPath();
        List<Object> edges = path.getPathEdgeList();

        String item;
        int count = 1;
        double totalDuration = 0;

        if(edges!=null)
        {
            System.out.println("\nShortest ( i . e . cheapest ) path :");
            String[] labels = {"Leg.", "Leave", "At", "On", "Arrive", "At"};
            System.out.println(textToPrint(labels));

            for(Object edge : edges)
            {
                    item = edge.toString();

                    StringTokenizer st = new StringTokenizer(item, ":");

                    String firstAirport = st.nextToken().trim().substring(1);
                    String secondAirport = st.nextToken().trim();
                    secondAirport = secondAirport.substring(0, secondAirport.length()-1);

                    String depTime = null, arrTime = null, flightNo = null;
                    double price, flightDuration;

                    for(int i=0;i<flight.size();i++)
                    {
                        if(firstAirport.equals(myEdges.get(i)[0]) && secondAirport.equals(myEdges.get(i)[1]))
                        {
                            Flight details = flight.get(i);
                            flightNo = details.flightNo;
                            depTime = details.depTime;
                            arrTime = details.arrTime;
                            price = details.price;
                            flightDuration = details.duration;
                            totalDuration = totalDuration + flightDuration;

                            String[] tmp = {count+".", firstAirport, depTime, flightNo, secondAirport, arrTime};
                            System.out.println(textToPrint(tmp));
                        }
                    }
                    count++;

            }
            System.out.println("Cost of shortest (i.e. cheapest) path = £"+path.getPathLength());
            System.out.println("Total time in the air = "+totalDuration+"hrs");
        }
        else
            System.err.println("The path doesn't exist.");
    }

}

And Flight.java class:

public class Flight {

    String depTime;
    String arrTime;
    String flightNo;
    int duration;
    int price;

    public Flight(String depTime, String arrTime, String flightNo, double duration){
        this.depTime = new String();
        this.arrTime = new String();
        this.flightNo = new String();
        //this.duration = this.getDuration();

        }


    public int getDuration(){

        int duration = Integer.parseInt(arrTime) - Integer.parseInt(depTime);
        return duration;
    }

    public String getFlightNo(){
        return flightNo;
    }


    public double getPrice(){
        return price;
    }



}

Duration is also not working, but that's not as important. Thanks!!

1

There are 1 answers

0
Chris Stillwell On BEST ANSWER

This looks to be your main problem you aren't storing your arguments in your variables. Rather you are just initializing them. Change your Flight constructor to look like this.

public Flight(String depTime, String arrTime, String flightNo, double duration){
    this.depTime = depTime;
    this.arrTime = arrTime;
    this.flightNo = flightNo;
    this.duration = duration;
}