0

Okay so I'm creating a simple flight booking system. I've spent a fair bit of time debugging and now even the simplest things are stumping me.

I have an interface which looks close to:

public class Flight implements IFlight {

String destination;
String departure;
int clubrows = 0;
int ecorows = 0;

public Flight(String dest, String dept, int clubrow, int ecorow) {
    destination = dest;
    departure = dept;
    clubrows = clubrow;
    ecorows = ecorow;


}


public String getDestination() {
    return destination;
}

This class has many similar get methods. Now i'm trying to write a for loop where every value that is put in is printed out. So i need to access all the 0 values then all the 1 values etc. it looks kinda like this right now:

public void flightManifest() { 
    System.out.println("Available flights: ");
    for(int i=0; i<flightCount ;i++){
        System.out.println("Flight number: "+flightCount  +", Destination: "+  +", Departure time: "+  );
    }

So essentially whenever i try to access the variables i keep balls-ing it up, so how am i meant to access these values each time round?

So the way I store them is as such: flightArr[flightCount] = new Flight (dest, dept, clubrow, ecorow); flightCount++; or at least thats how it is made.

2
  • 3
    Show us how you're storing your list/array of flights. Commented Mar 17, 2013 at 6:24
  • Your Flight class should have private fields. Use getXX() methods to access the values. There is also no need to set int values to 0 unless you have a constructor that doesn't assign them values. Commented Mar 17, 2013 at 6:26

1 Answer 1

1

Provide a toString() method to your Flight class, and then simply call it within your loop.

Within that toString() method, return the String concatenation of your class fields. That's all.

Sign up to request clarification or add additional context in comments.

1 Comment

You might want to call this method toFlightInfoString() or some such. That way of you have a couple of other different formats you can add them later.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.