-1

I am making exactly the same Java program that is in this link:

Run-time Polymorphism in Java without "abstract"?

But I am having problems in the Main Application.

In my problem I have to ask the user to enter which types does he want(Bicycle,MountainBike,RoadBike).

And then I add what type he chose to an array of 5 indexes.

How can I do that?

Please give me some help.

Thanks in advance.

Community
  • 1
  • 1
Mike
  • 1
  • 2

2 Answers2

0

I don't know how you look for answers from your users, but if it's buttons you do it with, you can just add the requested type of object, when a user press specific buttons.

But what you have to do, is make an ArrayList<Bicycle> and then just add the objects that extends Bicycle.

OmniOwl
  • 5,477
  • 17
  • 67
  • 116
0

You should create a factory method that returns an instance of a Bicycle:

For example:

public Bicycle createBicycle(String type) {
    if (type.equals("Bicycle")) {
        return new Bicycle(20, 10, 1);
    } else if (type.equals("MountainBike")) {
        return new MountainBike(20, 10, 5, "Dual");
    }
    //and so on..
}

And use it like this for example:

Bicycle[] bicycles = new Bicycle[5];
bicycle[0] = createBicycle("Bicycle");
Felix Glas
  • 15,065
  • 7
  • 53
  • 82