• Post Reply Bookmark Topic Watch Topic
  • New Topic

Populating an ArrayList object with a nested ArrayList object  RSS feed

 
Jarrad MacAteer
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello everyone,

I have stumbled onto a problem with ArrayLists (not sure if nested ArrayList objects would be a more accurate description) but would appreciate any help.

As simply put as I can describe it, I have an Inventory class which creates an ArrayList of a Product superclass, which has two subclasses, PerishableProduct and ItemisedProduct.
By default, the program is meant to have a starting inventory, which is why I have added them in the constructor



Within the ItemisedProduct subclass is yet another ArrayList which is meant to hold serial numbers.



My problem is that I do not know how to populate the serialNumbers ArrayList from the Inventory class' constructor. Because technically, quantity is defined by how many serial numbers are created.
Does anyone have any idea on how I go about this? Any help would be much appreciated.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch
Jarrad MacAteer wrote:Hello everyone,
My problem is that I do not know how to populate the serialNumbers ArrayList from the Inventory class' constructor. Because technically, quantity is defined by how many serial numbers are created.
Does anyone have any idea on how I go about this? Any help would be much appreciated.

First of all the line you have added to your constructor is pointless as you are setting serialNumbers to itself.
It sounds like ItemisedProduct works in a different way to your other products in that you don't set the quantity of them but rather give a list of serial numbers. Therefore there is no point in having a constructor that takes a quantity as a parameter as it is meaningless, so you need to change your constructor to take a collection of serial numbers. This could be a List, an array, a varargs parameter etc. you should then set the Product super classes qty to the length/size of this parameter, you can do this when calling the super classes constructor. You will also need to change the qty variable whenever a serial number is added/removed.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jarrad MacAteer wrote:Within the ItemisedProduct subclass is yet another ArrayList which is meant to hold serial numbers...

Why? It seems like an odd way to go about things.

There may well be a good reason, but it has the feel of a "solution" to a problem that might not actually exist; so if could you explain how these ItemisedProducts are supposed to work, we might be able to suggest an alternative strategy for you.

I'm also a bit dubious about storing a "serial number" as a String.

Winston
 
Jarrad MacAteer
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Winston,

What I want to do is allow the user to add new products either of the general Product supertype or its subtypes to the Inventory class' ArrayList.

The thing is, with perishable products, you don't need serial numbers since they don't have any, so nothing really affects adjusting its quantity.

However, if the user creates a new ItemisedProduct, say Coco Pops, the quantity they have in the inventory is determined by the number of serials they have for that range of ItemisedProduct (which can be alphanumeric, hence why I used String e.g. Coco Pops can have serials ranging from CRLCC00001 to CRLCC99999). So ItemisedProducts always start off with quantity of 0 when created. The user then has to type in each serial number after the product is created (not realistic, but let's say it's a small shop), which incrementally increases its quantity via the ItemisedProduct's addSerialNumber() method via this.addQuantity(1);.

My problem here is that I want the inventory to start off with a predetermined set of products and quantities, but can't figure out how to add the serials for ItemisedProducts. i.e. It's not the user doing it, but the program that has to enter the serials for this part of the program. I've tried random things like:

but to no avail.

Hi Tony,

Thanks for noting my silly error in the constructor, I've removed it.

And yes, as I replied to Winston, ItemisedProduct does work differently. What I want to know is how to get the program to set those serials itself. I already know how to get the user to do it, it's getting the program to "enter" serials itself for each ItemisedProduct that it creates which I'm having issues with, and I want it done from the Inventory class alongside the other products being pre-supplied by the program.

As for your recommendation with changing my Product superclass' constructor, I can't do that, because not all Product subclass quantities are determined by serials, as indicated above.

Thank you both for taking time to look at my weird problem
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jarrad MacAteer wrote:
I've tried random things like:

And therein lies your problem, trying random things is never a good way to program.
The best way forward now is to turn off your computer and write down in your native language what you are trying to achieve and how would would do this with just a pencil and paper if you had to take the place of the computer.

Jarrad MacAteer wrote:
As for your recommendation with changing my Product superclass' constructor, I can't do that, because not all Product subclass quantities are determined by serials, as indicated above.

I didn't suggest changing the Product constructor, I said change the ItemisedProduct constructor and call the <existing> super class constructor.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!