User creates objects. How to?
Hey all!
I have a question regarding how a user can generate a new object.
For example, lets say that I have a GUI application in which the user will be able to put an order. That order has to be created, with a new name and new values. The order itself will be stored in an array or some other type of collection.
My question is how do you put it in code in the most simplest way? The area I'm stuck on is generating new object names, and these must be different (hence different orders).
Here is an example without GUI:
Code Java:
// Using scanner class
int a = 0;
System.out.println("Hey! Place a new order!");
a = scan.nextInt();
if(a>0){
Order x = new Order();
x.setOrderID(a);
CollectionOfOrders.put(x);
}
Can you point me towards the right direction: for-loop? It's the names :)
Re: User creates objects. How to?
What do you mean by "object name"? What do you mean by "simplest way"?
Re: User creates objects. How to?
Hey Kevin!
What I mean is that an instance of a class is being created everytime that above piece of code is repeated. But the name of the instance created remains the same - especially if I want to put it in a container of some sort. What happens if that piece of code is repeated, and I try to add the same instance name to the container - no go.
How do I make sure that everytime that instance is being created has a unique identifier (i.e. name).
Order o = new Order();
The next Order created can't be called "o" again, can't it, so how do I make sure otherwise? :confused:
Re: User creates objects. How to?
Why can't it be called "o" again? What happened when you tried your original code?
Re: User creates objects. How to?
You can use an ArrayList of objects.
Code java:
ArrayList<order> orderArrayList = new ArrayList<order>(); //Makes an arraylist to hold all of the order objects
for (int i=0; i < 10; i++)
orderArrayList.add(new order()); //Makes a new orderobject and adds it to the list
Re: User creates objects. How to?
Quote:
Originally Posted by
Brt93yoda
You can use an ArrayList of objects.
That's what the OP is doing. From the original post:
Code :
CollectionOfOrders.put(x);