user2169422
user2169422

Reputation:

Accessing an Objects values Java

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.

Upvotes: 0

Views: 101

Answers (1)

Juvanis
Juvanis

Reputation: 25950

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.

Upvotes: 1

Related Questions