I'm looking for a starting point on this. Can someone point me in the right direction?

I want to allow a user to create a new class object that will just store data.

My class is data and my type is "flight" so:

Data flight1 = new Data("A string", "A string", "A string", an int);

If I wanted to allow a user to create new flights I would have to predefine those with blank data and use set methods to define the flights to the users input.

The problem with this is that I would have to create x amount of blank variables which would make my program bulky and they could only create as many variables as I had predefined.

What I'm looking to achieve is when I prompt if they choose to create a new variable the system will automatically create a new variable with a computer generated name (maybe "flight" and then an incrementer for 1,2,3...).[code=java]
Data flight1 = new Data("A string", "A string", "A string", an int);


If I wanted to allow a user to create new flights I would have to predefine those with blank data and use set methods to define the flights to the users input.

The problem with this is that I would have to create x amount of blank variables which would make my program bulky and they could only create as many variables as I had predefined.

What I'm looking to achieve is when I prompt if they choose to create a new variable the system will automatically create a new variable with a computer generated name (maybe "flight" and then an incrementer for 1,2,3...).

Recommended Answers

All 5 Replies

You could use an ArrayList. Your addFlight method would then create a Flight object with their data and add it to the list, and you're sorted.

EDIT: sorted as in "you're all set", not as in your data is now in order.

Well yes I thought about that but I'm not sure how to implement it. Lets say i had multiple variables, flight1 through flight 10, and I wanted to use a loop to access each element. Well this doesn't work, but here is what I would like to be able to do.

for (i=0; i <=10; i++)
{ tempstring = flight.getName();
if tempstring flight.equals("") then something,....

So i guess what I'm asking is if there is a way to include counters in the variable name?

public class Flight {

    private final String name;
    private final int id;

    Flight(String name, int id) {
        this.name = name;
        this.id = id;
    }

    String getName() {
        return name;
    }

    int getId() {
        return id;
    }

    String getFullName() {
        return name + id;
    }
}
public class FlightSet {

    private Flight[] array;

    FlightSet(int size) {
        this.array = new Flight[size];
    }

    void generate() {
        int index = 0;
        for (int i = 0; i < array.length; i++) {
            array[i] = new Flight("AAA", index++);
        }

    }

    void dump() {
        for (Flight inner : array) {
            System.out.println(inner.getFullName());
        }
    }
}
public class Starter {
    public static void main(String[] args) {
        int size = 10;
        FlightSet fs = new FlightSet(size);
        fs.generate();
        fs.dump();
    }
}

Looping through the members of an ArrayList is easy enough:

for (Flight f: flightsList)
{
  // do what you want to do with each flight: f will take the value of each one in turn
}

Quuba's solution can work, but arrays are fixed, so you'd have to put in some provision for resizing. That's not very difficult, but the work has been done for you if you use an ArrayList, and you can be pretty sure that the library methods will do it better than you will.

Awesome. Thanks for the help. This should give me a good starting point for what I would like to do.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.