How to associate an object with the quantity of that object
<p>I am working on a <em>Java</em> application and came across a general implementation/meta question and wanted to reach out for suggestions.</p>
<p>I am looking to associate a Java object with a quantity. The java object is complex. In my case, it is a serializable object that represents JSON data from a 3rd party API. I am looking to associate a quantity with this complex Java object.</p>
<p>As this may be something that is easier to understand with an example, here is one. Say I have a <code>Car</code> class that is used to represent a car. It contains all the details of what make a car a car and is a general form that can be used to communicate over an API. Say I am making an inventory app for a dealership. The dealership would want to know how many of each <code>Car</code> they have. Hence the need for the association.</p>
<h1>Ideas</h1>
<p>There are some ways I can think of the do this.</p>
<h3>Class it out</h3>
<p>One idea would be to create classes that capture this association. One could have an <code>InventoryEntry</code> class that contains a <code>Car</code> and a quantity. Your dealerships inventory would then consist of a <code>List</code> of <code>InventoryEntry</code> objects.</p>
<h3>Arrays</h3>
<p>One can also implement this association via an Array mechanism. This can be done by creating an <code>ArrayList&lt;Car&gt;</code> for the cars and an <code>ArrayList&lt;Integer&gt;</code> for the quantity. The index for each list would be used to associate the two.</p>
<p>Would you recommend one of these method or some other implementation?</p>