2

I have an arraylist (orderlist) of class ProductionOrder with five fields (String ID, int Quantity, int Frequency_max, int Frequency_average, int Frequency_min), which are filled by an excel file so each element of the collection is an excel row.

Each row of the excel represents the ID of the order and has quantity and frequency.

for example:

ID            Quantity     Frequency_max       Frequency_average        Frequency_min
123FD99         2            12                 5                        2
14ZY201         4            59                 18                       1
...

I need to duplicate the number of elements in the collection based on the quantity field of the collection itself so the result should look like this:

ID           Frequency_max        Frequency_average        Frequency_min
14ZY201        59                    18                       1
14ZY201        59                    18                       1
14ZY201        59                    18                       1
14ZY201        59                    18                       1
123FD99        12                    5                        2
123FD99        12                    5                        2

How may I do it? Is it also possible to make this operation before filling the elements in the collection (ps. I'm using Apache POI for the excel)?

After that, I need to assign to each of the duplicated ID a random value based on a triangular distribution, with max, average and minimum set in the frequency columns. How may I do it?

Thanks

dawis11
  • 820
  • 1
  • 9
  • 24
TomC
  • 55
  • 3

1 Answers1

1

For your first issue, you can do something like that:

  1. If the fields are currently stored in another array (or collection):
for (int i = 0; i < fields.length; i++) // Iterate through all fields
    for (int j = 0 ; j < fields[i].getQuantity(); j++)
        list.add(fields[i]);
  1. If the fields are already stored in the collection:
for (int i = 0; i < fields.size(); i++) // Iterate through all fields
    for (int j = 0 ; j < fields.get(i).getQuantity() - 1; j++)
        list.add(fields.get(i));

For your second issue, take a look at the answer posted by Tunaki in here:

   public double triangularDistribution(double a, double b, double c) {
        double F = (c - a) / (b - a);
        double rand = Math.random();
        if (rand < F) {
            return a + Math.sqrt(rand * (b - a) * (c - a));
        } else {
            return b - Math.sqrt((1 - rand) * (b - a) * (b - c));
        }

    }
  • Many thanks, I'll be using the second method. So fields in here does not means the collection right? Is it a count of the fields I have in the arraylist? What's getQuantity()? I don't get it from the compiler. Can you explain better? – TomC Apr 26 '20 at 15:19
  • _fields_ is the collection (or array) that holds the initial five fields that you already read from the file. What type of objects does your ArrayList contain? I thought of it as some object that you created to hold a field, that way it could have a method called getQuantity() – LastAirBender Apr 26 '20 at 15:35
  • Great! Sounds very interesting – LastAirBender Apr 26 '20 at 16:07
  • Ok got it. I've created a custom class (named Exceller) with 5 fields (as reported above), which allows me to store the exact format and rows of the excel file, so the arraylist is of type . It stores production orders. Do I have to add the getQuantity method in this way? public int getQuantity() { return quantity; ?? – TomC Apr 26 '20 at 16:37
  • You can also define quantity as public, and access it regularly. Whatever is more convenient – LastAirBender Apr 26 '20 at 17:22