-2

So for example I have this class that outputs my detailed flight output that is in the java doc "Flight.java" :

public String toDetailedString() {
    String output =
        getDeparture() + " - " + getArrival() + "\n" + Airport.getAirportCity(source) + " (" +
        source + ") - " + Airport.getAirportCity(destination) + " (" + destination + ")" +
        "\n" + plane.getAirline() + " " + number + " * " + plane.getModel();
    return output;

}

and I have an Itinerary class "Itinerary.java" and I want to pull the info from Flight.java without making toDetailedString static, is that possible? For added info, each of the variables you see "source, number, destination" are all private variables in Flight.java which I know are enclosed encapsulation. Any help is greatly valuable.

Example implementation in Itinerary.java

public String toString() {
    return "The total cost is" + getTotalCost() + Flight.toDetailedString();
}

UPDATE:

In my Flight constructor I have:

public Flight(Plane plane, String number, double cost, Time departure, int duration, Airport source, Airport destination) {
    this.plane = plane;
    this.number = number;
    this.cost = cost;
    this.departure = departure;
    this.duration = duration;
    this.source = source;
    this.destination = destination;
}

In which I created the object "f1" in my ItineraryTest class:

Flight f1 = new Flight(new Plane(Airline.American, "Airbus A321"),
        "495",
        79,
        new Time(7, 10), 100,
        Airport.PHX, Airport.LAX);

Thus I linked my object to the toDetailedString() the object "f1" was renamed to "first" in my Itinerary.java and a second object was created called "f2" and moved to "second" (to avoid confusion) :

public String toString() {
    return "The total cost: " 
            + getTotalCost()
            + " "
            + first.toDetailedString()
            + second.toDetailedString();
}

I thought I answered my own question, but now receive an error of:

Exception in thread "main" java.lang.NullPointerException at Itinerary.toString(Itinerary.java:117) at java.lang.String.valueOf(String.java:2994) at java.io.PrintStream.println(PrintStream.java:821) at ItineraryTest.main(ItineraryTest.java:20)

TheMuffinMan
  • 55
  • 2
  • 7

2 Answers2

0

Possible, clean option is creating another class named FlightToDetailedString, with (possibly static) method toDetailedString. That method would accept instance of Flight as parameter

class FlightToDetailedString {
  public String toDetailedString(Flight flight) {
    String output =
        getDeparture() + " - " + getArrival() + "\n" + Airport.getAirportCity(source) + " (" +
        source + ") - " + Airport.getAirportCity(destination) + " (" + destination + ")" +
        "\n" + plane.getAirline() + " " + number + " * " + plane.getModel();
    return output;    
}
}

In my example method is not static. With non-static member mehod, FlightToDetailedString could have (and use in toDetailedString) fields of FlightToDetailedString class. For example, Airport could be member variable of FlightToDetailedString. Are you sure you want to limit yourself to one airport?

Why you do not like toDetailedString as non-static member of Flight class in first place? In example you give it is most natural solution. if your Itinerary needs to access details of Flight class, just pass instance of Flight object as argument!

Bartosz Bilicki
  • 10,502
  • 8
  • 63
  • 98
  • I created an object under Flight that reflects the information of the toDetailedString() which is what I was initially trying to accomplish but now I receive an error when compiling. – TheMuffinMan Oct 30 '16 at 16:44
0

The consumer of toDetailedString() doesn't need access to anything inside Flight.

Your NullPointerException is likely caused by one of the fields in the Flight instance being null.

Finally, consider using a StringBuilder instead of concatenating strings.

zerobandwidth
  • 1,005
  • 9
  • 17