(I’m using Java therefore added the ‘Java’ tag incase it influences any answers, however some may argue that the tag is unecessary.)
Consider the following:
I have a veranda/balcony to plan graphically. As part of the final plan I am required to list any materials required to construct the veranda. To simplify the calculating process I’ve split the veranda into different Sections:
Above Deck – which involves the plastic fencing components and fixings (i.e Screws, bolts)
Below Deck – which involves timber subframes and more fixings.
…Plus others which I won’t go in depth.
For arguments sake: There are 100 different stock items available due to the different fencing designs we offer. Each item has its own class including information such as dimensions, colour, quantity and its ID reference.
What I would like to do is create a list of fixings required for each section and add them all up into one final list without any duplications.
The final idea is to display the item name and a quantity next to the processed plan.
So for example:
Section 1 requires:
– 25 x Screw A
– 15 x Screw B
– 5 x Screw C
Section 2 requires:
– 25 x Screw A
– 15 x Screw B
– 5 x Screw F
Section 3 requires:
– 45 x Screw B
– 50 x Screw C
– 24 x Screw G
Total List
Section 1 + Section 2 + Section 3 = {Complete list of materials}
What I’ve tried/considered:
All the screws I’ve mentioned above have their own class and extend the super class “Fasteners”. Each Section mentioned above has a variable:
ArrayList<Fasteners>
So as I’m calculating I can add the screws to this variable. Once each section has calculated how many Fasteners it requires I then add them altogether.
I’ve considered making a “add(Fixings)” method in the “Fasteners” super class which adds any duplicate items together. But because there are 100 items I would of thought the coding wouldn’t be very efficient and suspect there is a better way of making use of the polymorphism I’ve set up here. Any online references or hints would be helpful and very much appreciated.
Here’s a suggestion:
Make an enum called FastenerType, then use an
EnumMap<FastenerType, AtomicInteger>to hold your fasteners. Let’s call it the bag.Then do something like
Don’t worry too much about performance. First get it working. Then get it readable (refactor), then optimize.