• Post Reply Bookmark Topic Watch Topic
  • New Topic

How to have unlimited Object Parameters in a Constructor?  RSS feed

 
Kevin Olome
Ranch Hand
Posts: 44
1
Android Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I want to create an order system.
My Problem is that I want to create it dynamic, with ArrayLists and no fixed Array size.

Basically I have already a User which is able to purchase something.



This will work!

But I want something like this:
a.purchase(new Order(new orderThing(5,car), new orderThing(2,car), new OrderThing(1,car),...)
Basically I don't know how much OrderThings I want to create before I type in the OrderThings with new Order().

But in java it is before you can construct orderThings you must already know in the constructer how much objects you do want.

Now as im writing this question I got a idea of waiting for an Array of new OrderThing but it don't work. When I write
a.purchase(new Order(new orderThing(5,car), new orderThing(2,car)))
it wants a constructor which is based on Order(orderThing,orderThing)

I hope you can help me and sorry my first post is a question and not an introduction.
It will come soon

 
Paul Clapham
Sheriff
Posts: 22832
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Kevin, welcome to the Ranch!

You're right, it's perfectly normal for an Order to contain an unspecified number of OrderThings. So if you want to pass them all to a constructor or a method, you could either pass a List of OrderThings or an array of OrderThings. If you chose a List then your constructor would look like this:



Or if you chose an array then it would look like this:



It's more likely that a List would be useful, because then you don't have to decide in advance exactly how big the array of OrderThings is going to have to be.

(P.S. When I wrote code to deal with orders I called the things inside the order "OrderItem". Sounds more professional, ya know?)

 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In addition to the suggestions already made, Java supports varargs. This is a concept that allows a method or constructor to declare that it supports any number of a particular parameter. Behind the scenes the multiple instances will be presented to the method or constructor as an area array.



All of those constructor calls are allowed, and you can add as many parameters as you want.

Obviously this only works if you know how many parameters you want as you are writing the code. If you won't know that until runtime then a List would be a better option.

A varargs parameter will let you pass an array too though, so that is also an option. A List would still be better though, unless you also need the varargs feature of being able to pass different numbers of parameters.
 
Kevin Olome
Ranch Hand
Posts: 44
1
Android Firefox Browser Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for you answers. It helped me greatly.

But I got one more problem:
Is it possible to have in a return statement a for-loop?

Because I have for each Order a special toString method which should be called.
I want something to return like this:


 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, you can't have a for-loop in a return statement. You will have to construct the String separately, and then return it.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike. J. Thompson wrote:Behind the scenes the multiple instances will be presented to the method or constructor as an area...

Really? And there was me thinking all this time it was an array.

Phone trouble again?

Winston
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Oh I give up. I want a proper keyboard.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kevin Olome wrote:But I got one more problem:
Is it possible to have in a return statement a for-loop?
Because ... I want something to return like this:

It's possible that you could return something like it in version 8 using a Stream; but I wouldn't advise it - mainly because it will be very difficult for anyone reading your program to work out what it's trying to do.

There are many ways to construct a formatted String using a StringBuilder, or String.format() (or both) - and for arrays, there's also Arrays.toString() - but I'd definitely try to keep your code clear rather than compact.

Just one other point: OrderThing is an interesting name, but a more usual one would be OrderItem (sometimes also called a "line item").

Winston
 
Kevin Olome
Ranch Hand
Posts: 44
1
Android Firefox Browser Linux
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for you answers.

You guys are so friendly and helpful. I think I will stay here a bit.

Not like stack overflow where I get only negative rep.

 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!